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-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::{
|
2023-03-21 15:03:05 +00:00
|
|
|
config::Data,
|
|
|
|
protocol::{values::MediaTypeHtml, verification::verify_domains_match},
|
|
|
|
traits::Object,
|
2021-07-31 20:58:11 +00:00
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_api_common::{context::LemmyContext, 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,
|
2023-02-16 04:05:14 +00:00
|
|
|
utils::{markdown::markdown_to_html, time::convert_datetime},
|
2021-11-02 11:49:28 +00:00
|
|
|
};
|
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)]
|
2022-11-28 14:29:33 +00:00
|
|
|
pub struct ApubPrivateMessage(pub(crate) PrivateMessage);
|
2021-10-18 21:36:44 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl Object for ApubPrivateMessage {
|
2021-10-18 21:36:44 +00:00
|
|
|
type DataType = LemmyContext;
|
2023-03-21 15:03:05 +00:00
|
|
|
type Kind = ChatMessage;
|
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)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn read_from_id(
|
2021-10-18 21:36:44 +00:00
|
|
|
object_id: Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<Self::DataType>,
|
2021-10-18 21:36:44 +00:00
|
|
|
) -> 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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn delete(self, _context: &Data<Self::DataType>) -> Result<(), LemmyError> {
|
2021-10-18 21:36:44 +00:00
|
|
|
// 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)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn into_json(self, context: &Data<Self::DataType>) -> 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,
|
2023-03-21 15:03:05 +00:00
|
|
|
id: self.ap_id.clone().into(),
|
|
|
|
attributed_to: creator.actor_id.into(),
|
|
|
|
to: [recipient.actor_id.into()],
|
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,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<Self::DataType>,
|
2021-11-06 17:35:14 +00:00
|
|
|
) -> 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(),
|
|
|
|
)?;
|
2023-03-21 15:03:05 +00:00
|
|
|
let person = note.attributed_to.dereference(context).await?;
|
2021-11-06 17:35:14 +00:00
|
|
|
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)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn from_json(
|
2021-11-06 12:37:55 +00:00
|
|
|
note: ChatMessage,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<Self::DataType>,
|
2021-10-18 21:36:44 +00:00
|
|
|
) -> Result<ApubPrivateMessage, LemmyError> {
|
2023-03-21 15:03:05 +00:00
|
|
|
let creator = note.attributed_to.dereference(context).await?;
|
|
|
|
let recipient = note.to[0].dereference(context).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,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2022-03-03 17:51:55 +00:00
|
|
|
) -> (ApubPerson, ApubPerson, ApubSite) {
|
2023-03-21 15:03:05 +00:00
|
|
|
let context2 = context.reset_request_count();
|
2022-01-17 14:40:47 +00:00
|
|
|
let lemmy_person = file_to_json_object("assets/lemmy/objects/person.json").unwrap();
|
2023-03-21 15:03:05 +00:00
|
|
|
let site = parse_lemmy_instance(&context2).await;
|
|
|
|
ApubPerson::verify(&lemmy_person, url, &context2)
|
2021-11-06 17:35:14 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-03-21 15:03:05 +00:00
|
|
|
let person1 = ApubPerson::from_json(lemmy_person, &context2)
|
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();
|
2023-03-21 15:03:05 +00:00
|
|
|
ApubPerson::verify(&pleroma_person, &pleroma_url, &context2)
|
2021-11-06 17:35:14 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-03-21 15:03:05 +00:00
|
|
|
let person2 = ApubPerson::from_json(pleroma_person, &context2)
|
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
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn cleanup(data: (ApubPerson, ApubPerson, ApubSite), context: &Data<LemmyContext>) {
|
2022-11-09 10:05:00 +00:00
|
|
|
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();
|
2023-03-21 15:03:05 +00:00
|
|
|
ApubPrivateMessage::verify(&json, &url, &context)
|
2021-11-06 17:35:14 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-03-21 15:03:05 +00:00
|
|
|
let pm = ApubPrivateMessage::from_json(json.clone(), &context)
|
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);
|
2023-03-21 15:03:05 +00:00
|
|
|
assert_eq!(context.request_count(), 0);
|
2021-10-21 17:25:35 +00:00
|
|
|
|
2021-11-06 12:37:55 +00:00
|
|
|
let pm_id = pm.id;
|
2023-03-21 15:03:05 +00:00
|
|
|
let to_apub = pm.into_json(&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();
|
2023-03-21 15:03:05 +00:00
|
|
|
ApubPrivateMessage::verify(&json, &pleroma_url, &context)
|
2021-10-22 16:21:26 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-03-21 15:03:05 +00:00
|
|
|
let pm = ApubPrivateMessage::from_json(json, &context).await.unwrap();
|
2021-10-22 16:21:26 +00:00
|
|
|
|
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);
|
2023-03-21 15:03:05 +00:00
|
|
|
assert_eq!(context.request_count(), 0);
|
2021-10-22 16:21:26 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|