Adding MarkPostAsRead to API. Fixes #1784

This commit is contained in:
Dessalines 2021-11-22 22:58:14 -05:00
parent 3e062a9959
commit c36213162c
6 changed files with 65 additions and 0 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

@ -84,6 +84,20 @@ pub async fn mark_post_as_read(
.map_err(|_| ApiError::err_plain("couldnt_mark_post_as_read").into()) .map_err(|_| ApiError::err_plain("couldnt_mark_post_as_read").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(|_| ApiError::err_plain("couldnt_mark_post_as_read").into())
}
pub async fn get_local_user_view_from_jwt( pub async fn get_local_user_view_from_jwt(
jwt: &str, jwt: &str,
pool: &DbPool, pool: &DbPool,

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>))