示例#1
0
    def post(self):
        json_data = request.get_json(force=True)
        broup_id = json_data["broup_id"]
        token = json_data["token"]
        logged_in_bro = Bro.verify_auth_token(token)

        if not logged_in_bro:
            return {
                "result": False,
                "message": "Your credentials are not valid."
            }

        broup_that_is_deleted = Broup.query.filter_by(broup_id=broup_id, bro_id=logged_in_bro.id).first()
        broup_objects = Broup.query.filter_by(broup_id=broup_id)
        remove_bro = Bro.query.filter_by(id=logged_in_bro.id).first()
        if not broup_that_is_deleted or not broup_objects or not remove_bro:
            return {
                "result": False,
                "message": "Broup not found."
            }

        broup_that_is_deleted.mute_broup(True)
        broup_that_is_deleted.broup_removed()

        db.session.add(broup_that_is_deleted)
        db.session.commit()

        return {
                "result": True,
                "chat": broup_that_is_deleted.serialize
            }
示例#2
0
    def post(self):
        json_data = request.get_json(force=True)
        bros_bro_id = json_data["bros_bro_id"]
        token = json_data["token"]
        logged_in_bro = Bro.verify_auth_token(token)
        blocked = json_data["blocked"]
        if not logged_in_bro:
            return {
                "result": False,
                "message": "Your credentials are not valid."
            }
        if not bros_bro_id:
            return {"result": False, "message": "Bro not found."}

        chat = BroBros.query.filter_by(bro_id=logged_in_bro.id,
                                       bros_bro_id=bros_bro_id).first()
        if chat is None:
            return {"result": False, "message": "Chat not found."}

        if blocked == "true":
            chat.block_chat(True)
        else:
            chat.block_chat(False)
        chat.add_blocked_timestamp()
        db.session.add(chat)
        db.session.commit()

        return {"result": True, "chat": chat.serialize}
示例#3
0
    def post(self):
        json_data = request.get_json(force=True)
        token = json_data["token"]
        broup_id = json_data["broup_id"]
        logged_in_bro = Bro.verify_auth_token(token)
        if not logged_in_bro:
            return {
                "result": False,
                "message": "Your credentials are not valid."
            }

        chat = Broup.query.filter_by(broup_id=broup_id, bro_id=logged_in_bro.id).first()
        if chat is None:
            return {
                "result": False,
                "message": "Chat not found."
            }

        # We subtract a little from the last message read time. This is in case the user has the chat open and leaves
        # Sometimes not all the messages are received, we will receive them the next time
        messages = BroupMessage.query\
            .filter_by(broup_id=broup_id) \
            .filter(BroupMessage.timestamp >= (chat.last_message_read_time_bro - timedelta(minutes=5)))\
            .order_by(BroupMessage.timestamp.desc())\
            .all()

        if messages is None:
            return {'result': False}

        last_read_time = get_lowest_read_time_broup(broup_id, datetime.now())
        return {
            "result": True,
            "last_read_time_bro": last_read_time.strftime('%Y-%m-%dT%H:%M:%S.%f'),
            "message_list": [message.serialize for message in messages]
        }
    def post(self, page):
        json_data = request.get_json(force=True)
        token = json_data["token"]
        broup_id = json_data["broup_id"]
        logged_in_bro = Bro.verify_auth_token(token)
        if not logged_in_bro:
            return {
                "result": False,
                "message": "Your credentials are not valid."
            }

        chat = Broup.query.filter_by(broup_id=broup_id,
                                     bro_id=logged_in_bro.id).first()
        if chat is None:
            return {"result": False, "message": "Chat not found."}

        messages = BroupMessage.query.filter_by(broup_id=broup_id).\
            order_by(BroupMessage.timestamp.desc()).paginate(page, 20, False).items

        if messages is None:
            return {'result': False}

        last_read_time = get_lowest_read_time_broup(broup_id, datetime.now())
        return {
            "result": True,
            "last_read_time_bro":
            last_read_time.strftime('%Y-%m-%dT%H:%M:%S.%f'),
            "message_list": [message.serialize for message in messages]
        }
