Exemple #1
0
def send_message_received_email(user_recipient):
    messages_link = urls.messages_link()
    logger.info(f"Sending message received email to {user_recipient=}:")
    logger.info(
        f"Email for {user_recipient.username=} sent to {user_recipient.email=}"
    )

    email.enqueue_email_from_template(
        user_recipient.email,
        "message_received",
        template_args={
            "user": user_recipient,
            "messages_link": messages_link,
        },
    )
Exemple #2
0
def process_send_message_notifications(payload):
    """
    Sends out email notifications for messages that have been unseen for a long enough time
    """
    # very crude and dumb algorithm
    logger.info(f"Sending out email notifications for unseen messages")

    with session_scope() as session:
        # users who have unnotified messages older than 5 minutes in any group chat
        users = (
            session.query(User).join(
                GroupChatSubscription,
                GroupChatSubscription.user_id == User.id).join(
                    Message, Message.conversation_id ==
                    GroupChatSubscription.group_chat_id).filter(
                        Message.time >= GroupChatSubscription.joined).filter(
                            or_(Message.time <= GroupChatSubscription.left,
                                GroupChatSubscription.left == None)).
            filter(Message.id > User.last_notified_message_id).filter(
                Message.id > GroupChatSubscription.last_seen_message_id).
            filter(Message.time < now() - timedelta(minutes=5)).filter(
                Message.message_type ==
                MessageType.text)  # TODO: only text messages for now
            .all())

        for user in users:
            # now actually grab all the group chats, not just less than 5 min old
            subquery = (
                session.query(
                    GroupChatSubscription.group_chat_id.label("group_chat_id"),
                    func.max(GroupChatSubscription.id).label(
                        "group_chat_subscriptions_id"),
                    func.max(Message.id).label("message_id"),
                    func.count(Message.id).label("count_unseen"),
                ).join(
                    Message, Message.conversation_id ==
                    GroupChatSubscription.group_chat_id).filter(
                        GroupChatSubscription.user_id == user.id).
                filter(Message.id > user.last_notified_message_id).filter(
                    Message.id > GroupChatSubscription.last_seen_message_id).
                filter(Message.time >= GroupChatSubscription.joined).filter(
                    Message.message_type ==
                    MessageType.text)  # TODO: only text messages for now
                .filter(
                    or_(Message.time <= GroupChatSubscription.left,
                        GroupChatSubscription.left == None)).group_by(
                            GroupChatSubscription.group_chat_id).order_by(
                                func.max(Message.id).desc()).subquery())

            unseen_messages = (session.query(
                GroupChat, Message, subquery.c.count_unseen).join(
                    subquery, subquery.c.message_id == Message.id).join(
                        GroupChat, GroupChat.conversation_id ==
                        subquery.c.group_chat_id).order_by(
                            subquery.c.message_id.desc()).all())

            user.last_notified_message_id = max(
                message.id for _, message, _ in unseen_messages)
            session.commit()

            total_unseen_message_count = sum(
                count for _, _, count in unseen_messages)

            email.enqueue_email_from_template(
                user.email,
                "unseen_messages",
                template_args={
                    "user":
                    user,
                    "total_unseen_message_count":
                    total_unseen_message_count,
                    "unseen_messages":
                    [(group_chat, latest_message, count)
                     for group_chat, latest_message, count in unseen_messages],
                    "group_chats_link":
                    urls.messages_link(),
                },
            )