コード例 #1
0
    def SendFriendRequest(self, request, context):
        with session_scope() as session:
            from_user = session.query(User).filter(
                User.id == context.user_id).one_or_none()

            if not from_user:
                context.abort(grpc.StatusCode.NOT_FOUND, errors.USER_NOT_FOUND)

            to_user = session.query(User).filter(
                User.id == request.user_id).one_or_none()

            if not to_user:
                context.abort(grpc.StatusCode.NOT_FOUND, errors.USER_NOT_FOUND)

            if get_friends_status(
                    session, from_user.id,
                    to_user.id) != api_pb2.User.FriendshipStatus.NOT_FRIENDS:
                context.abort(grpc.StatusCode.FAILED_PRECONDITION,
                              errors.FRIENDS_ALREADY_OR_PENDING)

            # Race condition!

            friend_relationship = FriendRelationship(
                from_user=from_user,
                to_user=to_user,
                status=FriendStatus.pending)
            session.add(friend_relationship)

            send_friend_request_email(friend_relationship)

            return empty_pb2.Empty()
コード例 #2
0
    def InviteToGroupChat(self, request, context):
        with session_scope(self._Session) as session:
            result = (session.query(GroupChatSubscription, GroupChat).join(
                GroupChat, GroupChat.conversation_id ==
                GroupChatSubscription.group_chat_id).filter(
                    GroupChatSubscription.group_chat_id ==
                    request.group_chat_id).filter(
                        GroupChatSubscription.user_id == context.user_id).
                      filter(GroupChatSubscription.left == None).one_or_none())

            if not result:
                context.abort(grpc.StatusCode.NOT_FOUND, errors.CHAT_NOT_FOUND)

            your_subscription, group_chat = result

            if not your_subscription or not group_chat:
                context.abort(grpc.StatusCode.NOT_FOUND, errors.CHAT_NOT_FOUND)

            if request.user_id == context.user_id:
                context.abort(grpc.StatusCode.FAILED_PRECONDITION,
                              errors.CANT_INVITE_SELF)

            if your_subscription.role != GroupChatRole.admin and your_subscription.group_chat.only_admins_invite:
                context.abort(grpc.StatusCode.PERMISSION_DENIED,
                              errors.INVITE_PERMISSION_DENIED)

            if group_chat.is_dm:
                context.abort(grpc.StatusCode.FAILED_PRECONDITION,
                              errors.CANT_INVITE_TO_DM)

            their_subscription = (session.query(GroupChatSubscription).filter(
                GroupChatSubscription.group_chat_id == request.group_chat_id
            ).filter(GroupChatSubscription.user_id == request.user_id).filter(
                GroupChatSubscription.left == None).one_or_none())

            if their_subscription:
                context.abort(grpc.StatusCode.FAILED_PRECONDITION,
                              errors.ALREADY_IN_CHAT)

            # TODO: race condition!

            if get_friends_status(
                    session, context.user_id,
                    request.user_id) != api_pb2.User.FriendshipStatus.FRIENDS:
                context.abort(grpc.StatusCode.FAILED_PRECONDITION,
                              errors.GROUP_CHAT_ONLY_INVITE_FRIENDS)

            subscription = GroupChatSubscription(
                user_id=request.user_id,
                group_chat=your_subscription.group_chat,
                role=GroupChatRole.participant,
            )
            session.add(subscription)

            _add_message_to_subscription(session,
                                         your_subscription,
                                         message_type=MessageType.user_invited,
                                         target_id=request.user_id)

        return empty_pb2.Empty()