示例#5
0
def add_bro(data):
    token = data["token"]
    bros_bro_id = data["bros_bro_id"]
    logged_in_bro = Bro.verify_auth_token(token)
    if logged_in_bro:
        bro_to_be_added = Bro.query.filter_by(id=bros_bro_id).first()

        logged_in_bro_room = "room_%s" % logged_in_bro.id
        bro_added_exists = BroBros.query.filter_by(bro_id=logged_in_bro.id, bros_bro_id=bros_bro_id).first()
        if bro_added_exists:
            # If the bro object already exists, he is probably removed. We will undo the removing.
            bro_added_exists.re_join()
            db.session.add(bro_added_exists)
            db.session.commit()
            emit("message_event_add_bro_success", bro_added_exists.serialize, room=logged_in_bro_room)
        elif bro_to_be_added:
            if bro_to_be_added.id != logged_in_bro.id:
                chat_colour = '%02X%02X%02X' % (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
                bros_bro = logged_in_bro.add_bro(bro_to_be_added, chat_colour)
                bros_bro_added_bro = bro_to_be_added.add_bro(logged_in_bro, chat_colour)
                db.session.commit()
                if bros_bro:
                    emit("message_event_add_bro_success", bros_bro.serialize, room=logged_in_bro_room)
                else:
                    emit("message_event_add_bro_failed", "Bro was already added", room=logged_in_bro_room)

                if bros_bro_added_bro:
                    bro_room = "room_%s" % bro_to_be_added.id
                    emit("message_event_bro_added_you", bros_bro_added_bro.serialize, room=bro_room)
            else:
                emit("message_event_add_bro_failed", "You tried to add yourself", room=logged_in_bro_room)
        else:
            emit("message_event_add_bro_failed", "failed to add bro", room=logged_in_bro_room)
    else:
        emit("message_event_add_bro_failed", "failed to add bro", room=request.sid)
示例#6
0
    def post(self):
        json_data = request.get_json(force=True)
        bro_name = json_data["bro_name"]
        token = json_data["token"]
        bromotion = json_data["bromotion"]
        searching_bro = Bro.verify_auth_token(token)
        if not searching_bro:
            return {
                "result":
                False,
                "message":
                "You're not authorized to view this. Please make an account at BroCast!"
            }
        else:
            if bromotion == "":
                bros = Bro.query.filter(
                    func.lower(Bro.bro_name) == func.lower(bro_name))
            else:
                bros = Bro.query.filter(
                    func.lower(Bro.bro_name) == func.lower(
                        bro_name)).filter_by(bromotion=bromotion)
            bro_list = []
            for bro in bros:
                bro_list.append(bro.serialize)

            return {"result": True, "bro_list": bro_list}
示例#7
0
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)
示例#8
0
 def post(self):
     json_data = request.get_json(force=True)
     bro_name = json_data["bro_name"]
     bromotion = json_data["bromotion"]
     password = json_data["password"]
     registration_id = json_data["registration_id"]
     if bro_name is None or bromotion is None or password is None or registration_id is None:
         abort(400)  # missing arguments
     if Bro.query.filter_by(bro_name=bro_name,
                            bromotion=bromotion).first() is not None:
         return {
             'result':
             False,
             'message':
             "Bro name with bromotion combination is already taken, please provide another one."
         }, 400
     bro = Bro(bro_name=bro_name,
               bromotion=bromotion,
               device_type="Android")
     bro.hash_password(password)
     bro.set_registration_id(registration_id)
     db.session.add(bro)
     db.session.commit()
     token = bro.generate_auth_token().decode('ascii')
     return {
         'result': True,
         'message': 'Congratulations, you have just added a bro',
         'bro': bro.serialize,
         'token': token
     }, 200
