2021-10-29 10:32:42 +00:00
|
|
|
use crate::{
|
|
|
|
objects::community::ApubCommunity,
|
2021-11-11 19:49:15 +00:00
|
|
|
protocol::{
|
|
|
|
activities::{
|
2022-02-07 19:23:12 +00:00
|
|
|
block::{block_user::BlockUser, undo_block_user::UndoBlockUser},
|
2021-11-11 19:49:15 +00:00
|
|
|
community::{
|
2022-11-12 13:52:57 +00:00
|
|
|
announce::{AnnounceActivity, RawAnnouncableActivities},
|
2023-02-18 14:50:28 +00:00
|
|
|
collection_add::CollectionAdd,
|
|
|
|
collection_remove::CollectionRemove,
|
|
|
|
lock_page::{LockPage, UndoLockPage},
|
2021-11-11 19:49:15 +00:00
|
|
|
report::Report,
|
|
|
|
update::UpdateCommunity,
|
|
|
|
},
|
2022-02-14 15:14:24 +00:00
|
|
|
create_or_update::{
|
2022-12-01 20:52:49 +00:00
|
|
|
chat_message::CreateOrUpdateChatMessage,
|
|
|
|
note::CreateOrUpdateNote,
|
|
|
|
page::CreateOrUpdatePage,
|
2022-02-14 15:14:24 +00:00
|
|
|
},
|
2023-11-16 13:22:40 +00:00
|
|
|
deletion::{delete::Delete, undo_delete::UndoDelete},
|
2022-11-23 23:40:47 +00:00
|
|
|
following::{accept::AcceptFollow, follow::Follow, undo_follow::UndoFollow},
|
2021-11-11 19:49:15 +00:00
|
|
|
voting::{undo_vote::UndoVote, vote::Vote},
|
2021-10-29 10:32:42 +00:00
|
|
|
},
|
2021-11-18 15:20:35 +00:00
|
|
|
objects::page::Page,
|
2022-12-01 20:52:49 +00:00
|
|
|
InCommunity,
|
2021-10-29 10:32:42 +00:00
|
|
|
},
|
|
|
|
};
|
2023-07-27 10:17:40 +00:00
|
|
|
use activitypub_federation::{config::Data, traits::ActivityHandler};
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_api_common::context::LemmyContext;
|
2024-08-01 00:28:41 +00:00
|
|
|
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
|
2021-11-01 13:05:20 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-05-06 23:53:33 +00:00
|
|
|
use url::Url;
|
2021-10-29 10:32:42 +00:00
|
|
|
|
2023-07-27 10:17:40 +00:00
|
|
|
/// List of activities which the shared inbox can handle.
|
|
|
|
///
|
|
|
|
/// This could theoretically be defined as an enum with variants `GroupInboxActivities` and
|
|
|
|
/// `PersonInboxActivities`. In practice we need to write it out manually so that priorities
|
|
|
|
/// are handled correctly.
|
2024-01-05 14:42:46 +00:00
|
|
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
2021-10-29 10:32:42 +00:00
|
|
|
#[serde(untagged)]
|
2022-11-16 22:51:05 +00:00
|
|
|
#[enum_delegate::implement(ActivityHandler)]
|
2021-10-29 10:32:42 +00:00
|
|
|
pub enum SharedInboxActivities {
|
2023-07-27 10:17:40 +00:00
|
|
|
Follow(Follow),
|
|
|
|
AcceptFollow(AcceptFollow),
|
|
|
|
UndoFollow(UndoFollow),
|
|
|
|
CreateOrUpdatePrivateMessage(CreateOrUpdateChatMessage),
|
|
|
|
Report(Report),
|
|
|
|
AnnounceActivity(AnnounceActivity),
|
|
|
|
/// This is a catch-all and needs to be last
|
|
|
|
RawAnnouncableActivities(RawAnnouncableActivities),
|
2021-10-29 10:32:42 +00:00
|
|
|
}
|
|
|
|
|
2023-07-27 10:17:40 +00:00
|
|
|
/// List of activities which the group inbox can handle.
|
2022-06-02 14:33:41 +00:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
2021-10-29 10:32:42 +00:00
|
|
|
#[serde(untagged)]
|
2022-11-16 22:51:05 +00:00
|
|
|
#[enum_delegate::implement(ActivityHandler)]
|
2021-10-29 10:32:42 +00:00
|
|
|
pub enum GroupInboxActivities {
|
2022-11-23 23:40:47 +00:00
|
|
|
Follow(Follow),
|
|
|
|
UndoFollow(UndoFollow),
|
2021-10-29 10:32:42 +00:00
|
|
|
Report(Report),
|
2023-07-27 10:17:40 +00:00
|
|
|
/// This is a catch-all and needs to be last
|
2022-11-12 13:52:57 +00:00
|
|
|
AnnouncableActivities(RawAnnouncableActivities),
|
2021-10-29 10:32:42 +00:00
|
|
|
}
|
|
|
|
|
2023-07-27 10:17:40 +00:00
|
|
|
/// List of activities which the person inbox can handle.
|
2022-06-02 14:33:41 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2021-10-29 10:32:42 +00:00
|
|
|
#[serde(untagged)]
|
2022-11-16 22:51:05 +00:00
|
|
|
#[enum_delegate::implement(ActivityHandler)]
|
2021-10-29 10:32:42 +00:00
|
|
|
pub enum PersonInboxActivities {
|
2022-12-19 15:55:26 +00:00
|
|
|
Follow(Follow),
|
2022-11-23 23:40:47 +00:00
|
|
|
AcceptFollow(AcceptFollow),
|
|
|
|
UndoFollow(UndoFollow),
|
2022-12-01 20:52:49 +00:00
|
|
|
CreateOrUpdatePrivateMessage(CreateOrUpdateChatMessage),
|
2022-02-14 15:14:24 +00:00
|
|
|
Delete(Delete),
|
|
|
|
UndoDelete(UndoDelete),
|
2021-11-06 13:25:34 +00:00
|
|
|
AnnounceActivity(AnnounceActivity),
|
2023-07-27 10:17:40 +00:00
|
|
|
/// User can also receive some "announcable" activities, eg a comment mention.
|
|
|
|
AnnouncableActivities(AnnouncableActivities),
|
2022-11-12 13:52:57 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 14:33:41 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2021-10-29 10:32:42 +00:00
|
|
|
#[serde(untagged)]
|
2022-11-16 22:51:05 +00:00
|
|
|
#[enum_delegate::implement(ActivityHandler)]
|
2021-10-29 10:32:42 +00:00
|
|
|
pub enum AnnouncableActivities {
|
2022-12-01 20:52:49 +00:00
|
|
|
CreateOrUpdateComment(CreateOrUpdateNote),
|
|
|
|
CreateOrUpdatePost(CreateOrUpdatePage),
|
2021-10-29 10:32:42 +00:00
|
|
|
Vote(Vote),
|
|
|
|
UndoVote(UndoVote),
|
|
|
|
Delete(Delete),
|
|
|
|
UndoDelete(UndoDelete),
|
2021-11-06 13:25:34 +00:00
|
|
|
UpdateCommunity(UpdateCommunity),
|
2022-02-07 19:23:12 +00:00
|
|
|
BlockUser(BlockUser),
|
|
|
|
UndoBlockUser(UndoBlockUser),
|
2023-02-18 14:50:28 +00:00
|
|
|
CollectionAdd(CollectionAdd),
|
|
|
|
CollectionRemove(CollectionRemove),
|
|
|
|
LockPost(LockPage),
|
|
|
|
UndoLockPost(UndoLockPage),
|
2021-11-11 19:49:15 +00:00
|
|
|
// For compatibility with Pleroma/Mastodon (send only)
|
|
|
|
Page(Page),
|
2021-10-29 10:32:42 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2022-12-01 20:52:49 +00:00
|
|
|
impl InCommunity for AnnouncableActivities {
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(self, context))]
|
2024-04-10 14:14:11 +00:00
|
|
|
async fn community(&self, context: &Data<LemmyContext>) -> LemmyResult<ApubCommunity> {
|
2021-10-29 10:32:42 +00:00
|
|
|
use AnnouncableActivities::*;
|
2022-12-01 20:52:49 +00:00
|
|
|
match self {
|
2023-03-21 15:03:05 +00:00
|
|
|
CreateOrUpdateComment(a) => a.community(context).await,
|
|
|
|
CreateOrUpdatePost(a) => a.community(context).await,
|
|
|
|
Vote(a) => a.community(context).await,
|
|
|
|
UndoVote(a) => a.community(context).await,
|
|
|
|
Delete(a) => a.community(context).await,
|
|
|
|
UndoDelete(a) => a.community(context).await,
|
|
|
|
UpdateCommunity(a) => a.community(context).await,
|
|
|
|
BlockUser(a) => a.community(context).await,
|
|
|
|
UndoBlockUser(a) => a.community(context).await,
|
|
|
|
CollectionAdd(a) => a.community(context).await,
|
|
|
|
CollectionRemove(a) => a.community(context).await,
|
|
|
|
LockPost(a) => a.community(context).await,
|
|
|
|
UndoLockPost(a) => a.community(context).await,
|
2024-08-01 00:28:41 +00:00
|
|
|
Page(_) => Err(LemmyErrorType::CouldntFindPost.into()),
|
2022-12-01 20:52:49 +00:00
|
|
|
}
|
2021-10-29 10:32:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-06 23:53:33 +00:00
|
|
|
|
2022-07-29 13:32:12 +00:00
|
|
|
#[cfg(test)]
|
2024-03-26 09:17:42 +00:00
|
|
|
#[allow(clippy::indexing_slicing)]
|
2022-07-29 13:32:12 +00:00
|
|
|
mod tests {
|
2023-07-17 15:04:14 +00:00
|
|
|
|
2022-07-29 13:32:12 +00:00
|
|
|
use crate::{
|
2023-11-16 13:22:40 +00:00
|
|
|
activity_lists::{GroupInboxActivities, PersonInboxActivities, SharedInboxActivities},
|
2022-12-19 15:55:26 +00:00
|
|
|
protocol::tests::{test_json, test_parse_lemmy_item},
|
2022-07-29 13:32:12 +00:00
|
|
|
};
|
2023-11-17 03:51:33 +00:00
|
|
|
use lemmy_utils::error::LemmyResult;
|
2022-07-29 13:32:12 +00:00
|
|
|
|
|
|
|
#[test]
|
2023-11-17 03:51:33 +00:00
|
|
|
fn test_group_inbox() -> LemmyResult<()> {
|
|
|
|
test_parse_lemmy_item::<GroupInboxActivities>("assets/lemmy/activities/following/follow.json")?;
|
2022-07-29 13:32:12 +00:00
|
|
|
test_parse_lemmy_item::<GroupInboxActivities>(
|
|
|
|
"assets/lemmy/activities/create_or_update/create_note.json",
|
2023-11-17 03:51:33 +00:00
|
|
|
)?;
|
|
|
|
Ok(())
|
2022-07-29 13:32:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-11-17 03:51:33 +00:00
|
|
|
fn test_person_inbox() -> LemmyResult<()> {
|
|
|
|
test_parse_lemmy_item::<PersonInboxActivities>(
|
|
|
|
"assets/lemmy/activities/following/accept.json",
|
|
|
|
)?;
|
2023-07-27 10:17:40 +00:00
|
|
|
test_parse_lemmy_item::<PersonInboxActivities>(
|
2022-07-29 13:32:12 +00:00
|
|
|
"assets/lemmy/activities/create_or_update/create_note.json",
|
2023-11-17 03:51:33 +00:00
|
|
|
)?;
|
2023-07-27 10:17:40 +00:00
|
|
|
test_parse_lemmy_item::<PersonInboxActivities>(
|
2022-07-29 13:32:12 +00:00
|
|
|
"assets/lemmy/activities/create_or_update/create_private_message.json",
|
2023-11-17 03:51:33 +00:00
|
|
|
)?;
|
|
|
|
test_json::<PersonInboxActivities>("assets/mastodon/activities/follow.json")?;
|
|
|
|
Ok(())
|
2022-07-29 13:32:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-11-17 03:51:33 +00:00
|
|
|
fn test_shared_inbox() -> LemmyResult<()> {
|
2023-11-16 13:22:40 +00:00
|
|
|
test_parse_lemmy_item::<SharedInboxActivities>(
|
2022-07-29 13:32:12 +00:00
|
|
|
"assets/lemmy/activities/deletion/delete_user.json",
|
2023-11-17 03:51:33 +00:00
|
|
|
)?;
|
|
|
|
Ok(())
|
2022-07-29 13:32:12 +00:00
|
|
|
}
|
|
|
|
}
|