コード例 #3
0
    def GetUser(self, request, context):
        with session_scope(self._Session) as session:
            user = get_user_by_field(session, request.user)
            if not user:
                context.abort(grpc.StatusCode.NOT_FOUND, "No such user.")

            return api_pb2.User(
                username=user.username,
                name=user.name,
                city=user.city,
                verification=user.verification,
                community_standing=user.community_standing,
                num_references=0,
                gender=user.gender,
                age=user.age,
                color=user.color,
                joined=Timestamp_from_datetime(user.display_joined),
                last_active=Timestamp_from_datetime(user.display_last_active),
                occupation=user.occupation,
                about_me=user.about_me,
                about_place=user.about_place,
                languages=user.languages.split("|") if user.languages else [],
                countries_visited=user.countries_visited.split("|")
                if user.countries_visited else [],
                countries_lived=user.countries_lived.split("|")
                if user.countries_lived else [],
                friends=get_friends_status(session, context.user_id, user.id),
            )
コード例 #4
0
    def SendFriendRequest(self, request, context):
        with session_scope(self._Session) as session:
            from_user = session.query(User).filter(
                User.id == context.user_id).one_or_none()

            if not from_user:
                context.abort(grpc.StatusCode.NOT_FOUND, "User not found.")

            to_user = get_user_by_field(session, request.user)

            if not to_user:
                context.abort(grpc.StatusCode.NOT_FOUND, "User not found.")

            if get_friends_status(
                    session, from_user.id,
                    to_user.id) != api_pb2.User.FriendshipStatus.NOT_FRIENDS:
                context.abort(
                    grpc.StatusCode.FAILED_PRECONDITION,
                    "Can't send friend request. Already friends or pending.")

            # Race condition!

            friend_relationship = FriendRelationship(
                from_user=from_user,
                to_user=to_user,
                status=FriendStatus.pending,
            )
            session.add(friend_relationship)

            return empty_pb2.Empty()
コード例 #5
0
ファイル: api.py プロジェクト: telalpal/couchers
def user_model_to_pb(db_user, session, context):
    num_references = session.query(Reference.from_user_id).filter(Reference.to_user_id == db_user.id).count()

    # returns (lat, lng)
    # we put people without coords on null island
    # https://en.wikipedia.org/wiki/Null_Island
    lat, lng = db_user.coordinates or (0, 0)

    user = api_pb2.User(
        user_id=db_user.id,
        username=db_user.username,
        name=db_user.name,
        city=db_user.city,
        hometown=db_user.hometown,
        lat=lat,
        lng=lng,
        radius=db_user.geom_radius,
        verification=db_user.verification,
        community_standing=db_user.community_standing,
        num_references=num_references,
        gender=db_user.gender,
        pronouns=db_user.pronouns,
        age=db_user.age,
        joined=Timestamp_from_datetime(db_user.display_joined),
        last_active=Timestamp_from_datetime(db_user.display_last_active),
        hosting_status=hostingstatus2api[db_user.hosting_status],
        meetup_status=meetupstatus2api[db_user.meetup_status],
        occupation=db_user.occupation,
        education=db_user.education,
        about_me=db_user.about_me,
        my_travels=db_user.my_travels,
        things_i_like=db_user.things_i_like,
        about_place=db_user.about_place,
        languages=db_user.languages.split("|") if db_user.languages else [],
        countries_visited=db_user.countries_visited.split("|") if db_user.countries_visited else [],
        countries_lived=db_user.countries_lived.split("|") if db_user.countries_lived else [],
        additional_information=db_user.additional_information,
        friends=get_friends_status(session, context.user_id, db_user.id),
        mutual_friends=[
            api_pb2.MutualFriend(user_id=mutual_friend.id, username=mutual_friend.username, name=mutual_friend.name)
            for mutual_friend in db_user.mutual_friends(context.user_id)
        ],
        smoking_allowed=smokinglocation2api[db_user.smoking_allowed],
        sleeping_arrangement=sleepingarrangement2api[db_user.sleeping_arrangement],
        parking_details=parkingdetails2api[db_user.parking_details],
        avatar_url=db_user.avatar.thumbnail_url if db_user.avatar else None,
    )

    if db_user.max_guests is not None:
        user.max_guests.value = db_user.max_guests

    if db_user.last_minute is not None:
        user.last_minute.value = db_user.last_minute

    if db_user.has_pets is not None:
        user.has_pets.value = db_user.has_pets

    if db_user.accepts_pets is not None:
        user.accepts_pets.value = db_user.accepts_pets

    if db_user.pet_details is not None:
        user.pet_details.value = db_user.pet_details

    if db_user.has_kids is not None:
        user.has_kids.value = db_user.has_kids

    if db_user.accepts_kids is not None:
        user.accepts_kids.value = db_user.accepts_kids

    if db_user.kid_details is not None:
        user.kid_details.value = db_user.kid_details

    if db_user.has_housemates is not None:
        user.has_housemates.value = db_user.has_housemates

    if db_user.housemate_details is not None:
        user.housemate_details.value = db_user.housemate_details

    if db_user.wheelchair_accessible is not None:
        user.wheelchair_accessible.value = db_user.wheelchair_accessible

    if db_user.smokes_at_home is not None:
        user.smokes_at_home.value = db_user.smokes_at_home

    if db_user.drinking_allowed is not None:
        user.drinking_allowed.value = db_user.drinking_allowed

    if db_user.drinks_at_home is not None:
        user.drinks_at_home.value = db_user.drinks_at_home

    if db_user.other_host_info is not None:
        user.other_host_info.value = db_user.other_host_info

    if db_user.sleeping_details is not None:
        user.sleeping_details.value = db_user.sleeping_details

    if db_user.area is not None:
        user.area.value = db_user.area

    if db_user.house_rules is not None:
        user.house_rules.value = db_user.house_rules

    if db_user.parking is not None:
        user.parking.value = db_user.parking

    if db_user.camping_ok is not None:
        user.camping_ok.value = db_user.camping_ok

    return user