示例#9
0
def change_bromotion(data):
    token = data["token"]
    logged_in_bro = Bro.verify_auth_token(token)

    if logged_in_bro is None:
        emit("message_event_bromotion_change",
             "token authentication failed",
             room=request.sid)
    else:
        old_bromotion = logged_in_bro.get_bromotion()
        new_bromotion = data["bromotion"]
        if Bro.query.filter_by(bro_name=logged_in_bro.bro_name,
                               bromotion=new_bromotion).first() is not None:
            emit("message_event_bromotion_change",
                 "broName bromotion combination taken",
                 room=request.sid)
        else:
            logged_in_bro.set_bromotion(new_bromotion)
            all_chats = BroBros.query.filter_by(
                bros_bro_id=logged_in_bro.id).all()
            # update bromotion in the normal chats
            for chat in all_chats:
                chat.chat_name = logged_in_bro.get_full_name()
                db.session.add(chat)
                bro_room = "room_%s" % chat.bro_id
                emit("message_event_chat_changed",
                     chat.serialize,
                     room=bro_room)

            # update bromotion in all the broups this bro is a part of.
            all_broups = Broup.query.filter_by(bro_id=logged_in_bro.id).all()
            for brup in all_broups:
                # We need to update all the broup objects of a single broup id
                broups = Broup.query.filter_by(broup_id=brup.broup_id).all()
                for broup in broups:
                    broup_name = remove_last_occur(broup.get_broup_name(),
                                                   old_bromotion)
                    broup_name = broup_name + "" + new_bromotion
                    broup.set_broup_name(broup_name)
                    db.session.add(broup)

                    bro_room = "room_%s" % broup.bro_id
                    emit("message_event_chat_changed",
                         broup.serialize,
                         room=bro_room)

            db.session.add(logged_in_bro)
            db.session.commit()
            update_broups(all_broups)
            emit("message_event_bromotion_change",
                 "bromotion change successful",
                 room=request.sid)
示例#10
0
    def post(self):
        json_data = request.get_json(force=True)
        token = json_data["token"]
        bros_bro_id = json_data["bros_bro_id"]
        logged_in_bro = Bro.verify_auth_token(token)
        if not logged_in_bro:
            return {
                "result": False,
                "message": "Your credentials are not valid."
            }

        chat = BroBros.query.filter_by(bro_id=logged_in_bro.id,
                                       bros_bro_id=bros_bro_id).first()
        if chat is None:
            return {"result": False, "message": "Chat not found."}

        # We will retrieve all messages available on the server after the bro's last read time. Even our own.
        messages = Message.query.filter_by(sender_id=logged_in_bro.id, recipient_id=bros_bro_id) \
            .union(Message.query.filter_by(sender_id=bros_bro_id, recipient_id=logged_in_bro.id)) \
            .filter(Message.timestamp >= (chat.last_message_read_time_bro - timedelta(minutes=5)))\
            .order_by(Message.timestamp.desc())\
            .all()

        if chat.has_been_blocked():
            blocked_timestamps = chat.get_blocked_timestamps()
            for b in range(0, len(blocked_timestamps), 2):
                block_1 = blocked_timestamps[b]
                block_2 = datetime.utcnow()
                if b < len(blocked_timestamps) - 1:
                    block_2 = blocked_timestamps[b + 1]
                # We go over the block timestamp pairs.
                # If the user is currently blocked, the last pair will be the block time and now
                messages = [
                    message for message in messages
                    if not block_1 <= message.get_timestamp() <= block_2
                ]

        if messages is None:
            return {'result': False}

        last_read_time = get_last_read_time_other_bro(logged_in_bro.id,
                                                      bros_bro_id)
        return {
            "result": True,
            "last_read_time_bro":
            last_read_time.strftime('%Y-%m-%dT%H:%M:%S.%f'),
            "message_list": [message.serialize for message in messages]
        }
示例#11
0
 def post(self):
     json_data = request.get_json(force=True)
     token = json_data["token"]
     bro = Bro.verify_auth_token(token)
     if not bro:
         return {
             "result":
             False,
             "message":
             "You're not authorized to view this. Please make an account at BroCast!"
         }
     else:
         bros = Bro.query.all()
         return {
             "result": True,
             "bro_list": [bro.serialize for bro in bros]
         }
