From c6414338b69589e14688c911eb5e8cd1f1925e28 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Tue, 14 Jun 2022 17:32:43 +0100 Subject: [PATCH] fix: marking server as read would not mark it as read fixes #169 Porting code forwards from an older revision of the codebase; https://github.com/revoltchat/backend/blob/0.5.3-alpha.10/src/database/entities/server.rs --- .../src/impl/mongo/channels/channel_unread.rs | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/crates/quark/src/impl/mongo/channels/channel_unread.rs b/crates/quark/src/impl/mongo/channels/channel_unread.rs index 0f5fd733..8442f3cc 100644 --- a/crates/quark/src/impl/mongo/channels/channel_unread.rs +++ b/crates/quark/src/impl/mongo/channels/channel_unread.rs @@ -37,30 +37,46 @@ impl AbstractChannelUnread for MongoDb { } async fn acknowledge_channels(&self, user: &str, channels: &[String]) -> Result<()> { + let current_time = Ulid::new().to_string(); + self.col::(COL) - .update_one( + .delete_many( doc! { "_id.channel": { "$in": channels }, - "_id.user": user, + "_id.user": user }, - doc! { - "$unset": { - "mentions": 1_i32 - }, - "$set": { - "last_id": Ulid::new().to_string() - } - }, - UpdateOptions::builder().upsert(true).build(), + None, ) .await - .map(|_| ()) .map_err(|_| Error::DatabaseError { - operation: "update", - with: "channel_unread", + operation: "delete_many", + with: "channel_unreads", + })?; + + self.col::(COL) + .insert_many( + channels + .iter() + .map(|channel| { + doc! { + "_id": { + "channel": channel, + "user": user + }, + "last_id": ¤t_time + } + }) + .collect::>(), + None, + ) + .await + .map_err(|_| Error::DatabaseError { + operation: "update_many", + with: "channel_unreads", }) + .map(|_| ()) } async fn add_mention_to_unread<'a>(