Skip to content

Commit

Permalink
Implement restricted community (only mods can post) (fixes #187)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Apr 28, 2022
1 parent 9c1b9ff commit 8e24f3d
Show file tree
Hide file tree
Showing 21 changed files with 220 additions and 93 deletions.
82 changes: 82 additions & 0 deletions crates/api/src/community/hide.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use crate::Perform;
use actix_web::web::Data;
use lemmy_api_common::{
blocking,
community::{CommunityResponse, HideCommunity},
get_local_user_view_from_jwt,
is_admin,
};
use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
use lemmy_db_schema::{
naive_now,
source::{
community::{Community, CommunityForm},
moderator::{ModHideCommunity, ModHideCommunityForm},
},
traits::Crud,
};
use lemmy_utils::{ConnectionId, LemmyError};
use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};

#[async_trait::async_trait(?Send)]
impl Perform for HideCommunity {
type Response = CommunityResponse;

#[tracing::instrument(skip(context, websocket_id))]
async fn perform(
&self,
context: &Data<LemmyContext>,
websocket_id: Option<ConnectionId>,
) -> Result<CommunityResponse, LemmyError> {
let data: &HideCommunity = self;

// Verify its a admin (only admin can hide or unhide it)
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
is_admin(&local_user_view)?;

let community_id = data.community_id;
let read_community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;

let community_form = CommunityForm {
name: read_community.name,
title: read_community.title,
description: read_community.description.to_owned(),
hidden: Some(data.hidden),
updated: Some(naive_now()),
..CommunityForm::default()
};

let mod_hide_community_form = ModHideCommunityForm {
community_id: data.community_id,
mod_person_id: local_user_view.person.id,
reason: data.reason.clone(),
hidden: Some(data.hidden),
};

let community_id = data.community_id;
let updated_community = blocking(context.pool(), move |conn| {
Community::update(conn, community_id, &community_form)
})
.await?
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community_hidden_status"))?;

blocking(context.pool(), move |conn| {
ModHideCommunity::create(conn, &mod_hide_community_form)
})
.await??;

UpdateCommunity::send(
updated_community.into(),
&local_user_view.person.into(),
context,
)
.await?;

let op = UserOperationCrud::EditCommunity;
send_community_ws_message(data.community_id, op, websocket_id, None, context).await
}
}
2 changes: 2 additions & 0 deletions crates/api/src/community/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ mod add_mod;
mod ban;
mod block;
mod follow;
mod hide;
mod restrict;
mod transfer;
65 changes: 65 additions & 0 deletions crates/api/src/community/restrict.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use crate::Perform;
use actix_web::web::Data;
use lemmy_api_common::{
blocking,
community::{CommunityResponse, RestrictCommunity},
get_local_user_view_from_jwt,
is_mod_or_admin,
};
use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
use lemmy_db_schema::{
naive_now,
source::community::{Community, CommunityForm},
traits::Crud,
};
use lemmy_utils::{ConnectionId, LemmyError};
use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};