示例#12
0
    def on_password_change(self, data):
        token = data["token"]
        logged_in_bro = Bro.verify_auth_token(token)

        if logged_in_bro is None:
            emit("message_event_password_change",
                 "password change failed",
                 room=request.sid)
        else:
            new_password = data["password"]
            logged_in_bro.hash_password(new_password)

            db.session.add(logged_in_bro)
            db.session.commit()
            emit("message_event_password_change",
                 "password change successful",
                 room=request.sid)
示例#13
0
def change_chat_colour(data):
    token = data["token"]
    bros_bro_id = data["bros_bro_id"]
    colour = data["colour"]
    logged_in_bro = Bro.verify_auth_token(token)

    if logged_in_bro is None:
        emit("message_event_change_chat_colour_failed",
             "token authentication failed",
             room=request.sid)
    else:
        chat1 = BroBros.query.filter_by(bro_id=logged_in_bro.id,
                                        bros_bro_id=bros_bro_id).first()
        chat2 = BroBros.query.filter_by(bro_id=bros_bro_id,
                                        bros_bro_id=logged_in_bro.id).first()
        if chat1 is None or chat2 is None:
            emit("message_event_change_chat_colour_failed",
                 "chat colour change failed",
                 room=request.sid)
        else:
            chat1.update_colour(colour)
            db.session.add(chat1)
            chat2.update_colour(colour)
            db.session.add(chat2)
            db.session.commit()
            room_bro_1 = "room_%s" % logged_in_bro.id
            room_bro_2 = "room_%s" % bros_bro_id
            emit("message_event_chat_changed",
                 chat1.serialize,
                 room=room_bro_1)
            emit("message_event_chat_changed",
                 chat2.serialize,
                 room=room_bro_2)

            # We create an info message with the person who did the update as sender
            bro_message = Message(sender_id=logged_in_bro.id,
                                  recipient_id=bros_bro_id,
                                  body="%s changed the chat colour" %
                                  chat2.get_bros_bro_name_or_alias(),
                                  text_message="",
                                  timestamp=datetime.utcnow(),
                                  info=True)
            db.session.add(bro_message)
            db.session.commit()
            room = get_a_room_you_two(logged_in_bro.id, bros_bro_id)
            emit("message_event_send", bro_message.serialize, room=room)
示例#14
0
    def post(self):
        json_data = request.get_json(force=True)
        bros_bro_id = json_data["bros_bro_id"]
        token = json_data["token"]
        logged_in_bro = Bro.verify_auth_token(token)
        bro_that_is_reported = Bro.query.filter_by(id=bros_bro_id).first()

        if not logged_in_bro:
            return {
                "result": False,
                "message": "Your credentials are not valid."
            }

        if not bro_that_is_reported:
            return {"result": False, "message": "Bro not found."}

        chat = BroBros.query.filter_by(bro_id=logged_in_bro.id,
                                       bros_bro_id=bros_bro_id).first()
        if chat is None:
            return {"result": False, "message": "Chat not found."}

        chat.bro_removed()
        chat.block_chat(True)

        # We take the last 40 messages from these bros to log.
        messages = Message.query.filter_by(sender_id=logged_in_bro.id, recipient_id=bros_bro_id). \
            union(Message.query.filter_by(sender_id=bros_bro_id, recipient_id=logged_in_bro.id)). \
            order_by(Message.timestamp.desc()).paginate(1, 40, False).items

        message_log = []
        for message in messages:
            message_log.append(json.dumps(message.serialize))

        log = Log(report_from=json.dumps(logged_in_bro.serialize),
                  report_to=json.dumps(bro_that_is_reported.serialize),
                  messages=message_log,
                  report_date=datetime.utcnow())

        db.session.add(chat)
        db.session.add(log)
        db.session.commit()

        return {"result": True, "chat": chat.serialize}
