2020-10-12 14:10:09 +00:00
|
|
|
use crate::{
|
|
|
|
check_is_apub_id_valid,
|
2020-11-25 17:44:49 +00:00
|
|
|
extensions::context::lemmy_context,
|
2021-03-10 22:33:55 +00:00
|
|
|
fetcher::person::get_or_fetch_and_upsert_person,
|
2020-11-24 17:53:43 +00:00
|
|
|
objects::{
|
|
|
|
check_object_domain,
|
|
|
|
create_tombstone,
|
2020-12-08 17:38:48 +00:00
|
|
|
get_object_from_apub,
|
2020-11-24 17:53:43 +00:00
|
|
|
get_source_markdown_value,
|
|
|
|
set_content_and_source,
|
2020-12-08 17:38:48 +00:00
|
|
|
FromApub,
|
|
|
|
FromApubToForm,
|
|
|
|
ToApub,
|
2020-11-24 17:53:43 +00:00
|
|
|
},
|
|
|
|
NoteExt,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
|
|
|
use activitystreams::{
|
2020-11-24 17:53:43 +00:00
|
|
|
object::{kind::NoteType, ApObject, Note, Tombstone},
|
2020-10-12 14:10:09 +00:00
|
|
|
prelude::*,
|
|
|
|
};
|
|
|
|
use anyhow::Context;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::blocking;
|
2020-12-21 23:27:42 +00:00
|
|
|
use lemmy_db_queries::{Crud, DbPool};
|
2020-12-21 13:38:34 +00:00
|
|
|
use lemmy_db_schema::source::{
|
2021-03-10 22:33:55 +00:00
|
|
|
person::Person,
|
2021-03-11 04:43:11 +00:00
|
|
|
private_message::{PrivateMessage, PrivateMessageForm},
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
|
|
|
use lemmy_utils::{location_info, utils::convert_datetime, LemmyError};
|
|
|
|
use lemmy_websocket::LemmyContext;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl ToApub for PrivateMessage {
|
2020-11-24 17:53:43 +00:00
|
|
|
type ApubType = NoteExt;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2020-11-24 17:53:43 +00:00
|
|
|
async fn to_apub(&self, pool: &DbPool) -> Result<NoteExt, LemmyError> {
|
|
|
|
let mut private_message = ApObject::new(Note::new());
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
let creator_id = self.creator_id;
|
2021-03-10 22:33:55 +00:00
|
|
|
let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
let recipient_id = self.recipient_id;
|
2021-03-10 22:33:55 +00:00
|
|
|
let recipient = blocking(pool, move |conn| Person::read(conn, recipient_id)).await??;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
private_message
|
2020-11-25 17:44:49 +00:00
|
|
|
.set_many_contexts(lemmy_context()?)
|
2021-01-27 16:42:23 +00:00
|
|
|
.set_id(self.ap_id.to_owned().into_inner())
|
2020-10-12 14:10:09 +00:00
|
|
|
.set_published(convert_datetime(self.published))
|
2021-01-27 16:42:23 +00:00
|
|
|
.set_to(recipient.actor_id.into_inner())
|
|
|
|
.set_attributed_to(creator.actor_id.into_inner());
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2020-11-24 17:53:43 +00:00
|
|
|
set_content_and_source(&mut private_message, &self.content)?;
|
|
|
|
|
2020-10-12 14:10:09 +00:00
|
|
|
if let Some(u) = self.updated {
|
|
|
|
private_message.set_updated(convert_datetime(u));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(private_message)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
|
2021-01-27 16:42:23 +00:00
|
|
|
create_tombstone(
|
|
|
|
self.deleted,
|
|
|
|
self.ap_id.to_owned().into(),
|
|
|
|
self.updated,
|
|
|
|
NoteType::Note,
|
|
|
|
)
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-12-08 17:38:48 +00:00
|
|
|
impl FromApub for PrivateMessage {
|
2020-11-24 17:53:43 +00:00
|
|
|
type ApubType = NoteExt;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
async fn from_apub(
|
2020-11-24 17:53:43 +00:00
|
|
|
note: &NoteExt,
|
2020-10-12 14:10:09 +00:00
|
|
|
context: &LemmyContext,
|
2020-12-08 17:38:48 +00:00
|
|
|
expected_domain: Url,
|
|
|
|
request_counter: &mut i32,
|
2021-03-18 13:24:29 +00:00
|
|
|
mod_action_allowed: bool,
|
2020-12-08 17:38:48 +00:00
|
|
|
) -> Result<PrivateMessage, LemmyError> {
|
2021-03-16 17:26:19 +00:00
|
|
|
get_object_from_apub(
|
|
|
|
note,
|
|
|
|
context,
|
|
|
|
expected_domain,
|
|
|
|
request_counter,
|
2021-03-18 13:24:29 +00:00
|
|
|
mod_action_allowed,
|
2021-03-16 17:26:19 +00:00
|
|
|
)
|
|
|
|
.await
|
2020-12-08 17:38:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl FromApubToForm<NoteExt> for PrivateMessageForm {
|
|
|
|
async fn from_apub(
|
|
|
|
note: &NoteExt,
|
|
|
|
context: &LemmyContext,
|
|
|
|
expected_domain: Url,
|
2020-10-22 18:27:32 +00:00
|
|
|
request_counter: &mut i32,
|
2021-03-18 13:24:29 +00:00
|
|
|
_mod_action_allowed: bool,
|
2020-10-12 14:10:09 +00:00
|
|
|
) -> Result<PrivateMessageForm, LemmyError> {
|
|
|
|
let creator_actor_id = note
|
|
|
|
.attributed_to()
|
|
|
|
.context(location_info!())?
|
|
|
|
.clone()
|
|
|
|
.single_xsd_any_uri()
|
|
|
|
.context(location_info!())?;
|
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
let creator =
|
|
|
|
get_or_fetch_and_upsert_person(&creator_actor_id, context, request_counter).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
let recipient_actor_id = note
|
|
|
|
.to()
|
|
|
|
.context(location_info!())?
|
|
|
|
.clone()
|
|
|
|
.single_xsd_any_uri()
|
|
|
|
.context(location_info!())?;
|
2020-10-22 18:27:32 +00:00
|
|
|
let recipient =
|
2021-03-10 22:33:55 +00:00
|
|
|
get_or_fetch_and_upsert_person(&recipient_actor_id, context, request_counter).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
let ap_id = note.id_unchecked().context(location_info!())?.to_string();
|
|
|
|
check_is_apub_id_valid(&Url::parse(&ap_id)?)?;
|
|
|
|
|
2020-11-24 17:53:43 +00:00
|
|
|
let content = get_source_markdown_value(note)?.context(location_info!())?;
|
|
|
|
|
2020-10-12 14:10:09 +00:00
|
|
|
Ok(PrivateMessageForm {
|
|
|
|
creator_id: creator.id,
|
|
|
|
recipient_id: recipient.id,
|
2020-11-24 17:53:43 +00:00
|
|
|
content,
|
2020-10-12 14:10:09 +00:00
|
|
|
published: note.published().map(|u| u.to_owned().naive_local()),
|
|
|
|
updated: note.updated().map(|u| u.to_owned().naive_local()),
|
|
|
|
deleted: None,
|
|
|
|
read: None,
|
2020-10-14 15:34:11 +00:00
|
|
|
ap_id: Some(check_object_domain(note, expected_domain)?),
|
2021-03-20 20:59:07 +00:00
|
|
|
local: Some(false),
|
2020-10-12 14:10:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|