]> Untitled Git - lemmy.git/commitdiff
Adding MarkPostAsRead to API. Fixes #1784 (#1946)
authorDessalines <dessalines@users.noreply.github.com>
Tue, 23 Nov 2021 14:15:43 +0000 (09:15 -0500)
committerGitHub <noreply@github.com>
Tue, 23 Nov 2021 14:15:43 +0000 (14:15 +0000)
* Adding MarkPostAsRead to API. Fixes #1784

* Adding error

crates/api/src/lib.rs
crates/api/src/post.rs
crates/api_common/src/lib.rs
crates/api_common/src/post.rs
crates/websocket/src/lib.rs
src/api_routes.rs

index 74a3d3037098a86ddc27110080fb28a1759b894e..d535c4678fef51f813b96384d59b3c2858be85ce 100644 (file)
@@ -120,6 +120,9 @@ pub async fn match_websocket_operation(
     UserOperation::CreatePostLike => {
       do_websocket_operation::<CreatePostLike>(context, id, op, data).await
     }
+    UserOperation::MarkPostAsRead => {
+      do_websocket_operation::<MarkPostAsRead>(context, id, op, data).await
+    }
     UserOperation::SavePost => do_websocket_operation::<SavePost>(context, id, op, data).await,
     UserOperation::CreatePostReport => {
       do_websocket_operation::<CreatePostReport>(context, id, op, data).await
index c7acb08fb30da67a2e7e948214ddabe250b3a733..60a580b84830e1fa0fcf61cfe6436ef987b5436c 100644 (file)
@@ -9,6 +9,7 @@ use lemmy_api_common::{
   get_local_user_view_from_jwt,
   is_mod_or_admin,
   mark_post_as_read,
+  mark_post_as_unread,
   post::*,
 };
 use lemmy_apub::{
@@ -118,6 +119,41 @@ impl Perform for CreatePostLike {
   }
 }
 
+#[async_trait::async_trait(?Send)]
+impl Perform for MarkPostAsRead {
+  type Response = PostResponse;
+
+  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 post_id = data.post_id;
+    let person_id = local_user_view.person.id;
+
+    // Mark the post as read / unread
+    if data.read {
+      mark_post_as_read(person_id, post_id, context.pool()).await?;
+    } else {
+      mark_post_as_unread(person_id, post_id, context.pool()).await?;
+    }
+
+    // Fetch it
+    let post_view = blocking(context.pool(), move |conn| {
+      PostView::read(conn, post_id, Some(person_id))
+    })
+    .await??;
+
+    let res = Self::Response { post_view };
+
+    Ok(res)
+  }
+}
+
 #[async_trait::async_trait(?Send)]
 impl Perform for LockPost {
   type Response = PostResponse;
index bf6246a0dc087f67307bba8d410b773265e27487..f5b6ebad27a27264c0421c88571cbcf1f67e1dab 100644 (file)
@@ -81,7 +81,21 @@ pub async fn mark_post_as_read(
     PostRead::mark_as_read(conn, &post_read_form)
   })
   .await?
-  .map_err(|_| ApiError::err_plain("couldnt_mark_post_as_read").into())
+  .map_err(|e| ApiError::err("couldnt_mark_post_as_read", e).into())
+}
+
+pub async fn mark_post_as_unread(
+  person_id: PersonId,
+  post_id: PostId,
+  pool: &DbPool,
+) -> Result<usize, LemmyError> {
+  let post_read_form = PostReadForm { post_id, person_id };
+
+  blocking(pool, move |conn| {
+    PostRead::mark_as_unread(conn, &post_read_form)
+  })
+  .await?
+  .map_err(|e| ApiError::err("couldnt_mark_post_as_read", e).into())
 }
 
 pub async fn get_local_user_view_from_jwt(
index d4a8c3f521875e23e92d165151ca6bdadd36073a..6e2c3179734d2d001d1792cda5d31342758ae327 100644 (file)
@@ -92,6 +92,13 @@ pub struct RemovePost {
   pub auth: String,
 }
 
+#[derive(Serialize, Deserialize)]
+pub struct MarkPostAsRead {
+  pub post_id: PostId,
+  pub read: bool,
+  pub auth: String,
+}
+
 #[derive(Serialize, Deserialize)]
 pub struct LockPost {
   pub post_id: PostId,
index b7ab5bf28b17680efb4f90b02094909361cabfc0..251c7072a89d3b9b72dbe5c64350609cb886d46e 100644 (file)
@@ -110,6 +110,7 @@ pub enum UserOperation {
   CreatePostLike,
   LockPost,
   StickyPost,
+  MarkPostAsRead,
   SavePost,
   CreatePostReport,
   ResolvePostReport,
index 0349f518c0253e7ca8a273c5adb1727c68736522..a6a840395a6e7feca791386272969918b8a050ea 100644 (file)
@@ -83,6 +83,10 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
           .route("", web::put().to(route_post_crud::<EditPost>))
           .route("/delete", web::post().to(route_post_crud::<DeletePost>))
           .route("/remove", web::post().to(route_post_crud::<RemovePost>))
+          .route(
+            "/mark_as_read",
+            web::post().to(route_post::<MarkPostAsRead>),
+          )
           .route("/lock", web::post().to(route_post::<LockPost>))
           .route("/sticky", web::post().to(route_post::<StickyPost>))
           .route("/list", web::get().to(route_get_crud::<GetPosts>))