示例#15
0
def mute_broup(data):
    token = data["token"]
    broup_id = data["broup_id"]
    bro_id = data["bro_id"]
    mute_time = data["mute"]

    logged_in_bro = Bro.verify_auth_token(token)

    mute = True
    unmute_date = None
    if mute_time == -1:
        mute = False
    elif mute_time == 0:
        unmute_date = datetime.now().utcnow() + timedelta(hours=1)
    elif mute_time == 1:
        unmute_date = datetime.now().utcnow() + timedelta(hours=8)
    elif mute_time == 2:
        unmute_date = datetime.now().utcnow() + timedelta(days=7)

    if logged_in_bro is None:
        emit("message_event_change_broup_mute_failed",
             "token authentication failed",
             room=request.sid)
    else:
        broup_of_bro = Broup.query.filter_by(broup_id=broup_id,
                                             bro_id=bro_id).first()
        if broup_of_bro is None:
            emit("message_event_change_broup_mute_failed",
                 "broup finding failed",
                 room=request.sid)
        else:
            broup_of_bro.mute_broup(mute)
            if not mute:
                broup_of_bro.set_mute_timestamp(None)
            if unmute_date:
                broup_of_bro.set_mute_timestamp(unmute_date)
            db.session.add(broup_of_bro)
            db.session.commit()
            room_bro = "room_%s" % logged_in_bro.id
            emit("message_event_chat_changed",
                 broup_of_bro.serialize,
                 room=room_bro)
示例#16
0
def add_broup(data):
    token = data["token"]
    logged_in_bro = Bro.verify_auth_token(token)
    if not logged_in_bro:
        emit("message_event_add_broup_failed", "failed to add broup", room=request.sid)
    else:
        participants = loads(data["participants"])

        broup_name = data["broup_name"]
        broup_name += " " + logged_in_bro.bromotion

        max_broup_id = db.session.query(func.max(Broup.broup_id)).scalar()
        if max_broup_id is None:
            max_broup_id = 0
        broup_id = max_broup_id + 1

        bro_ids = [logged_in_bro.id]
        admins = [logged_in_bro.id]
        broup = [logged_in_bro]
        for part in participants:
            bro_for_broup = Bro.query.filter_by(id=part).first()
            if bro_for_broup is None:
                emit("message_event_add_broup_failed", "failed to add broup", room=request.sid)
                return
            else:
                broup.append(bro_for_broup)
                broup_name += "" + bro_for_broup.bromotion
                bro_ids.append(bro_for_broup.id)

        broup_colour = '%02X%02X%02X' % (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        for bro in broup:
            b = bro.add_broup(broup_name, broup_id, bro_ids, broup_colour, admins)
            room = "room_%s" % bro.id
            if b is not None:
                emit("message_event_added_to_broup", b.serialize, room=room)

        db.session.commit()

        emit("message_event_add_broup_success", "broup was added!", room=request.sid)
        return
示例#17
0
def change_chat_alias(data):
    token = data["token"]
    bros_bro_id = data["bros_bro_id"]
    alias = data["alias"]
    logged_in_bro = Bro.verify_auth_token(token)
    if logged_in_bro is None:
        emit("message_event_change_chat_alias_failed",
             "token authentication failed",
             room=request.sid)
    else:
        chat1 = BroBros.query.filter_by(bro_id=logged_in_bro.id,
                                        bros_bro_id=bros_bro_id).first()
        if chat1 is None:
            emit("message_event_change_chat_alias_failed",
                 "chat finding failed",
                 room=request.sid)
        else:
            chat1.update_alias(alias)
            db.session.add(chat1)
            db.session.commit()
            room_bro = "room_%s" % logged_in_bro.id
            emit("message_event_chat_changed", chat1.serialize, room=room_bro)
示例#18
0
    def post(self):
        json_data = request.get_json(force=True)
        token = json_data["token"]
        logged_in_bro = Bro.verify_auth_token(token)
        if not logged_in_bro:
            return {
                "result": False,
                "message": "Your credentials are not valid."
            }

        for bro_bro in logged_in_bro.get_bros():
            if bro_bro.check_mute():
                db.session.add(bro_bro)
                db.session.commit()
        for broup in logged_in_bro.get_broups():
            if broup.check_mute():
                db.session.add(broup)
                db.session.commit()
        bro_list = [bro_bro.serialize for bro_bro in logged_in_bro.get_bros()]
        broup_list = [broup.serialize for broup in logged_in_bro.get_broups()]

        return {"result": True, "bro_list": bro_list + broup_list}
示例#19
0
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)
示例#20
0
    def post(self):
        json_data = request.get_json(force=True)
        bro_name = json_data["bro_name"]
        bromotion = json_data["bromotion"]
        password = json_data["password"]
        token = json_data["token"]
        registration_id = json_data["registration_id"]

        bro = None
        if token is not None and not "":
            bro = Bro.verify_auth_token(token)

        if not bro:
            # The token was wrong (or was expired) Try to log in with username password and generate a new token
            bro = Bro.query.filter(
                func.lower(Bro.bro_name) == func.lower(bro_name)).filter_by(
                    bromotion=bromotion).first()
            if not bro or not bro.verify_password(password):
                return {
                    'result': False,
                    'message': 'The given credentials are not correct!'
                }, 401

        # Valid login, we refresh the token for this user.
        token = bro.generate_auth_token().decode('ascii')
        # update registration key if it's changed
        if bro.get_registration_id(
        ) != registration_id and registration_id != "":
            bro.set_registration_id(registration_id)
            db.session.add(bro)
            db.session.commit()

        return {
            'result': True,
            'message': 'Congratulations, you have just logged in',
            'bro': bro.serialize,
            'token': token
        }, 200
