- Extracted methods for is_mod_or_admin, and is_admin.
- Removed admins from GetPostResponse and GetCommunityResponse.
- Some cleanup.
data: {
community: CommunityView,
moderators: Vec<CommunityModeratorView>,
- admins: Vec<UserView>,
}
}
```
comments: Vec<CommentView>,
community: CommunityView,
moderators: Vec<CommunityModeratorView>,
- admins: Vec<UserView>,
}
}
```
use crate::{
- api::{claims::Claims, APIError, Oper, Perform},
+ api::{claims::Claims, is_mod_or_admin, APIError, Oper, Perform},
apub::{ApubLikeableType, ApubObjectType},
blocking,
websocket::{
use lemmy_db::{
comment::*,
comment_view::*,
- community::Community,
community_view::*,
moderator::*,
post::*,
}
// Verify that only a mod or admin can remove
- let is_mod_or_admin = blocking(pool, move |conn| {
- Community::is_mod_or_admin(conn, user_id, community_id)
- })
- .await?;
- if !is_mod_or_admin {
- return Err(APIError::err("not_an_admin").into());
- }
+ is_mod_or_admin(pool, user_id, community_id).await?;
// Do the remove
let removed = data.removed;
use super::*;
use crate::{
- api::{claims::Claims, APIError, Oper, Perform},
+ api::{claims::Claims, is_admin, is_mod_or_admin, APIError, Oper, Perform},
apub::ActorType,
blocking,
websocket::{
pub struct GetCommunityResponse {
pub community: CommunityView,
pub moderators: Vec<CommunityModeratorView>,
- pub admins: Vec<UserView>, // TODO this should be from GetSite, shouldn't need this
pub online: usize,
}
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
};
- let site = blocking(pool, move |conn| Site::read(conn, 1)).await??;
- let site_creator_id = site.creator_id;
- let mut admins = blocking(pool, move |conn| UserView::admins(conn)).await??;
- let creator_index = admins.iter().position(|r| r.id == site_creator_id).unwrap();
- let creator_user = admins.remove(creator_index);
- admins.insert(0, creator_user);
-
let online = if let Some(ws) = websocket_info {
if let Some(id) = ws.id {
ws.chatserver.do_send(JoinCommunityRoom {
let res = GetCommunityResponse {
community: community_view,
moderators,
- admins,
online,
};
}
// Verify its an admin (only an admin can remove a community)
- let admins: Vec<i32> = blocking(pool, move |conn| {
- UserView::admins(conn).map(|v| v.into_iter().map(|a| a.id).collect())
- })
- .await??;
- if !admins.contains(&user_id) {
- return Err(APIError::err("not_an_admin").into());
- }
+ is_admin(pool, user_id).await?;
// Do the remove
let edit_id = data.edit_id;
let community_id = data.community_id;
// Verify that only mods or admins can ban
- let is_mod_or_admin = blocking(pool, move |conn| {
- Community::is_mod_or_admin(conn, user_id, community_id)
- })
- .await?;
- if !is_mod_or_admin {
- return Err(APIError::err("not_an_admin").into());
- }
+ is_mod_or_admin(pool, user_id, community_id).await?;
let community_user_ban_form = CommunityUserBanForm {
community_id: data.community_id,
let community_id = data.community_id;
// Verify that only mods or admins can add mod
- let is_mod_or_admin = blocking(pool, move |conn| {
- Community::is_mod_or_admin(conn, user_id, community_id)
- })
- .await?;
- if !is_mod_or_admin {
- return Err(APIError::err("not_an_admin").into());
- }
+ is_mod_or_admin(pool, user_id, community_id).await?;
if data.added {
let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
Ok(GetCommunityResponse {
community: community_view,
moderators,
- admins,
online: 0,
})
}
-use crate::{websocket::WebsocketInfo, DbPool, LemmyError};
+use crate::{blocking, websocket::WebsocketInfo, DbPool, LemmyError};
use actix_web::client::Client;
-use lemmy_db::{community::*, community_view::*, moderator::*, site::*, user::*, user_view::*};
+use lemmy_db::{
+ community::*,
+ community_view::*,
+ moderator::*,
+ site::*,
+ user::*,
+ user_view::*,
+ Crud,
+};
pub mod claims;
pub mod comment;
websocket_info: Option<WebsocketInfo>,
) -> Result<Self::Response, LemmyError>;
}
+
+pub async fn is_mod_or_admin(
+ pool: &DbPool,
+ user_id: i32,
+ community_id: i32,
+) -> Result<(), LemmyError> {
+ let is_mod_or_admin = blocking(pool, move |conn| {
+ Community::is_mod_or_admin(conn, user_id, community_id)
+ })
+ .await?;
+ if !is_mod_or_admin {
+ return Err(APIError::err("not_an_admin").into());
+ }
+ Ok(())
+}
+pub async fn is_admin(pool: &DbPool, user_id: i32) -> Result<(), LemmyError> {
+ let user = blocking(pool, move |conn| User_::read(conn, user_id)).await??;
+ if !user.admin {
+ return Err(APIError::err("not_an_admin").into());
+ }
+ Ok(())
+}
use crate::{
- api::{claims::Claims, APIError, Oper, Perform},
+ api::{claims::Claims, is_mod_or_admin, APIError, Oper, Perform},
apub::{ApubLikeableType, ApubObjectType},
blocking,
fetch_iframely_and_pictrs_data,
};
use lemmy_db::{
comment_view::*,
- community::*,
community_view::*,
moderator::*,
naive_now,
post::*,
post_view::*,
- site::*,
site_view::*,
user::*,
- user_view::*,
Crud,
Likeable,
ListingType,
comments: Vec<CommentView>,
community: CommunityView,
moderators: Vec<CommunityModeratorView>,
- admins: Vec<UserView>,
pub online: usize,
}
})
.await??;
- let site_creator_id =
- blocking(pool, move |conn| Site::read(conn, 1).map(|s| s.creator_id)).await??;
-
- let mut admins = blocking(pool, move |conn| UserView::admins(conn)).await??;
- let creator_index = admins.iter().position(|r| r.id == site_creator_id).unwrap();
- let creator_user = admins.remove(creator_index);
- admins.insert(0, creator_user);
-
let online = if let Some(ws) = websocket_info {
if let Some(id) = ws.id {
ws.chatserver.do_send(JoinPostRoom {
comments,
community,
moderators,
- admins,
online,
})
}
}
// Verify that only the mods can remove
- let is_mod_or_admin = blocking(pool, move |conn| {
- Community::is_mod_or_admin(conn, user_id, community_id)
- })
- .await?;
- if !is_mod_or_admin {
- return Err(APIError::err("not_an_admin").into());
- }
+ is_mod_or_admin(pool, user_id, community_id).await?;
// Update the post
let edit_id = data.edit_id;
}
// Verify that only the mods can lock
- let is_mod_or_admin = blocking(pool, move |conn| {
- Community::is_mod_or_admin(conn, user_id, community_id)
- })
- .await?;
- if !is_mod_or_admin {
- return Err(APIError::err("not_an_admin").into());
- }
+ is_mod_or_admin(pool, user_id, community_id).await?;
// Update the post
let edit_id = data.edit_id;
}
// Verify that only the mods can sticky
- let is_mod_or_admin = blocking(pool, move |conn| {
- Community::is_mod_or_admin(conn, user_id, community_id)
- })
- .await?;
- if !is_mod_or_admin {
- return Err(APIError::err("not_an_admin").into());
- }
+ is_mod_or_admin(pool, user_id, community_id).await?;
// Update the post
let edit_id = data.edit_id;
use super::user::Register;
use crate::{
- api::{claims::Claims, APIError, Oper, Perform},
+ api::{claims::Claims, is_admin, APIError, Oper, Perform},
apub::fetcher::search_by_apub_id,
blocking,
version,
let user_id = claims.id;
// Make sure user is an admin
- let user = blocking(pool, move |conn| UserView::read(conn, user_id)).await??;
- if !user.admin {
- return Err(APIError::err("not_an_admin").into());
- }
+ is_admin(pool, user_id).await?;
let site_form = SiteForm {
name: data.name.to_owned(),
let user_id = claims.id;
// Make sure user is an admin
- let user = blocking(pool, move |conn| UserView::read(conn, user_id)).await??;
- if !user.admin {
- return Err(APIError::err("not_an_admin").into());
- }
+ is_admin(pool, user_id).await?;
let found_site = blocking(pool, move |conn| Site::read(conn, 1)).await??;
let user_id = claims.id;
// Only let admins read this
- let admins = blocking(pool, move |conn| UserView::admins(conn)).await??;
- let admin_ids: Vec<i32> = admins.into_iter().map(|m| m.id).collect();
-
- if !admin_ids.contains(&user_id) {
- return Err(APIError::err("not_an_admin").into());
- }
+ is_admin(pool, user_id).await?;
let config_hjson = Settings::read_config_file()?;
use crate::{
- api::{claims::Claims, APIError, Oper, Perform},
+ api::{claims::Claims, is_admin, APIError, Oper, Perform},
apub::ApubObjectType,
blocking,
websocket::{
let user_id = claims.id;
// Make sure user is an admin
- let is_admin = move |conn: &'_ _| UserView::read(conn, user_id).map(|u| u.admin);
- if !blocking(pool, is_admin).await?? {
- return Err(APIError::err("not_an_admin").into());
- }
+ is_admin(pool, user_id).await?;
let added = data.added;
let added_user_id = data.user_id;
let user_id = claims.id;
// Make sure user is an admin
- let is_admin = move |conn: &'_ _| UserView::read(conn, user_id).map(|u| u.admin);
- if !blocking(pool, is_admin).await?? {
- return Err(APIError::err("not_an_admin").into());
- }
+ is_admin(pool, user_id).await?;
let ban = data.ban;
let banned_user_id = data.user_id;
let data = res.data as GetCommunityResponse;
this.state.community = data.community;
this.state.moderators = data.moderators;
- this.state.admins = data.admins;
this.state.online = data.online;
document.title = `/c/${this.state.community.name} - ${this.state.site.name}`;
this.setState(this.state);
} else if (res.op == UserOperation.GetSite) {
let data = res.data as GetSiteResponse;
this.state.site = data.site;
+ this.state.admins = data.admins;
this.setState(this.state);
}
}
this.state.comments = data.comments;
this.state.community = data.community;
this.state.moderators = data.moderators;
- this.state.siteRes.admins = data.admins;
this.state.online = data.online;
this.state.loading = false;
document.title = `${this.state.post.name} - ${this.state.siteRes.site.name}`;
let data = res.data as GetCommunityResponse;
this.state.community = data.community;
this.state.moderators = data.moderators;
- this.state.siteRes.admins = data.admins;
this.setState(this.state);
}
}
export interface GetCommunityResponse {
community: Community;
moderators: Array<CommunityUser>;
- admins: Array<UserView>;
online: number;
}
comments: Array<Comment>;
community: Community;
moderators: Array<CommunityUser>;
- admins: Array<UserView>;
online: number;
}