Browse Source

Apply changes suggested by cargo clippy (fixes #395)

feature/settings-cleanup
Felix Ableitner 3 years ago
parent
commit
fe8b13bbce
  1. 42
      server/src/api/comment.rs
  2. 89
      server/src/api/community.rs
  3. 52
      server/src/api/post.rs
  4. 43
      server/src/api/site.rs
  5. 105
      server/src/api/user.rs
  6. 5
      server/src/apub/community.rs
  7. 2
      server/src/apub/post.rs
  8. 2
      server/src/db/community_view.rs
  9. 8
      server/src/db/mod.rs
  10. 9
      server/src/db/post_view.rs
  11. 2
      server/src/main.rs
  12. 5
      server/src/routes/feeds.rs
  13. 8
      server/src/routes/nodeinfo.rs
  14. 4
      server/src/routes/websocket.rs
  15. 6
      server/src/settings.rs
  16. 2
      server/src/version.rs
  17. 46
      server/src/websocket/server.rs

42
server/src/api/comment.rs

@ -51,7 +51,7 @@ impl Perform<CommentResponse> for Oper<CreateComment> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -59,12 +59,12 @@ impl Perform<CommentResponse> for Oper<CreateComment> {
// Check for a community ban
let post = Post::read(&conn, data.post_id)?;
if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() {
return Err(APIError::err(&self.op, "community_ban"))?;
return Err(APIError::err(&self.op, "community_ban").into());
}
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
return Err(APIError::err(&self.op, "site_ban"))?;
return Err(APIError::err(&self.op, "site_ban").into());
}
let content_slurs_removed = remove_slurs(&data.content.to_owned());
@ -82,14 +82,14 @@ impl Perform<CommentResponse> for Oper<CreateComment> {
let inserted_comment = match Comment::create(&conn, &comment_form) {
Ok(comment) => comment,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_create_comment"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_create_comment").into()),
};
// Scan the comment for user mentions, add those rows
let extracted_usernames = extract_usernames(&comment_form.content);
for username_mention in &extracted_usernames {
let mention_user = User_::read_from_name(&conn, username_mention.to_string());
let mention_user = User_::read_from_name(&conn, (*username_mention).to_string());
if mention_user.is_ok() {
let mention_user_id = mention_user?.id;
@ -124,7 +124,7 @@ impl Perform<CommentResponse> for Oper<CreateComment> {
let _inserted_like = match CommentLike::like(&conn, &like_form) {
Ok(like) => like,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_like_comment"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_like_comment").into()),
};
let comment_view = CommentView::read(&conn, inserted_comment.id, Some(user_id))?;
@ -143,7 +143,7 @@ impl Perform<CommentResponse> for Oper<EditComment> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -163,17 +163,17 @@ impl Perform<CommentResponse> for Oper<EditComment> {
editors.append(&mut UserView::admins(&conn)?.into_iter().map(|a| a.id).collect());
if !editors.contains(&user_id) {
return Err(APIError::err(&self.op, "no_comment_edit_allowed"))?;
return Err(APIError::err(&self.op, "no_comment_edit_allowed").into());
}
// Check for a community ban
if CommunityUserBanView::get(&conn, user_id, orig_comment.community_id).is_ok() {
return Err(APIError::err(&self.op, "community_ban"))?;
return Err(APIError::err(&self.op, "community_ban").into());
}
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
return Err(APIError::err(&self.op, "site_ban"))?;
return Err(APIError::err(&self.op, "site_ban").into());
}
}
@ -196,14 +196,14 @@ impl Perform<CommentResponse> for Oper<EditComment> {
let _updated_comment = match Comment::update(&conn, data.edit_id, &comment_form) {
Ok(comment) => comment,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment").into()),
};
// Scan the comment for user mentions, add those rows
let extracted_usernames = extract_usernames(&comment_form.content);
for username_mention in &extracted_usernames {
let mention_user = User_::read_from_name(&conn, username_mention.to_string());
let mention_user = User_::read_from_name(&conn, (*username_mention).to_string());
if mention_user.is_ok() {
let mention_user_id = mention_user?.id;
@ -255,7 +255,7 @@ impl Perform<CommentResponse> for Oper<SaveComment> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -268,12 +268,12 @@ impl Perform<CommentResponse> for Oper<SaveComment> {
if data.save {
match CommentSaved::save(&conn, &comment_saved_form) {
Ok(comment) => comment,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_save_comment"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_save_comment").into()),
};
} else {
match CommentSaved::unsave(&conn, &comment_saved_form) {
Ok(comment) => comment,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_save_comment"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_save_comment").into()),
};
}
@ -293,7 +293,7 @@ impl Perform<CommentResponse> for Oper<CreateCommentLike> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -301,20 +301,20 @@ impl Perform<CommentResponse> for Oper<CreateCommentLike> {
// Don't do a downvote if site has downvotes disabled
if data.score == -1 {
let site = SiteView::read(&conn)?;
if site.enable_downvotes == false {
return Err(APIError::err(&self.op, "downvotes_disabled"))?;
if !site.enable_downvotes {
return Err(APIError::err(&self.op, "downvotes_disabled").into());
}
}
// Check for a community ban
let post = Post::read(&conn, data.post_id)?;
if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() {
return Err(APIError::err(&self.op, "community_ban"))?;
return Err(APIError::err(&self.op, "community_ban").into());
}
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
return Err(APIError::err(&self.op, "site_ban"))?;
return Err(APIError::err(&self.op, "site_ban").into());
}
let like_form = CommentLikeForm {
@ -332,7 +332,7 @@ impl Perform<CommentResponse> for Oper<CreateCommentLike> {
if do_add {
let _inserted_like = match CommentLike::like(&conn, &like_form) {
Ok(like) => like,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_like_comment"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_like_comment").into()),
};
}

89
server/src/api/community.rs

@ -136,21 +136,24 @@ impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
let community_id = match data.id {
Some(id) => id,
None => {
match Community::read_from_name(&conn, data.name.to_owned().unwrap_or("main".to_string())) {
match Community::read_from_name(
&conn,
data.name.to_owned().unwrap_or_else(|| "main".to_string()),
) {
Ok(community) => community.id,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community").into()),
}
}
};
let community_view = match CommunityView::read(&conn, community_id, user_id) {
Ok(community) => community,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community").into()),
};
let moderators = match CommunityModeratorView::for_community(&conn, community_id) {
Ok(moderators) => moderators,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community").into()),
};
let site_creator_id = Site::read(&conn, 1)?.creator_id;
@ -176,21 +179,21 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
if has_slurs(&data.name)
|| has_slurs(&data.title)
|| (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap()))
{
return Err(APIError::err(&self.op, "no_slurs"))?;
return Err(APIError::err(&self.op, "no_slurs").into());
}
let user_id = claims.id;
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
return Err(APIError::err(&self.op, "site_ban"))?;
return Err(APIError::err(&self.op, "site_ban").into());
}
// When you create a community, make sure the user becomes a moderator and a follower
@ -208,7 +211,7 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> {
let inserted_community = match Community::create(&conn, &community_form) {
Ok(community) => community,
Err(_e) => return Err(APIError::err(&self.op, "community_already_exists"))?,
Err(_e) => return Err(APIError::err(&self.op, "community_already_exists").into()),
};
let community_moderator_form = CommunityModeratorForm {
@ -220,10 +223,7 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> {
match CommunityModerator::join(&conn, &community_moderator_form) {
Ok(user) => user,
Err(_e) => {
return Err(APIError::err(
&self.op,
"community_moderator_already_exists",
))?
return Err(APIError::err(&self.op, "community_moderator_already_exists").into())
}
};
@ -235,7 +235,7 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> {
let _inserted_community_follower =
match CommunityFollower::follow(&conn, &community_follower_form) {
Ok(user) => user,
Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists"))?,
Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists").into()),
};
let community_view = CommunityView::read(&conn, inserted_community.id, Some(user_id))?;
@ -252,21 +252,21 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> {
let data: &EditCommunity = &self.data;
if has_slurs(&data.name) || has_slurs(&data.title) {
return Err(APIError::err(&self.op, "no_slurs"))?;
return Err(APIError::err(&self.op, "no_slurs").into());
}
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
return Err(APIError::err(&self.op, "site_ban"))?;
return Err(APIError::err(&self.op, "site_ban").into());
}
// Verify its a mod
@ -279,7 +279,7 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> {
);
editors.append(&mut UserView::admins(&conn)?.into_iter().map(|a| a.id).collect());
if !editors.contains(&user_id) {
return Err(APIError::err(&self.op, "no_community_edit_allowed"))?;
return Err(APIError::err(&self.op, "no_community_edit_allowed").into());
}
let community_form = CommunityForm {
@ -296,7 +296,7 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> {
let _updated_community = match Community::update(&conn, data.edit_id, &community_form) {
Ok(community) => community,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_community"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_community").into()),
};
// Mod tables
@ -351,7 +351,7 @@ impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
let communities = CommunityQueryBuilder::create(&conn)
.sort(&sort)
.from_user_id(user_id)
.for_user(user_id)
.show_nsfw(show_nsfw)
.page(data.page)
.limit(data.limit)
@ -372,7 +372,7 @@ impl Perform<CommunityResponse> for Oper<FollowCommunity> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -385,12 +385,12 @@ impl Perform<CommunityResponse> for Oper<FollowCommunity> {
if data.follow {
match CommunityFollower::follow(&conn, &community_follower_form) {
Ok(user) => user,
Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists"))?,
Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists").into()),
};
} else {
match CommunityFollower::ignore(&conn, &community_follower_form) {
Ok(user) => user,
Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists"))?,
Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists").into()),
};
}
@ -410,7 +410,7 @@ impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -418,7 +418,7 @@ impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> {
let communities: Vec<CommunityFollowerView> =
match CommunityFollowerView::for_user(&conn, user_id) {
Ok(communities) => communities,
Err(_e) => return Err(APIError::err(&self.op, "system_err_login"))?,
Err(_e) => return Err(APIError::err(&self.op, "system_err_login").into()),
};
// Return the jwt
@ -436,7 +436,7 @@ impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -449,12 +449,12 @@ impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> {
if data.ban {
match CommunityUserBan::ban(&conn, &community_user_ban_form) {
Ok(user) => user,
Err(_e) => return Err(APIError::err(&self.op, "community_user_already_banned"))?,
Err(_e) => return Err(APIError::err(&self.op, "community_user_already_banned").into()),
};
} else {
match CommunityUserBan::unban(&conn, &community_user_ban_form) {
Ok(user) => user,
Err(_e) => return Err(APIError::err(&self.op, "community_user_already_banned"))?,
Err(_e) => return Err(APIError::err(&self.op, "community_user_already_banned").into()),
};
}
@ -491,7 +491,7 @@ impl Perform<AddModToCommunityResponse> for Oper<AddModToCommunity> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -505,20 +505,14 @@ impl Perform<AddModToCommunityResponse> for Oper<AddModToCommunity> {
match CommunityModerator::join(&conn, &community_moderator_form) {
Ok(user) => user,
Err(_e) => {
return Err(APIError::err(
&self.op,
"community_moderator_already_exists",
))?
return Err(APIError::err(&self.op, "community_moderator_already_exists").into())
}
};
} else {
match CommunityModerator::leave(&conn, &community_moderator_form) {
Ok(user) => user,
Err(_e) => {
return Err(APIError::err(
&self.op,
"community_moderator_already_exists",
))?
return Err(APIError::err(&self.op, "community_moderator_already_exists").into())
}
};
}
@ -548,7 +542,7 @@ impl Perform<GetCommunityResponse> for Oper<TransferCommunity> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -562,14 +556,8 @@ impl Perform<GetCommunityResponse> for Oper<TransferCommunity> {
admins.insert(0, creator_user);
// Make sure user is the creator, or an admin
if user_id != read_community.creator_id
&& !admins
.iter()
.map(|a| a.id)
.collect::<Vec<i32>>()
.contains(&user_id)
{
return Err(APIError::err(&self.op, "not_an_admin"))?;
if user_id != read_community.creator_id && !admins.iter().map(|a| a.id).any(|x| x == user_id) {
return Err(APIError::err(&self.op, "not_an_admin").into());
}
let community_form = CommunityForm {
@ -586,7 +574,7 @@ impl Perform<GetCommunityResponse> for Oper<TransferCommunity> {
let _updated_community = match Community::update(&conn, data.community_id, &community_form) {
Ok(community) => community,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_community"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_community").into()),
};
// You also have to re-do the community_moderator table, reordering it.
@ -610,10 +598,7 @@ impl Perform<GetCommunityResponse> for Oper<TransferCommunity> {
match CommunityModerator::join(&conn, &community_moderator_form) {
Ok(user) => user,
Err(_e) => {
return Err(APIError::err(
&self.op,
"community_moderator_already_exists",
))?
return Err(APIError::err(&self.op, "community_moderator_already_exists").into())
}
};
}
@ -629,12 +614,12 @@ impl Perform<GetCommunityResponse> for Oper<TransferCommunity> {
let community_view = match CommunityView::read(&conn, data.community_id, Some(user_id)) {
Ok(community) => community,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community").into()),
};
let moderators = match CommunityModeratorView::for_community(&conn, data.community_id) {
Ok(moderators) => moderators,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community").into()),
};
// Return the jwt

52
server/src/api/post.rs

@ -93,23 +93,23 @@ impl Perform<PostResponse> for Oper<CreatePost> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
if has_slurs(&data.name) || (data.body.is_some() && has_slurs(&data.body.to_owned().unwrap())) {
return Err(APIError::err(&self.op, "no_slurs"))?;
return Err(APIError::err(&self.op, "no_slurs").into());
}
let user_id = claims.id;
// Check for a community ban
if CommunityUserBanView::get(&conn, user_id, data.community_id).is_ok() {
return Err(APIError::err(&self.op, "community_ban"))?;
return Err(APIError::err(&self.op, "community_ban").into());
}
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
return Err(APIError::err(&self.op, "site_ban"))?;
return Err(APIError::err(&self.op, "site_ban").into());
}
let post_form = PostForm {
@ -128,7 +128,7 @@ impl Perform<PostResponse> for Oper<CreatePost> {
let inserted_post = match Post::create(&conn, &post_form) {
Ok(post) => post,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_create_post"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_create_post").into()),
};
// They like their own post by default
@ -141,13 +141,13 @@ impl Perform<PostResponse> for Oper<CreatePost> {
// Only add the like if the score isnt 0
let _inserted_like = match PostLike::like(&conn, &like_form) {
Ok(like) => like,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_like_post"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_like_post").into()),
};
// Refetch the view
let post_view = match PostView::read(&conn, inserted_post.id, Some(user_id)) {
Ok(post) => post,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_post"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_post").into()),
};
Ok(PostResponse {
@ -175,7 +175,7 @@ impl Perform<GetPostResponse> for Oper<GetPost> {
let post_view = match PostView::read(&conn, data.id, user_id) {
Ok(post) => post,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_post"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_post").into()),
};
let comments = CommentQueryBuilder::create(&conn)
@ -243,7 +243,7 @@ impl Perform<GetPostsResponse> for Oper<GetPosts> {
.list()
{
Ok(posts) => posts,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_get_posts"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_get_posts").into()),
};
Ok(GetPostsResponse {
@ -260,7 +260,7 @@ impl Perform<CreatePostLikeResponse> for Oper<CreatePostLike> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -268,20 +268,20 @@ impl Perform<CreatePostLikeResponse> for Oper<CreatePostLike> {
// Don't do a downvote if site has downvotes disabled
if data.score == -1 {
let site = SiteView::read(&conn)?;
if site.enable_downvotes == false {
return Err(APIError::err(&self.op, "downvotes_disabled"))?;
if !site.enable_downvotes {
return Err(APIError::err(&self.op, "downvotes_disabled").into());
}
}
// Check for a community ban
let post = Post::read(&conn, data.post_id)?;
if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() {
return Err(APIError::err(&self.op, "community_ban"))?;
return Err(APIError::err(&self.op, "community_ban").into());
}
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
return Err(APIError::err(&self.op, "site_ban"))?;
return Err(APIError::err(&self.op, "site_ban").into());
}
let like_form = PostLikeForm {
@ -294,17 +294,17 @@ impl Perform<CreatePostLikeResponse> for Oper<CreatePostLike> {
PostLike::remove(&conn, &like_form)?;
// Only add the like if the score isnt 0
let do_add = &like_form.score != &0 && (&like_form.score == &1 || &like_form.score == &-1);
let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
if do_add {
let _inserted_like = match PostLike::like(&conn, &like_form) {
Ok(like) => like,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_like_post"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_like_post").into()),
};
}
let post_view = match PostView::read(&conn, data.post_id, Some(user_id)) {
Ok(post) => post,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_post"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_post").into()),
};
// just output the score
@ -319,14 +319,14 @@ impl Perform<PostResponse> for Oper<EditPost> {
fn perform(&self) -> Result<PostResponse, Error> {
let data: &EditPost = &self.data;
if has_slurs(&data.name) || (data.body.is_some() && has_slurs(&data.body.to_owned().unwrap())) {
return Err(APIError::err(&self.op, "no_slurs"))?;
return Err(APIError::err(&self.op, "no_slurs").into());
}
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -341,17 +341,17 @@ impl Perform<PostResponse> for Oper<EditPost> {
);
editors.append(&mut UserView::admins(&conn)?.into_iter().map(|a| a.id).collect());
if !editors.contains(&user_id) {
return Err(APIError::err(&self.op, "no_post_edit_allowed"))?;
return Err(APIError::err(&self.op, "no_post_edit_allowed").into());
}
// Check for a community ban
if CommunityUserBanView::get(&conn, user_id, data.community_id).is_ok() {
return Err(APIError::err(&self.op, "community_ban"))?;
return Err(APIError::err(&self.op, "community_ban").into());
}
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
return Err(APIError::err(&self.op, "site_ban"))?;
return Err(APIError::err(&self.op, "site_ban").into());
}
let post_form = PostForm {
@ -370,7 +370,7 @@ impl Perform<PostResponse> for Oper<EditPost> {
let _updated_post = match Post::update(&conn, data.edit_id, &post_form) {
Ok(post) => post,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_post"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_post").into()),
};
// Mod tables
@ -418,7 +418,7 @@ impl Perform<PostResponse> for Oper<SavePost> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -431,12 +431,12 @@ impl Perform<PostResponse> for Oper<SavePost> {
if data.save {
match PostSaved::save(&conn, &post_saved_form) {
Ok(post) => post,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_save_post"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_save_post").into()),
};
} else {
match PostSaved::unsave(&conn, &post_saved_form) {
Ok(post) => post,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_save_post"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_save_post").into()),
};
}

43
server/src/api/site.rs

@ -160,16 +160,15 @@ impl Perform<GetModlogResponse> for Oper<GetModlog> {
)?;
// These arrays are only for the full modlog, when a community isn't given
let mut removed_communities = Vec::new();
let mut banned = Vec::new();
let mut added = Vec::new();
if data.community_id.is_none() {
removed_communities =
ModRemoveCommunityView::list(&conn, data.mod_user_id, data.page, data.limit)?;
banned = ModBanView::list(&conn, data.mod_user_id, data.page, data.limit)?;
added = ModAddView::list(&conn, data.mod_user_id, data.page, data.limit)?;
}
let (removed_communities, banned, added) = if data.community_id.is_none() {
(
ModRemoveCommunityView::list(&conn, data.mod_user_id, data.page, data.limit)?,
ModBanView::list(&conn, data.mod_user_id, data.page, data.limit)?,
ModAddView::list(&conn, data.mod_user_id, data.page, data.limit)?,
)
} else {
(Vec::new(), Vec::new(), Vec::new())
};
// Return the jwt
Ok(GetModlogResponse {
@ -194,20 +193,20 @@ impl Perform<SiteResponse> for Oper<CreateSite> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
if has_slurs(&data.name)
|| (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap()))
{
return Err(APIError::err(&self.op, "no_slurs"))?;
return Err(APIError::err(&self.op, "no_slurs").into());
}
let user_id = claims.id;
// Make sure user is an admin
if !UserView::read(&conn, user_id)?.admin {
return Err(APIError::err(&self.op, "not_an_admin"))?;
return Err(APIError::err(&self.op, "not_an_admin").into());
}
let site_form = SiteForm {
@ -222,7 +221,7 @@ impl Perform<SiteResponse> for Oper<CreateSite> {
match Site::create(&conn, &site_form) {
Ok(site) => site,
Err(_e) => return Err(APIError::err(&self.op, "site_already_exists"))?,
Err(_e) => return Err(APIError::err(&self.op, "site_already_exists").into()),
};
let site_view = SiteView::read(&conn)?;
@ -241,20 +240,20 @@ impl Perform<SiteResponse> for Oper<EditSite> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
if has_slurs(&data.name)
|| (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap()))
{
return Err(APIError::err(&self.op, "no_slurs"))?;
return Err(APIError::err(&self.op, "no_slurs").into());
}
let user_id = claims.id;
// Make sure user is an admin
if UserView::read(&conn, user_id)?.admin == false {
return Err(APIError::err(&self.op, "not_an_admin"))?;
if !UserView::read(&conn, user_id)?.admin {
return Err(APIError::err(&self.op, "not_an_admin").into());
}
let found_site = Site::read(&conn, 1)?;
@ -271,7 +270,7 @@ impl Perform<SiteResponse> for Oper<EditSite> {
match Site::update(&conn, 1, &site_form) {
Ok(site) => site,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_site"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_site").into()),
};
let site_view = SiteView::read(&conn)?;
@ -426,7 +425,7 @@ impl Perform<GetSiteResponse> for Oper<TransferSite> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -435,7 +434,7 @@ impl Perform<GetSiteResponse> for Oper<TransferSite> {
// Make sure user is the creator
if read_site.creator_id != user_id {
return Err(APIError::err(&self.op, "not_an_admin"))?;
return Err(APIError::err(&self.op, "not_an_admin").into());
}
let site_form = SiteForm {
@ -450,7 +449,7 @@ impl Perform<GetSiteResponse> for Oper<TransferSite> {
match Site::update(&conn, 1, &site_form) {
Ok(site) => site,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_site"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_site").into()),
};
// Mod tables

105
server/src/api/user.rs

@ -172,18 +172,13 @@ impl Perform<LoginResponse> for Oper<Login> {
// Fetch that username / email
let user: User_ = match User_::find_by_email_or_username(&conn, &data.username_or_email) {
Ok(user) => user,
Err(_e) => {
return Err(APIError::err(
&self.op,
"couldnt_find_that_username_or_email",
))?
}
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_that_username_or_email").into()),
};
// Verify the password
let valid: bool = verify(&data.password, &user.password_encrypted).unwrap_or(false);
if !valid {
return Err(APIError::err(&self.op, "password_incorrect"))?;
return Err(APIError::err(&self.op, "password_incorrect").into());
}
// Return the jwt
@ -202,22 +197,22 @@ impl Perform<LoginResponse> for Oper<Register> {
// Make sure site has open registration
if let Ok(site) = SiteView::read(&conn) {
if !site.open_registration {
return Err(APIError::err(&self.op, "registration_closed"))?;
return Err(APIError::err(&self.op, "registration_closed").into());
}
}
// Make sure passwords match
if &data.password != &data.password_verify {
return Err(APIError::err(&self.op, "passwords_dont_match"))?;
if data.password != data.password_verify {
return Err(APIError::err(&self.op, "passwords_dont_match").into());
}
if has_slurs(&data.username) {
return Err(APIError::err(&self.op, "no_slurs"))?;
return Err(APIError::err(&self.op, "no_slurs").into());
}
// Make sure there are no admins
if data.admin && UserView::admins(&conn)?.len() > 0 {
return Err(APIError::err(&self.op, "admin_already_created"))?;
if data.admin && !UserView::admins(&conn)?.is_empty() {
return Err(APIError::err(&self.op, "admin_already_created").into());
}
// Register the new user
@ -241,7 +236,7 @@ impl Perform<LoginResponse> for Oper<Register> {
// Create the user
let inserted_user = match User_::register(&conn, &user_form) {
Ok(user) => user,
Err(_e) => return Err(APIError::err(&self.op, "user_already_exists"))?,
Err(_e) => return Err(APIError::err(&self.op, "user_already_exists").into()),
};
// Create the main community if it doesn't exist
@ -272,7 +267,7 @@ impl Perform<LoginResponse> for Oper<Register> {
let _inserted_community_follower =
match CommunityFollower::follow(&conn, &community_follower_form) {
Ok(user) => user,
Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists"))?,
Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists").into()),
};
// If its an admin, add them as a mod and follower to main
@ -286,10 +281,7 @@ impl Perform<LoginResponse> for Oper<Register> {
match CommunityModerator::join(&conn, &community_moderator_form) {
Ok(user) => user,
Err(_e) => {
return Err(APIError::err(
&self.op,
"community_moderator_already_exists",
))?
return Err(APIError::err(&self.op, "community_moderator_already_exists").into())
}
};
}
@ -309,7 +301,7 @@ impl Perform<LoginResponse> for Oper<SaveUserSettings> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -327,7 +319,7 @@ impl Perform<LoginResponse> for Oper<SaveUserSettings> {
Some(new_password_verify) => {
// Make sure passwords match
if new_password != new_password_verify {
return Err(APIError::err(&self.op, "passwords_dont_match"))?;
return Err(APIError::err(&self.op, "passwords_dont_match").into());
}
// Check the old password
@ -336,14 +328,14 @@ impl Perform<LoginResponse> for Oper<SaveUserSettings> {
let valid: bool =
verify(old_password, &read_user.password_encrypted).unwrap_or(false);
if !valid {
return Err(APIError::err(&self.op, "password_incorrect"))?;
return Err(APIError::err(&self.op, "password_incorrect").into());
}
User_::update_password(&conn, user_id, &new_password)?.password_encrypted
}
None => return Err(APIError::err(&self.op, "password_incorrect"))?,
None => return Err(APIError::err(&self.op, "password_incorrect").into()),
}
}
None => return Err(APIError::err(&self.op, "passwords_dont_match"))?,
None => return Err(APIError::err(&self.op, "passwords_dont_match").into()),
}
}
None => read_user.password_encrypted,
@ -368,7 +360,7 @@ impl Perform<LoginResponse> for Oper<SaveUserSettings> {
let updated_user = match User_::update(&conn, user_id, &user_form) {
Ok(user) => user,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user").into()),
};
// Return the jwt
@ -409,14 +401,14 @@ impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> {
None => {
match User_::read_from_name(
&conn,
data.username.to_owned().unwrap_or("admin".to_string()),
data
.username
.to_owned()
.unwrap_or_else(|| "admin".to_string()),
) {
Ok(user) => user.id,
Err(_e) => {
return Err(APIError::err(
&self.op,
"couldnt_find_that_username_or_email",
))?
return Err(APIError::err(&self.op, "couldnt_find_that_username_or_email").into())
}
}
}
@ -478,14 +470,14 @@ impl Perform<AddAdminResponse> for Oper<AddAdmin> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
// Make sure user is an admin
if UserView::read(&conn, user_id)?.admin == false {
return Err(APIError::err(&self.op, "not_an_admin"))?;
if !UserView::read(&conn, user_id)?.admin {
return Err(APIError::err(&self.op, "not_an_admin").into());
}
let read_user = User_::read(&conn, data.user_id)?;
@ -509,7 +501,7 @@ impl Perform<AddAdminResponse> for Oper<AddAdmin> {
match User_::update(&conn, data.user_id, &user_form) {
Ok(user) => user,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user").into()),
};
// Mod tables
@ -541,14 +533,14 @@ impl Perform<BanUserResponse> for Oper<BanUser> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
// Make sure user is an admin
if UserView::read(&conn, user_id)?.admin == false {
return Err(APIError::err(&self.op, "not_an_admin"))?;
if !UserView::read(&conn, user_id)?.admin {
return Err(APIError::err(&self.op, "not_an_admin").into());
}
let read_user = User_::read(&conn, data.user_id)?;
@ -572,7 +564,7 @@ impl Perform<BanUserResponse> for Oper<BanUser> {
match User_::update(&conn, data.user_id, &user_form) {
Ok(user) => user,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user").into()),
};
// Mod tables
@ -608,7 +600,7 @@ impl Perform<GetRepliesResponse> for Oper<GetReplies> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -636,7 +628,7 @@ impl Perform<GetUserMentionsResponse> for Oper<GetUserMentions> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -664,7 +656,7 @@ impl Perform<UserMentionResponse> for Oper<EditUserMention> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -680,7 +672,7 @@ impl Perform<UserMentionResponse> for Oper<EditUserMention> {
let _updated_user_mention =
match UserMention::update(&conn, user_mention.id, &user_mention_form) {
Ok(comment) => comment,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment").into()),
};
let user_mention_view = UserMentionView::read(&conn, user_mention.id, user_id)?;
@ -699,7 +691,7 @@ impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -724,7 +716,7 @@ impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> {
let _updated_comment = match Comment::update(&conn, reply.id, &comment_form) {
Ok(comment) => comment,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment").into()),
};
}
@ -745,7 +737,7 @@ impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> {
let _updated_mention =
match UserMention::update(&conn, mention.user_mention_id, &mention_form) {
Ok(mention) => mention,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment").into()),
};
}
@ -763,7 +755,7 @@ impl Perform<LoginResponse> for Oper<DeleteAccount> {
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()),
};
let user_id = claims.id;
@ -773,7 +765,7 @@ impl Perform<LoginResponse> for Oper<DeleteAccount> {
// Verify the password
let valid: bool = verify(&data.password, &user.password_encrypted).unwrap_or(false);
if !valid {
return Err(APIError::err(&self.op, "password_incorrect"))?;
return Err(APIError::err(&self.op, "password_incorrect").into());
}
// Comments
@ -796,7 +788,7 @@ impl Perform<LoginResponse> for Oper<DeleteAccount> {
let _updated_comment = match Comment::update(&conn, comment.id, &comment_form) {
Ok(comment) => comment,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment").into()),
};
}
@ -824,7 +816,7 @@ impl Perform<LoginResponse> for Oper<DeleteAccount> {
let _updated_post = match Post::update(&conn, post.id, &post_form) {
Ok(post) => post,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_post"))?,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_post").into()),
};
}
@ -843,12 +835,7 @@ impl Perform<PasswordResetResponse> for Oper<PasswordReset> {
// Fetch that email
let user: User_ = match User_::find_by_email(&conn, &data.email) {
Ok(user) => user,
Err(_e) => {
return Err(APIError::err(
&self.op,
"couldnt_find_that_username_or_email",
))?
}
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_that_username_or_email").into()),
};
// Generate a random token
@ -865,7 +852,7 @@ impl Perform<PasswordResetResponse> for Oper<PasswordReset> {
let html = &format!("<h1>Password Reset Request for {}</h1><br><a href={}/password_change/{}>Click here to reset your password</a>", user.name, hostname, &token);
match send_email(subject, user_email, &user.name, html) {
Ok(_o) => _o,
Err(_e) => return Err(APIError::err(&self.op, &_e.to_string()))?,
Err(_e) => return Err(APIError::err(&self.op, &_e).into()),
};