2021-03-25 19:19:40 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
|
|
|
person::{GetPrivateMessages, PrivateMessagesResponse},
|
2022-05-03 17:44:13 +00:00
|
|
|
utils::{blocking, get_local_user_view_from_jwt},
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::traits::DeleteableOrRemoveable;
|
2022-08-04 19:30:17 +00:00
|
|
|
use lemmy_db_views::private_message_view::PrivateMessageQuery;
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::{error::LemmyError, ConnectionId};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for GetPrivateMessages {
|
|
|
|
type Response = PrivateMessagesResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(self, context, _websocket_id))]
|
2021-03-25 19:19:40 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<PrivateMessagesResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &GetPrivateMessages = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
2021-12-06 14:54:47 +00:00
|
|
|
get_local_user_view_from_jwt(data.auth.as_ref(), context.pool(), context.secret()).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;
|
2021-07-30 18:44:15 +00:00
|
|
|
let mut messages = blocking(context.pool(), move |conn| {
|
2022-08-04 19:30:17 +00:00
|
|
|
PrivateMessageQuery::builder()
|
|
|
|
.conn(conn)
|
|
|
|
.recipient_id(person_id)
|
2021-03-25 19:19:40 +00:00
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.unread_only(unread_only)
|
2022-08-04 19:30:17 +00:00
|
|
|
.build()
|
2021-03-25 19:19:40 +00:00
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2021-07-30 18:44:15 +00:00
|
|
|
// Blank out deleted or removed info
|
|
|
|
for pmv in messages
|
|
|
|
.iter_mut()
|
|
|
|
.filter(|pmv| pmv.private_message.deleted)
|
|
|
|
{
|
|
|
|
pmv.private_message = pmv
|
|
|
|
.to_owned()
|
|
|
|
.private_message
|
|
|
|
.blank_out_deleted_or_removed_info();
|
|
|
|
}
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
Ok(PrivateMessagesResponse {
|
|
|
|
private_messages: messages,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|