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 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());
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;
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))?;
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;
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());
}
}
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;
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;
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()),
};
}
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;
// 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 {
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()),
};
}
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;
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
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 {
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())
}
};
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))?;
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
);
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 {
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
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)
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;
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()),
};
}
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;
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
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;
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()),
};
}
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;
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())
}
};
}
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;
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 {
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.
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())
}
};
}
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
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 {
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
// 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 {
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)
.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 {
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;
// 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 {
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
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;
);
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 {
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
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;
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()),
};
}
)?;
// 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 {
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 {
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)?;
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)?;
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)?;
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 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 {
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
// 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
// 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
// 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
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
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())
}
};
}
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;
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
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,
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
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())
}
}
}
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)?;
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
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)?;
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
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;
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;
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;
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)?;
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;
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()),
};
}
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()),
};
}
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;
// 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
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()),
};
}
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()),
};
}
// 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
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()),
};
Ok(PasswordResetResponse {
let user_id = PasswordResetRequest::read_from_token(&conn, &data.token)?.user_id;
// 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());
}
// Update the user with the new password
let updated_user = match User_::update_password(&conn, user_id, &data.password) {
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
let mut collection = UnorderedCollection::default();
collection.object_props.set_context_object(context()).ok();
- collection
- .object_props
- .set_id_string(base_url.to_string())
- .ok();
+ collection.object_props.set_id_string(base_url).ok();
let connection = establish_connection();
//As we are an object, we validated that the community id was valid
let mut page = Page::default();
page.object_props.set_context_object(context()).ok();
- page.object_props.set_id_string(base_url.to_string()).ok();
+ page.object_props.set_id_string(base_url).ok();
page.object_props.set_name_string(self.name.to_owned()).ok();
if let Some(body) = &self.body {
self
}
- pub fn from_user_id<T: MaybeOptional<i32>>(mut self, from_user_id: T) -> Self {
+ pub fn for_user<T: MaybeOptional<i32>>(mut self, from_user_id: T) -> Self {
self.from_user_id = from_user_id.get_optional();
self
}
impl<T> MaybeOptional<T> for T {
fn get_optional(self) -> Option<T> {
- return Some(self);
+ Some(self)
}
}
impl<T> MaybeOptional<T> for Option<T> {
fn get_optional(self) -> Option<T> {
- return self;
+ self
}
}
Pool::builder()
.max_size(Settings::get().database.pool_size)
.build(manager)
- .expect(&format!("Error connecting to {}", db_url))
+ .unwrap_or_else(|_| panic!("Error connecting to {}", db_url))
};
}
pub fn establish_connection() -> PooledConnection<ConnectionManager<PgConnection>> {
- return PG_POOL.get().unwrap();
+ PG_POOL.get().unwrap()
}
#[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
let mut query = self.query;
- match self.listing_type {
- ListingType::Subscribed => {
- query = query.filter(subscribed.eq(true));
- }
- _ => {}
- };
+ if let ListingType::Subscribed = self.listing_type {
+ query = query.filter(subscribed.eq(true));
+ }
query = match self.sort {
SortType::Hot => query
embed_migrations!();
fn main() {
- let _ = env_logger::init();
+ env_logger::init();
let sys = actix::System::new("lemmy");
// Run the migrations from code
}
fn get_sort_type(info: web::Query<Params>) -> Result<SortType, ParseError> {
- let sort_query = info.sort.to_owned().unwrap_or(SortType::Hot.to_string());
+ let sort_query = info
+ .sort
+ .to_owned()
+ .unwrap_or_else(|| SortType::Hot.to_string());
SortType::from_str(&sort_query)
}
}
});
- return HttpResponse::Ok()
+ HttpResponse::Ok()
.content_type("application/json")
- .body(json.to_string());
+ .body(json.to_string())
}
fn node_info() -> HttpResponse<Body> {
"openRegistrations": site_view.open_registration,
}
});
- return HttpResponse::Ok()
+ HttpResponse::Ok()
.content_type("application/json")
- .body(json.to_string());
+ .body(json.to_string())
}
// Start chat server actor in separate thread
let server = ChatServer::default().start();
cfg
- .data(server.clone())
+ .data(server)
.service(web::resource("/api/v1/ws").to(chat_route));
}
.connection_info()
.remote()
.unwrap_or("127.0.0.1:12345")
- .split(":")
+ .split(':')
.next()
.unwrap_or("127.0.0.1")
.to_string(),
lazy_static! {
static ref SETTINGS: Settings = {
- return match Settings::init() {
+ match Settings::init() {
Ok(c) => c,
Err(e) => panic!("{}", e),
- };
+ }
};
}
// https://github.com/mehcode/config-rs/issues/73
s.merge(Environment::with_prefix("LEMMY").separator("__"))?;
- return s.try_into();
+ s.try_into()
}
/// Returns the config as a struct.
-pub const VERSION: &'static str = "v0.5.12";
+pub const VERSION: &str = "v0.5.12";
ChatServer {
sessions: HashMap::new(),
rate_limits: HashMap::new(),
- rooms: rooms,
+ rooms,
rng: rand::thread_rng(),
}
}
impl ChatServer {
/// Send message to all users in the room
- fn send_room_message(&self, room: &i32, message: &str, skip_id: usize) {
- if let Some(sessions) = self.rooms.get(room) {
+ fn send_room_message(&self, room: i32, message: &str, skip_id: usize) {
+ if let Some(sessions) = self.rooms.get(&room) {
for id in sessions {
if *id != skip_id {
if let Some(info) = self.sessions.get(id) {
fn join_room(&mut self, room_id: i32, id: usize) {
// remove session from all rooms
- for (_n, sessions) in &mut self.rooms {
+ for sessions in self.rooms.values_mut() {
sessions.remove(&id);
}
self.rooms.insert(room_id, HashSet::new());
}
- &self.rooms.get_mut(&room_id).unwrap().insert(id);
+ self.rooms.get_mut(&room_id).unwrap().insert(id);
}
fn send_community_message(
&self,
- community_id: &i32,
+ community_id: i32,
message: &str,
skip_id: usize,
) -> Result<(), Error> {
let posts = PostQueryBuilder::create(&conn)
.listing_type(ListingType::Community)
.sort(&SortType::New)
- .for_community_id(*community_id)
+ .for_community_id(community_id)
.limit(9999)
.list()?;
for post in posts {
- self.send_room_message(&post.id, message, skip_id);
+ self.send_room_message(post.id, message, skip_id);
}
Ok(())
)
}
+ #[allow(clippy::float_cmp)]
fn check_rate_limit_full(&mut self, id: usize, rate: i32, per: i32) -> Result<(), Error> {
if let Some(info) = self.sessions.get(&id) {
if let Some(rate_limit) = self.rate_limits.get_mut(&info.ip) {
"Rate limited IP: {}, time_passed: {}, allowance: {}",
&info.ip, time_passed, rate_limit.allowance
);
- Err(APIError {
- op: "Rate Limit".to_string(),
- message: format!("Too many requests. {} per {} seconds", rate, per),
- })?
+ Err(
+ APIError {
+ op: "Rate Limit".to_string(),
+ message: format!("Too many requests. {} per {} seconds", rate, per),
+ }
+ .into(),
+ )
} else {
rate_limit.allowance -= 1.0;
Ok(())
// remove address
if self.sessions.remove(&msg.id).is_some() {
// remove session from all rooms
- for (_id, sessions) in &mut self.rooms {
+ for sessions in self.rooms.values_mut() {
if sessions.remove(&msg.id) {
// rooms.push(*id);
}
let data = &json["data"].to_string();
let op = &json["op"].as_str().ok_or(APIError {
op: "Unknown op type".to_string(),
- message: format!("Unknown op type"),
+ message: "Unknown op type".to_string(),
})?;
let user_operation: UserOperation = UserOperation::from_str(&op)?;
community_sent.community.user_id = None;
community_sent.community.subscribed = None;
let community_sent_str = serde_json::to_string(&community_sent)?;
- chat.send_community_message(&community_sent.community.id, &community_sent_str, msg.id)?;
+ chat.send_community_message(community_sent.community.id, &community_sent_str, msg.id)?;
Ok(serde_json::to_string(&res)?)
}
UserOperation::FollowCommunity => {
let community_id = ban_from_community.community_id;
let res = Oper::new(user_operation, ban_from_community).perform()?;
let res_str = serde_json::to_string(&res)?;
- chat.send_community_message(&community_id, &res_str, msg.id)?;
+ chat.send_community_message(community_id, &res_str, msg.id)?;
Ok(res_str)
}
UserOperation::AddModToCommunity => {
let community_id = mod_add_to_community.community_id;
let res = Oper::new(user_operation, mod_add_to_community).perform()?;
let res_str = serde_json::to_string(&res)?;
- chat.send_community_message(&community_id, &res_str, msg.id)?;
+ chat.send_community_message(community_id, &res_str, msg.id)?;
Ok(res_str)
}
UserOperation::ListCategories => {
let mut post_sent = res.clone();
post_sent.post.my_vote = None;
let post_sent_str = serde_json::to_string(&post_sent)?;
- chat.send_room_message(&post_sent.post.id, &post_sent_str, msg.id);
+ chat.send_room_message(post_sent.post.id, &post_sent_str, msg.id);
Ok(serde_json::to_string(&res)?)
}
UserOperation::SavePost => {
comment_sent.comment.my_vote = None;
comment_sent.comment.user_id = None;
let comment_sent_str = serde_json::to_string(&comment_sent)?;
- chat.send_room_message(&post_id, &comment_sent_str, msg.id);
+ chat.send_room_message(post_id, &comment_sent_str, msg.id);
Ok(serde_json::to_string(&res)?)
}
UserOperation::EditComment => {
comment_sent.comment.my_vote = None;
comment_sent.comment.user_id = None;
let comment_sent_str = serde_json::to_string(&comment_sent)?;
- chat.send_room_message(&post_id, &comment_sent_str, msg.id);
+ chat.send_room_message(post_id, &comment_sent_str, msg.id);
Ok(serde_json::to_string(&res)?)
}
UserOperation::SaveComment => {
comment_sent.comment.my_vote = None;
comment_sent.comment.user_id = None;
let comment_sent_str = serde_json::to_string(&comment_sent)?;
- chat.send_room_message(&post_id, &comment_sent_str, msg.id);
+ chat.send_room_message(post_id, &comment_sent_str, msg.id);
Ok(serde_json::to_string(&res)?)
}
UserOperation::GetModlog => {