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
get_local_user_view_from_jwt,
is_mod_or_admin,
mark_post_as_read,
+ mark_post_as_unread,
post::*,
};
use lemmy_apub::{
}
}
+#[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;
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(
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,
CreatePostLike,
LockPost,
StickyPost,
+ MarkPostAsRead,
SavePost,
CreatePostReport,
ResolvePostReport,
.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>))