コード例 #1
0
def route_create_favorite(post_id: str):
    uid = request.headers.get("uid", None)
    if not uid:
        abort(401)

    user = User.objects.get_or_404(uid=uid)
    post = Post.objects.get_or_404(id=post_id)
    post.update(add_to_set__favorite_users=user,
                add_to_set__favorite_user_ids=user.id)

    user_from = user
    user_to = post.author

    alert = alerts_blueprint.create_alert(
        user_from=user_from,
        user_to=user_to,
        push_type="FAVORITE",
        post=post,
        message="{nick_name} 님이 당신의 게시물을 좋아합니다.".format(
            nick_name=user_from.nick_name))
    push_item = alert.records[-1]
    data = alerts_blueprint.dictify_push_item(push_item)
    message_service.push(data, user_to.r_token)

    return Response(post.to_json(follow_reference=True, max_depth=1),
                    mimetype="application/json")
コード例 #2
0
def route_update_star_rating(user_id: str, score: int):
    """Endpoint for getting users."""
    uid = request.headers.get("uid", None)
    
    user_from = User.objects.get_or_404(uid=uid)
    user_to = User.objects.get_or_404(id=user_id)
    
    past_star_rating = StarRating.objects(
        user_from=user_from, user_to=user_to).first()
    
    if not past_star_rating:
        StarRating(
            user_from=user_from, user_to=user_to,
            rated_at=pendulum.now().int_timestamp,
            score=score
        ).save()
        if score > 3:
            alert = alerts_blueprint.create_alert(
                user_from=user_from, user_to=user_to,
                push_type="STAR_RATING",
                message="{nick_name} 님이 당신을 높게 평가 하였습니다.".format(
                    nick_name=user_from.nick_name))
            push_item = alert.records[-1]
            data = alerts_blueprint.dictify_push_item(push_item)
            message_service.push(data, user_to.r_token)
    
    return Response("", mimetype="application/json")
コード例 #3
0
def route_poke(user_id_to):
    uid = request.headers.get("uid", None)
    
    user_from = User.objects(uid=uid).get_or_404()
    
    user_to = User.objects(id=user_id_to).get_or_404()
    
    alert = alerts_blueprint.create_alert(
        user_from, user_to, push_type="POKE",
        message="{nick_name} 님이 당신을 찔렀습니다.".format(
            nick_name=user_from.nick_name))
    push_item = alert.records[-1]
    data: dict = alerts_blueprint.dictify_push_item(push_item)
    
    try:
        message = messaging.Message(
            data=data, token=user_to.r_token,
            apns=messaging.APNSConfig(),
            android=messaging.AndroidConfig(priority="high"),
            notification=messaging.Notification())
        messaging.send(message)
    except Exception as e:
        logging.exception(e)
    
    return Response(
        user_to.to_json(),
        mimetype="application/json")
コード例 #4
0
def route_update_chat_room_available(room_id: str, available: bool):
    id_token = request.headers.get("id_token", None)
    decoded_token = auth.verify_id_token(id_token)
    uid_to_verify = decoded_token['uid']
    uid = request.headers.get("uid", None)

    if uid_to_verify != uid:
        raise Exception("Illegal verify_id_token found.")

    chat_room = ChatRoom.objects.get_or_404(id=room_id)
    room_open_user = next(
        (member for member in chat_room.members if member.uid == uid), None)
    if not room_open_user:
        raise Exception("If not belong to the chat room, can't open it.")

    chat_room.available = bool(available)
    chat_room.available_at = pendulum.now().int_timestamp
    chat_room.save()

    for user_to in chat_room.members:
        if user_to.uid == room_open_user.uid:
            continue
        push = AlertRecord(push_type="OPENED",
                           user_id=room_open_user.id,
                           created_at=pendulum.now().int_timestamp,
                           chat_room_id=chat_room.id,
                           message="{nick_name} 님이 대화방을 열었습니다.".format(
                               nick_name=room_open_user.nick_name))
        data = alerts_blueprint.dictify_push_item(push)
        message_service.push(data, user_to.r_token)

    return Response("", mimetype="application/json")
コード例 #5
0
def route_create_message(room_id: str, message: str):
    uid = request.headers.get("uid", None)

    user = User.objects.get_or_404(uid=uid)

    message = Message(user_id=str(user.id),
                      message=message,
                      created_at=pendulum.now().int_timestamp)

    chat_room = ChatRoom.objects.get_or_404(id=room_id, members=user)
    chat_room.messages.append(message)
    chat_room.save()

    user_from = user
    user_to_list = [
        member for member in chat_room.members if member.id != user.id
    ]

    for user_to in user_to_list:
        push = AlertRecord(push_type="MESSAGE",
                           user_id=user_from.id,
                           created_at=pendulum.now().int_timestamp,
                           chat_room_id=chat_room.id,
                           message_id=message.id,
                           message=message.message)
        data = alerts_blueprint.dictify_push_item(push)
        message_service.push(data, user_to.r_token)

    return Response(message.to_json(), mimetype="application/json")
