2022-03-24 16:33:42 +00:00
|
|
|
use crate::{
|
2022-06-02 14:33:41 +00:00
|
|
|
check_apub_id_valid_with_strictness,
|
2022-10-27 09:24:07 +00:00
|
|
|
fetch_local_site_data,
|
2022-06-02 14:33:41 +00:00
|
|
|
local_instance,
|
2022-03-24 16:33:42 +00:00
|
|
|
objects::read_from_string_or_source,
|
|
|
|
protocol::{
|
|
|
|
objects::chat_message::{ChatMessage, ChatMessageType},
|
2022-04-01 18:25:19 +00:00
|
|
|
Source,
|
2022-03-24 16:33:42 +00:00
|
|
|
},
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use activitypub_federation::{
|
|
|
|
core::object_id::ObjectId,
|
|
|
|
deser::values::MediaTypeHtml,
|
2021-10-27 16:03:07 +00:00
|
|
|
traits::ApubObject,
|
2022-06-02 14:33:41 +00:00
|
|
|
utils::verify_domains_match,
|
2021-07-31 20:58:11 +00:00
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2022-11-09 10:05:00 +00:00
|
|
|
use lemmy_api_common::utils::check_person_block;
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
person::Person,
|
2022-10-27 09:24:07 +00:00
|
|
|
private_message::{PrivateMessage, PrivateMessageInsertForm},
|
2021-10-16 13:33:38 +00:00
|
|
|
},
|
|
|
|
traits::Crud,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
2021-11-02 11:49:28 +00:00
|
|
|
use lemmy_utils::{
|
2022-06-02 14:33:41 +00:00
|
|
|
error::LemmyError,
|
2021-11-02 11:49:28 +00:00
|
|
|
utils::{convert_datetime, markdown_to_html},
|
|
|
|
};
|
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 {
|
2022-03-30 14:58:03 +00:00
|
|
|
ApubPrivateMessage(pm)
|
2021-10-18 21:36:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2022-03-23 21:27:51 +00:00
|
|
|
type DbType = PrivateMessage;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
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(
|
2022-11-09 10:05:00 +00:00
|
|
|
PrivateMessage::read_from_apub_id(context.pool(), object_id)
|
|
|
|
.await?
|
|
|
|
.map(Into::into),
|
2021-10-18 21:36:44 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-11-09 10:05:00 +00:00
|
|
|
let creator = Person::read(context.pool(), creator_id).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
let recipient_id = self.recipient_id;
|
2022-11-09 10:05:00 +00:00
|
|
|
let recipient = Person::read(context.pool(), 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),
|
2022-06-08 15:45:39 +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-04-01 18:25:19 +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-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())?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let local_site_data = fetch_local_site_data(context.pool()).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
check_apub_id_valid_with_strictness(
|
|
|
|
note.id.inner(),
|
|
|
|
false,
|
|
|
|
&local_site_data,
|
|
|
|
context.settings(),
|
|
|
|
)?;
|
2021-11-06 17:35:14 +00:00
|
|
|
let person = note
|
|
|
|
.attributed_to
|
2022-11-09 10:05:00 +00:00
|
|
|
.dereference(context, local_instance(context).await, 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
|
2022-11-09 10:05:00 +00:00
|
|
|
.dereference(context, local_instance(context).await, request_counter)
|
2021-12-06 22:54:34 +00:00
|
|
|
.await?;
|
2022-06-08 15:45:39 +00:00
|
|
|
let recipient = note.to[0]
|
2022-11-09 10:05:00 +00:00
|
|
|
.dereference(context, local_instance(context).await, request_counter)
|
2021-09-25 15:44:52 +00:00
|
|
|
.await?;
|
2022-08-17 20:53:19 +00:00
|
|
|
check_person_block(creator.id, recipient.id, context.pool()).await?;
|
2020-11-24 17:53:43 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let form = PrivateMessageInsertForm {
|
2020-10-12 14:10:09 +00:00
|
|
|
creator_id: creator.id,
|
|
|
|
recipient_id: recipient.id,
|
2022-05-06 23:53:33 +00:00
|
|
|
content: read_from_string_or_source(¬e.content, &None, ¬e.source),
|
2021-11-06 12:37:55 +00:00
|
|
|
published: note.published.map(|u| u.naive_local()),
|
|
|
|
updated: note.updated.map(|u| u.naive_local()),
|
2022-11-03 13:39:30 +00:00
|
|
|
deleted: Some(false),
|
2020-10-12 14:10:09 +00:00
|
|
|
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
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
let pm = PrivateMessage::create(context.pool(), &form).await?;
|
2021-10-18 21:36:44 +00:00
|
|
|
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-11-09 10:05:00 +00:00
|
|
|
async fn cleanup(data: (ApubPerson, ApubPerson, ApubSite), context: &LemmyContext) {
|
|
|
|
Person::delete(context.pool(), data.0.id).await.unwrap();
|
|
|
|
Person::delete(context.pool(), data.1.id).await.unwrap();
|
|
|
|
Site::delete(context.pool(), data.2.id).await.unwrap();
|
2021-10-22 16:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_parse_lemmy_pm() {
|
2022-11-09 10:05:00 +00:00
|
|
|
let context = init_context().await;
|
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);
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
PrivateMessage::delete(context.pool(), pm_id).await.unwrap();
|
|
|
|
cleanup(data, &context).await;
|
2021-10-22 16:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_parse_pleroma_pm() {
|
2022-11-09 10:05:00 +00:00
|
|
|
let context = init_context().await;
|
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);
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
PrivateMessage::delete(context.pool(), pm.id).await.unwrap();
|
|
|
|
cleanup(data, &context).await;
|
2021-10-21 17:25:35 +00:00
|
|
|
}
|
|
|
|
}
|