2021-07-17 16:20:44 +00:00
|
|
|
use crate::{
|
2021-08-12 12:48:09 +00:00
|
|
|
activities::{
|
|
|
|
generate_activity_id,
|
2021-10-28 15:25:26 +00:00
|
|
|
send_lemmy_activity,
|
2021-08-12 12:48:09 +00:00
|
|
|
verify_person,
|
2021-10-28 11:46:48 +00:00
|
|
|
verify_person_in_community,
|
2021-08-12 12:48:09 +00:00
|
|
|
},
|
2022-11-23 23:40:47 +00:00
|
|
|
fetcher::user_or_community::UserOrCommunity,
|
2022-06-02 14:33:41 +00:00
|
|
|
local_instance,
|
2021-10-18 21:36:44 +00:00
|
|
|
objects::{community::ApubCommunity, person::ApubPerson},
|
2022-11-28 14:29:33 +00:00
|
|
|
protocol::activities::following::{
|
|
|
|
accept::AcceptFollow,
|
|
|
|
follow::Follow,
|
|
|
|
undo_follow::UndoFollow,
|
|
|
|
},
|
2022-06-02 14:33:41 +00:00
|
|
|
ActorType,
|
2022-11-28 14:29:33 +00:00
|
|
|
SendActivity,
|
2021-08-19 21:24:33 +00:00
|
|
|
};
|
2022-06-08 15:45:39 +00:00
|
|
|
use activitypub_federation::{
|
|
|
|
core::object_id::ObjectId,
|
|
|
|
data::Data,
|
|
|
|
traits::{ActivityHandler, Actor},
|
|
|
|
};
|
2021-11-19 17:47:06 +00:00
|
|
|
use activitystreams_kinds::activity::FollowType;
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
community::{BlockCommunity, BlockCommunityResponse},
|
|
|
|
context::LemmyContext,
|
|
|
|
utils::get_local_user_view_from_jwt,
|
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
2022-11-23 23:40:47 +00:00
|
|
|
source::{
|
2022-11-28 14:29:33 +00:00
|
|
|
community::{Community, CommunityFollower, CommunityFollowerForm},
|
2022-11-23 23:40:47 +00:00
|
|
|
person::{PersonFollower, PersonFollowerForm},
|
|
|
|
},
|
2022-11-28 14:29:33 +00:00
|
|
|
traits::{Crud, Followable},
|
2021-08-12 12:48:09 +00:00
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
|
|
|
use url::Url;
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
|
2022-11-23 23:40:47 +00:00
|
|
|
impl Follow {
|
2021-08-19 21:24:33 +00:00
|
|
|
pub(in crate::activities::following) fn new(
|
2021-10-18 21:36:44 +00:00
|
|
|
actor: &ApubPerson,
|
|
|
|
community: &ApubCommunity,
|
2021-09-22 15:57:09 +00:00
|
|
|
context: &LemmyContext,
|
2022-11-23 23:40:47 +00:00
|
|
|
) -> Result<Follow, LemmyError> {
|
|
|
|
Ok(Follow {
|
2021-09-25 15:44:52 +00:00
|
|
|
actor: ObjectId::new(actor.actor_id()),
|
|
|
|
object: ObjectId::new(community.actor_id()),
|
2021-08-19 21:24:33 +00:00
|
|
|
kind: FollowType::Follow,
|
2021-09-22 15:57:09 +00:00
|
|
|
id: generate_activity_id(
|
|
|
|
FollowType::Follow,
|
|
|
|
&context.settings().get_protocol_and_hostname(),
|
|
|
|
)?,
|
2021-08-19 21:24:33 +00:00
|
|
|
})
|
|
|
|
}
|
2021-12-06 14:54:47 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2021-08-12 12:48:09 +00:00
|
|
|
pub async fn send(
|
2021-10-18 21:36:44 +00:00
|
|
|
actor: &ApubPerson,
|
|
|
|
community: &ApubCommunity,
|
2021-08-12 12:48:09 +00:00
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let community_follower_form = CommunityFollowerForm {
|
|
|
|
community_id: community.id,
|
|
|
|
person_id: actor.id,
|
|
|
|
pending: true,
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
CommunityFollower::follow(context.pool(), &community_follower_form)
|
|
|
|
.await
|
|
|
|
.ok();
|
2021-08-12 12:48:09 +00:00
|
|
|
|
2022-11-23 23:40:47 +00:00
|
|
|
let follow = Follow::new(actor, community, context)?;
|
2022-06-08 15:45:39 +00:00
|
|
|
let inbox = vec![community.shared_inbox_or_inbox()];
|
|
|
|
send_lemmy_activity(context, follow, actor, inbox, true).await
|
2021-08-12 12:48:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2022-11-23 23:40:47 +00:00
|
|
|
impl ActivityHandler for Follow {
|
2021-10-06 20:20:05 +00:00
|
|
|
type DataType = LemmyContext;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
|
|
|
|
|
|
|
fn id(&self) -> &Url {
|
|
|
|
&self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn actor(&self) -> &Url {
|
|
|
|
self.actor.inner()
|
|
|
|
}
|
2021-12-06 14:54:47 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
async fn verify(
|
|
|
|
&self,
|
2021-10-06 20:20:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
2021-08-19 21:24:33 +00:00
|
|
|
verify_person(&self.actor, context, request_counter).await?;
|
2022-11-23 23:40:47 +00:00
|
|
|
let object = self
|
2021-12-06 22:54:34 +00:00
|
|
|
.object
|
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-11-23 23:40:47 +00:00
|
|
|
if let UserOrCommunity::Community(c) = object {
|
|
|
|
verify_person_in_community(&self.actor, &c, context, request_counter).await?;
|
|
|
|
}
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
async fn receive(
|
2021-08-12 12:48:09 +00:00
|
|
|
self,
|
2021-10-06 20:20:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
2022-11-23 23:40:47 +00:00
|
|
|
let actor = self
|
2021-12-06 22:54:34 +00:00
|
|
|
.actor
|
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-11-23 23:40:47 +00:00
|
|
|
let object = self
|
2021-12-06 22:54:34 +00:00
|
|
|
.object
|
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-11-23 23:40:47 +00:00
|
|
|
match object {
|
|
|
|
UserOrCommunity::User(u) => {
|
|
|
|
let form = PersonFollowerForm {
|
|
|
|
person_id: u.id,
|
|
|
|
follower_id: actor.id,
|
|
|
|
pending: false,
|
|
|
|
};
|
|
|
|
PersonFollower::follow(context.pool(), &form).await?;
|
|
|
|
}
|
|
|
|
UserOrCommunity::Community(c) => {
|
|
|
|
let form = CommunityFollowerForm {
|
|
|
|
community_id: c.id,
|
|
|
|
person_id: actor.id,
|
|
|
|
pending: false,
|
|
|
|
};
|
|
|
|
CommunityFollower::follow(context.pool(), &form).await?;
|
|
|
|
}
|
|
|
|
}
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
|
2022-11-23 23:40:47 +00:00
|
|
|
AcceptFollow::send(self, context, request_counter).await
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-28 14:29:33 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl SendActivity for BlockCommunity {
|
|
|
|
type Response = BlockCommunityResponse;
|
|
|
|
|
|
|
|
async fn send_activity(
|
|
|
|
request: &Self,
|
|
|
|
_response: &Self::Response,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
|
|
|
|
let community = Community::read(context.pool(), request.community_id).await?;
|
|
|
|
UndoFollow::send(&local_user_view.person.into(), &community.into(), context).await
|
|
|
|
}
|
|
|
|
}
|