2020-10-12 14:10:09 +00:00
|
|
|
use crate::{
|
2021-10-06 20:20:05 +00:00
|
|
|
context::lemmy_context,
|
2021-09-25 15:44:52 +00:00
|
|
|
fetcher::object_id::ObjectId,
|
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},
|
2021-10-06 20:20:05 +00:00
|
|
|
verify::verify_domains_match,
|
2021-07-31 20:58:11 +00:00
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
person::Person,
|
|
|
|
private_message::{PrivateMessage, PrivateMessageForm},
|
|
|
|
},
|
|
|
|
traits::Crud,
|
|
|
|
DbPool,
|
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-09-25 15:44:52 +00:00
|
|
|
pub(crate) attributed_to: ObjectId<Person>,
|
|
|
|
to: ObjectId<Person>,
|
2021-07-31 20:58:11 +00:00
|
|
|
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> {
|
2021-09-25 15:44:52 +00:00
|
|
|
verify_domains_match(self.attributed_to.inner(), &self.id)?;
|
|
|
|
let person = self
|
|
|
|
.attributed_to
|
|
|
|
.dereference(context, request_counter)
|
|
|
|
.await?;
|
2021-07-31 20:58:11 +00:00
|
|
|
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(),
|
2021-09-25 15:44:52 +00:00
|
|
|
attributed_to: ObjectId::new(creator.actor_id),
|
|
|
|
to: ObjectId::new(recipient.actor_id),
|
2021-07-31 20:58:11 +00:00
|
|
|
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-09-25 15:44:52 +00:00
|
|
|
let creator = note
|
|
|
|
.attributed_to
|
|
|
|
.dereference(context, request_counter)
|
|
|
|
.await?;
|
|
|
|
let recipient = note.to.dereference(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
|
|
|
}
|
|
|
|
}
|