コード例 #1
0
def test_prevent_duplicate_notifications(db, user_list, topic):
    """Test that notifications are cleaned up for edits.

    Flow:
        1. A comment is created by user A that mentions user B. Notifications are
           generated, and yield A mentioning B.
        2. The comment is edited to mention C and not B.
        3. The comment is edited to mention B and C.
        4. The comment is deleted.
    """
    # 1
    comment = Comment(topic, user_list[0], f"@{user_list[1].username}")
    db.add(comment)
    db.commit()
    mentions = CommentNotification.get_mentions_for_comment(db, comment)
    assert len(mentions) == 1
    assert mentions[0].user == user_list[1]
    db.add_all(mentions)
    db.commit()

    # 2
    comment.markdown = f"@{user_list[2].username}"
    db.commit()
    mentions = CommentNotification.get_mentions_for_comment(db, comment)
    assert len(mentions) == 1
    to_delete, to_add = CommentNotification.prevent_duplicate_notifications(
        db, comment, mentions)
    assert len(to_delete) == 1
    assert mentions == to_add
    assert to_delete[0].user.username == user_list[1].username

    # 3
    comment.markdown = f"@{user_list[1].username} @{user_list[2].username}"
    db.commit()
    mentions = CommentNotification.get_mentions_for_comment(db, comment)
    assert len(mentions) == 2
    to_delete, to_add = CommentNotification.prevent_duplicate_notifications(
        db, comment, mentions)
    assert not to_delete
    assert len(to_add) == 1

    # 4
    comment.is_deleted = True
    db.commit()
    notifications = (db.query(CommentNotification.user_id).filter(
        and_(
            CommentNotification.comment_id == comment.comment_id,
            CommentNotification.notification_type ==
            CommentNotificationType.USER_MENTION,
        )).all())
    assert not notifications
コード例 #2
0
    def process_message(self, message: Message) -> None:
        """Process a message from the stream."""
        comment = (self.db_session.query(Comment).filter_by(
            comment_id=message.fields["comment_id"]).one())

        # don't generate mentions for deleted/removed comments
        if comment.is_deleted or comment.is_removed:
            return

        new_mentions = CommentNotification.get_mentions_for_comment(
            self.db_session, comment)

        if message.stream == "comments.insert":
            for user_mention in new_mentions:
                self.db_session.add(user_mention)
        elif message.stream == "comments.update.markdown":
            to_delete, to_add = CommentNotification.prevent_duplicate_notifications(
                self.db_session, comment, new_mentions)

            for user_mention in to_delete:
                self.db_session.delete(user_mention)

            for user_mention in to_add:
                self.db_session.add(user_mention)
コード例 #3
0
    def run(self, msg: Message) -> None:
        """Process a delivered message."""
        comment = (self.db_session.query(Comment).filter_by(
            comment_id=msg.body["comment_id"]).one())

        # don't generate mentions for deleted/removed comments
        if comment.is_deleted or comment.is_removed:
            return

        new_mentions = CommentNotification.get_mentions_for_comment(
            self.db_session, comment)

        if msg.delivery_info["routing_key"] == "comment.created":
            for user_mention in new_mentions:
                self.db_session.add(user_mention)
        elif msg.delivery_info["routing_key"] == "comment.edited":
            to_delete, to_add = CommentNotification.prevent_duplicate_notifications(
                self.db_session, comment, new_mentions)

            for user_mention in to_delete:
                self.db_session.delete(user_mention)

            for user_mention in to_add:
                self.db_session.add(user_mention)