Beispiel #1
0
    def WriteHostRequestReference(self, request, context):
        with session_scope() as session:
            host_request = (session.query(HostRequest).filter(
                HostRequest.conversation_id == request.host_request_id).filter(
                    or_(HostRequest.from_user_id == context.user_id,
                        HostRequest.to_user_id ==
                        context.user_id)).one_or_none())

            if not host_request:
                context.abort(grpc.StatusCode.NOT_FOUND,
                              errors.HOST_REQUEST_NOT_FOUND)

            if not host_request.can_write_reference:
                context.abort(grpc.StatusCode.FAILED_PRECONDITION,
                              errors.CANT_WRITE_REFERENCE_FOR_REQUEST)

            if (session.query(Reference).filter(
                    Reference.host_request_id == host_request.conversation_id
            ).filter(Reference.from_user_id == context.user_id).one_or_none()):
                context.abort(grpc.StatusCode.FAILED_PRECONDITION,
                              errors.REFERENCE_ALREADY_GIVEN)

            if request.rating < 0 or request.rating > 1:
                context.abort(grpc.StatusCode.INVALID_ARGUMENT,
                              errors.REFERENCE_INVALID_RATING)

            other_reference = (session.query(Reference).filter(
                Reference.host_request_id == host_request.conversation_id
            ).filter(Reference.to_user_id == context.user_id).one_or_none())

            reference = Reference(
                from_user_id=context.user_id,
                host_request_id=host_request.conversation_id,
                text=request.text,
                rating=request.rating,
                was_appropriate=request.was_appropriate,
            )

            if host_request.from_user_id == context.user_id:
                # we requested to surf with someone
                reference.reference_type = ReferenceType.surfed
                reference.to_user_id = host_request.to_user_id
                assert context.user_id == host_request.from_user_id
            else:
                # we hosted someone
                reference.reference_type = ReferenceType.hosted
                reference.to_user_id = host_request.from_user_id
                assert context.user_id == host_request.to_user_id

            session.add(reference)
            session.commit()

            # send the recipient of the reference an email
            send_host_reference_email(reference,
                                      both_written=other_reference is not None)

            return reference_to_pb(reference, context.user_id)
Beispiel #2
0
def create_host_reference(session,
                          from_user_id,
                          to_user_id,
                          reference_age,
                          *,
                          surfing=True,
                          host_request_id=None):
    if host_request_id:
        actual_host_request_id = host_request_id
    else:
        if surfing:
            actual_host_request_id = host_request_id or create_host_request(
                session, from_user_id, to_user_id,
                reference_age - timedelta(days=1))
        else:
            actual_host_request_id = host_request_id or create_host_request(
                session, to_user_id, from_user_id,
                reference_age - timedelta(days=1))

    host_request = session.query(HostRequest).filter(
        HostRequest.conversation_id == actual_host_request_id).one()

    other_reference = (session.query(Reference).filter(
        Reference.host_request_id == host_request.conversation_id).filter(
            Reference.to_user_id == from_user_id).one_or_none())

    reference = Reference(
        time=now() - reference_age,
        from_user_id=from_user_id,
        host_request_id=host_request.conversation_id,
        text="Dummy reference",
        rating=0.5,
        was_appropriate=True,
    )

    if host_request.from_user_id == from_user_id:
        reference.reference_type = ReferenceType.surfed
        reference.to_user_id = host_request.to_user_id
        assert from_user_id == host_request.from_user_id
    else:
        reference.reference_type = ReferenceType.hosted
        reference.to_user_id = host_request.from_user_id
        assert from_user_id == host_request.to_user_id

    session.add(reference)
    session.commit()
    return reference.id, actual_host_request_id
def create_host_reference(session,
                          from_user_id,
                          to_user_id,
                          reference_age,
                          *,
                          surfing=True,
                          host_request_id=None):
    if host_request_id:
        actual_host_request_id = host_request_id
    else:
        if surfing:
            actual_host_request_id = host_request_id or create_host_request(
                session, from_user_id, to_user_id,
                reference_age + timedelta(days=1))
        else:
            actual_host_request_id = host_request_id or create_host_request(
                session, to_user_id, from_user_id,
                reference_age + timedelta(days=1))

    host_request = session.execute(
        select(HostRequest).where(HostRequest.conversation_id ==
                                  actual_host_request_id)).scalar_one()

    reference = Reference(
        time=now() - reference_age,
        from_user_id=from_user_id,
        host_request_id=host_request.conversation_id,
        text="Dummy reference",
        rating=0.5,
        was_appropriate=True,
    )

    if host_request.surfer_user_id == from_user_id:
        reference.reference_type = ReferenceType.surfed
        reference.to_user_id = host_request.host_user_id
        assert from_user_id == host_request.surfer_user_id
    else:
        reference.reference_type = ReferenceType.hosted
        reference.to_user_id = host_request.surfer_user_id
        assert from_user_id == host_request.host_user_id

    session.add(reference)
    session.commit()
    return reference.id, actual_host_request_id
Beispiel #4
0
    def WriteHostRequestReference(self, request, context):
        with session_scope() as session:
            check_valid_reference(request, context)

            host_request = session.execute(
                select(HostRequest).where_users_column_visible(
                    context,
                    HostRequest.surfer_user_id).where_users_column_visible(
                        context, HostRequest.host_user_id).
                where(HostRequest.conversation_id ==
                      request.host_request_id).where(
                          or_(HostRequest.surfer_user_id == context.user_id,
                              HostRequest.host_user_id ==
                              context.user_id))).scalar_one_or_none()

            if not host_request:
                context.abort(grpc.StatusCode.NOT_FOUND,
                              errors.HOST_REQUEST_NOT_FOUND)

            if not host_request.can_write_reference:
                context.abort(grpc.StatusCode.FAILED_PRECONDITION,
                              errors.CANT_WRITE_REFERENCE_FOR_REQUEST)

            if session.execute(
                    select(Reference).where(
                        Reference.host_request_id ==
                        host_request.conversation_id).where(
                            Reference.from_user_id ==
                            context.user_id)).scalar_one_or_none():
                context.abort(grpc.StatusCode.FAILED_PRECONDITION,
                              errors.REFERENCE_ALREADY_GIVEN)

            other_reference = session.execute(
                select(Reference).where(
                    Reference.host_request_id == host_request.conversation_id).
                where(Reference.to_user_id ==
                      context.user_id)).scalar_one_or_none()

            reference = Reference(
                from_user_id=context.user_id,
                host_request_id=host_request.conversation_id,
                text=request.text.strip(),
                private_text=request.private_text.strip(),
                rating=request.rating,
                was_appropriate=request.was_appropriate,
            )

            if host_request.surfer_user_id == context.user_id:
                # we requested to surf with someone
                reference.reference_type = ReferenceType.surfed
                reference.to_user_id = host_request.host_user_id
                assert context.user_id == host_request.surfer_user_id
            else:
                # we hosted someone
                reference.reference_type = ReferenceType.hosted
                reference.to_user_id = host_request.surfer_user_id
                assert context.user_id == host_request.host_user_id

            session.add(reference)
            session.commit()

            # send the recipient of the reference an email
            send_host_reference_email(reference,
                                      both_written=other_reference is not None)

            # possibly send out an alert to the mod team if the reference was bad
            maybe_send_reference_report_email(reference)

            return reference_to_pb(reference, context)