#[async_trait::async_trait(?Send)]
impl Perform for RestrictCommunity {
type Response = CommunityResponse;

#[tracing::instrument(skip(context, websocket_id))]
async fn perform(
&self,
context: &Data<LemmyContext>,
websocket_id: Option<ConnectionId>,
) -> Result<CommunityResponse, LemmyError> {
let data: &RestrictCommunity = self;

let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
is_mod_or_admin(context.pool(), local_user_view.person.id, data.community_id).await?;

let community_id = data.community_id;
let read_community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;

let community_form = CommunityForm {
name: read_community.name,
title: read_community.title,
posting_restricted: Some(data.restricted),
updated: Some(naive_now()),
..CommunityForm::default()
};

let community_id = data.community_id;
let updated_community = blocking(context.pool(), move |conn| {
Community::update(conn, community_id, &community_form)
})
.await?
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community_hidden_status"))?;

UpdateCommunity::send(
updated_community.into(),
&local_user_view.person.into(),
context,
)
.await?;

let op = UserOperationCrud::EditCommunity;
send_community_ws_message(data.community_id, op, websocket_id, None, context).await
}
}
7 changes: 7 additions & 0 deletions crates/api_common/src/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,10 @@ pub struct TransferCommunity {
pub person_id: PersonId,
pub auth: Sensitive<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RestrictCommunity {
pub community_id: CommunityId,
pub restricted: bool,
pub auth: Sensitive<String>,
}
77 changes: 2 additions & 75 deletions crates/api_crud/src/community/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ use actix_web::web::Data;
use lemmy_api_common::{
blocking,
check_image_has_local_domain,
community::{CommunityResponse, EditCommunity, HideCommunity},
community::{CommunityResponse, EditCommunity},
get_local_user_view_from_jwt,
is_admin,
};
use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
use lemmy_db_schema::{
diesel_option_overwrite_to_url,
naive_now,
newtypes::PersonId,
source::{
community::{Community, CommunityForm},
moderator::{ModHideCommunity, ModHideCommunityForm},
},
source::community::{Community, CommunityForm},
traits::Crud,
};
use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
Expand Down Expand Up @@ -65,11 +61,9 @@ impl PerformCrud for EditCommunity {
name: read_community.name,
title: data.title.to_owned().unwrap_or(read_community.title),
description: data.description.to_owned(),
public_key: read_community.public_key,
icon,
banner,
nsfw: data.nsfw,
hidden: Some(read_community.hidden),
updated: Some(naive_now()),
..CommunityForm::default()
};
Expand All @@ -92,70 +86,3 @@ impl PerformCrud for EditCommunity {
send_community_ws_message(data.community_id, op, websocket_id, None, context).await
}
}