コード例 #6
0
    def CreateGroupChat(self, request, context):
        if len(request.recipient_user_ids) < 1:
            context.abort(grpc.StatusCode.INVALID_ARGUMENT,
                          errors.NO_RECIPIENTS)

        if len(request.recipient_user_ids) != len(
                set(request.recipient_user_ids)):
            # make sure there's no duplicate users
            context.abort(grpc.StatusCode.INVALID_ARGUMENT,
                          errors.INVALID_RECIPIENTS)

        if context.user_id in request.recipient_user_ids:
            context.abort(grpc.StatusCode.INVALID_ARGUMENT,
                          errors.CANT_ADD_SELF)

        with session_scope(self._Session) as session:
            if len(request.recipient_user_ids) == 1:
                # can only have one DM at a time between any two users
                other_user_id = request.recipient_user_ids[0]

                # the following query selects subscriptions that are DMs and have the same group_chat_id, and have
                # user_id either this user or the recipient user. If you find two subscriptions to the same DM group
                # chat, you know they already have a shared group chat
                count = func.count(GroupChatSubscription.id).label("count")
                if (session.query(count).filter(
                        or_(
                            GroupChatSubscription.user_id == context.user_id,
                            GroupChatSubscription.user_id == other_user_id,
                        )).filter(GroupChatSubscription.left == None).join(
                            GroupChat, GroupChat.conversation_id ==
                            GroupChatSubscription.group_chat_id).
                        filter(GroupChat.is_dm == True).group_by(
                            GroupChatSubscription.group_chat_id).having(
                                count == 2).one_or_none()):
                    context.abort(
                        grpc.StatusCode.FAILED_PRECONDITION,
                        "You already have a direct message chat with this user."
                    )

            conversation = Conversation()
            session.add(conversation)

            group_chat = GroupChat(
                conversation=conversation,
                title=request.title.value,
                creator_id=context.user_id,
                is_dm=True
                if len(request.recipient_user_ids) == 1 else False,  # TODO
            )
            session.add(group_chat)

            your_subscription = GroupChatSubscription(
                user_id=context.user_id,
                group_chat=group_chat,
                role=GroupChatRole.admin,
            )
            session.add(your_subscription)

            for recipient in request.recipient_user_ids:
                if get_friends_status(
                        session, context.user_id,
                        recipient) != api_pb2.User.FriendshipStatus.FRIENDS:
                    if len(request.recipient_user_ids) > 1:
                        context.abort(grpc.StatusCode.FAILED_PRECONDITION,
                                      errors.GROUP_CHAT_ONLY_ADD_FRIENDS)
                    else:
                        context.abort(grpc.StatusCode.FAILED_PRECONDITION,
                                      errors.DIRECT_MESSAGE_ONLY_FRIENDS)

                subscription = GroupChatSubscription(
                    user_id=recipient,
                    group_chat=group_chat,
                    role=GroupChatRole.participant,
                )
                session.add(subscription)

            _add_message_to_subscription(session,
                                         your_subscription,
                                         message_type=MessageType.chat_created)

            session.flush()

            return conversations_pb2.GroupChat(
                group_chat_id=group_chat.conversation_id,
                title=group_chat.title,
                member_user_ids=_get_visible_members_for_subscription(
                    your_subscription),
                admin_user_ids=_get_visible_admins_for_subscription(
                    your_subscription),
                only_admins_invite=group_chat.only_admins_invite,
                is_dm=group_chat.is_dm,
                created=Timestamp_from_datetime(
                    group_chat.conversation.created),
            )
