def change_broup_colour(data): token = data["token"] broup_id = data["broup_id"] colour = data["colour"] logged_in_bro = Bro.verify_auth_token(token) if logged_in_bro is None: emit("message_event_change_broup_colour_failed", "token authentication failed", room=request.sid) else: broup_objects = Broup.query.filter_by(broup_id=broup_id) if broup_objects is None: emit("message_event_change_broup_details_failed", "broup finding failed", room=request.sid) else: for broup in broup_objects: broup.update_colour(colour) db.session.add(broup) update_broups(broup_objects) # We create an info message with the person who did the update as sender broup_message = BroupMessage(sender_id=logged_in_bro.id, broup_id=broup_id, body="%s changed the chat colour" % logged_in_bro.get_full_name(), text_message="", timestamp=datetime.utcnow(), info=True) db.session.add(broup_message) db.session.commit() broup_room = "broup_%s" % broup_id emit("message_event_send", broup_message.serialize, room=broup_room)
def send_message_broup(data): bro_id = data["bro_id"] broup_id = data["broup_id"] message = data["message"] text_message = data["text_message"] message_data = None if "message_data" in data: message_data = data["message_data"] broup_message = BroupMessage( sender_id=bro_id, broup_id=broup_id, body=message, text_message=text_message, timestamp=datetime.utcnow(), info=False, data=message_data ) broup_objects = Broup.query.filter_by(broup_id=broup_id) bro_ids = [] if broup_objects is None: emit("message_event_send_broup", "broup finding failed", room=request.sid) else: for broup in broup_objects: if broup.bro_id == bro_id: # The bro that send the message obviously also read it. broup.update_last_message_read_time_bro() bro_ids = broup.get_participants() broup.update_last_activity() else: # The other bro's now gets an extra unread message and their chat is moved to the top of their list. if not broup.has_left() and not broup.is_removed(): broup.update_unread_messages() broup.update_last_activity() broup.check_mute() db.session.add(broup) db.session.add(broup_message) db.session.commit() update_broups(broup_objects) send_notification_broup(bro_ids, message, broup_id, broup_objects, bro_id) broup_room = "broup_%s" % broup_id emit("message_event_send", broup_message.serialize, room=broup_room)
def change_broup_add_admin(data): token = data["token"] broup_id = data["broup_id"] bro_id = data["bro_id"] logged_in_bro = Bro.verify_auth_token(token) new_admin_bro = Bro.query.filter_by(id=bro_id).first() if logged_in_bro is None or new_admin_bro is None: emit("message_event_change_broup_add_admin_failed", "token authentication failed", room=request.sid) else: broup_objects = Broup.query.filter_by(broup_id=broup_id) if broup_objects is None: emit("message_event_change_broup_add_admin_failed", "broup finding failed", room=request.sid) else: for broup in broup_objects: broup.add_admin(bro_id) db.session.add(broup) db.session.commit() # We create an info message with the person who is admin as sender (even if they didn't send the update) broup_message = BroupMessage(sender_id=bro_id, broup_id=broup_id, body="%s has been made admin" % new_admin_bro.get_full_name(), text_message="", timestamp=datetime.utcnow(), info=True) db.session.add(broup_message) db.session.commit() update_broups(broup_objects) broup_room = "broup_%s" % broup_id emit("message_event_send", broup_message.serialize, room=broup_room)
def add_bro_to_broup(data): token = data["token"] logged_in_bro = Bro.verify_auth_token(token) if not logged_in_bro: emit("message_event_add_bro_to_broup_failed", "adding bro to broup failed", room=request.sid) else: broup_id = data["broup_id"] bro_id = data["bro_id"] broup_objects = Broup.query.filter_by(broup_id=broup_id) if broup_objects is None: emit("message_event_add_bro_to_broup_failed", "adding bro to broup failed", room=request.sid) return else: new_bro_for_broup = Bro.query.filter_by(id=bro_id).first() if not new_bro_for_broup: emit("message_event_add_bro_to_broup_failed", "adding bro to broup failed", room=request.sid) return bro_broup = Broup.query.filter_by(broup_id=broup_id, bro_id=bro_id).first() if bro_broup is not None and not bro_broup.has_left() and not bro_broup.is_removed(): emit("message_event_add_bro_to_broup_failed", "adding bro to broup failed", room=request.sid) else: broup_name = "" broup_colour = "" broup_description = "" bro_ids = [bro_id] admins = [] for broup in broup_objects: if not broup.has_left() and not broup.is_removed(): broup_name = broup.get_broup_name() + "" + new_bro_for_broup.get_bromotion() broup.set_broup_name(broup_name) broup.add_participant(bro_id) db.session.add(broup) # Appending the bro ids from each of the broups bro_ids.append(broup.get_bro_id()) # These should be the same in all broup objects admins = broup.get_admins() broup_colour = broup.get_broup_colour() broup_description = broup.get_broup_description() new_bro_room = "room_%s" % new_bro_for_broup.id if bro_broup: # If a broup object already exists than it has to be because the bro left or removed this broup. # We add the bro again to the broup bro_broup.set_participants(bro_ids) bro_broup.set_broup_name(broup_name) bro_broup.set_admins(admins) bro_broup.update_description(broup_description) bro_broup.update_colour(broup_colour) bro_broup.rejoin() db.session.add(bro_broup) emit("message_event_added_to_broup", bro_broup.serialize, room=new_bro_room) else: b = new_bro_for_broup.add_broup(broup_name, broup_id, bro_ids, broup_colour, admins, broup_description) if b is not None: emit("message_event_added_to_broup", b.serialize, room=new_bro_room) broup_message = BroupMessage( sender_id=bro_id, broup_id=broup_id, body="%s has been added to the broup!" % new_bro_for_broup.get_full_name(), text_message="", timestamp=datetime.utcnow(), info=True ) db.session.add(broup_message) db.session.commit() broup_room = "broup_%s" % broup_id emit("message_event_send", broup_message.serialize, room=broup_room) update_broups(broup_objects)
def change_broup_remove_bro(data): token = data["token"] broup_id = data["broup_id"] bro_id = data["bro_id"] logged_in_bro = Bro.verify_auth_token(token) if logged_in_bro is None: emit("message_event_change_broup_remove_bro_failed", "token authentication failed", room=request.sid) else: broup_objects = Broup.query.filter_by(broup_id=broup_id) remove_broup = Broup.query.filter_by(broup_id=broup_id, bro_id=bro_id).first() remove_bro = Bro.query.filter_by(id=bro_id).first() if broup_objects is None or remove_broup is None or remove_bro is None: emit("message_event_change_broup_remove_bro_failed", "broup finding failed", room=request.sid) else: remove_bromotion = remove_bro.get_bromotion() for broup in broup_objects: broup_name = remove_last_occur(broup.get_broup_name(), remove_bromotion) broup.set_broup_name(broup_name) broup.remove_bro(bro_id) db.session.add(broup) # It's possible that the user who left was the only admin in the broup at the moment. # When there are no admins left we randomly assign a user to be admin. if len(remove_broup.get_admins()) == 0: # In this special event we will make everyone remaining an admin. for broup in broup_objects: if not broup.has_left() and not broup.is_removed(): new_admin_id = broup.bro_id for broup2 in broup_objects: broup2.add_admin(new_admin_id) db.session.add(broup2) # It's possible, that the user left the broup and that there are no more bros left. # In this case we will remove all the messages if len(remove_broup.get_participants()) == 0: messages = BroupMessage.query.filter_by(broup_id=broup_id) for message in messages: db.session.delete(message) remove_broup.leave_broup() db.session.add(remove_broup) leave_message = "%s has been removed" % remove_bro.get_full_name() if bro_id == logged_in_bro.id: leave_message = "%s has left" % remove_bro.get_full_name() broup_message = BroupMessage( sender_id=bro_id, broup_id=broup_id, body=leave_message, text_message="", timestamp=datetime.utcnow(), info=True ) db.session.add(broup_message) db.session.commit() update_broups(broup_objects) broup_room = "broup_%s" % broup_id emit("message_event_send", broup_message.serialize, room=broup_room) bro_room = "room_%s" % remove_broup.bro_id emit("message_event_chat_changed", remove_broup.serialize, room=bro_room)