2 activities::receive::verify_activity_domains_valid,
4 assert_activity_not_local,
6 get_activity_to_and_cc,
7 inbox_verify_http_signature,
8 is_activity_already_known,
9 is_addressed_to_public,
10 receive_for_community::{
11 receive_create_for_community,
12 receive_delete_for_community,
13 receive_dislike_for_community,
14 receive_like_for_community,
15 receive_undo_for_community,
16 receive_update_for_community,
22 use activitystreams::{
23 activity::{kind::FollowType, ActorAndObject, Follow, Undo},
27 use actix_web::{web, HttpRequest, HttpResponse};
28 use anyhow::{anyhow, Context};
30 community::{Community, CommunityFollower, CommunityFollowerForm},
32 views::community_user_ban_view::CommunityUserBanView,
36 use lemmy_structs::blocking;
37 use lemmy_utils::{location_info, LemmyError};
38 use lemmy_websocket::LemmyContext;
40 use serde::{Deserialize, Serialize};
44 /// Allowed activities for community inbox.
45 #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
46 #[serde(rename_all = "PascalCase")]
47 pub enum CommunityValidTypes {
48 Follow, // follow request from a user
49 Undo, // unfollow from a user
50 Create, // create post or comment
51 Update, // update post or comment
52 Like, // upvote post or comment
53 Dislike, // downvote post or comment
54 Delete, // post or comment deleted by creator
55 Remove, // post or comment removed by mod or admin
58 pub type CommunityAcceptedActivities = ActorAndObject<CommunityValidTypes>;
60 /// Handler for all incoming receive to community inboxes.
61 pub async fn community_inbox(
63 input: web::Json<CommunityAcceptedActivities>,
64 path: web::Path<String>,
65 context: web::Data<LemmyContext>,
66 ) -> Result<HttpResponse, LemmyError> {
67 let activity = input.into_inner();
68 // First of all check the http signature
69 let request_counter = &mut 0;
70 let actor = inbox_verify_http_signature(&activity, &context, request, request_counter).await?;
72 // Do nothing if we received the same activity before
73 let activity_id = get_activity_id(&activity, &actor.actor_id()?)?;
74 if is_activity_already_known(context.pool(), &activity_id).await? {
75 return Ok(HttpResponse::Ok().finish());
78 // Check if the activity is actually meant for us
79 let path = path.into_inner();
80 let community = blocking(&context.pool(), move |conn| {
81 Community::read_from_name(&conn, &path)
84 let to_and_cc = get_activity_to_and_cc(&activity);
85 if !to_and_cc.contains(&&community.actor_id()?) {
86 return Err(anyhow!("Activity delivered to wrong community").into());
89 assert_activity_not_local(&activity)?;
90 insert_activity(&activity_id, activity.clone(), false, true, context.pool()).await?;
93 "Community {} received activity {:?} from {}",
95 &activity.id_unchecked(),
99 community_receive_message(
109 /// Receives Follow, Undo/Follow, post actions, comment actions (including votes)
110 pub(crate) async fn community_receive_message(
111 activity: CommunityAcceptedActivities,
112 to_community: Community,
113 actor: &dyn ActorType,
114 context: &LemmyContext,
115 request_counter: &mut i32,
116 ) -> Result<HttpResponse, LemmyError> {
117 // Only users can send activities to the community, so we can get the actor as user
119 let actor_id = actor.actor_id_str();
120 let user = blocking(&context.pool(), move |conn| {
121 User_::read_from_actor_id(&conn, &actor_id)
124 check_community_or_site_ban(&user, &to_community, context.pool()).await?;
126 let any_base = activity.clone().into_any_base()?;
127 let actor_url = actor.actor_id()?;
128 let activity_kind = activity.kind().context(location_info!())?;
129 let do_announce = match activity_kind {
130 CommunityValidTypes::Follow => {
131 handle_follow(any_base.clone(), user, &to_community, &context).await?;
134 CommunityValidTypes::Undo => {
144 CommunityValidTypes::Create => {
145 receive_create_for_community(context, any_base.clone(), &actor_url, request_counter).await?;
148 CommunityValidTypes::Update => {
149 receive_update_for_community(context, any_base.clone(), &actor_url, request_counter).await?;
152 CommunityValidTypes::Like => {
153 receive_like_for_community(context, any_base.clone(), &actor_url, request_counter).await?;
156 CommunityValidTypes::Dislike => {
157 receive_dislike_for_community(context, any_base.clone(), &actor_url, request_counter).await?;
160 CommunityValidTypes::Delete => {
161 receive_delete_for_community(context, any_base.clone(), &actor_url).await?;
164 CommunityValidTypes::Remove => {
165 // TODO: we dont support remote mods, so this is ignored for now
166 //receive_remove_for_community(context, any_base.clone(), &user_url).await?
172 // Check again that the activity is public, just to be sure
173 is_addressed_to_public(&activity)?;
175 .send_announce(activity.into_any_base()?, context)
179 return Ok(HttpResponse::Ok().finish());
182 /// Handle a follow request from a remote user, adding the user as follower and returning an
184 async fn handle_follow(
187 community: &Community,
188 context: &LemmyContext,
189 ) -> Result<HttpResponse, LemmyError> {
190 let follow = Follow::from_any_base(activity)?.context(location_info!())?;
191 verify_activity_domains_valid(&follow, &user.actor_id()?, false)?;
193 let community_follower_form = CommunityFollowerForm {
194 community_id: community.id,
199 // This will fail if they're already a follower, but ignore the error.
200 blocking(&context.pool(), move |conn| {
201 CommunityFollower::follow(&conn, &community_follower_form).ok()
205 community.send_accept_follow(follow, context).await?;
207 Ok(HttpResponse::Ok().finish())
210 async fn handle_undo(
211 context: &LemmyContext,
212 activity: CommunityAcceptedActivities,
214 to_community: &Community,
215 request_counter: &mut i32,
216 ) -> Result<bool, LemmyError> {
217 let inner_kind = activity
219 .is_single_kind(&FollowType::Follow.to_string());
220 let any_base = activity.into_any_base()?;
222 handle_undo_follow(any_base, actor_url, to_community, &context).await?;
225 receive_undo_for_community(context, any_base, &actor_url, request_counter).await?;
230 /// Handle `Undo/Follow` from a user, removing the user from followers list.
231 async fn handle_undo_follow(
234 community: &Community,
235 context: &LemmyContext,
236 ) -> Result<(), LemmyError> {
237 let undo = Undo::from_any_base(activity)?.context(location_info!())?;
238 verify_activity_domains_valid(&undo, &user_url, true)?;
240 let object = undo.object().to_owned().one().context(location_info!())?;
241 let follow = Follow::from_any_base(object)?.context(location_info!())?;
242 verify_activity_domains_valid(&follow, &user_url, false)?;
244 let user = blocking(&context.pool(), move |conn| {
245 User_::read_from_actor_id(&conn, user_url.as_str())
248 let community_follower_form = CommunityFollowerForm {
249 community_id: community.id,
254 // This will fail if they aren't a follower, but ignore the error.
255 blocking(&context.pool(), move |conn| {
256 CommunityFollower::unfollow(&conn, &community_follower_form).ok()
263 async fn check_community_or_site_ban(
265 community: &Community,
267 ) -> Result<(), LemmyError> {
269 return Err(anyhow!("User is banned from site").into());
271 let user_id = user.id;
272 let community_id = community.id;
273 let is_banned = move |conn: &'_ _| CommunityUserBanView::get(conn, user_id, community_id).is_ok();
274 if blocking(pool, is_banned).await? {
275 return Err(anyhow!("User is banned from community").into());