lemmy/crates/apub/src/activity_lists.rs
Nutomic a2a594b763
Extract Activitypub logic into separate library (#2288)
* Create example for apub lib

* some rewriting of apub lib

* Add LocalInstance struct for apub lib to avoid using Lemmy Settings

* Move ActorType trait to lemmy_apub, because its not needed in library

* Use reqwest_retry instead of custom impl, dont specify timeout on every send()

* Some improvements to example

* Moved inbox handling to library

* bug fixes

* Move context and serde helpers into library

* wip: example changes

* Add lemmy_utils feature to build only LemmyError

* Rename to activitypub_federation

* Remove lemmy_utils dep from activitypub_federation using generic error type

* Finish activitypub example

* Cleanup and fix tests

* Reorganize library files

* Remove ApubObject.to_tombstone()

* Extract activitypub library into separate git repository
2022-06-02 16:33:41 +02:00

199 lines
6.8 KiB
Rust

use crate::{
activities::{community::announce::GetCommunity, verify_person_in_community},
objects::community::ApubCommunity,
protocol::{
activities::{
block::{block_user::BlockUser, undo_block_user::UndoBlockUser},
community::{
add_mod::AddMod,
announce::AnnounceActivity,
remove_mod::RemoveMod,
report::Report,
update::UpdateCommunity,
},
create_or_update::{
comment::CreateOrUpdateComment,
post::CreateOrUpdatePost,
private_message::CreateOrUpdatePrivateMessage,
},
deletion::{delete::Delete, delete_user::DeleteUser, undo_delete::UndoDelete},
following::{
accept::AcceptFollowCommunity,
follow::FollowCommunity,
undo_follow::UndoFollowCommunity,
},
voting::{undo_vote::UndoVote, vote::Vote},
},
objects::page::Page,
Id,
},
};
use activitypub_federation::{
core::object_id::ObjectId,
data::Data,
deser::context::WithContext,
traits::{activity_handler, ActivityHandler},
};
use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Debug, Deserialize, Serialize)]
#[serde(untagged)]
#[activity_handler(LemmyContext, LemmyError)]
pub enum SharedInboxActivities {
GroupInboxActivities(Box<WithContext<GroupInboxActivities>>),
// Note, pm activities need to be at the end, otherwise comments will end up here. We can probably
// avoid this problem by replacing createpm.object with our own struct, instead of NoteExt.
PersonInboxActivities(Box<WithContext<PersonInboxActivities>>),
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum GroupInboxActivities {
FollowCommunity(FollowCommunity),
UndoFollowCommunity(UndoFollowCommunity),
AnnouncableActivities(Box<AnnouncableActivities>),
Report(Report),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
#[activity_handler(LemmyContext, LemmyError)]
pub enum PersonInboxActivities {
AcceptFollowCommunity(AcceptFollowCommunity),
/// Some activities can also be sent from user to user, eg a comment with mentions
AnnouncableActivities(AnnouncableActivities),
CreateOrUpdatePrivateMessage(CreateOrUpdatePrivateMessage),
Delete(Delete),
UndoDelete(UndoDelete),
AnnounceActivity(AnnounceActivity),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
#[activity_handler(LemmyContext, LemmyError)]
pub enum AnnouncableActivities {
CreateOrUpdateComment(CreateOrUpdateComment),
CreateOrUpdatePost(Box<CreateOrUpdatePost>),
Vote(Vote),
UndoVote(UndoVote),
Delete(Delete),
UndoDelete(UndoDelete),
UpdateCommunity(UpdateCommunity),
BlockUser(BlockUser),
UndoBlockUser(UndoBlockUser),
AddMod(AddMod),
RemoveMod(RemoveMod),
// For compatibility with Pleroma/Mastodon (send only)
Page(Page),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
#[activity_handler(LemmyContext, LemmyError)]
#[allow(clippy::enum_variant_names)]
pub enum SiteInboxActivities {
BlockUser(BlockUser),
UndoBlockUser(UndoBlockUser),
DeleteUser(DeleteUser),
}
#[async_trait::async_trait(?Send)]
impl GetCommunity for AnnouncableActivities {
#[tracing::instrument(skip(self, context))]
async fn get_community(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
use AnnouncableActivities::*;
let community = match self {
CreateOrUpdateComment(a) => a.get_community(context, request_counter).await?,
CreateOrUpdatePost(a) => a.get_community(context, request_counter).await?,
Vote(a) => a.get_community(context, request_counter).await?,
UndoVote(a) => a.get_community(context, request_counter).await?,
Delete(a) => a.get_community(context, request_counter).await?,
UndoDelete(a) => a.get_community(context, request_counter).await?,
UpdateCommunity(a) => a.get_community(context, request_counter).await?,
BlockUser(a) => a.get_community(context, request_counter).await?,
UndoBlockUser(a) => a.get_community(context, request_counter).await?,
AddMod(a) => a.get_community(context, request_counter).await?,
RemoveMod(a) => a.get_community(context, request_counter).await?,
Page(_) => unimplemented!(),
};
Ok(community)
}
}
impl Id for AnnouncableActivities {
fn object_id(&self) -> &Url {
ActivityHandler::id(self)
}
}
// Need to implement this manually to announce matching activities
#[async_trait::async_trait(?Send)]
impl ActivityHandler for GroupInboxActivities {
type DataType = LemmyContext;
type Error = LemmyError;
fn id(&self) -> &Url {
match self {
GroupInboxActivities::FollowCommunity(a) => a.id(),
GroupInboxActivities::UndoFollowCommunity(a) => a.id(),
GroupInboxActivities::AnnouncableActivities(a) => a.object_id(),
GroupInboxActivities::Report(a) => a.id(),
}
}
fn actor(&self) -> &Url {
match self {
GroupInboxActivities::FollowCommunity(a) => a.actor(),
GroupInboxActivities::UndoFollowCommunity(a) => a.actor(),
GroupInboxActivities::AnnouncableActivities(a) => a.actor(),
GroupInboxActivities::Report(a) => a.actor(),
}
}
async fn verify(
&self,
data: &Data<Self::DataType>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
match self {
GroupInboxActivities::FollowCommunity(a) => a.verify(data, request_counter).await,
GroupInboxActivities::UndoFollowCommunity(a) => a.verify(data, request_counter).await,
GroupInboxActivities::AnnouncableActivities(a) => a.verify(data, request_counter).await,
GroupInboxActivities::Report(a) => a.verify(data, request_counter).await,
}
}
async fn receive(
self,
data: &Data<Self::DataType>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
match self {
GroupInboxActivities::FollowCommunity(a) => a.receive(data, request_counter).await,
GroupInboxActivities::UndoFollowCommunity(a) => a.receive(data, request_counter).await,
GroupInboxActivities::AnnouncableActivities(activity) => {
activity.clone().receive(data, request_counter).await?;
// Ignore failures in get_community(). those happen because Delete/PrivateMessage is not in a
// community, but looks identical to Delete/Post or Delete/Comment which are in a community.
let community = activity.get_community(data, &mut 0).await;
if let Ok(community) = community {
if community.local {
let actor_id = ObjectId::new(activity.actor().clone());
verify_person_in_community(&actor_id, &community, data, &mut 0).await?;
AnnounceActivity::send(*activity, &community, data).await?;
}
}
Ok(())
}
GroupInboxActivities::Report(a) => a.receive(data, request_counter).await,
}
}
}