Adding MarkPostAsRead to API. Fixes #1784 (#1946)

* Adding MarkPostAsRead to API. Fixes #1784

* Adding error
This commit is contained in:
Dessalines 2021-11-23 09:15:43 -05:00 committed by GitHub
parent 1579ee566f
commit 86b2901e8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 1 deletions

View File

@ -120,6 +120,9 @@ pub async fn match_websocket_operation(
UserOperation::CreatePostLike => { UserOperation::CreatePostLike => {
do_websocket_operation::<CreatePostLike>(context, id, op, data).await 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::SavePost => do_websocket_operation::<SavePost>(context, id, op, data).await,
UserOperation::CreatePostReport => { UserOperation::CreatePostReport => {
do_websocket_operation::<CreatePostReport>(context, id, op, data).await do_websocket_operation::<CreatePostReport>(context, id, op, data).await

View File

@ -9,6 +9,7 @@ use lemmy_api_common::{
get_local_user_view_from_jwt, get_local_user_view_from_jwt,
is_mod_or_admin, is_mod_or_admin,
mark_post_as_read, mark_post_as_read,
mark_post_as_unread,
post::*, post::*,
}; };
use lemmy_apub::{ 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)] #[async_trait::async_trait(?Send)]
impl Perform for LockPost { impl Perform for LockPost {
type Response = PostResponse; type Response = PostResponse;

View File

@ -81,7 +81,21 @@ pub async fn mark_post_as_read(
PostRead::mark_as_read(conn, &post_read_form) PostRead::mark_as_read(conn, &post_read_form)
}) })
.await? .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 async fn get_local_user_view_from_jwt(

View File

@ -92,6 +92,13 @@ pub struct RemovePost {
pub auth: String, pub auth: String,
} }
#[derive(Serialize, Deserialize)]
pub struct MarkPostAsRead {
pub post_id: PostId,
pub read: bool,
pub auth: String,
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct LockPost { pub struct LockPost {
pub post_id: PostId, pub post_id: PostId,

View File

@ -110,6 +110,7 @@ pub enum UserOperation {
CreatePostLike, CreatePostLike,
LockPost, LockPost,
StickyPost, StickyPost,
MarkPostAsRead,
SavePost, SavePost,
CreatePostReport, CreatePostReport,
ResolvePostReport, ResolvePostReport,

View File

@ -83,6 +83,10 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
.route("", web::put().to(route_post_crud::<EditPost>)) .route("", web::put().to(route_post_crud::<EditPost>))
.route("/delete", web::post().to(route_post_crud::<DeletePost>)) .route("/delete", web::post().to(route_post_crud::<DeletePost>))
.route("/remove", web::post().to(route_post_crud::<RemovePost>)) .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("/lock", web::post().to(route_post::<LockPost>))
.route("/sticky", web::post().to(route_post::<StickyPost>)) .route("/sticky", web::post().to(route_post::<StickyPost>))
.route("/list", web::get().to(route_get_crud::<GetPosts>)) .route("/list", web::get().to(route_get_crud::<GetPosts>))