summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorColin Reeder <colin@vpzom.click>2020-09-17 22:04:42 -0600
committerColin Reeder <colin@vpzom.click>2020-09-17 22:04:42 -0600
commit8256492cbd43ded7041360ebc084665ab7eb53ed (patch)
tree182273f9fd18ef7740002169a868f599a707e1cb
parent3fb5dbdcf22d3ea269e93b0b17eca8cfea18f379 (diff)
Add API for removing community moderators
-rw-r--r--openapi/openapi.json23
-rw-r--r--src/routes/api/communities.rs50
2 files changed, 69 insertions, 4 deletions
diff --git a/openapi/openapi.json b/openapi/openapi.json
index 3b04b05..f4eee43 100644
--- a/openapi/openapi.json
+++ b/openapi/openapi.json
@@ -637,6 +637,29 @@
}
},
"security": [{"bearer": []}]
+ },
+ "delete": {
+ "summary": "Remove a moderator from a community",
+ "parameters": [
+ {
+ "name": "communityID",
+ "in": "path",
+ "required": true,
+ "schema": {"type": "integer"}
+ },
+ {
+ "name": "userID",
+ "in": "path",
+ "required": true,
+ "schema": {"type": "integer"}
+ }
+ ],
+ "responses": {
+ "204": {
+ "description": "Successfully removed."
+ }
+ },
+ "security": [{"bearer": []}]
}
},
"/api/unstable/communities/{communityID}/posts": {
diff --git a/src/routes/api/communities.rs b/src/routes/api/communities.rs
index 81c52f1..3ba4bb7 100644
--- a/src/routes/api/communities.rs
+++ b/src/routes/api/communities.rs
@@ -458,6 +458,43 @@ async fn route_unstable_communities_moderators_add(
Ok(crate::empty_response())
}
+async fn route_unstable_communities_moderators_remove(
+ params: (CommunityLocalID, UserLocalID),
+ ctx: Arc<crate::RouteContext>,
+ req: hyper::Request<hyper::Body>,
+) -> Result<hyper::Response<hyper::Body>, crate::Error> {
+ let (community_id, user_id) = params;
+
+ let db = ctx.db_pool.get().await?;
+
+ let lang = crate::get_lang_for_req(&req);
+ let login_user = crate::require_login(&req, &db).await?;
+
+ ({
+ let row = db
+ .query_opt(
+ "SELECT 1 FROM community_moderator WHERE community=$1 AND person=$2",
+ &[&community_id, &login_user],
+ )
+ .await?;
+ match row {
+ None => Err(crate::Error::UserError(crate::simple_response(
+ hyper::StatusCode::FORBIDDEN,
+ lang.tr("must_be_moderator", None).into_owned(),
+ ))),
+ Some(_) => Ok(()),
+ }
+ })?;
+
+ db.execute(
+ "DELETE FROM community_moderator WHERE community=$1 AND person=$2",
+ &[&community_id, &user_id],
+ )
+ .await?;
+
+ Ok(crate::empty_response())
+}
+
async fn route_unstable_communities_unfollow(
params: (CommunityLocalID,),
ctx: Arc<crate::RouteContext>,
@@ -735,10 +772,15 @@ pub fn route_communities() -> crate::RouteNode<()> {
crate::RouteNode::new()
.with_handler_async("GET", route_unstable_communities_moderators_list)
.with_child_parse::<UserLocalID, _>(
- crate::RouteNode::new().with_handler_async(
- "PUT",
- route_unstable_communities_moderators_add,
- ),
+ crate::RouteNode::new()
+ .with_handler_async(
+ "PUT",
+ route_unstable_communities_moderators_add,
+ )
+ .with_handler_async(
+ "DELETE",
+ route_unstable_communities_moderators_remove,
+ ),
),
)
.with_child(