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 = User.get(id=comment.user_id) alarm = Alarm.create_alarm( user_from=user_from, user_to=user_to, event=Alarm.Event.COMMENT_THUMB_UP, post=post, comment=comment, message="{nickname} 님이 당신의 댓글을 좋아합니다.".format( nickname=user_from.nickname)) alarm_record = alarm.records[-1] data = alarm_record.as_dict() message_service.push(data, user_to) return Response("", mimetype="application/json")
def route_push_poke(user_id): uid = request.headers.get("uid", None) user_from = User.get(uid=uid) user_to = User.get(id=user_id) alarm = Alarm.create_alarm( event=Alarm.Event.POKE, user_from=user_from, user_to=user_to, message="{nickname} 님이 당신을 찔렀습니다.".format(nickname=user_from.nickname) ) alarm_record = alarm.records[-1] data: dict = alarm_record.as_dict() message_service.push(data, user_to) return Response("", mimetype="application/json")
def route_update_response_of_request(request_id: 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.get_or_404(id=request_id) if _request.user_to.id != me.id: abort(400) # update request table. _request.response = result _request.responded_at = pendulum.now().int_timestamp _request.save() _request.reload() if int(result) == 1: _request.user_to.remove_user_from_recommendation(_request.user_from) # create chat room conversation = Conversation( title=None, participants=[_request.user_from, _request.user_to], references=[_request.user_from, _request.user_to], created_at=pendulum.now().int_timestamp) conversation.save() conversation.reload() # alarm by push below user_alarm_from = _request.user_to user_alarm_to = _request.user_from alarm = Alarm.create_alarm(user_from=user_alarm_from, user_to=user_alarm_to, event=Alarm.Event.MATCHED, request=_request, conversation=conversation, message="{nickname} 님과 연결 되었습니다.".format( nickname=_request.user_to.nickname)) alarm_record = alarm.records[-1] data = alarm_record.as_dict() message_service.push(data, user_alarm_to) response = encode(Request.get(id=_request.id)) return Response(response, mimetype="application/json")
def route_create_comment(post_id: str): uid = request.headers.get("uid", None) if not uid: abort(401) # if exists, create a comment as a sub comment comment_id = request.form.get("comment_id", None) # parent_comment_id comment = request.form.get("comment", "") post = Post.objects.get_or_404(id=post_id) user = User.objects.get_or_404(uid=uid) comment_to_create = Comment( post_id=post_id, user_id=user.id, comment=comment, comments=[], # child comments created_at=pendulum.now().int_timestamp, is_deleted=False).save() post.add_comment(comment_to_create, parent_id=comment_id) alarm = Alarm.create_alarm( user_from=user, user_to=post.author, event=Alarm.Event.COMMENT, post=post, comment=comment_to_create, message="{nickname} 님이 당신의 게시물에 댓글을 남겼습니다.".format( nickname=user.nickname)) alarm_record = alarm.records[-1] data = alarm_record.as_dict() message_service.push(data, post.author) comment = comment_to_create.to_mongo() comment["commenter"] = User.get(id=user.id).to_mongo() response = encode(comment) return Response(response, mimetype="application/json")
def route_update_star_rating(user_id: str, score: int): """Endpoint for getting users.""" uid = request.headers.get("uid", None) user_from = User.get(uid=uid) user_to = User.get(id=user_id) is_already_rated = StarRating.objects(user_from=user_from, user_to=user_to).first() if not is_already_rated: StarRating( user_from=user_from, user_to=user_to, rated_at=pendulum.now().int_timestamp, score=score ).save() if score > 3: alarm = Alarm.create_alarm( user_from=user_from, user_to=user_to, event=Alarm.Event.STAR_RATING, message="{nickname} 님이 당신을 높게 평가 하였습니다.".format(nickname=user_from.nickname)) alarm_record = alarm.records[-1] data = alarm_record.as_dict() message_service.push(data, user_to) star_ratings = StarRating.objects(user_to=user_to).all() star_rating_sum = 0 for rating in star_ratings: star_rating_sum += rating.score star_rating_avg = star_rating_sum / len(star_ratings) user_to.update(star_rating_avg=star_rating_avg) return Response("", mimetype="application/json")
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_user_ids=user.id) user_from = user user_to = post.author alarm = Alarm.create_alarm(user_from=user_from, user_to=user_to, event=Alarm.Event.POST_FAVORITE, post=post, message="{nickname} 님이 당신의 게시물을 좋아합니다.".format( nickname=user_from.nickname)) alarm_record: AlarmRecord = alarm.records[-1] data: dict = alarm_record.as_dict() message_service.push(data, user_to) return Response("", mimetype="application/json")
def route_create_request(user_id: str, r_type: int): """Endpoint to request like.""" uid = request.headers.get("uid", None) # already verified uid. user = User.objects.get_or_404(uid=uid) user_from = user # 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, user_from=user_from).first() if request_i_sent: raise ValueError("a duplicate request already exists.") # checks if there is a one I have already received. request_i_received = Request.objects(user_to=user_from, user_from=user_to).first() if request_i_received: if request_i_received.response is None: return route_update_response_of_request(request_i_received.id, 1) else: raise ValueError("a duplicate request already exists.") is_available_for_free = user_from.is_available_for_free_pass_token() amount_remaining = user_from.get_current_amount_of_point() if not is_available_for_free and amount_remaining <= 0: raise Exception("Unavailable for the request.") _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() _request.reload() # if the target exists in recommendation, remove them. user_from.remove_user_from_recommendation(user_to) if is_available_for_free: user_from.consume_free_pass_token() else: user_from.consume(5) alarm = Alarm.create_alarm( user_from=user_from, user_to=user_to, event=Alarm.Event.REQUEST, request=_request, message="{nickname} 님이 당신에게 친구 신청을 보냈습니다.".format( nickname=user_from.nickname)) alarm_record = alarm.records[-1] data = alarm_record.as_dict() message_service.push(data, user_to) response = encode(Request.get(id=_request.id)) return Response(response, mimetype="application/json")