def _push_messages()

in backend/services/messaging/message_service.py [0:0]


    def _push_messages(messages):
        if len(messages) == 0:
            return

        messages_objs = []
        for i, message in enumerate(messages):
            user = message.get("user")
            obj = message.get("message")
            # Store message in the database only if mentions option are disabled.
            if (
                user.mentions_notifications is False
                and obj.message_type == MessageType.MENTION_NOTIFICATION.value
            ):
                messages_objs.append(obj)
                continue
            if (
                user.projects_notifications is False
                and obj.message_type == MessageType.PROJECT_ACTIVITY_NOTIFICATION.value
            ):
                continue
            if (
                user.projects_notifications is False
                and obj.message_type == MessageType.BROADCAST.value
            ):
                continue
            if (
                user.teams_notifications is False
                and obj.message_type == MessageType.TEAM_BROADCAST.value
            ):
                messages_objs.append(obj)
                continue
            if user.comments_notifications is False and obj.message_type in (
                MessageType.TASK_COMMENT_NOTIFICATION.value,
                MessageType.PROJECT_CHAT_NOTIFICATION.value,
            ):
                continue
            if user.tasks_notifications is False and obj.message_type in (
                MessageType.VALIDATION_NOTIFICATION.value,
                MessageType.INVALIDATION_NOTIFICATION.value,
            ):
                messages_objs.append(obj)
                continue
            messages_objs.append(obj)
            SMTPService.send_email_alert(
                user.email_address,
                user.username,
                user.is_email_verified,
                message["message"].id,
                UserService.get_user_by_id(message["message"].from_user_id).username,
                message["message"].project_id,
                message["message"].task_id,
                clean_html(message["message"].subject),
                message["message"].message,
                obj.message_type,
            )

            if i + 1 % 10 == 0:
                time.sleep(0.5)

        # Flush messages to the database.
        if len(messages_objs) > 0:
            db.session.add_all(messages_objs)
            db.session.flush()
            db.session.commit()