def private_message(payload): message = payload['message'] # verify recipient can receive message from sender by checking blocks and likes parent = aliased(Like, name="parent") child = aliased(Like, name="child") can_message = db.session.query(User.id) \ .outerjoin(Block, \ ((session['user_id'] == Block.blocked_id) & \ (payload['user_id'] == Block.blocked_by_id)) | \ ((session['user_id'] == Block.blocked_by_id) & \ (payload['user_id'] == Block.blocked_id))) \ .filter(Block.blocked_id == None, User.id == payload['user_id']) \ .join(parent, ((session['user_id'] == parent.liked_id) & (payload['user_id'] == parent.liked_by_id))) \ .filter(session['user_id'] == parent.liked_id) \ .join(child, \ (child.liked_id == parent.liked_by_id) & \ (child.liked_by_id == parent.liked_id)) \ .first() if can_message and can_message[0] == int(payload['user_id']): Chat.save_message(sent_by_id=session['user_id'], received_by_id=payload['user_id'], message=message, message_time=payload['timestamp']) notification = Notification.query.filter_by( owner_id=payload['user_id'], sent_by_id=payload['sender'], event_id=5).first() if not notification: Notification.create_notification(payload['user_id'], payload['sender'], 5, datetime.now()) else: notification.update_notification() if users.get(payload['user_id']): recipient_session_id = users[payload['user_id']] emit('new_private_message', payload, room=recipient_session_id)
def like_user(): try: if request.json['liked_id'].isdigit() is False: return "error" liked_id = int(request.json['liked_id']) exists = Like.query.filter_by(liked_id=liked_id, liked_by_id=session['user_id']).first() timestamp = datetime.now() if not exists: #Like like = Like.like_user(liked_id=liked_id, liked_by_id=session['user_id']) mutual_like = Like.query.filter_by(liked_id=session['user_id'], liked_by_id=liked_id).first() if mutual_like: notification = Notification.query.filter_by( owner_id=liked_id, sent_by_id=session['user_id'], event_id=3).first() if not notification: Notification.create_notification(liked_id, session['user_id'], 3, timestamp) mutual = Notification.query.filter_by( owner_id=session['user_id'], sent_by_id=liked_id, event_id=3).first() if not mutual: Notification.create_notification( session['user_id'], liked_id, 3, timestamp) else: mutual.update_notification() opposite = Notification.query.filter_by( owner_id=liked_id, sent_by_id=session['user_id'], event_id=4).first() if opposite: opposite.remove_notification() else: notification.update_notification() mutual = Notification.query.filter_by( owner_id=session['user_id'], sent_by_id=liked_id, event_id=3).first() if not mutual: Notification.create_notification( session['user_id'], liked_id, 3, timestamp) else: mutual.update_notification() opposite = Notification.query.filter_by( owner_id=liked_id, sent_by_id=session['user_id'], event_id=4).first() if opposite: opposite.remove_notification() else: notification = Notification.query.filter_by( owner_id=liked_id, sent_by_id=session['user_id'], event_id=2).first() if not notification: Notification.create_notification(liked_id, session['user_id'], 2, timestamp) opposite = Notification.query.filter_by( owner_id=liked_id, sent_by_id=session['user_id'], event_id=4).first() if opposite: opposite.remove_notification() else: notification.update_notification() if mutual_like: return jsonify(like_result="Unlike", chat=True) return jsonify(like_result="Unlike", chat=False) else: #Unlike notification = Notification.query.filter_by( owner_id=liked_id, sent_by_id=session['user_id'], event_id=4).first() #Someone unliked if not notification: Notification.create_notification(liked_id, session['user_id'], 4, timestamp) opposite = Notification.query.filter_by( owner_id=liked_id, sent_by_id=session['user_id'], event_id=2).first() if opposite: opposite.remove_notification() opposite = Notification.query.filter_by( owner_id=liked_id, sent_by_id=session['user_id'], event_id=3).first() if opposite: opposite.remove_notification() opposite = Notification.query.filter_by( owner_id=session['user_id'], sent_by_id=liked_id, event_id=3).first() opposite.remove_notification() else: notification.update_notification() opposite = Notification.query.filter_by( owner_id=liked_id, sent_by_id=session['user_id'], event_id=2).first() if opposite: opposite.remove_notification() opposite = Notification.query.filter_by( owner_id=liked_id, sent_by_id=session['user_id'], event_id=3).first() if opposite: opposite.remove_notification() opposite = Notification.query.filter_by( owner_id=session['user_id'], sent_by_id=liked_id, event_id=3).first() opposite.remove_notification() exists.remove_like() return jsonify(like_result="Like", chat=False) except: return "error"