]> Untitled Git - lemmy.git/blobdiff - crates/api/src/local_user.rs
Adding GetUnreadCount to the API. Fixes #1794 (#1842)
[lemmy.git] / crates / api / src / local_user.rs
index a7d2f0c3cf8f1d86357b4d57d30e9921e970f793..d5a1533f99dd4b5b67a4e41f98c0a766875d74a2 100644 (file)
@@ -47,9 +47,10 @@ use lemmy_db_schema::{
 };
 use lemmy_db_views::{
   comment_report_view::CommentReportView,
-  comment_view::CommentQueryBuilder,
+  comment_view::{CommentQueryBuilder, CommentView},
   local_user_view::LocalUserView,
   post_report_view::PostReportView,
+  private_message_view::PrivateMessageView,
 };
 use lemmy_db_views_actor::{
   community_moderator_view::CommunityModeratorView,
@@ -831,3 +832,43 @@ impl Perform for GetReportCount {
     Ok(res)
   }
 }
+
+#[async_trait::async_trait(?Send)]
+impl Perform for GetUnreadCount {
+  type Response = GetUnreadCountResponse;
+
+  async fn perform(
+    &self,
+    context: &Data<LemmyContext>,
+    _websocket_id: Option<ConnectionId>,
+  ) -> Result<Self::Response, LemmyError> {
+    let data = self;
+    let local_user_view =
+      get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
+
+    let person_id = local_user_view.person.id;
+
+    let replies = blocking(context.pool(), move |conn| {
+      CommentView::get_unread_replies(conn, person_id)
+    })
+    .await??;
+
+    let mentions = blocking(context.pool(), move |conn| {
+      PersonMentionView::get_unread_mentions(conn, person_id)
+    })
+    .await??;
+
+    let private_messages = blocking(context.pool(), move |conn| {
+      PrivateMessageView::get_unread_messages(conn, person_id)
+    })
+    .await??;
+
+    let res = Self::Response {
+      replies,
+      mentions,
+      private_messages,
+    };
+
+    Ok(res)
+  }
+}