コード例 #6
0
def route_create_request(user_id: str, r_type: int):
    """Endpoint to request like."""
    id_token = request.headers.get("id_token", None)
    decoded_token = auth.verify_id_token(id_token)
    uid_to_verify = decoded_token["uid"]
    uid = request.headers.get("uid", None)
    
    if uid_to_verify != uid:
        raise Exception("Illegal verify_id_token found.")
    
    user_from = User.objects.get_or_404(uid=uid)  # me
    user_to = User.objects.get_or_404(id=user_id)  # target
    
    # checks if there is a one I have already sent
    request_i_sent = Request.objects(
        user_to=user_to,  # target
        user_from=user_from  # me
    ).first()
    
    if request_i_sent:
        raise ValueError(
            "a duplicate request already exists.")
    
    # checks if there is a one I have received.
    request_i_received = Request.objects(
        user_to=user_from, user_from=user_to).first()
    
    if request_i_received:
        if request_i_received.response == None:
            return route_update_response_of_request(
                request_i_received.id, 1)
        else:
            raise ValueError(
                "a duplicate request already exists.")
    
    _request = Request(
        user_from=user_from, user_to=user_to,
        request_type_id=r_type,
        requested_at=pendulum.now().int_timestamp,
        response=None, responded_at=None)
    _request.save()
    
    alert = alerts_blueprint.create_alert(
        user_from=user_from, user_to=user_to,
        push_type="REQUEST", _request=_request,
        message="{nick_name} 님이 당신에게 친구 신청을 보냈습니다.".format(
            nick_name=user_from.nick_name))
    push_item = alert.records[-1]
    data = alerts_blueprint.dictify_push_item(push_item)
    message_service.push(data, user_to.r_token)
    
    return Response(
        _request.to_json(follow_reference=True, max_depth=1),
        mimetype="application/json")
コード例 #7
0
def route_update_response_of_request(rid: str, result: int):
    """Updates a received like request.
       ACCEPT: 1
       DECLINE: 0
    """
    
    uid = request.headers.get("uid", None)
    me = User.objects.get_or_404(uid=uid)
    
    _request = Request.objects(id=rid).get_or_404()
    
    if _request.user_to.id != me.id:
        abort(400)
    
    # update request table.
    _request.response = result
    _request.responded_at = pendulum.now().int_timestamp
    _request.save()
    
    if int(result) == 1:
        # create chat room
        chat_room = ChatRoom(
            title=None,
            members=[_request.user_from, _request.user_to],
            members_history=[_request.user_from, _request.user_to],
            created_at=pendulum.now().int_timestamp)
        chat_room.save()
        
        # watch out here..
        user_from = _request.user_to
        user_to = _request.user_from
        
        alert = alerts_blueprint.create_alert(
            user_from=user_from, user_to=user_to,
            push_type="MATCHED", _request=_request, chat_room=chat_room,
            message="{nick_name} 님과 연결 되었습니다.".format(
                nick_name=_request.user_to.nick_name))
        push_item = alert.records[-1]
        data = alerts_blueprint.dictify_push_item(push_item)
        message_service.push(data, user_to.r_token)
    
    return Response(
        _request.to_json(follow_reference=True, max_depth=1),
        mimetype="application/json")
コード例 #8
0
def route_create_thumb_up(post_id, comment_id):
    uid = request.headers.get("uid", None)
    if not uid:
        abort(401)

    user = User.objects.get_or_404(uid=uid)
    post = Post.objects.get_or_404(id=post_id)
    comment = next(
        (comment
         for comment in post.comments if str(comment.id) == comment_id), None)

    if not comment:
        abort(404)

    comment.update(add_to_set__thumb_up_user_ids=user.id,
                   pull__thumb_down_user_ids=user.id)
    comment = Comment.objects.get_or_404(id=comment_id)

    if user.id == post.author.id:
        user_from = user
        user_to = comment.user

        alert = alerts_blueprint.create_alert(
            user_from=user_from,
            user_to=user_to,
            push_type="THUMB_UP",
            post=post,
            comment=comment,
            message="{nick_name} 님이 당신의 댓글을 좋아합니다.".format(
                nick_name=user_from.nick_name))
        push_item = alert.records[-1]
        data = alerts_blueprint.dictify_push_item(push_item)
        message_service.push(data, user_to.r_token)

    return Response(comment.to_json(follow_reference=True, max_depth=2),
                    mimetype="application/json")