#[async_trait::async_trait(?Send)]
impl PerformCrud for HideCommunity {
type Response = CommunityResponse;

#[tracing::instrument(skip(context, websocket_id))]
async fn perform(
&self,
context: &Data<LemmyContext>,
websocket_id: Option<ConnectionId>,
) -> Result<CommunityResponse, LemmyError> {
let data: &HideCommunity = self;

// Verify its a admin (only admin can hide or unhide it)
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
is_admin(&local_user_view)?;

let community_id = data.community_id;
let read_community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;

let community_form = CommunityForm {
name: read_community.name,
title: read_community.title,
description: read_community.description.to_owned(),
public_key: read_community.public_key,
icon: Some(read_community.icon),
banner: Some(read_community.banner),
nsfw: Some(read_community.nsfw),
updated: Some(naive_now()),
hidden: Some(data.hidden),
..CommunityForm::default()
};

let mod_hide_community_form = ModHideCommunityForm {
community_id: data.community_id,
mod_person_id: local_user_view.person.id,
reason: data.reason.clone(),
hidden: Some(data.hidden),
};

let community_id = data.community_id;
let updated_community = blocking(context.pool(), move |conn| {
Community::update(conn, community_id, &community_form)
})
.await?
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community_hidden_status"))?;

blocking(context.pool(), move |conn| {
ModHideCommunity::create(conn, &mod_hide_community_form)
})
.await??;

UpdateCommunity::send(
updated_community.into(),
&local_user_view.person.into(),
context,
)
.await?;

let op = UserOperationCrud::EditCommunity;
send_community_ws_message(data.community_id, op, websocket_id, None, context).await
}
}
22 changes: 21 additions & 1 deletion crates/api_crud/src/post/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ use lemmy_apub::{
EndpointType,
};
use lemmy_db_schema::{
source::post::{Post, PostForm, PostLike, PostLikeForm},
source::{
community::Community,
post::{Post, PostForm, PostLike, PostLikeForm},
},
traits::{Crud, Likeable},
};
use lemmy_db_views_actor::community_view::CommunityView;
use lemmy_utils::{
request::fetch_site_data,
utils::{
Expand Down Expand Up @@ -62,6 +66,22 @@ impl PerformCrud for CreatePost {
check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?;
check_community_deleted_or_removed(data.community_id, context.pool()).await?;

let community_id = data.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;
if community.posting_restricted {
let community_id = data.community_id;
let is_mod = blocking(context.pool(), move |conn| {
CommunityView::is_mod(conn, local_user_view.local_user.person_id, community_id)
})
.await?;
if !is_mod {
return Err(LemmyError::from_message("only_mods_can_post_in_community"));
}
}

// Fetch post links and pictrs cached image
let data_url = data.url.as_ref();
let (metadata_res, pictrs_thumbnail) =
Expand Down
1 change: 1 addition & 0 deletions crates/apub/assets/lemmy/objects/group.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"inbox": "https://enterprise.lemmy.ml/c/tenforward/inbox",
"followers": "https://enterprise.lemmy.ml/c/tenforward/followers",
"moderators": "https://enterprise.lemmy.ml/c/tenforward/moderators",
"posting_restricted": false,
"endpoints": {
"sharedInbox": "https://enterprise.lemmy.ml/inbox"
},
Expand Down
1 change: 1 addition & 0 deletions crates/apub/src/objects/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl ApubObject for ApubCommunity {
public_key: self.get_public_key()?,
published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime),
posting_restricted: Some(self.posting_restricted),
};
Ok(group)
}
Expand Down
3 changes: 3 additions & 0 deletions crates/apub/src/protocol/objects/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub struct Group {
pub(crate) sensitive: Option<bool>,
// lemmy extension
pub(crate) moderators: Option<ObjectId<ApubCommunityModerators>>,
// lemmy extension
pub(crate) posting_restricted: Option<bool>,
pub(crate) outbox: ObjectId<ApubCommunityOutbox>,
pub(crate) endpoints: Option<Endpoints>,
pub(crate) published: Option<DateTime<FixedOffset>>,
Expand Down Expand Up @@ -96,6 +98,7 @@ impl Group {
followers_url: Some(self.followers.into()),
inbox_url: Some(self.inbox.into()),
shared_inbox_url: Some(self.endpoints.map(|e| e.shared_inbox.into())),
posting_restricted: self.posting_restricted,
}
}
}
3 changes: 3 additions & 0 deletions crates/db_schema/src/impls/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod safe_type {
icon,
banner,
hidden,
posting_restricted,
);

impl ToSafe for Community {
Expand All @@ -63,6 +64,7 @@ mod safe_type {
icon,
banner,
hidden,
posting_restricted,
)
}
}
Expand Down Expand Up @@ -373,6 +375,7 @@ mod tests {
inbox_url: inserted_community.inbox_url.to_owned(),
shared_inbox_url: None,
hidden: false,
posting_restricted: false,
};

let community_follower_form = CommunityFollowerForm {
Expand Down
1 change: 1 addition & 0 deletions crates/db_schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ table! {
inbox_url -> Varchar,
shared_inbox_url -> Nullable<Varchar>,
hidden -> Bool,
posting_restricted -> Bool,
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/db_schema/src/source/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct Community {
pub inbox_url: DbUrl,
pub shared_inbox_url: Option<DbUrl>,
pub hidden: bool,
pub posting_restricted: bool,
}

/// A safe representation of community, without the sensitive info
Expand All @@ -47,6 +48,7 @@ pub struct CommunitySafe {
pub icon: Option<DbUrl>,
pub banner: Option<DbUrl>,
pub hidden: bool,
pub posting_restricted: bool,
}

#[derive(Insertable, AsChangeset, Debug, Default)]
Expand All @@ -71,6 +73,7 @@ pub struct CommunityForm {
pub inbox_url: Option<DbUrl>,
pub shared_inbox_url: Option<Option<DbUrl>>,
pub hidden: Option<bool>,
pub posting_restricted: Option<bool>,
}

#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
Expand Down
Loading

0 comments on commit 8e24f3d

Please sign in to comment.