def find_group(username, user, session): group = Group.find(username=username) if not group: abort(404) return response({'group': group.make_json()})
def join_group(username, user, session): group = Group.find(username=username) if not group: abort(404) group.add_user(user) ChatController.user_joined_group(group, user) return response({'joined': True, 'group': group.make_json()})
def leave_group(username, user, session): group = Group.find(username=username) if not group: abort(404) if not group.has_user(user): return error_response('you are not a member of this group') group.remove_user(user) ChatController.user_left_group(group, user) return response({'joined': True, 'group': group.make_json()})
def new_msg(cls, sender_id, recipient_id, text): """ when a user sends a new message to the server :param sender_id: sender's id :param recipient_id: recipient (group/user) id :param text: message's text :return: """ sender = User.find(id=sender_id) sender_sid = cls.get_user_sid(sender.id) if is_group(recipient_id): recipient_group = Group.find(id=recipient_id) if not recipient_group: raise Exception('recipient was not found') if not recipient_group.has_user(sender): raise Exception('user is not a member of this group') cls._broadcast_group(sender, sender_sid, recipient_group, text) elif is_user(recipient_id): recipient = User.find(id=recipient_id) if not sender.is_friends(recipient): raise Exception('user is not friends with recipient') if recipient.blocked(sender): raise Exception('recipient has blocked you') if not recipient: raise Exception('recipient was not found') cls._broadcast_user(sender, sender_sid, recipient, text) else: raise Exception('bad recipient id')