2021-11-05 00:24:10 +00:00
|
|
|
use crate::protocol::{
|
|
|
|
objects::chat_message::{ChatMessage, ChatMessageType},
|
|
|
|
Source,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
2021-10-28 21:17:59 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2021-10-22 16:21:26 +00:00
|
|
|
use html2md::parse_html;
|
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::{
|
2021-11-05 00:24:10 +00:00
|
|
|
object_id::ObjectId,
|
2021-10-27 16:03:07 +00:00
|
|
|
traits::ApubObject,
|
2022-02-07 19:23:12 +00:00
|
|
|
values::MediaTypeHtml,
|
2021-11-03 16:26:09 +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,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
2021-11-02 11:49:28 +00:00
|
|
|
use lemmy_utils::{
|
|
|
|
utils::{convert_datetime, markdown_to_html},
|
|
|
|
LemmyError,
|
|
|
|
};
|
2020-10-12 14:10:09 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
2021-10-18 21:36:44 +00:00
|
|
|
use std::ops::Deref;
|
2020-10-12 14:10:09 +00:00
|
|
|
use url::Url;
|
|
|
|
|
2021-10-18 21:36:44 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct ApubPrivateMessage(PrivateMessage);
|
|
|
|
|
|
|
|
impl Deref for ApubPrivateMessage {
|
|
|
|
type Target = PrivateMessage;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PrivateMessage> for ApubPrivateMessage {
|
|
|
|
fn from(pm: PrivateMessage) -> Self {
|
|
|
|
ApubPrivateMessage { 0: pm }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:10:09 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2021-10-18 21:36:44 +00:00
|
|
|
impl ApubObject for ApubPrivateMessage {
|
|
|
|
type DataType = LemmyContext;
|
2021-10-28 11:46:48 +00:00
|
|
|
type ApubType = ChatMessage;
|
2021-10-28 15:25:26 +00:00
|
|
|
type TombstoneType = ();
|
2021-10-18 21:36:44 +00:00
|
|
|
|
|
|
|
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-10-18 21:36:44 +00:00
|
|
|
async fn read_from_apub_id(
|
|
|
|
object_id: Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<Option<Self>, LemmyError> {
|
|
|
|
Ok(
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
PrivateMessage::read_from_apub_id(conn, object_id)
|
|
|
|
})
|
|
|
|
.await??
|
|
|
|
.map(Into::into),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn delete(self, _context: &LemmyContext) -> Result<(), LemmyError> {
|
|
|
|
// do nothing, because pm can't be fetched over http
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-11-06 12:37:55 +00:00
|
|
|
async fn into_apub(self, context: &LemmyContext) -> Result<ChatMessage, LemmyError> {
|
2020-10-12 14:10:09 +00:00
|
|
|
let creator_id = self.creator_id;
|
2021-10-27 16:03:07 +00:00
|
|
|
let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
let recipient_id = self.recipient_id;
|
2021-10-27 16:03:07 +00:00
|
|
|
let recipient =
|
|
|
|
blocking(context.pool(), move |conn| Person::read(conn, recipient_id)).await??;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-10-28 11:46:48 +00:00
|
|
|
let note = ChatMessage {
|
2021-10-22 16:21:26 +00:00
|
|
|
r#type: ChatMessageType::ChatMessage,
|
2021-11-03 16:26:09 +00:00
|
|
|
id: ObjectId::new(self.ap_id.clone()),
|
2021-09-25 15:44:52 +00:00
|
|
|
attributed_to: ObjectId::new(creator.actor_id),
|
2021-10-22 16:21:26 +00:00
|
|
|
to: [ObjectId::new(recipient.actor_id)],
|
2021-11-02 11:49:28 +00:00
|
|
|
content: markdown_to_html(&self.content),
|
2021-10-22 16:21:26 +00:00
|
|
|
media_type: Some(MediaTypeHtml::Html),
|
2022-02-07 19:23:12 +00:00
|
|
|
source: Some(Source::new(self.content.clone())),
|
2021-10-21 17:25:35 +00:00
|
|
|
published: Some(convert_datetime(self.published)),
|
2021-07-31 20:58:11 +00:00
|
|
|
updated: self.updated.map(convert_datetime),
|
|
|
|
};
|
|
|
|
Ok(note)
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 15:25:26 +00:00
|
|
|
fn to_tombstone(&self) -> Result<(), LemmyError> {
|
2021-10-27 16:03:07 +00:00
|
|
|
unimplemented!()
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-11-06 17:35:14 +00:00
|
|
|
async fn verify(
|
|
|
|
note: &ChatMessage,
|
|
|
|
expected_domain: &Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
verify_domains_match(note.id.inner(), expected_domain)?;
|
|
|
|
verify_domains_match(note.attributed_to.inner(), note.id.inner())?;
|
|
|
|
let person = note
|
|
|
|
.attributed_to
|
2021-12-06 22:54:34 +00:00
|
|
|
.dereference(context, context.client(), request_counter)
|
2021-11-06 17:35:14 +00:00
|
|
|
.await?;
|
|
|
|
if person.banned {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("Person is banned from site"));
|
2021-11-06 17:35:14 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2020-10-12 14:10:09 +00:00
|
|
|
async fn from_apub(
|
2021-11-06 12:37:55 +00:00
|
|
|
note: ChatMessage,
|
2020-10-12 14:10:09 +00:00
|
|
|
context: &LemmyContext,
|
2020-10-22 18:27:32 +00:00
|
|
|
request_counter: &mut i32,
|
2021-10-18 21:36:44 +00:00
|
|
|
) -> Result<ApubPrivateMessage, LemmyError> {
|
2021-09-25 15:44:52 +00:00
|
|
|
let creator = note
|
|
|
|
.attributed_to
|
2021-12-06 22:54:34 +00:00
|
|
|
.dereference(context, context.client(), request_counter)
|
|
|
|
.await?;
|
|
|
|
let recipient = note.to[0]
|
|
|
|
.dereference(context, context.client(), request_counter)
|
2021-09-25 15:44:52 +00:00
|
|
|
.await?;
|
2021-10-22 16:21:26 +00:00
|
|
|
let content = if let Some(source) = ¬e.source {
|
|
|
|
source.content.clone()
|
|
|
|
} else {
|
|
|
|
parse_html(¬e.content)
|
|
|
|
};
|
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-10-22 16:21:26 +00:00
|
|
|
content,
|
2021-11-06 12:37:55 +00:00
|
|
|
published: note.published.map(|u| u.naive_local()),
|
|
|
|
updated: note.updated.map(|u| u.naive_local()),
|
2020-10-12 14:10:09 +00:00
|
|
|
deleted: None,
|
|
|
|
read: None,
|
2021-11-06 17:35:14 +00:00
|
|
|
ap_id: Some(note.id.into()),
|
2021-03-20 20:59:07 +00:00
|
|
|
local: Some(false),
|
2021-07-31 20:58:11 +00:00
|
|
|
};
|
2021-10-18 21:36:44 +00:00
|
|
|
let pm = blocking(context.pool(), move |conn| {
|
|
|
|
PrivateMessage::upsert(conn, &form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
Ok(pm.into())
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-21 17:25:35 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2022-02-17 22:04:01 +00:00
|
|
|
use crate::{
|
2022-03-03 17:51:55 +00:00
|
|
|
objects::{
|
|
|
|
instance::{tests::parse_lemmy_instance, ApubSite},
|
|
|
|
person::ApubPerson,
|
|
|
|
tests::init_context,
|
|
|
|
},
|
2022-02-17 22:04:01 +00:00
|
|
|
protocol::tests::file_to_json_object,
|
2021-10-28 21:17:59 +00:00
|
|
|
};
|
2021-10-21 17:25:35 +00:00
|
|
|
use assert_json_diff::assert_json_include;
|
2022-03-03 17:51:55 +00:00
|
|
|
use lemmy_db_schema::source::site::Site;
|
2021-10-21 17:25:35 +00:00
|
|
|
use serial_test::serial;
|
|
|
|
|
2022-03-03 17:51:55 +00:00
|
|
|
async fn prepare_comment_test(
|
|
|
|
url: &Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> (ApubPerson, ApubPerson, ApubSite) {
|
2022-01-17 14:40:47 +00:00
|
|
|
let lemmy_person = file_to_json_object("assets/lemmy/objects/person.json").unwrap();
|
2022-03-03 17:51:55 +00:00
|
|
|
let site = parse_lemmy_instance(context).await;
|
2021-11-06 17:35:14 +00:00
|
|
|
ApubPerson::verify(&lemmy_person, url, context, &mut 0)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let person1 = ApubPerson::from_apub(lemmy_person, context, &mut 0)
|
2021-10-21 17:25:35 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-01-17 14:40:47 +00:00
|
|
|
let pleroma_person = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
|
2021-10-21 17:25:35 +00:00
|
|
|
let pleroma_url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
|
2021-11-06 17:35:14 +00:00
|
|
|
ApubPerson::verify(&pleroma_person, &pleroma_url, context, &mut 0)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let person2 = ApubPerson::from_apub(pleroma_person, context, &mut 0)
|
2021-10-21 17:25:35 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-03-03 17:51:55 +00:00
|
|
|
(person1, person2, site)
|
2021-10-22 16:21:26 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 17:51:55 +00:00
|
|
|
fn cleanup(data: (ApubPerson, ApubPerson, ApubSite), context: &LemmyContext) {
|
2021-10-22 16:21:26 +00:00
|
|
|
Person::delete(&*context.pool().get().unwrap(), data.0.id).unwrap();
|
|
|
|
Person::delete(&*context.pool().get().unwrap(), data.1.id).unwrap();
|
2022-03-03 17:51:55 +00:00
|
|
|
Site::delete(&*context.pool().get().unwrap(), data.2.id).unwrap();
|
2021-10-22 16:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_parse_lemmy_pm() {
|
2022-03-03 18:54:33 +00:00
|
|
|
let context = init_context();
|
2021-10-22 16:21:26 +00:00
|
|
|
let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
|
|
|
|
let data = prepare_comment_test(&url, &context).await;
|
2022-01-17 14:40:47 +00:00
|
|
|
let json: ChatMessage = file_to_json_object("assets/lemmy/objects/chat_message.json").unwrap();
|
2021-10-21 17:25:35 +00:00
|
|
|
let mut request_counter = 0;
|
2021-11-06 17:35:14 +00:00
|
|
|
ApubPrivateMessage::verify(&json, &url, &context, &mut request_counter)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let pm = ApubPrivateMessage::from_apub(json.clone(), &context, &mut request_counter)
|
2021-10-21 17:25:35 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-11-05 00:24:10 +00:00
|
|
|
assert_eq!(pm.ap_id.clone(), url.into());
|
2021-10-22 16:21:26 +00:00
|
|
|
assert_eq!(pm.content.len(), 20);
|
2021-10-21 17:25:35 +00:00
|
|
|
assert_eq!(request_counter, 0);
|
|
|
|
|
2021-11-06 12:37:55 +00:00
|
|
|
let pm_id = pm.id;
|
|
|
|
let to_apub = pm.into_apub(&context).await.unwrap();
|
2021-10-21 17:25:35 +00:00
|
|
|
assert_json_include!(actual: json, expected: to_apub);
|
|
|
|
|
2021-11-06 12:37:55 +00:00
|
|
|
PrivateMessage::delete(&*context.pool().get().unwrap(), pm_id).unwrap();
|
2021-10-22 16:21:26 +00:00
|
|
|
cleanup(data, &context);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_parse_pleroma_pm() {
|
2022-03-03 18:54:33 +00:00
|
|
|
let context = init_context();
|
2021-10-22 16:21:26 +00:00
|
|
|
let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
|
|
|
|
let data = prepare_comment_test(&url, &context).await;
|
|
|
|
let pleroma_url = Url::parse("https://queer.hacktivis.me/objects/2").unwrap();
|
2022-01-17 14:40:47 +00:00
|
|
|
let json = file_to_json_object("assets/pleroma/objects/chat_message.json").unwrap();
|
2021-10-22 16:21:26 +00:00
|
|
|
let mut request_counter = 0;
|
2021-11-06 17:35:14 +00:00
|
|
|
ApubPrivateMessage::verify(&json, &pleroma_url, &context, &mut request_counter)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let pm = ApubPrivateMessage::from_apub(json, &context, &mut request_counter)
|
2021-10-22 16:21:26 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-11-05 00:24:10 +00:00
|
|
|
assert_eq!(pm.ap_id, pleroma_url.into());
|
2021-10-22 16:21:26 +00:00
|
|
|
assert_eq!(pm.content.len(), 3);
|
|
|
|
assert_eq!(request_counter, 0);
|
|
|
|
|
|
|
|
PrivateMessage::delete(&*context.pool().get().unwrap(), pm.id).unwrap();
|
|
|
|
cleanup(data, &context);
|
2021-10-21 17:25:35 +00:00
|
|
|
}
|
|
|
|
}
|