2020-05-16 14:04:08 +00:00
|
|
|
use crate::{
|
|
|
|
apub::{
|
2020-08-31 13:48:02 +00:00
|
|
|
activities::generate_activity_id,
|
|
|
|
activity_queue::send_activity,
|
2020-08-06 12:53:58 +00:00
|
|
|
check_actor_domain,
|
2020-08-05 12:18:08 +00:00
|
|
|
check_is_apub_id_valid,
|
2020-07-19 16:26:23 +00:00
|
|
|
create_tombstone,
|
2020-07-29 11:46:11 +00:00
|
|
|
fetcher::get_or_fetch_and_upsert_user,
|
|
|
|
insert_activity,
|
2020-08-11 14:31:05 +00:00
|
|
|
ActorType,
|
2020-07-29 11:46:11 +00:00
|
|
|
ApubObjectType,
|
|
|
|
FromApub,
|
|
|
|
ToApub,
|
2020-05-16 14:04:08 +00:00
|
|
|
},
|
2020-07-29 11:46:11 +00:00
|
|
|
DbPool,
|
2020-08-18 13:43:50 +00:00
|
|
|
LemmyContext,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2020-08-01 13:25:17 +00:00
|
|
|
use activitystreams::{
|
2020-07-28 16:47:26 +00:00
|
|
|
activity::{
|
|
|
|
kind::{CreateType, DeleteType, UndoType, UpdateType},
|
2020-07-29 11:46:11 +00:00
|
|
|
Create,
|
|
|
|
Delete,
|
|
|
|
Undo,
|
|
|
|
Update,
|
2020-07-28 16:47:26 +00:00
|
|
|
},
|
2020-07-17 21:11:07 +00:00
|
|
|
object::{kind::NoteType, Note, Tombstone},
|
|
|
|
prelude::*,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2020-08-11 14:31:05 +00:00
|
|
|
use anyhow::Context;
|
2020-07-10 18:15:41 +00:00
|
|
|
use lemmy_db::{
|
|
|
|
private_message::{PrivateMessage, PrivateMessageForm},
|
|
|
|
user::User_,
|
|
|
|
Crud,
|
|
|
|
};
|
2020-09-16 13:31:30 +00:00
|
|
|
use lemmy_structs::blocking;
|
2020-09-14 15:29:50 +00:00
|
|
|
use lemmy_utils::{location_info, utils::convert_datetime, LemmyError};
|
2020-07-17 21:11:07 +00:00
|
|
|
use url::Url;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-05-06 02:06:24 +00:00
|
|
|
impl ToApub for PrivateMessage {
|
|
|
|
type Response = Note;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn to_apub(&self, pool: &DbPool) -> Result<Note, LemmyError> {
|
2020-07-17 21:11:07 +00:00
|
|
|
let mut private_message = Note::new();
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let creator_id = self.creator_id;
|
|
|
|
let creator = blocking(pool, move |conn| User_::read(conn, creator_id)).await??;
|
|
|
|
|
|
|
|
let recipient_id = self.recipient_id;
|
|
|
|
let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-17 21:11:07 +00:00
|
|
|
private_message
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-17 21:11:07 +00:00
|
|
|
.set_id(Url::parse(&self.ap_id.to_owned())?)
|
|
|
|
.set_published(convert_datetime(self.published))
|
|
|
|
.set_content(self.content.to_owned())
|
|
|
|
.set_to(recipient.actor_id)
|
|
|
|
.set_attributed_to(creator.actor_id);
|
2020-05-06 02:06:24 +00:00
|
|
|
|
|
|
|
if let Some(u) = self.updated {
|
2020-07-17 21:11:07 +00:00
|
|
|
private_message.set_updated(convert_datetime(u));
|
2020-05-06 02:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(private_message)
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
|
2020-07-28 16:47:26 +00:00
|
|
|
create_tombstone(self.deleted, &self.ap_id, self.updated, NoteType::Note)
|
2020-05-06 02:06:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-05-06 02:06:24 +00:00
|
|
|
impl FromApub for PrivateMessageForm {
|
|
|
|
type ApubType = Note;
|
|
|
|
|
|
|
|
/// Parse an ActivityPub note received from another instance into a Lemmy Private message
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn from_apub(
|
2020-07-13 13:56:58 +00:00
|
|
|
note: &Note,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-08-06 12:53:58 +00:00
|
|
|
expected_domain: Option<Url>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<PrivateMessageForm, LemmyError> {
|
2020-07-17 21:11:07 +00:00
|
|
|
let creator_actor_id = note
|
|
|
|
.attributed_to()
|
2020-08-11 14:31:05 +00:00
|
|
|
.context(location_info!())?
|
2020-07-17 21:11:07 +00:00
|
|
|
.clone()
|
|
|
|
.single_xsd_any_uri()
|
2020-08-11 14:31:05 +00:00
|
|
|
.context(location_info!())?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let creator = get_or_fetch_and_upsert_user(&creator_actor_id, context).await?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let recipient_actor_id = note
|
|
|
|
.to()
|
|
|
|
.context(location_info!())?
|
|
|
|
.clone()
|
|
|
|
.single_xsd_any_uri()
|
|
|
|
.context(location_info!())?;
|
2020-08-18 13:43:50 +00:00
|
|
|
let recipient = get_or_fetch_and_upsert_user(&recipient_actor_id, context).await?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let ap_id = note.id_unchecked().context(location_info!())?.to_string();
|
2020-08-05 12:18:08 +00:00
|
|
|
check_is_apub_id_valid(&Url::parse(&ap_id)?)?;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
|
|
|
Ok(PrivateMessageForm {
|
|
|
|
creator_id: creator.id,
|
|
|
|
recipient_id: recipient.id,
|
2020-07-17 21:11:07 +00:00
|
|
|
content: note
|
|
|
|
.content()
|
2020-08-11 14:31:05 +00:00
|
|
|
.context(location_info!())?
|
2020-07-17 21:11:07 +00:00
|
|
|
.as_single_xsd_string()
|
2020-08-11 14:31:05 +00:00
|
|
|
.context(location_info!())?
|
2020-07-17 21:11:07 +00:00
|
|
|
.to_string(),
|
|
|
|
published: note.published().map(|u| u.to_owned().naive_local()),
|
|
|
|
updated: note.updated().map(|u| u.to_owned().naive_local()),
|
2020-05-06 02:06:24 +00:00
|
|
|
deleted: None,
|
|
|
|
read: None,
|
2020-08-31 13:48:02 +00:00
|
|
|
ap_id: Some(check_actor_domain(note, expected_domain)?),
|
2020-05-06 02:06:24 +00:00
|
|
|
local: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-05-06 02:06:24 +00:00
|
|
|
impl ApubObjectType for PrivateMessage {
|
|
|
|
/// Send out information about a newly created private message
|
2020-08-18 13:43:50 +00:00
|
|
|
async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
|
|
|
|
let note = self.to_apub(context.pool()).await?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let recipient_id = self.recipient_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-17 21:11:07 +00:00
|
|
|
let mut create = Create::new(creator.actor_id.to_owned(), note.into_any_base()?);
|
2020-08-11 14:31:05 +00:00
|
|
|
let to = recipient.get_inbox_url()?;
|
2020-05-06 02:06:24 +00:00
|
|
|
create
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(CreateType::Create)?)
|
2020-07-17 21:11:07 +00:00
|
|
|
.set_to(to.clone());
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
insert_activity(creator.id, create.clone(), true, context.pool()).await?;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-08-31 13:48:02 +00:00
|
|
|
send_activity(context.activity_queue(), create, creator, vec![to])?;
|
2020-05-06 02:06:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send out information about an edited post, to the followers of the community.
|
2020-08-18 13:43:50 +00:00
|
|
|
async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
|
|
|
|
let note = self.to_apub(context.pool()).await?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let recipient_id = self.recipient_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-17 21:11:07 +00:00
|
|
|
let mut update = Update::new(creator.actor_id.to_owned(), note.into_any_base()?);
|
2020-08-11 14:31:05 +00:00
|
|
|
let to = recipient.get_inbox_url()?;
|
2020-05-06 02:06:24 +00:00
|
|
|
update
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(UpdateType::Update)?)
|
2020-07-17 21:11:07 +00:00
|
|
|
.set_to(to.clone());
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
insert_activity(creator.id, update.clone(), true, context.pool()).await?;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-08-31 13:48:02 +00:00
|
|
|
send_activity(context.activity_queue(), update, creator, vec![to])?;
|
2020-05-06 02:06:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
|
|
|
|
let note = self.to_apub(context.pool()).await?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let recipient_id = self.recipient_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-17 21:11:07 +00:00
|
|
|
let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?);
|
2020-08-11 14:31:05 +00:00
|
|
|
let to = recipient.get_inbox_url()?;
|
2020-05-06 02:06:24 +00:00
|
|
|
delete
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(DeleteType::Delete)?)
|
2020-07-17 21:11:07 +00:00
|
|
|
.set_to(to.clone());
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
insert_activity(creator.id, delete.clone(), true, context.pool()).await?;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-08-31 13:48:02 +00:00
|
|
|
send_activity(context.activity_queue(), delete, creator, vec![to])?;
|
2020-05-06 02:06:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn send_undo_delete(
|
|
|
|
&self,
|
|
|
|
creator: &User_,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2020-08-18 13:43:50 +00:00
|
|
|
let note = self.to_apub(context.pool()).await?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let recipient_id = self.recipient_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-17 21:11:07 +00:00
|
|
|
let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?);
|
2020-08-11 14:31:05 +00:00
|
|
|
let to = recipient.get_inbox_url()?;
|
2020-05-06 02:06:24 +00:00
|
|
|
delete
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(DeleteType::Delete)?)
|
2020-07-17 21:11:07 +00:00
|
|
|
.set_to(to.clone());
|
2020-05-06 02:06:24 +00:00
|
|
|
|
|
|
|
// Undo that fake activity
|
2020-07-17 21:11:07 +00:00
|
|
|
let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?);
|
2020-05-06 02:06:24 +00:00
|
|
|
undo
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(UndoType::Undo)?)
|
2020-07-17 21:11:07 +00:00
|
|
|
.set_to(to.clone());
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
insert_activity(creator.id, undo.clone(), true, context.pool()).await?;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-08-31 13:48:02 +00:00
|
|
|
send_activity(context.activity_queue(), undo, creator, vec![to])?;
|
2020-05-06 02:06:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
async fn send_remove(&self, _mod_: &User_, _context: &LemmyContext) -> Result<(), LemmyError> {
|
2020-05-06 02:06:24 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn send_undo_remove(
|
|
|
|
&self,
|
|
|
|
_mod_: &User_,
|
2020-08-18 13:43:50 +00:00
|
|
|
_context: &LemmyContext,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2020-05-06 02:06:24 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|