示例#21
0
    def post(self):
        json_data = request.get_json(force=True)
        token = json_data["token"]
        logged_in_bro = Bro.verify_auth_token(token)
        if not logged_in_bro:
            return {
                "result": False,
                "message": "Your credentials are not valid."
            }

        participants = loads(json_data["participants"])

        if not participants:
            return {"result": False, "message": "Something went wrong."}

        bros = []
        for part in participants:
            bro_for_broup = Bro.query.filter_by(id=part).first()
            if bro_for_broup is None:
                return {'result': False}
            bros.append(bro_for_broup)

        bro_list = [bro.serialize for bro in bros]
        return {"result": True, "bro_list": bro_list}
示例#22
0
    def post(self):
        json_data = request.get_json(force=True)
        broup_id = json_data["broup_id"]
        token = json_data["token"]
        logged_in_bro = Bro.verify_auth_token(token)

        if not logged_in_bro:
            return {
                "result": False,
                "message": "Your credentials are not valid."
            }

        broup_that_is_reported = Broup.query.filter_by(
            broup_id=broup_id, bro_id=logged_in_bro.id).first()
        broup_objects = Broup.query.filter_by(broup_id=broup_id)
        remove_bro = Bro.query.filter_by(id=logged_in_bro.id).first()
        if not broup_that_is_reported or not broup_objects or not remove_bro:
            return {"result": False, "message": "Broup not found."}

        # We take the last 100 messages from this broup to log.
        messages = BroupMessage.query.filter_by(broup_id=broup_id).\
            order_by(BroupMessage.timestamp.desc()).paginate(1, 100, False).items

        message_log = []
        for message in messages:
            message_log.append(json.dumps(message.serialize))

        log = Log(report_from=json.dumps(logged_in_bro.serialize),
                  report_to=json.dumps(broup_that_is_reported.serialize),
                  messages=message_log,
                  report_date=datetime.utcnow())

        db.session.add(log)

        if not broup_that_is_reported.has_left():
            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(logged_in_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(broup_that_is_reported.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(broup_that_is_reported.get_participants()) == 0:
                messages = BroupMessage.query.filter_by(broup_id=broup_id)
                for message in messages:
                    db.session.delete(message)
            broup_that_is_reported.leave_broup()

        broup_that_is_reported.mute_broup(True)
        broup_that_is_reported.broup_removed()

        db.session.add(broup_that_is_reported)
        db.session.commit()

        return {"result": True, "chat": broup_that_is_reported.serialize}
示例#23
0
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)
示例#24
0
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)