2020-10-12 14:10:09 +00:00
|
|
|
use crate::{
|
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,
|
2021-07-31 20:58:11 +00:00
|
|
|
objects::{create_tombstone, FromApub, Source, ToApub},
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
|
|
|
use activitystreams::{
|
2021-07-31 20:58:11 +00:00
|
|
|
base::AnyBase,
|
|
|
|
object::{kind::NoteType, Tombstone},
|
|
|
|
primitives::OneOrMany,
|
|
|
|
unparsed::Unparsed,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
2021-07-31 20:58:11 +00:00
|
|
|
use anyhow::anyhow;
|
|
|
|
use chrono::{DateTime, FixedOffset};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::blocking;
|
2021-07-31 20:58:11 +00:00
|
|
|
use lemmy_apub_lib::{
|
|
|
|
values::{MediaTypeHtml, MediaTypeMarkdown},
|
|
|
|
verify_domains_match,
|
|
|
|
};
|
|
|
|
use lemmy_db_queries::{ApubObject, 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
|
|
|
};
|
2021-07-31 20:58:11 +00:00
|
|
|
use lemmy_utils::{utils::convert_datetime, LemmyError};
|
2020-10-12 14:10:09 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
2021-07-31 20:58:11 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-08-12 12:48:09 +00:00
|
|
|
use serde_with::skip_serializing_none;
|
2020-10-12 14:10:09 +00:00
|
|
|
use url::Url;
|
|
|
|
|
2021-08-12 12:48:09 +00:00
|
|
|
#[skip_serializing_none]
|
2021-07-31 20:58:11 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Note {
|
|
|
|
#[serde(rename = "@context")]
|
|
|
|
context: OneOrMany<AnyBase>,
|
|
|
|
r#type: NoteType,
|
2021-08-12 12:48:09 +00:00
|
|
|
id: Url,
|
2021-07-31 20:58:11 +00:00
|
|
|
pub(crate) attributed_to: Url,
|
|
|
|
to: Url,
|
|
|
|
content: String,
|
|
|
|
media_type: MediaTypeHtml,
|
|
|
|
source: Source,
|
|
|
|
published: DateTime<FixedOffset>,
|
|
|
|
updated: Option<DateTime<FixedOffset>>,
|
|
|
|
#[serde(flatten)]
|
|
|
|
unparsed: Unparsed,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Note {
|
2021-08-12 12:48:09 +00:00
|
|
|
pub(crate) fn id_unchecked(&self) -> &Url {
|
|
|
|
&self.id
|
|
|
|
}
|
|
|
|
pub(crate) fn id(&self, expected_domain: &Url) -> Result<&Url, LemmyError> {
|
|
|
|
verify_domains_match(&self.id, expected_domain)?;
|
|
|
|
Ok(&self.id)
|
|
|
|
}
|
|
|
|
|
2021-07-31 20:58:11 +00:00
|
|
|
pub(crate) async fn verify(
|
|
|
|
&self,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
verify_domains_match(&self.attributed_to, &self.id)?;
|
|
|
|
let person =
|
|
|
|
get_or_fetch_and_upsert_person(&self.attributed_to, context, request_counter).await?;
|
|
|
|
if person.banned {
|
|
|
|
return Err(anyhow!("Person is banned from site").into());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:10:09 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl ToApub for PrivateMessage {
|
2021-07-31 20:58:11 +00:00
|
|
|
type ApubType = Note;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-07-31 20:58:11 +00:00
|
|
|
async fn to_apub(&self, pool: &DbPool) -> Result<Note, LemmyError> {
|
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
|
|
|
|
2021-07-31 20:58:11 +00:00
|
|
|
let note = Note {
|
|
|
|
context: lemmy_context(),
|
|
|
|
r#type: NoteType::Note,
|
|
|
|
id: self.ap_id.clone().into(),
|
|
|
|
attributed_to: creator.actor_id.into_inner(),
|
|
|
|
to: recipient.actor_id.into(),
|
|
|
|
content: self.content.clone(),
|
|
|
|
media_type: MediaTypeHtml::Html,
|
|
|
|
source: Source {
|
|
|
|
content: self.content.clone(),
|
|
|
|
media_type: MediaTypeMarkdown::Markdown,
|
|
|
|
},
|
|
|
|
published: convert_datetime(self.published),
|
|
|
|
updated: self.updated.map(convert_datetime),
|
|
|
|
unparsed: Default::default(),
|
|
|
|
};
|
|
|
|
Ok(note)
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-07-31 20:58:11 +00:00
|
|
|
type ApubType = Note;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
async fn from_apub(
|
2021-07-31 20:58:11 +00:00
|
|
|
note: &Note,
|
2020-10-12 14:10:09 +00:00
|
|
|
context: &LemmyContext,
|
2021-08-12 12:48:09 +00:00
|
|
|
expected_domain: &Url,
|
2020-10-22 18:27:32 +00:00
|
|
|
request_counter: &mut i32,
|
2021-07-31 20:58:11 +00:00
|
|
|
) -> Result<PrivateMessage, LemmyError> {
|
2021-08-12 12:48:09 +00:00
|
|
|
let ap_id = Some(note.id(expected_domain)?.clone().into());
|
2021-03-11 04:43:11 +00:00
|
|
|
let creator =
|
2021-07-31 20:58:11 +00:00
|
|
|
get_or_fetch_and_upsert_person(¬e.attributed_to, context, request_counter).await?;
|
|
|
|
let recipient = get_or_fetch_and_upsert_person(¬e.to, context, request_counter).await?;
|
2020-11-24 17:53:43 +00:00
|
|
|
|
2021-07-31 20:58:11 +00:00
|
|
|
let form = PrivateMessageForm {
|
2020-10-12 14:10:09 +00:00
|
|
|
creator_id: creator.id,
|
|
|
|
recipient_id: recipient.id,
|
2021-07-31 20:58:11 +00:00
|
|
|
content: note.source.content.clone(),
|
|
|
|
published: Some(note.published.naive_local()),
|
|
|
|
updated: note.updated.map(|u| u.to_owned().naive_local()),
|
2020-10-12 14:10:09 +00:00
|
|
|
deleted: None,
|
|
|
|
read: None,
|
2021-08-12 12:48:09 +00:00
|
|
|
ap_id,
|
2021-03-20 20:59:07 +00:00
|
|
|
local: Some(false),
|
2021-07-31 20:58:11 +00:00
|
|
|
};
|
|
|
|
Ok(
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
PrivateMessage::upsert(conn, &form)
|
|
|
|
})
|
|
|
|
.await??,
|
|
|
|
)
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
}
|