2021-03-25 19:19:40 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-09-19 22:58:42 +00:00
|
|
|
private_message::{GetPrivateMessages, PrivateMessagesResponse},
|
2023-05-25 14:50:07 +00:00
|
|
|
utils::local_user_view_from_jwt,
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
2022-08-04 19:30:17 +00:00
|
|
|
use lemmy_db_views::private_message_view::PrivateMessageQuery;
|
2023-06-06 16:27:22 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for GetPrivateMessages {
|
|
|
|
type Response = PrivateMessagesResponse;
|
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
#[tracing::instrument(skip(self, context))]
|
2021-03-25 19:19:40 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
) -> Result<PrivateMessagesResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &GetPrivateMessages = self;
|
2023-05-25 14:50:07 +00:00
|
|
|
let local_user_view = local_user_view_from_jwt(data.auth.as_ref(), context).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
|
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
|
|
|
let unread_only = data.unread_only;
|
2023-07-17 10:20:25 +00:00
|
|
|
let mut messages = PrivateMessageQuery {
|
|
|
|
page,
|
|
|
|
limit,
|
|
|
|
unread_only,
|
|
|
|
}
|
|
|
|
.list(&mut context.pool(), person_id)
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2022-11-03 17:41:44 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
Ok(PrivateMessagesResponse {
|
|
|
|
private_messages: messages,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|