コード例 #7
0
def user_model_to_pb(db_user, session, context):
    num_references = session.query(Reference.from_user_id).filter(
        Reference.to_user_id == db_user.id).count()

    user = api_pb2.User(
        user_id=db_user.id,
        username=db_user.username,
        name=db_user.name,
        city=db_user.city,
        verification=db_user.verification,
        community_standing=db_user.community_standing,
        num_references=num_references,
        gender=db_user.gender,
        age=db_user.age,
        color=db_user.color,
        joined=Timestamp_from_datetime(db_user.display_joined),
        last_active=Timestamp_from_datetime(db_user.display_last_active),
        hosting_status=hostingstatus2api[db_user.hosting_status],
        occupation=db_user.occupation,
        about_me=db_user.about_me,
        about_place=db_user.about_place,
        languages=db_user.languages.split("|") if db_user.languages else [],
        countries_visited=db_user.countries_visited.split("|")
        if db_user.countries_visited else [],
        countries_lived=db_user.countries_lived.split("|")
        if db_user.countries_lived else [],
        friends=get_friends_status(session, context.user_id, db_user.id),
        mutual_friends=[
            api_pb2.MutualFriend(user_id=mutual_friend.id,
                                 username=mutual_friend.username,
                                 name=mutual_friend.name)
            for mutual_friend in db_user.mutual_friends(context.user_id)
        ],
        smoking_allowed=smokinglocation2api[db_user.smoking_allowed],
        avatar_url=db_user.avatar_url,
    )

    if db_user.max_guests is not None:
        user.max_guests.value = db_user.max_guests

    if db_user.multiple_groups is not None:
        user.multiple_groups.value = db_user.multiple_groups

    if db_user.last_minute is not None:
        user.last_minute.value = db_user.last_minute

    if db_user.accepts_pets is not None:
        user.accepts_pets.value = db_user.accepts_pets

    if db_user.accepts_kids is not None:
        user.accepts_kids.value = db_user.accepts_kids

    if db_user.wheelchair_accessible is not None:
        user.wheelchair_accessible.value = db_user.wheelchair_accessible

    if db_user.sleeping_arrangement is not None:
        user.sleeping_arrangement.value = db_user.sleeping_arrangement

    if db_user.area is not None:
        user.area.value = db_user.area

    if db_user.house_rules is not None:
        user.house_rules.value = db_user.house_rules

    return user