88215bfbc9
* change pool fields to parameters for list * remove my_person_id and admin fields * Change recipient id to list param * Remove TypedBuilder from db_views and db_views_actor
47 lines
1.4 KiB
Rust
47 lines
1.4 KiB
Rust
use crate::PerformCrud;
|
|
use actix_web::web::Data;
|
|
use lemmy_api_common::{
|
|
context::LemmyContext,
|
|
private_message::{GetPrivateMessages, PrivateMessagesResponse},
|
|
utils::local_user_view_from_jwt,
|
|
};
|
|
use lemmy_db_views::private_message_view::PrivateMessageQuery;
|
|
use lemmy_utils::error::LemmyError;
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
impl PerformCrud for GetPrivateMessages {
|
|
type Response = PrivateMessagesResponse;
|
|
|
|
#[tracing::instrument(skip(self, context))]
|
|
async fn perform(
|
|
&self,
|
|
context: &Data<LemmyContext>,
|
|
) -> Result<PrivateMessagesResponse, LemmyError> {
|
|
let data: &GetPrivateMessages = self;
|
|
let local_user_view = local_user_view_from_jwt(data.auth.as_ref(), context).await?;
|
|
let person_id = local_user_view.person.id;
|
|
|
|
let page = data.page;
|
|
let limit = data.limit;
|
|
let unread_only = data.unread_only;
|
|
let mut messages = PrivateMessageQuery {
|
|
page,
|
|
limit,
|
|
unread_only,
|
|
}
|
|
.list(&mut context.pool(), person_id)
|
|
.await?;
|
|
|
|
// Messages sent by ourselves should be marked as read. The `read` column in database is only
|
|
// for the recipient, and shouldnt be exposed to sender.
|
|
messages.iter_mut().for_each(|pmv| {
|
|
if pmv.creator.id == person_id {
|
|
pmv.private_message.read = true
|
|
}
|
|
});
|
|
|
|
Ok(PrivateMessagesResponse {
|
|
private_messages: messages,
|
|
})
|
|
}
|
|
}
|