2020-10-12 14:10:09 +00:00
|
|
|
use crate::{
|
|
|
|
activities::send::generate_activity_id,
|
|
|
|
activity_queue::{send_activity_single_dest, send_to_community_followers},
|
|
|
|
check_is_apub_id_valid,
|
2020-11-25 17:44:49 +00:00
|
|
|
extensions::context::lemmy_context,
|
2021-01-12 16:12:41 +00:00
|
|
|
fetcher::user::get_or_fetch_and_upsert_user,
|
2020-10-12 14:10:09 +00:00
|
|
|
ActorType,
|
|
|
|
};
|
|
|
|
use activitystreams::{
|
|
|
|
activity::{
|
|
|
|
kind::{AcceptType, AnnounceType, DeleteType, LikeType, RemoveType, UndoType},
|
|
|
|
Accept,
|
|
|
|
ActorAndObjectRefExt,
|
|
|
|
Announce,
|
|
|
|
Delete,
|
|
|
|
Follow,
|
|
|
|
Remove,
|
|
|
|
Undo,
|
|
|
|
},
|
|
|
|
base::{AnyBase, BaseExt, ExtendsExt},
|
|
|
|
object::ObjectExt,
|
|
|
|
public,
|
|
|
|
};
|
|
|
|
use anyhow::Context;
|
|
|
|
use itertools::Itertools;
|
2020-12-21 23:27:42 +00:00
|
|
|
use lemmy_db_queries::DbPool;
|
2020-12-21 12:28:12 +00:00
|
|
|
use lemmy_db_schema::source::community::Community;
|
2020-12-21 23:27:42 +00:00
|
|
|
use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
|
2020-10-12 14:10:09 +00:00
|
|
|
use lemmy_structs::blocking;
|
2021-02-04 16:34:58 +00:00
|
|
|
use lemmy_utils::{location_info, LemmyError};
|
2020-10-12 14:10:09 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl ActorType for Community {
|
2021-02-04 16:34:58 +00:00
|
|
|
fn is_local(&self) -> bool {
|
|
|
|
self.local
|
|
|
|
}
|
2021-01-27 16:42:23 +00:00
|
|
|
fn actor_id(&self) -> Url {
|
|
|
|
self.actor_id.to_owned().into_inner()
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
fn public_key(&self) -> Option<String> {
|
|
|
|
self.public_key.to_owned()
|
|
|
|
}
|
|
|
|
fn private_key(&self) -> Option<String> {
|
|
|
|
self.private_key.to_owned()
|
|
|
|
}
|
|
|
|
|
2021-02-04 16:34:58 +00:00
|
|
|
fn get_shared_inbox_or_inbox_url(&self) -> Url {
|
|
|
|
self
|
|
|
|
.shared_inbox_url
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| self.inbox_url.to_owned())
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:10:09 +00:00
|
|
|
async fn send_follow(
|
|
|
|
&self,
|
|
|
|
_follow_actor_id: &Url,
|
|
|
|
_context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn send_unfollow(
|
|
|
|
&self,
|
|
|
|
_follow_actor_id: &Url,
|
|
|
|
_context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// As a local community, accept the follow request from a remote user.
|
|
|
|
async fn send_accept_follow(
|
|
|
|
&self,
|
|
|
|
follow: Follow,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let actor_uri = follow
|
|
|
|
.actor()?
|
|
|
|
.as_single_xsd_any_uri()
|
|
|
|
.context(location_info!())?;
|
2020-10-22 18:27:32 +00:00
|
|
|
let user = get_or_fetch_and_upsert_user(actor_uri, context, &mut 0).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-01-27 16:42:23 +00:00
|
|
|
let mut accept = Accept::new(
|
|
|
|
self.actor_id.to_owned().into_inner(),
|
|
|
|
follow.into_any_base()?,
|
|
|
|
);
|
2020-10-12 14:10:09 +00:00
|
|
|
accept
|
2020-11-25 17:44:49 +00:00
|
|
|
.set_many_contexts(lemmy_context()?)
|
2020-10-12 14:10:09 +00:00
|
|
|
.set_id(generate_activity_id(AcceptType::Accept)?)
|
2021-01-27 16:42:23 +00:00
|
|
|
.set_to(user.actor_id());
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-02-04 16:34:58 +00:00
|
|
|
send_activity_single_dest(accept, self, user.inbox_url.into(), context).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
/// If the creator of a community deletes the community, send this to all followers.
|
2020-11-05 20:19:06 +00:00
|
|
|
async fn send_delete(&self, context: &LemmyContext) -> Result<(), LemmyError> {
|
2021-01-27 16:42:23 +00:00
|
|
|
let mut delete = Delete::new(self.actor_id(), self.actor_id());
|
2020-10-12 14:10:09 +00:00
|
|
|
delete
|
2020-11-25 17:44:49 +00:00
|
|
|
.set_many_contexts(lemmy_context()?)
|
2020-10-12 14:10:09 +00:00
|
|
|
.set_id(generate_activity_id(DeleteType::Delete)?)
|
|
|
|
.set_to(public())
|
2021-02-04 16:34:58 +00:00
|
|
|
.set_many_ccs(vec![self.followers_url.clone().into_inner()]);
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2020-10-23 12:29:56 +00:00
|
|
|
send_to_community_followers(delete, self, context).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
/// If the creator of a community reverts the deletion of a community, send this to all followers.
|
2020-11-05 20:19:06 +00:00
|
|
|
async fn send_undo_delete(&self, context: &LemmyContext) -> Result<(), LemmyError> {
|
2021-01-27 16:42:23 +00:00
|
|
|
let mut delete = Delete::new(self.actor_id(), self.actor_id());
|
2020-10-12 14:10:09 +00:00
|
|
|
delete
|
2020-11-25 17:44:49 +00:00
|
|
|
.set_many_contexts(lemmy_context()?)
|
2020-10-12 14:10:09 +00:00
|
|
|
.set_id(generate_activity_id(DeleteType::Delete)?)
|
|
|
|
.set_to(public())
|
2021-02-04 16:34:58 +00:00
|
|
|
.set_many_ccs(vec![self.followers_url.clone().into_inner()]);
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-01-27 16:42:23 +00:00
|
|
|
let mut undo = Undo::new(self.actor_id(), delete.into_any_base()?);
|
2020-10-12 14:10:09 +00:00
|
|
|
undo
|
2020-11-25 17:44:49 +00:00
|
|
|
.set_many_contexts(lemmy_context()?)
|
2020-10-12 14:10:09 +00:00
|
|
|
.set_id(generate_activity_id(UndoType::Undo)?)
|
|
|
|
.set_to(public())
|
2021-02-04 16:34:58 +00:00
|
|
|
.set_many_ccs(vec![self.followers_url.clone().into_inner()]);
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2020-10-23 12:29:56 +00:00
|
|
|
send_to_community_followers(undo, self, context).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
/// If an admin removes a community, send this to all followers.
|
2020-11-05 20:19:06 +00:00
|
|
|
async fn send_remove(&self, context: &LemmyContext) -> Result<(), LemmyError> {
|
2021-01-27 16:42:23 +00:00
|
|
|
let mut remove = Remove::new(self.actor_id(), self.actor_id());
|
2020-10-12 14:10:09 +00:00
|
|
|
remove
|
2020-11-25 17:44:49 +00:00
|
|
|
.set_many_contexts(lemmy_context()?)
|
2020-10-12 14:10:09 +00:00
|
|
|
.set_id(generate_activity_id(RemoveType::Remove)?)
|
|
|
|
.set_to(public())
|
2021-02-04 16:34:58 +00:00
|
|
|
.set_many_ccs(vec![self.followers_url.clone().into_inner()]);
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2020-10-23 12:29:56 +00:00
|
|
|
send_to_community_followers(remove, self, context).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
/// If an admin reverts the removal of a community, send this to all followers.
|
2020-11-05 20:19:06 +00:00
|
|
|
async fn send_undo_remove(&self, context: &LemmyContext) -> Result<(), LemmyError> {
|
2021-01-27 16:42:23 +00:00
|
|
|
let mut remove = Remove::new(self.actor_id(), self.actor_id());
|
2020-10-12 14:10:09 +00:00
|
|
|
remove
|
2020-11-25 17:44:49 +00:00
|
|
|
.set_many_contexts(lemmy_context()?)
|
2020-10-12 14:10:09 +00:00
|
|
|
.set_id(generate_activity_id(RemoveType::Remove)?)
|
|
|
|
.set_to(public())
|
2021-02-04 16:34:58 +00:00
|
|
|
.set_many_ccs(vec![self.followers_url.clone().into_inner()]);
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
// Undo that fake activity
|
2021-01-27 16:42:23 +00:00
|
|
|
let mut undo = Undo::new(self.actor_id(), remove.into_any_base()?);
|
2020-10-12 14:10:09 +00:00
|
|
|
undo
|
2020-11-25 17:44:49 +00:00
|
|
|
.set_many_contexts(lemmy_context()?)
|
2020-10-12 14:10:09 +00:00
|
|
|
.set_id(generate_activity_id(LikeType::Like)?)
|
|
|
|
.set_to(public())
|
2021-02-04 16:34:58 +00:00
|
|
|
.set_many_ccs(vec![self.followers_url.clone().into_inner()]);
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2020-10-23 12:29:56 +00:00
|
|
|
send_to_community_followers(undo, self, context).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
/// Wraps an activity sent to the community in an announce, and then sends the announce to all
|
|
|
|
/// community followers.
|
2020-10-12 14:10:09 +00:00
|
|
|
async fn send_announce(
|
|
|
|
&self,
|
|
|
|
activity: AnyBase,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError> {
|
2021-01-27 16:42:23 +00:00
|
|
|
let mut announce = Announce::new(self.actor_id.to_owned().into_inner(), activity);
|
2020-10-12 14:10:09 +00:00
|
|
|
announce
|
2020-11-25 17:44:49 +00:00
|
|
|
.set_many_contexts(lemmy_context()?)
|
2020-10-12 14:10:09 +00:00
|
|
|
.set_id(generate_activity_id(AnnounceType::Announce)?)
|
|
|
|
.set_to(public())
|
2021-02-04 16:34:58 +00:00
|
|
|
.set_many_ccs(vec![self.followers_url.clone().into_inner()]);
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2020-10-23 12:29:56 +00:00
|
|
|
send_to_community_followers(announce, self, context).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// For a given community, returns the inboxes of all followers.
|
|
|
|
async fn get_follower_inboxes(&self, pool: &DbPool) -> Result<Vec<Url>, LemmyError> {
|
|
|
|
let id = self.id;
|
|
|
|
|
2021-02-04 16:34:58 +00:00
|
|
|
let follows = blocking(pool, move |conn| {
|
2020-10-12 14:10:09 +00:00
|
|
|
CommunityFollowerView::for_community(conn, id)
|
|
|
|
})
|
|
|
|
.await??;
|
2021-02-04 16:34:58 +00:00
|
|
|
let inboxes = follows
|
2020-10-12 14:10:09 +00:00
|
|
|
.into_iter()
|
2021-02-04 16:34:58 +00:00
|
|
|
.filter(|f| !f.follower.local)
|
|
|
|
.map(|f| f.follower.shared_inbox_url.unwrap_or(f.follower.inbox_url))
|
|
|
|
.map(|i| i.into_inner())
|
|
|
|
.unique()
|
2020-10-12 14:10:09 +00:00
|
|
|
// Don't send to blocked instances
|
|
|
|
.filter(|inbox| check_is_apub_id_valid(inbox).is_ok())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(inboxes)
|
|
|
|
}
|
|
|
|
}
|