2 use actix_web::web::Data;
4 use lemmy_api_common::{
8 get_local_user_view_from_jwt,
11 use lemmy_apub::{ActorType, CommunityType, UserType};
12 use lemmy_db_queries::{
13 source::{comment::Comment_, community::CommunityModerator_, post::Post_},
19 use lemmy_db_schema::source::{
27 use lemmy_db_views::comment_view::CommentQueryBuilder;
28 use lemmy_db_views_actor::{
29 community_moderator_view::CommunityModeratorView,
30 community_view::CommunityView,
31 person_view::PersonViewSafe,
33 use lemmy_utils::{location_info, utils::naive_from_unix, ApiError, ConnectionId, LemmyError};
34 use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
36 #[async_trait::async_trait(?Send)]
37 impl Perform for FollowCommunity {
38 type Response = CommunityResponse;
42 context: &Data<LemmyContext>,
43 _websocket_id: Option<ConnectionId>,
44 ) -> Result<CommunityResponse, LemmyError> {
45 let data: &FollowCommunity = &self;
46 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
48 let community_id = data.community_id;
49 let community = blocking(context.pool(), move |conn| {
50 Community::read(conn, community_id)
53 let community_follower_form = CommunityFollowerForm {
54 community_id: data.community_id,
55 person_id: local_user_view.person.id,
61 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
63 let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
64 if blocking(context.pool(), follow).await?.is_err() {
65 return Err(ApiError::err("community_follower_already_exists").into());
69 move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
70 if blocking(context.pool(), unfollow).await?.is_err() {
71 return Err(ApiError::err("community_follower_already_exists").into());
74 } else if data.follow {
75 // Dont actually add to the community followers here, because you need
76 // to wait for the accept
79 .send_follow(&community.actor_id(), context)
84 .send_unfollow(&community.actor_id(), context)
86 let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
87 if blocking(context.pool(), unfollow).await?.is_err() {
88 return Err(ApiError::err("community_follower_already_exists").into());
92 let community_id = data.community_id;
93 let person_id = local_user_view.person.id;
94 let mut community_view = blocking(context.pool(), move |conn| {
95 CommunityView::read(conn, community_id, Some(person_id))
99 // TODO: this needs to return a "pending" state, until Accept is received from the remote server
100 // For now, just assume that remote follows are accepted.
101 // Otherwise, the subscribed will be null
102 if !community.local {
103 community_view.subscribed = data.follow;
106 Ok(CommunityResponse { community_view })
110 #[async_trait::async_trait(?Send)]
111 impl Perform for BanFromCommunity {
112 type Response = BanFromCommunityResponse;
116 context: &Data<LemmyContext>,
117 websocket_id: Option<ConnectionId>,
118 ) -> Result<BanFromCommunityResponse, LemmyError> {
119 let data: &BanFromCommunity = &self;
120 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
122 let community_id = data.community_id;
123 let banned_person_id = data.person_id;
125 // Verify that only mods or admins can ban
126 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
128 let community_user_ban_form = CommunityPersonBanForm {
129 community_id: data.community_id,
130 person_id: data.person_id,
133 let community = blocking(context.pool(), move |conn: &'_ _| {
134 Community::read(conn, community_id)
137 let banned_person = blocking(context.pool(), move |conn: &'_ _| {
138 Person::read(conn, banned_person_id)
143 let ban = move |conn: &'_ _| CommunityPersonBan::ban(conn, &community_user_ban_form);
144 if blocking(context.pool(), ban).await?.is_err() {
145 return Err(ApiError::err("community_user_already_banned").into());
148 // Also unsubscribe them from the community, if they are subscribed
149 let community_follower_form = CommunityFollowerForm {
150 community_id: data.community_id,
151 person_id: banned_person_id,
154 blocking(context.pool(), move |conn: &'_ _| {
155 CommunityFollower::unfollow(conn, &community_follower_form)
161 .send_block_user(&local_user_view.person, banned_person, context)
164 let unban = move |conn: &'_ _| CommunityPersonBan::unban(conn, &community_user_ban_form);
165 if blocking(context.pool(), unban).await?.is_err() {
166 return Err(ApiError::err("community_user_already_banned").into());
169 .send_undo_block_user(&local_user_view.person, banned_person, context)
173 // Remove/Restore their data if that's desired
174 if data.remove_data {
176 blocking(context.pool(), move |conn: &'_ _| {
177 Post::update_removed_for_creator(conn, banned_person_id, Some(community_id), true)
182 // TODO Diesel doesn't allow updates with joins, so this has to be a loop
183 let comments = blocking(context.pool(), move |conn| {
184 CommentQueryBuilder::create(conn)
185 .creator_id(banned_person_id)
186 .community_id(community_id)
187 .limit(std::i64::MAX)
192 for comment_view in &comments {
193 let comment_id = comment_view.comment.id;
194 blocking(context.pool(), move |conn: &'_ _| {
195 Comment::update_removed(conn, comment_id, true)
202 // TODO eventually do correct expires
203 let expires = data.expires.map(naive_from_unix);
205 let form = ModBanFromCommunityForm {
206 mod_person_id: local_user_view.person.id,
207 other_person_id: data.person_id,
208 community_id: data.community_id,
209 reason: data.reason.to_owned(),
210 banned: Some(data.ban),
213 blocking(context.pool(), move |conn| {
214 ModBanFromCommunity::create(conn, &form)
218 let person_id = data.person_id;
219 let person_view = blocking(context.pool(), move |conn| {
220 PersonViewSafe::read(conn, person_id)
224 let res = BanFromCommunityResponse {
229 context.chat_server().do_send(SendCommunityRoomMessage {
230 op: UserOperation::BanFromCommunity,
231 response: res.clone(),
240 #[async_trait::async_trait(?Send)]
241 impl Perform for AddModToCommunity {
242 type Response = AddModToCommunityResponse;
246 context: &Data<LemmyContext>,
247 websocket_id: Option<ConnectionId>,
248 ) -> Result<AddModToCommunityResponse, LemmyError> {
249 let data: &AddModToCommunity = &self;
250 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
252 let community_id = data.community_id;
254 // Verify that only mods or admins can add mod
255 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
257 // Update in local database
258 let community_moderator_form = CommunityModeratorForm {
259 community_id: data.community_id,
260 person_id: data.person_id,
263 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
264 if blocking(context.pool(), join).await?.is_err() {
265 return Err(ApiError::err("community_moderator_already_exists").into());
268 let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
269 if blocking(context.pool(), leave).await?.is_err() {
270 return Err(ApiError::err("community_moderator_already_exists").into());
275 let form = ModAddCommunityForm {
276 mod_person_id: local_user_view.person.id,
277 other_person_id: data.person_id,
278 community_id: data.community_id,
279 removed: Some(!data.added),
281 blocking(context.pool(), move |conn| {
282 ModAddCommunity::create(conn, &form)
286 // Send to federated instances
287 let updated_mod_id = data.person_id;
288 let updated_mod = blocking(context.pool(), move |conn| {
289 Person::read(conn, updated_mod_id)
292 let community = blocking(context.pool(), move |conn| {
293 Community::read(conn, community_id)
298 .send_add_mod(&local_user_view.person, updated_mod, context)
302 .send_remove_mod(&local_user_view.person, updated_mod, context)
306 // Note: in case a remote mod is added, this returns the old moderators list, it will only get
307 // updated once we receive an activity from the community (like `Announce/Add/Moderator`)
308 let community_id = data.community_id;
309 let moderators = blocking(context.pool(), move |conn| {
310 CommunityModeratorView::for_community(conn, community_id)
314 let res = AddModToCommunityResponse { moderators };
315 context.chat_server().do_send(SendCommunityRoomMessage {
316 op: UserOperation::AddModToCommunity,
317 response: res.clone(),
325 // TODO: we dont do anything for federation here, it should be updated the next time the community
326 // gets fetched. i hope we can get rid of the community creator role soon.
327 #[async_trait::async_trait(?Send)]
328 impl Perform for TransferCommunity {
329 type Response = GetCommunityResponse;
333 context: &Data<LemmyContext>,
334 _websocket_id: Option<ConnectionId>,
335 ) -> Result<GetCommunityResponse, LemmyError> {
336 let data: &TransferCommunity = &self;
337 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
339 let site_creator_id = blocking(context.pool(), move |conn| {
340 Site::read(conn, 1).map(|s| s.creator_id)
344 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
346 // Making sure the site creator, if an admin, is at the top
347 let creator_index = admins
349 .position(|r| r.person.id == site_creator_id)
350 .context(location_info!())?;
351 let creator_person = admins.remove(creator_index);
352 admins.insert(0, creator_person);
354 // Fetch the community mods
355 let community_id = data.community_id;
356 let mut community_mods = blocking(context.pool(), move |conn| {
357 CommunityModeratorView::for_community(conn, community_id)
361 // Make sure transferrer is either the top community mod, or an admin
362 if local_user_view.person.id != community_mods[0].moderator.id
365 .map(|a| a.person.id)
366 .any(|x| x == local_user_view.person.id)
368 return Err(ApiError::err("not_an_admin").into());
371 // You have to re-do the community_moderator table, reordering it.
372 // Add the transferee to the top
373 let creator_index = community_mods
375 .position(|r| r.moderator.id == data.person_id)
376 .context(location_info!())?;
377 let creator_person = community_mods.remove(creator_index);
378 community_mods.insert(0, creator_person);
380 // Delete all the mods
381 let community_id = data.community_id;
382 blocking(context.pool(), move |conn| {
383 CommunityModerator::delete_for_community(conn, community_id)
387 // TODO: this should probably be a bulk operation
388 // Re-add the mods, in the new order
389 for cmod in &community_mods {
390 let community_moderator_form = CommunityModeratorForm {
391 community_id: cmod.community.id,
392 person_id: cmod.moderator.id,
395 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
396 if blocking(context.pool(), join).await?.is_err() {
397 return Err(ApiError::err("community_moderator_already_exists").into());
402 // TODO there should probably be another table for transfer community
403 // Right now, it will just look like it modded them twice
404 let form = ModAddCommunityForm {
405 mod_person_id: local_user_view.person.id,
406 other_person_id: data.person_id,
407 community_id: data.community_id,
408 removed: Some(false),
410 blocking(context.pool(), move |conn| {
411 ModAddCommunity::create(conn, &form)
415 let community_id = data.community_id;
416 let person_id = local_user_view.person.id;
417 let community_view = blocking(context.pool(), move |conn| {
418 CommunityView::read(conn, community_id, Some(person_id))
421 .map_err(|_| ApiError::err("couldnt_find_community"))?;
423 let community_id = data.community_id;
424 let moderators = blocking(context.pool(), move |conn| {
425 CommunityModeratorView::for_community(conn, community_id)
428 .map_err(|_| ApiError::err("couldnt_find_community"))?;
431 Ok(GetCommunityResponse {