From 3f6f1eda9f4fdaf7c67b986cf0cdf0e629c06a6e Mon Sep 17 00:00:00 2001 From: trisua Date: Thu, 22 May 2025 22:54:06 -0400 Subject: [PATCH] add: mark all notifications as read/unread fix: anonymous avatar/username --- crates/app/src/langs/en-US.toml | 2 + crates/app/src/public/html/components.html | 4 +- .../src/public/html/misc/notifications.html | 73 +++++++++++++++++-- crates/app/src/public/html/misc/requests.html | 2 +- crates/app/src/routes/api/v1/mod.rs | 4 + crates/app/src/routes/api/v1/notifications.rs | 21 ++++++ crates/core/src/database/notifications.rs | 22 ++++++ 7 files changed, 118 insertions(+), 10 deletions(-) diff --git a/crates/app/src/langs/en-US.toml b/crates/app/src/langs/en-US.toml index c635cdf..6cef894 100644 --- a/crates/app/src/langs/en-US.toml +++ b/crates/app/src/langs/en-US.toml @@ -134,6 +134,8 @@ version = "1.0.0" "notifs:action.mark_as_unread" = "Mark as unread" "notifs:action.clear" = "Clear" "notifs:label.notifications" = "Notifications" +"notifs:label.mark_all_as_read" = "Mark all as read" +"notifs:label.mark_all_as_unread" = "Mark all as unread" "settings:tab.general" = "General" "settings:tab.account" = "Account" diff --git a/crates/app/src/public/html/components.html b/crates/app/src/public/html/components.html index 05ffe89..06d0aea 100644 --- a/crates/app/src/public/html/components.html +++ b/crates/app/src/public/html/components.html @@ -124,7 +124,7 @@ expect_repost=false) -%} {% if community and show_community and community.id != config.town_square or question %}
{% if question -%} {{ self::question(question=question[0], - owner=question[1]) }} {% else %} + owner=question[1], profile=owner) }} {% else %} + + {% endblock %} diff --git a/crates/app/src/public/html/misc/requests.html b/crates/app/src/public/html/misc/requests.html index c1f2396..2d0b77e 100644 --- a/crates/app/src/public/html/misc/requests.html +++ b/crates/app/src/public/html/misc/requests.html @@ -89,7 +89,7 @@ {%- endif %} {% endfor %} {% for question in questions %}
- {{ components::question(question=question[0], owner=question[1]) }} + {{ components::question(question=question[0], owner=question[1], profile=user) }}
Router { "/notifications/{id}/read_status", post(notifications::update_read_status_request), ) + .route( + "/notifications/all/read_status", + post(notifications::update_all_read_status_request), + ) // community memberships .route( "/communities/{id}/join", diff --git a/crates/app/src/routes/api/v1/notifications.rs b/crates/app/src/routes/api/v1/notifications.rs index 41a4e3c..aa8664b 100644 --- a/crates/app/src/routes/api/v1/notifications.rs +++ b/crates/app/src/routes/api/v1/notifications.rs @@ -87,3 +87,24 @@ pub async fn update_read_status_request( Err(e) => Json(e.into()), } } + +pub async fn update_all_read_status_request( + jar: CookieJar, + Extension(data): Extension, + Json(req): Json, +) -> impl IntoResponse { + let data = &(data.read().await).0; + let user = match get_user_from_token!(jar, data) { + Some(ua) => ua, + None => return Json(Error::NotAllowed.into()), + }; + + match data.update_all_notifications_read(&user, req.read).await { + Ok(_) => Json(ApiReturn { + ok: true, + message: "Notifications updated".to_string(), + payload: (), + }), + Err(e) => Json(e.into()), + } +} diff --git a/crates/core/src/database/notifications.rs b/crates/core/src/database/notifications.rs index 0a1e146..c501caf 100644 --- a/crates/core/src/database/notifications.rs +++ b/crates/core/src/database/notifications.rs @@ -246,4 +246,26 @@ impl DataManager { Ok(()) } + + pub async fn update_all_notifications_read(&self, user: &User, read: bool) -> Result<()> { + let notifications = self.get_notifications_by_owner(user.id).await?; + + if notifications.len() > 1000 { + return Err(Error::MiscError( + "Too many notifications to do this".to_string(), + )); + } + + for notification in notifications { + if notification.read == read { + // no need to update this + continue; + } + + self.update_notification_read(notification.id, read, user) + .await? + } + + Ok(()) + } }