- Adding a .rustfmt.toml for the 2 space indent.
--- /dev/null
+tab_spaces = 2
parent_id: Option<i32>,
edit_id: Option<i32>,
pub post_id: i32,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize)]
deleted: Option<bool>,
reason: Option<String>,
read: Option<bool>,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize)]
pub struct SaveComment {
comment_id: i32,
save: bool,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct CommentResponse {
op: String,
- pub comment: CommentView
+ pub comment: CommentView,
}
#[derive(Serialize, Deserialize)]
comment_id: i32,
pub post_id: i32,
score: i16,
- auth: String
+ auth: String,
}
-
impl Perform<CommentResponse> for Oper<CreateComment> {
fn perform(&self) -> Result<CommentResponse, Error> {
let data: &CreateComment = &self.data;
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"))?,
};
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"))?;
}
// 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"))?;
}
let content_slurs_removed = remove_slurs(&data.content.to_owned());
removed: None,
deleted: None,
read: None,
- updated: None
+ updated: None,
};
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"))?,
};
// You like your own comment by default
comment_id: inserted_comment.id,
post_id: data.post_id,
user_id: user_id,
- score: 1
+ score: 1,
};
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"))?,
};
let comment_view = CommentView::read(&conn, inserted_comment.id, Some(user_id))?;
- Ok(
- CommentResponse {
- op: self.op.to_string(),
- comment: comment_view
- }
- )
+ Ok(CommentResponse {
+ op: self.op.to_string(),
+ comment: comment_view,
+ })
}
}
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"))?,
};
let user_id = claims.id;
// You are allowed to mark the comment as read even if you're banned.
if data.read.is_none() {
-
// Verify its the creator or a mod, or an admin
let mut editors: Vec<i32> = vec![data.creator_id];
editors.append(
- &mut CommunityModeratorView::for_community(&conn, orig_comment.community_id)
- ?
- .into_iter()
- .map(|m| m.user_id)
- .collect()
- );
- editors.append(
- &mut UserView::admins(&conn)
- ?
- .into_iter()
- .map(|a| a.id)
- .collect()
- );
+ &mut CommunityModeratorView::for_community(&conn, orig_comment.community_id)?
+ .into_iter()
+ .map(|m| m.user_id)
+ .collect(),
+ );
+ 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"))?;
}
// 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"))?;
}
// 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"))?;
}
-
}
let content_slurs_removed = remove_slurs(&data.content.to_owned());
removed: data.removed.to_owned(),
deleted: data.deleted.to_owned(),
read: data.read.to_owned(),
- updated: if data.read.is_some() { orig_comment.updated } else {Some(naive_now())}
+ updated: if data.read.is_some() {
+ orig_comment.updated
+ } else {
+ Some(naive_now())
+ },
};
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"))?,
};
// Mod tables
ModRemoveComment::create(&conn, &form)?;
}
-
let comment_view = CommentView::read(&conn, data.edit_id, Some(user_id))?;
- Ok(
- CommentResponse {
- op: self.op.to_string(),
- comment: comment_view
- }
- )
-
+ Ok(CommentResponse {
+ op: self.op.to_string(),
+ comment: comment_view,
+ })
}
}
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"))?,
};
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"))?,
};
} 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"))?,
};
}
let comment_view = CommentView::read(&conn, data.comment_id, Some(user_id))?;
- Ok(
- CommentResponse {
- op: self.op.to_string(),
- comment: comment_view
- }
- )
+ Ok(CommentResponse {
+ op: self.op.to_string(),
+ comment: comment_view,
+ })
}
}
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"))?,
};
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"))?;
}
// 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"))?;
}
let like_form = CommentLikeForm {
comment_id: data.comment_id,
post_id: data.post_id,
user_id: user_id,
- score: data.score
+ score: data.score,
};
// Remove any likes first
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"))?,
};
}
// Have to refetch the comment to get the current state
let liked_comment = CommentView::read(&conn, data.comment_id, Some(user_id))?;
- Ok(
- CommentResponse {
- op: self.op.to_string(),
- comment: liked_comment
- }
- )
+ Ok(CommentResponse {
+ op: self.op.to_string(),
+ comment: liked_comment,
+ })
}
}
pub struct GetCommunity {
id: Option<i32>,
name: Option<String>,
- auth: Option<String>
+ auth: Option<String>,
}
#[derive(Serialize, Deserialize)]
admins: Vec<UserView>,
}
-
#[derive(Serialize, Deserialize)]
pub struct CreateCommunity {
name: String,
description: Option<String>,
category_id: i32,
nsfw: bool,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct CommunityResponse {
op: String,
- pub community: CommunityView
+ pub community: CommunityView,
}
#[derive(Serialize, Deserialize)]
sort: String,
page: Option<i64>,
limit: Option<i64>,
- auth: Option<String>
+ auth: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct ListCommunitiesResponse {
op: String,
- communities: Vec<CommunityView>
+ communities: Vec<CommunityView>,
}
#[derive(Serialize, Deserialize, Clone)]
ban: bool,
reason: Option<String>,
expires: Option<i64>,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize)]
pub community_id: i32,
user_id: i32,
added: bool,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize)]
nsfw: bool,
reason: Option<String>,
expires: Option<i64>,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize)]
pub struct FollowCommunity {
community_id: i32,
follow: bool,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize)]
pub struct GetFollowedCommunities {
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize)]
pub struct GetFollowedCommunitiesResponse {
op: String,
- communities: Vec<CommunityFollowerView>
+ communities: Vec<CommunityFollowerView>,
}
#[derive(Serialize, Deserialize)]
pub struct TransferCommunity {
community_id: i32,
user_id: i32,
- auth: String
+ auth: String,
}
impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
let conn = establish_connection();
let user_id: Option<i32> = match &data.auth {
- Some(auth) => {
- match Claims::decode(&auth) {
- Ok(claims) => {
- let user_id = claims.claims.id;
- Some(user_id)
- }
- Err(_e) => None
+ Some(auth) => match Claims::decode(&auth) {
+ Ok(claims) => {
+ let user_id = claims.claims.id;
+ Some(user_id)
}
- }
- None => None
+ Err(_e) => None,
+ },
+ None => None,
};
let community_id = match data.id {
Some(id) => id,
- None => Community::read_from_name(&conn, data.name.to_owned().unwrap_or("main".to_string()))?.id
+ None => {
+ Community::read_from_name(&conn, data.name.to_owned().unwrap_or("main".to_string()))?.id
+ }
};
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"))?,
};
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"))?,
};
let site_creator_id = Site::read(&conn, 1)?.creator_id;
admins.insert(0, creator_user);
// Return the jwt
- Ok(
- GetCommunityResponse {
- op: self.op.to_string(),
- community: community_view,
- moderators: moderators,
- admins: admins,
- }
- )
+ Ok(GetCommunityResponse {
+ op: self.op.to_string(),
+ community: community_view,
+ moderators: moderators,
+ admins: admins,
+ })
}
}
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"))?,
};
- 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"))?
- }
+ 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"))?;
+ }
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"))?;
}
// 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"))?,
};
let community_moderator_form = CommunityModeratorForm {
community_id: inserted_community.id,
- user_id: user_id
+ user_id: user_id,
};
- let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) {
- Ok(user) => user,
- Err(_e) => {
- return Err(APIError::err(&self.op, "community_moderator_already_exists"))?
- }
- };
+ let _inserted_community_moderator =
+ match CommunityModerator::join(&conn, &community_moderator_form) {
+ Ok(user) => user,
+ Err(_e) => {
+ return Err(APIError::err(
+ &self.op,
+ "community_moderator_already_exists",
+ ))?
+ }
+ };
let community_follower_form = CommunityFollowerForm {
community_id: inserted_community.id,
- user_id: user_id
+ user_id: user_id,
};
- 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"))?
- }
- };
+ 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"))?,
+ };
let community_view = CommunityView::read(&conn, inserted_community.id, Some(user_id))?;
- Ok(
- CommunityResponse {
- op: self.op.to_string(),
- community: community_view
- }
- )
+ Ok(CommunityResponse {
+ op: self.op.to_string(),
+ community: community_view,
+ })
}
}
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"))?;
}
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"))?,
};
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"))?;
}
// Verify its a mod
let mut editors: Vec<i32> = Vec::new();
editors.append(
- &mut CommunityModeratorView::for_community(&conn, data.edit_id)
- ?
- .into_iter()
- .map(|m| m.user_id)
- .collect()
- );
- editors.append(
- &mut UserView::admins(&conn)
- ?
- .into_iter()
- .map(|a| a.id)
- .collect()
- );
+ &mut CommunityModeratorView::for_community(&conn, data.edit_id)?
+ .into_iter()
+ .map(|m| m.user_id)
+ .collect(),
+ );
+ 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"))?;
}
let community_form = CommunityForm {
removed: data.removed.to_owned(),
deleted: data.deleted.to_owned(),
nsfw: data.nsfw,
- updated: Some(naive_now())
+ updated: Some(naive_now()),
};
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"))?,
};
// Mod tables
if let Some(removed) = data.removed.to_owned() {
let expires = match data.expires {
Some(time) => Some(naive_from_unix(time)),
- None => None
+ None => None,
};
let form = ModRemoveCommunityForm {
mod_user_id: user_id,
community_id: data.edit_id,
removed: Some(removed),
reason: data.reason.to_owned(),
- expires: expires
+ expires: expires,
};
ModRemoveCommunity::create(&conn, &form)?;
}
let community_view = CommunityView::read(&conn, data.edit_id, Some(user_id))?;
- Ok(
- CommunityResponse {
- op: self.op.to_string(),
- community: community_view
- }
- )
+ Ok(CommunityResponse {
+ op: self.op.to_string(),
+ community: community_view,
+ })
}
}
let conn = establish_connection();
let user_claims: Option<Claims> = match &data.auth {
- Some(auth) => {
- match Claims::decode(&auth) {
- Ok(claims) => {
- Some(claims.claims)
- }
- Err(_e) => None
- }
- }
- None => None
+ Some(auth) => match Claims::decode(&auth) {
+ Ok(claims) => Some(claims.claims),
+ Err(_e) => None,
+ },
+ None => None,
};
-
+
let user_id = match &user_claims {
Some(claims) => Some(claims.id),
- None => None
+ None => None,
};
let show_nsfw = match &user_claims {
Some(claims) => claims.show_nsfw,
- None => false
+ None => false,
};
let sort = SortType::from_str(&data.sort)?;
let communities: Vec<CommunityView> = CommunityView::list(
- &conn,
- &sort,
- user_id,
- show_nsfw,
- None,
- data.page,
- data.limit)?;
+ &conn, &sort, user_id, show_nsfw, None, data.page, data.limit,
+ )?;
// Return the jwt
- Ok(
- ListCommunitiesResponse {
- op: self.op.to_string(),
- communities: communities
- }
- )
+ Ok(ListCommunitiesResponse {
+ op: self.op.to_string(),
+ communities: communities,
+ })
}
}
-
impl Perform<CommunityResponse> for Oper<FollowCommunity> {
fn perform(&self) -> Result<CommunityResponse, Error> {
let data: &FollowCommunity = &self.data;
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"))?,
};
let user_id = claims.id;
let community_follower_form = CommunityFollowerForm {
community_id: data.community_id,
- user_id: user_id
+ user_id: user_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"))?,
};
} 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"))?,
};
}
let community_view = CommunityView::read(&conn, data.community_id, Some(user_id))?;
- Ok(
- CommunityResponse {
- op: self.op.to_string(),
- community: community_view
- }
- )
+ Ok(CommunityResponse {
+ op: self.op.to_string(),
+ community: community_view,
+ })
}
}
-
impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> {
fn perform(&self) -> Result<GetFollowedCommunitiesResponse, Error> {
let data: &GetFollowedCommunities = &self.data;
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"))?,
};
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"))?
- }
- };
+ 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"))?,
+ };
// Return the jwt
- Ok(
- GetFollowedCommunitiesResponse {
- op: self.op.to_string(),
- communities: communities
- }
- )
+ Ok(GetFollowedCommunitiesResponse {
+ op: self.op.to_string(),
+ communities: communities,
+ })
}
}
-
impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> {
fn perform(&self) -> Result<BanFromCommunityResponse, Error> {
let data: &BanFromCommunity = &self.data;
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"))?,
};
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"))?,
};
} 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"))?,
};
}
// Mod tables
let expires = match data.expires {
Some(time) => Some(naive_from_unix(time)),
- None => None
+ None => None,
};
let form = ModBanFromCommunityForm {
let user_view = UserView::read(&conn, data.user_id)?;
- Ok(
- BanFromCommunityResponse {
- op: self.op.to_string(),
- user: user_view,
- banned: data.ban
- }
- )
+ Ok(BanFromCommunityResponse {
+ op: self.op.to_string(),
+ user: user_view,
+ banned: data.ban,
+ })
}
}
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"))?,
};
let user_id = claims.id;
let community_moderator_form = CommunityModeratorForm {
community_id: data.community_id,
- user_id: data.user_id
+ user_id: data.user_id,
};
if data.added {
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",
+ ))?
}
};
} 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",
+ ))?
}
};
}
let moderators = CommunityModeratorView::for_community(&conn, data.community_id)?;
- Ok(
- AddModToCommunityResponse {
- op: self.op.to_string(),
- moderators: moderators,
- }
- )
+ Ok(AddModToCommunityResponse {
+ op: self.op.to_string(),
+ moderators: moderators,
+ })
}
}
-
+
impl Perform<GetCommunityResponse> for Oper<TransferCommunity> {
fn perform(&self) -> Result<GetCommunityResponse, Error> {
let data: &TransferCommunity = &self.data;
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"))?,
};
let user_id = claims.id;
let creator_user = admins.remove(creator_index);
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)
+ .collect::<Vec<i32>>()
+ .contains(&user_id)
+ {
+ return Err(APIError::err(&self.op, "not_an_admin"))?;
}
-
+
let community_form = CommunityForm {
name: read_community.name,
title: read_community.title,
removed: None,
deleted: None,
nsfw: read_community.nsfw,
- updated: Some(naive_now())
+ updated: Some(naive_now()),
};
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"))?,
};
// You also have to re-do the community_moderator table, reordering it.
let mut community_mods = CommunityModeratorView::for_community(&conn, data.community_id)?;
- let creator_index = community_mods.iter().position(|r| r.user_id == data.user_id).unwrap();
+ let creator_index = community_mods
+ .iter()
+ .position(|r| r.user_id == data.user_id)
+ .unwrap();
let creator_user = community_mods.remove(creator_index);
community_mods.insert(0, creator_user);
CommunityModerator::delete_for_community(&conn, data.community_id)?;
for cmod in &community_mods {
-
let community_moderator_form = CommunityModeratorForm {
community_id: cmod.community_id,
- user_id: cmod.user_id
+ user_id: cmod.user_id,
};
- let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) {
- Ok(user) => user,
- Err(_e) => {
- return Err(APIError::err(&self.op, "community_moderator_already_exists"))?
- }
- };
+ let _inserted_community_moderator =
+ match CommunityModerator::join(&conn, &community_moderator_form) {
+ Ok(user) => user,
+ Err(_e) => {
+ return Err(APIError::err(
+ &self.op,
+ "community_moderator_already_exists",
+ ))?
+ }
+ };
}
// Mod tables
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"))?,
};
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"))?,
};
-
// Return the jwt
- Ok(
- GetCommunityResponse {
- op: self.op.to_string(),
- community: community_view,
- moderators: moderators,
- admins: admins,
- }
- )
+ Ok(GetCommunityResponse {
+ op: self.op.to_string(),
+ community: community_view,
+ moderators: moderators,
+ admins: admins,
+ })
}
}
-use serde::{Deserialize, Serialize};
-use failure::Error;
-use crate::db::*;
-use crate::db::community::*;
-use crate::db::user::*;
-use crate::db::post::*;
+use crate::db::category::*;
use crate::db::comment::*;
-use crate::db::post_view::*;
use crate::db::comment_view::*;
-use crate::db::category::*;
+use crate::db::community::*;
use crate::db::community_view::*;
-use crate::db::user_view::*;
-use crate::db::moderator_views::*;
use crate::db::moderator::*;
-use crate::{has_slurs, remove_slurs, Settings, naive_now, naive_from_unix};
+use crate::db::moderator_views::*;
+use crate::db::post::*;
+use crate::db::post_view::*;
+use crate::db::user::*;
+use crate::db::user_view::*;
+use crate::db::*;
+use crate::{has_slurs, naive_from_unix, naive_now, remove_slurs, Settings};
+use failure::Error;
+use serde::{Deserialize, Serialize};
-pub mod user;
+pub mod comment;
pub mod community;
pub mod post;
-pub mod comment;
pub mod site;
+pub mod user;
-#[derive(EnumString,ToString,Debug)]
+#[derive(EnumString, ToString, Debug)]
pub enum UserOperation {
- Login, Register, CreateCommunity, CreatePost, ListCommunities, ListCategories, GetPost, GetCommunity, CreateComment, EditComment, SaveComment, CreateCommentLike, GetPosts, CreatePostLike, EditPost, SavePost, EditCommunity, FollowCommunity, GetFollowedCommunities, GetUserDetails, GetReplies, GetModlog, BanFromCommunity, AddModToCommunity, CreateSite, EditSite, GetSite, AddAdmin, BanUser, Search, MarkAllAsRead, SaveUserSettings, TransferCommunity, TransferSite
+ Login,
+ Register,
+ CreateCommunity,
+ CreatePost,
+ ListCommunities,
+ ListCategories,
+ GetPost,
+ GetCommunity,
+ CreateComment,
+ EditComment,
+ SaveComment,
+ CreateCommentLike,
+ GetPosts,
+ CreatePostLike,
+ EditPost,
+ SavePost,
+ EditCommunity,
+ FollowCommunity,
+ GetFollowedCommunities,
+ GetUserDetails,
+ GetReplies,
+ GetModlog,
+ BanFromCommunity,
+ AddModToCommunity,
+ CreateSite,
+ EditSite,
+ GetSite,
+ AddAdmin,
+ BanUser,
+ Search,
+ MarkAllAsRead,
+ SaveUserSettings,
+ TransferCommunity,
+ TransferSite,
}
#[derive(Fail, Debug)]
pub struct Oper<T> {
op: UserOperation,
- data: T
+ data: T,
}
-impl <T> Oper<T> {
+impl<T> Oper<T> {
pub fn new(op: UserOperation, data: T) -> Oper<T> {
- Oper {
- op: op,
- data: data
- }
+ Oper { op: op, data: data }
}
}
pub trait Perform<T> {
- fn perform(&self) -> Result<T, Error> where T: Sized;
+ fn perform(&self) -> Result<T, Error>
+ where
+ T: Sized;
}
body: Option<String>,
nsfw: bool,
community_id: i32,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct PostResponse {
op: String,
- pub post: PostView
+ pub post: PostView,
}
-
#[derive(Serialize, Deserialize)]
pub struct GetPost {
pub id: i32,
- auth: Option<String>
+ auth: Option<String>,
}
#[derive(Serialize, Deserialize)]
page: Option<i64>,
limit: Option<i64>,
community_id: Option<i32>,
- auth: Option<String>
+ auth: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct CreatePostLike {
post_id: i32,
score: i16,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize)]
pub struct CreatePostLikeResponse {
op: String,
- post: PostView
+ post: PostView,
}
-
#[derive(Serialize, Deserialize)]
pub struct EditPost {
pub edit_id: i32,
nsfw: bool,
locked: Option<bool>,
reason: Option<String>,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize)]
pub struct SavePost {
post_id: i32,
save: bool,
- auth: String
+ auth: String,
}
impl Perform<PostResponse> for Oper<CreatePost> {
let data: &CreatePost = &self.data;
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"))?,
};
- if has_slurs(&data.name) ||
- (data.body.is_some() && has_slurs(&data.body.to_owned().unwrap())) {
- return Err(APIError::err(&self.op, "no_slurs"))?
- }
+ if has_slurs(&data.name) || (data.body.is_some() && has_slurs(&data.body.to_owned().unwrap())) {
+ return Err(APIError::err(&self.op, "no_slurs"))?;
+ }
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"))?;
}
// 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"))?;
}
let post_form = PostForm {
deleted: None,
nsfw: data.nsfw,
locked: None,
- updated: None
+ updated: None,
};
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"))?,
};
// They like their own post by default
let like_form = PostLikeForm {
post_id: inserted_post.id,
user_id: user_id,
- score: 1
+ score: 1,
};
// 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"))?,
};
// 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"))?,
};
- Ok(
- PostResponse {
- op: self.op.to_string(),
- post: post_view
- }
- )
+ Ok(PostResponse {
+ op: self.op.to_string(),
+ post: post_view,
+ })
}
}
let conn = establish_connection();
let user_id: Option<i32> = match &data.auth {
- Some(auth) => {
- match Claims::decode(&auth) {
- Ok(claims) => {
- let user_id = claims.claims.id;
- Some(user_id)
- }
- Err(_e) => None
+ Some(auth) => match Claims::decode(&auth) {
+ Ok(claims) => {
+ let user_id = claims.claims.id;
+ Some(user_id)
}
- }
- None => None
+ Err(_e) => None,
+ },
+ None => None,
};
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"))?,
};
- let comments = CommentView::list(&conn, &SortType::New, Some(data.id), None, None, user_id, false, None, Some(9999))?;
+ let comments = CommentView::list(
+ &conn,
+ &SortType::New,
+ Some(data.id),
+ None,
+ None,
+ user_id,
+ false,
+ None,
+ Some(9999),
+ )?;
let community = CommunityView::read(&conn, post_view.community_id, user_id)?;
admins.insert(0, creator_user);
// Return the jwt
- Ok(
- GetPostResponse {
- op: self.op.to_string(),
- post: post_view,
- comments: comments,
- community: community,
- moderators: moderators,
- admins: admins,
- }
- )
+ Ok(GetPostResponse {
+ op: self.op.to_string(),
+ post: post_view,
+ comments: comments,
+ community: community,
+ moderators: moderators,
+ admins: admins,
+ })
}
}
-
impl Perform<GetPostsResponse> for Oper<GetPosts> {
fn perform(&self) -> Result<GetPostsResponse, Error> {
let data: &GetPosts = &self.data;
let conn = establish_connection();
let user_claims: Option<Claims> = match &data.auth {
- Some(auth) => {
- match Claims::decode(&auth) {
- Ok(claims) => {
- Some(claims.claims)
- }
- Err(_e) => None
- }
- }
- None => None
+ Some(auth) => match Claims::decode(&auth) {
+ Ok(claims) => Some(claims.claims),
+ Err(_e) => None,
+ },
+ None => None,
};
-
+
let user_id = match &user_claims {
Some(claims) => Some(claims.id),
- None => None
+ None => None,
};
let show_nsfw = match &user_claims {
Some(claims) => claims.show_nsfw,
- None => false
+ None => false,
};
let type_ = PostListingType::from_str(&data.type_)?;
let sort = SortType::from_str(&data.sort)?;
let posts = match PostView::list(
- &conn,
- type_,
- &sort,
- data.community_id,
+ &conn,
+ type_,
+ &sort,
+ data.community_id,
None,
None,
None,
- user_id,
+ user_id,
show_nsfw,
- false,
- false,
- data.page,
- data.limit) {
+ false,
+ false,
+ data.page,
+ data.limit,
+ ) {
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"))?,
};
- Ok(
- GetPostsResponse {
- op: self.op.to_string(),
- posts: posts
- }
- )
+ Ok(GetPostsResponse {
+ op: self.op.to_string(),
+ posts: posts,
+ })
}
}
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"))?,
};
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"))?;
}
// 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"))?;
}
let like_form = PostLikeForm {
post_id: data.post_id,
user_id: user_id,
- score: data.score
+ score: data.score,
};
// Remove any likes first
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"))?,
};
}
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"))?,
};
// just output the score
- Ok(
- CreatePostLikeResponse {
- op: self.op.to_string(),
- post: post_view
- }
- )
+ Ok(CreatePostLikeResponse {
+ op: self.op.to_string(),
+ post: post_view,
+ })
}
}
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"))?
- }
+ if has_slurs(&data.name) || (data.body.is_some() && has_slurs(&data.body.to_owned().unwrap())) {
+ return Err(APIError::err(&self.op, "no_slurs"))?;
+ }
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"))?,
};
let user_id = claims.id;
// Verify its the creator or a mod or admin
let mut editors: Vec<i32> = vec![data.creator_id];
editors.append(
- &mut CommunityModeratorView::for_community(&conn, data.community_id)
- ?
- .into_iter()
- .map(|m| m.user_id)
- .collect()
- );
- editors.append(
- &mut UserView::admins(&conn)
- ?
- .into_iter()
- .map(|a| a.id)
- .collect()
- );
+ &mut CommunityModeratorView::for_community(&conn, data.community_id)?
+ .into_iter()
+ .map(|m| m.user_id)
+ .collect(),
+ );
+ 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"))?;
}
// 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"))?;
}
// 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"))?;
}
let post_form = PostForm {
deleted: data.deleted.to_owned(),
nsfw: data.nsfw,
locked: data.locked.to_owned(),
- updated: Some(naive_now())
+ updated: Some(naive_now()),
};
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"))?,
};
// Mod tables
let post_view = PostView::read(&conn, data.edit_id, Some(user_id))?;
- Ok(
- PostResponse {
- op: self.op.to_string(),
- post: post_view
- }
- )
+ Ok(PostResponse {
+ op: self.op.to_string(),
+ post: post_view,
+ })
}
}
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"))?,
};
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"))?,
};
} 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"))?,
};
}
let post_view = PostView::read(&conn, data.post_id, Some(user_id))?;
- Ok(
- PostResponse {
- op: self.op.to_string(),
- post: post_view
- }
- )
+ Ok(PostResponse {
+ op: self.op.to_string(),
+ post: post_view,
+ })
}
}
#[derive(Serialize, Deserialize)]
pub struct ListCategoriesResponse {
op: String,
- categories: Vec<Category>
+ categories: Vec<Category>,
}
#[derive(Serialize, Deserialize)]
added: Vec<ModAddView>,
}
-
#[derive(Serialize, Deserialize)]
pub struct CreateSite {
name: String,
description: Option<String>,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize)]
pub struct EditSite {
name: String,
description: Option<String>,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
pub struct TransferSite {
user_id: i32,
- auth: String
+ auth: String,
}
impl Perform<ListCategoriesResponse> for Oper<ListCategories> {
let categories: Vec<Category> = Category::list_all(&conn)?;
// Return the jwt
- Ok(
- ListCategoriesResponse {
- op: self.op.to_string(),
- categories: categories
- }
- )
+ Ok(ListCategoriesResponse {
+ op: self.op.to_string(),
+ categories: categories,
+ })
}
}
let data: &GetModlog = &self.data;
let conn = establish_connection();
- let removed_posts = ModRemovePostView::list(&conn, data.community_id, data.mod_user_id, data.page, data.limit)?;
- let locked_posts = ModLockPostView::list(&conn, data.community_id, data.mod_user_id, data.page, data.limit)?;
- let removed_comments = ModRemoveCommentView::list(&conn, data.community_id, data.mod_user_id, data.page, data.limit)?;
- let banned_from_community = ModBanFromCommunityView::list(&conn, data.community_id, data.mod_user_id, data.page, data.limit)?;
- let added_to_community = ModAddCommunityView::list(&conn, data.community_id, data.mod_user_id, data.page, data.limit)?;
+ let removed_posts = ModRemovePostView::list(
+ &conn,
+ data.community_id,
+ data.mod_user_id,
+ data.page,
+ data.limit,
+ )?;
+ let locked_posts = ModLockPostView::list(
+ &conn,
+ data.community_id,
+ data.mod_user_id,
+ data.page,
+ data.limit,
+ )?;
+ let removed_comments = ModRemoveCommentView::list(
+ &conn,
+ data.community_id,
+ data.mod_user_id,
+ data.page,
+ data.limit,
+ )?;
+ let banned_from_community = ModBanFromCommunityView::list(
+ &conn,
+ data.community_id,
+ data.mod_user_id,
+ data.page,
+ data.limit,
+ )?;
+ let added_to_community = ModAddCommunityView::list(
+ &conn,
+ data.community_id,
+ data.mod_user_id,
+ data.page,
+ data.limit,
+ )?;
// These arrays are only for the full modlog, when a community isn't given
let mut removed_communities = 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)?;
+ 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)?;
}
// Return the jwt
- Ok(
- GetModlogResponse {
- op: self.op.to_string(),
- removed_posts: removed_posts,
- locked_posts: locked_posts,
- removed_comments: removed_comments,
- removed_communities: removed_communities,
- banned_from_community: banned_from_community,
- banned: banned,
- added_to_community: added_to_community,
- added: added,
- }
- )
+ Ok(GetModlogResponse {
+ op: self.op.to_string(),
+ removed_posts: removed_posts,
+ locked_posts: locked_posts,
+ removed_comments: removed_comments,
+ removed_communities: removed_communities,
+ banned_from_community: banned_from_community,
+ banned: banned,
+ added_to_community: added_to_community,
+ added: added,
+ })
}
}
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"))?,
};
- if has_slurs(&data.name) ||
- (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) {
- return Err(APIError::err(&self.op, "no_slurs"))?
- }
+ if has_slurs(&data.name)
+ || (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap()))
+ {
+ return Err(APIError::err(&self.op, "no_slurs"))?;
+ }
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"))?;
}
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"))?,
};
let site_view = SiteView::read(&conn)?;
- Ok(
- SiteResponse {
- op: self.op.to_string(),
- site: site_view,
- }
- )
+ Ok(SiteResponse {
+ op: self.op.to_string(),
+ site: site_view,
+ })
}
}
-
impl Perform<SiteResponse> for Oper<EditSite> {
fn perform(&self) -> Result<SiteResponse, Error> {
let data: &EditSite = &self.data;
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"))?,
};
- if has_slurs(&data.name) ||
- (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) {
- return Err(APIError::err(&self.op, "no_slurs"))?
- }
+ if has_slurs(&data.name)
+ || (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap()))
+ {
+ return Err(APIError::err(&self.op, "no_slurs"))?;
+ }
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"))?
+ return Err(APIError::err(&self.op, "not_an_admin"))?;
}
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"))?,
};
let site_view = SiteView::read(&conn)?;
- Ok(
- SiteResponse {
- op: self.op.to_string(),
- site: site_view,
- }
- )
+ Ok(SiteResponse {
+ op: self.op.to_string(),
+ site: site_view,
+ })
}
}
// It can return a null site in order to redirect
let site_view = match Site::read(&conn, 1) {
Ok(_site) => Some(SiteView::read(&conn)?),
- Err(_e) => None
+ Err(_e) => None,
};
let mut admins = UserView::admins(&conn)?;
let banned = UserView::banned(&conn)?;
- Ok(
- GetSiteResponse {
- op: self.op.to_string(),
- site: site_view,
- admins: admins,
- banned: banned,
- }
- )
+ Ok(GetSiteResponse {
+ op: self.op.to_string(),
+ site: site_view,
+ admins: admins,
+ banned: banned,
+ })
}
}
match type_ {
SearchType::Posts => {
posts = PostView::list(
- &conn,
- PostListingType::All,
- &sort,
- data.community_id,
+ &conn,
+ PostListingType::All,
+ &sort,
+ data.community_id,
None,
Some(data.q.to_owned()),
None,
- None,
+ None,
true,
- false,
- false,
- data.page,
- data.limit)?;
- },
+ false,
+ false,
+ data.page,
+ data.limit,
+ )?;
+ }
SearchType::Comments => {
comments = CommentView::list(
- &conn,
- &sort,
- None,
- None,
+ &conn,
+ &sort,
+ None,
+ None,
Some(data.q.to_owned()),
None,
- false,
+ false,
data.page,
- data.limit)?;
- },
+ data.limit,
+ )?;
+ }
SearchType::Communities => {
communities = CommunityView::list(
- &conn,
- &sort,
- None,
+ &conn,
+ &sort,
+ None,
true,
Some(data.q.to_owned()),
- data.page,
- data.limit)?;
- },
+ data.page,
+ data.limit,
+ )?;
+ }
SearchType::Users => {
- users = UserView::list(
- &conn,
- &sort,
- Some(data.q.to_owned()),
- data.page,
- data.limit)?;
- },
+ users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?;
+ }
SearchType::All => {
posts = PostView::list(
- &conn,
- PostListingType::All,
- &sort,
- data.community_id,
+ &conn,
+ PostListingType::All,
+ &sort,
+ data.community_id,
None,
Some(data.q.to_owned()),
None,
- None,
+ None,
true,
- false,
- false,
- data.page,
- data.limit)?;
+ false,
+ false,
+ data.page,
+ data.limit,
+ )?;
comments = CommentView::list(
- &conn,
- &sort,
- None,
- None,
+ &conn,
+ &sort,
+ None,
+ None,
Some(data.q.to_owned()),
None,
- false,
+ false,
data.page,
- data.limit)?;
+ data.limit,
+ )?;
communities = CommunityView::list(
- &conn,
- &sort,
- None,
+ &conn,
+ &sort,
+ None,
true,
Some(data.q.to_owned()),
- data.page,
- data.limit)?;
- users = UserView::list(
- &conn,
- &sort,
- Some(data.q.to_owned()),
- data.page,
- data.limit)?;
- },
+ data.page,
+ data.limit,
+ )?;
+ users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?;
+ }
SearchType::Url => {
posts = PostView::list(
- &conn,
- PostListingType::All,
- &sort,
- data.community_id,
+ &conn,
+ PostListingType::All,
+ &sort,
+ data.community_id,
None,
None,
Some(data.q.to_owned()),
- None,
+ None,
true,
- false,
- false,
- data.page,
- data.limit)?;
+ false,
+ false,
+ data.page,
+ data.limit,
+ )?;
}
};
-
// Return the jwt
- Ok(
- SearchResponse {
- op: self.op.to_string(),
- type_: data.type_.to_owned(),
- comments: comments,
- posts: posts,
- communities: communities,
- users: users,
- }
- )
+ Ok(SearchResponse {
+ op: self.op.to_string(),
+ type_: data.type_.to_owned(),
+ comments: comments,
+ posts: posts,
+ communities: communities,
+ users: users,
+ })
}
}
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"))?,
};
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"))?;
}
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"))?,
};
// Mod tables
let site_view = SiteView::read(&conn)?;
let mut admins = UserView::admins(&conn)?;
- let creator_index = admins.iter().position(|r| r.id == site_view.creator_id).unwrap();
+ let creator_index = admins
+ .iter()
+ .position(|r| r.id == site_view.creator_id)
+ .unwrap();
let creator_user = admins.remove(creator_index);
admins.insert(0, creator_user);
let banned = UserView::banned(&conn)?;
- Ok(
- GetSiteResponse {
- op: self.op.to_string(),
- site: Some(site_view),
- admins: admins,
- banned: banned,
- }
- )
+ Ok(GetSiteResponse {
+ op: self.op.to_string(),
+ site: Some(site_view),
+ admins: admins,
+ banned: banned,
+ })
}
}
-
use super::*;
+use bcrypt::verify;
use std::str::FromStr;
-use bcrypt::{verify};
#[derive(Serialize, Deserialize, Debug)]
pub struct Login {
username_or_email: String,
- password: String
+ password: String,
}
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
pub struct LoginResponse {
op: String,
- jwt: String
+ jwt: String,
}
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
pub struct MarkAllAsRead {
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize)]
pub struct AddAdmin {
user_id: i32,
added: bool,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize)]
ban: bool,
reason: Option<String>,
expires: Option<i64>,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize)]
page: Option<i64>,
limit: Option<i64>,
unread_only: bool,
- auth: String
+ auth: String,
}
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",
+ ))?
+ }
};
// 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"))?;
}
// Return the jwt
- Ok(
- LoginResponse {
- op: self.op.to_string(),
- jwt: user.jwt()
- }
- )
+ Ok(LoginResponse {
+ op: self.op.to_string(),
+ jwt: user.jwt(),
+ })
}
}
-
impl Perform<LoginResponse> for Oper<Register> {
fn perform(&self) -> Result<LoginResponse, Error> {
let data: &Register = &self.data;
// Make sure passwords match
if &data.password != &data.password_verify {
- return Err(APIError::err(&self.op, "passwords_dont_match"))?
+ return Err(APIError::err(&self.op, "passwords_dont_match"))?;
}
if has_slurs(&data.username) {
- return Err(APIError::err(&self.op, "no_slurs"))?
+ return Err(APIError::err(&self.op, "no_slurs"))?;
}
// Make sure there are no admins
if data.admin && UserView::admins(&conn)?.len() > 0 {
- return Err(APIError::err(&self.op, "admin_already_created"))?
+ return Err(APIError::err(&self.op, "admin_already_created"))?;
}
// 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"))?,
};
// Create the main community if it doesn't exist
user_id: inserted_user.id,
};
- 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"))?
- }
- };
+ 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"))?,
+ };
// If its an admin, add them as a mod and follower to main
if data.admin {
user_id: inserted_user.id,
};
- let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) {
- Ok(user) => user,
- Err(_e) => {
- return Err(APIError::err(&self.op, "community_moderator_already_exists"))?
- }
- };
-
+ let _inserted_community_moderator =
+ match CommunityModerator::join(&conn, &community_moderator_form) {
+ Ok(user) => user,
+ Err(_e) => {
+ return Err(APIError::err(
+ &self.op,
+ "community_moderator_already_exists",
+ ))?
+ }
+ };
}
// Return the jwt
- Ok(
- LoginResponse {
- op: self.op.to_string(),
- jwt: inserted_user.jwt()
- }
- )
+ Ok(LoginResponse {
+ op: self.op.to_string(),
+ jwt: inserted_user.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"))?,
};
let user_id = claims.id;
-
+
let read_user = User_::read(&conn, user_id)?;
let user_form = UserForm {
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"))?,
};
// Return the jwt
- Ok(
- LoginResponse {
- op: self.op.to_string(),
- jwt: updated_user.jwt()
- }
- )
+ Ok(LoginResponse {
+ op: self.op.to_string(),
+ jwt: updated_user.jwt(),
+ })
}
}
let conn = establish_connection();
let user_claims: Option<Claims> = match &data.auth {
- Some(auth) => {
- match Claims::decode(&auth) {
- Ok(claims) => {
- Some(claims.claims)
- }
- Err(_e) => None
- }
- }
- None => None
+ Some(auth) => match Claims::decode(&auth) {
+ Ok(claims) => Some(claims.claims),
+ Err(_e) => None,
+ },
+ None => None,
};
-
+
let user_id = match &user_claims {
Some(claims) => Some(claims.id),
- None => None
+ None => None,
};
let show_nsfw = match &user_claims {
Some(claims) => claims.show_nsfw,
- None => false
+ None => false,
};
//TODO add save
let user_details_id = match data.user_id {
Some(id) => id,
- None => User_::read_from_name(&conn, data.username.to_owned().unwrap_or("admin".to_string()))?.id
+ None => {
+ User_::read_from_name(
+ &conn,
+ data.username.to_owned().unwrap_or("admin".to_string()),
+ )?
+ .id
+ }
};
let user_view = UserView::read(&conn, user_details_id)?;
// If its saved only, you don't care what creator it was
let posts = if data.saved_only {
PostView::list(
- &conn,
- PostListingType::All,
- &sort,
- data.community_id,
- None,
+ &conn,
+ PostListingType::All,
+ &sort,
+ data.community_id,
+ None,
None,
None,
- Some(user_details_id),
+ Some(user_details_id),
show_nsfw,
- data.saved_only,
- false,
- data.page,
- data.limit)?
+ data.saved_only,
+ false,
+ data.page,
+ data.limit,
+ )?
} else {
PostView::list(
- &conn,
- PostListingType::All,
- &sort,
- data.community_id,
- Some(user_details_id),
- None,
+ &conn,
+ PostListingType::All,
+ &sort,
+ data.community_id,
+ Some(user_details_id),
None,
- user_id,
+ None,
+ user_id,
show_nsfw,
- data.saved_only,
- false,
- data.page,
- data.limit)?
+ data.saved_only,
+ false,
+ data.page,
+ data.limit,
+ )?
};
let comments = if data.saved_only {
CommentView::list(
- &conn,
- &sort,
- None,
- None,
- None,
- Some(user_details_id),
- data.saved_only,
- data.page,
- data.limit)?
+ &conn,
+ &sort,
+ None,
+ None,
+ None,
+ Some(user_details_id),
+ data.saved_only,
+ data.page,
+ data.limit,
+ )?
} else {
CommentView::list(
- &conn,
- &sort,
- None,
- Some(user_details_id),
- None,
- user_id,
- data.saved_only,
- data.page,
- data.limit)?
+ &conn,
+ &sort,
+ None,
+ Some(user_details_id),
+ None,
+ user_id,
+ data.saved_only,
+ data.page,
+ data.limit,
+ )?
};
let follows = CommunityFollowerView::for_user(&conn, user_details_id)?;
let moderates = CommunityModeratorView::for_user(&conn, user_details_id)?;
// Return the jwt
- Ok(
- GetUserDetailsResponse {
- op: self.op.to_string(),
- user: user_view,
- follows: follows,
- moderates: moderates,
- comments: comments,
- posts: posts,
- }
- )
+ Ok(GetUserDetailsResponse {
+ op: self.op.to_string(),
+ user: user_view,
+ follows: follows,
+ moderates: moderates,
+ comments: comments,
+ posts: posts,
+ })
}
}
-
impl Perform<AddAdminResponse> for Oper<AddAdmin> {
fn perform(&self) -> Result<AddAdminResponse, Error> {
let data: &AddAdmin = &self.data;
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"))?,
};
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"))?
+ return Err(APIError::err(&self.op, "not_an_admin"))?;
}
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"))?,
};
// Mod tables
let creator_user = admins.remove(creator_index);
admins.insert(0, creator_user);
- Ok(
- AddAdminResponse {
- op: self.op.to_string(),
- admins: admins,
- }
- )
+ Ok(AddAdminResponse {
+ op: self.op.to_string(),
+ admins: admins,
+ })
}
}
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"))?,
};
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"))?
+ return Err(APIError::err(&self.op, "not_an_admin"))?;
}
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"))?,
};
// Mod tables
let expires = match data.expires {
Some(time) => Some(naive_from_unix(time)),
- None => None
+ None => None,
};
let form = ModBanForm {
let user_view = UserView::read(&conn, data.user_id)?;
- Ok(
- BanUserResponse {
- op: self.op.to_string(),
- user: user_view,
- banned: data.ban
- }
- )
-
+ Ok(BanUserResponse {
+ op: self.op.to_string(),
+ user: user_view,
+ banned: data.ban,
+ })
}
}
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"))?,
};
let user_id = claims.id;
let sort = SortType::from_str(&data.sort)?;
- let replies = ReplyView::get_replies(&conn, user_id, &sort, data.unread_only, data.page, data.limit)?;
+ let replies = ReplyView::get_replies(
+ &conn,
+ user_id,
+ &sort,
+ data.unread_only,
+ data.page,
+ data.limit,
+ )?;
// Return the jwt
- Ok(
- GetRepliesResponse {
- op: self.op.to_string(),
- replies: replies,
- }
- )
+ Ok(GetRepliesResponse {
+ op: self.op.to_string(),
+ replies: replies,
+ })
}
}
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"))?,
};
let user_id = claims.id;
removed: None,
deleted: None,
read: Some(true),
- updated: reply.to_owned().updated
+ updated: reply.to_owned().updated,
};
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"))?,
};
}
let replies = ReplyView::get_replies(&conn, user_id, &SortType::New, true, Some(1), Some(999))?;
- Ok(
- GetRepliesResponse {
- op: self.op.to_string(),
- replies: replies,
- }
- )
+ Ok(GetRepliesResponse {
+ op: self.op.to_string(),
+ replies: replies,
+ })
}
}
extern crate activitypub;
-use self::activitypub::{context, actor::Person};
+use self::activitypub::{actor::Person, context};
use crate::db::user::User_;
impl User_ {
pub fn person(&self) -> Person {
- use crate::{Settings, to_datetime_utc};
+ use crate::{to_datetime_utc, Settings};
let base_url = &format!("{}/user/{}", Settings::get().api_endpoint(), self.name);
- let mut person = Person::default();
+ let mut person = Person::default();
person.object_props.set_context_object(context()).ok();
person.object_props.set_id_string(base_url.to_string()).ok();
- person.object_props.set_name_string(self.name.to_owned()).ok();
- person.object_props.set_published_utctime(to_datetime_utc(self.published)).ok();
+ person
+ .object_props
+ .set_name_string(self.name.to_owned())
+ .ok();
+ person
+ .object_props
+ .set_published_utctime(to_datetime_utc(self.published))
+ .ok();
if let Some(i) = self.updated {
- person.object_props.set_updated_utctime(to_datetime_utc(i)).ok();
+ person
+ .object_props
+ .set_updated_utctime(to_datetime_utc(i))
+ .ok();
}
// person.object_props.summary = self.summary;
- person.ap_actor_props.set_inbox_string(format!("{}/inbox", &base_url)).ok();
- person.ap_actor_props.set_outbox_string(format!("{}/outbox", &base_url)).ok();
- person.ap_actor_props.set_following_string(format!("{}/following", &base_url)).ok();
- person.ap_actor_props.set_liked_string(format!("{}/liked", &base_url)).ok();
+ person
+ .ap_actor_props
+ .set_inbox_string(format!("{}/inbox", &base_url))
+ .ok();
+ person
+ .ap_actor_props
+ .set_outbox_string(format!("{}/outbox", &base_url))
+ .ok();
+ person
+ .ap_actor_props
+ .set_following_string(format!("{}/following", &base_url))
+ .ok();
+ person
+ .ap_actor_props
+ .set_liked_string(format!("{}/liked", &base_url))
+ .ok();
if let Some(i) = &self.preferred_username {
- person.ap_actor_props.set_preferred_username_string(i.to_string()).ok();
+ person
+ .ap_actor_props
+ .set_preferred_username_string(i.to_string())
+ .ok();
}
person
};
let person = expected_user.person();
- assert_eq!("rrr/api/v1/user/thom", person.object_props.id_string().unwrap());
+ assert_eq!(
+ "rrr/api/v1/user/thom",
+ person.object_props.id_string().unwrap()
+ );
let json = serde_json::to_string_pretty(&person).unwrap();
println!("{}", json);
-
}
}
-
-use crate::schema::{category};
-use crate::schema::category::dsl::*;
use super::*;
+use crate::schema::category;
+use crate::schema::category::dsl::*;
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
-#[table_name="category"]
+#[table_name = "category"]
pub struct Category {
pub id: i32,
- pub name: String
+ pub name: String,
}
#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
-#[table_name="category"]
+#[table_name = "category"]
pub struct CategoryForm {
pub name: String,
}
impl Crud<CategoryForm> for Category {
fn read(conn: &PgConnection, category_id: i32) -> Result<Self, Error> {
- category.find(category_id)
- .first::<Self>(conn)
+ category.find(category_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, category_id: i32) -> Result<usize, Error> {
- diesel::delete(category.find(category_id))
- .execute(conn)
+ diesel::delete(category.find(category_id)).execute(conn)
}
fn create(conn: &PgConnection, new_category: &CategoryForm) -> Result<Self, Error> {
- insert_into(category)
- .values(new_category)
- .get_result::<Self>(conn)
+ insert_into(category)
+ .values(new_category)
+ .get_result::<Self>(conn)
}
- fn update(conn: &PgConnection, category_id: i32, new_category: &CategoryForm) -> Result<Self, Error> {
+ fn update(
+ conn: &PgConnection,
+ category_id: i32,
+ new_category: &CategoryForm,
+ ) -> Result<Self, Error> {
diesel::update(category.find(category_id))
.set(new_category)
.get_result::<Self>(conn)
#[cfg(test)]
mod tests {
use super::*;
- #[test]
+ #[test]
fn test_crud() {
let conn = establish_connection();
let categories = Category::list_all(&conn).unwrap();
let expected_first_category = Category {
id: 1,
- name: "Discussion".into()
+ name: "Discussion".into(),
};
assert_eq!(expected_first_category, categories[0]);
-use crate::schema::{comment, comment_like, comment_saved};
-use super::*;
use super::post::Post;
+use super::*;
+use crate::schema::{comment, comment_like, comment_saved};
// WITH RECURSIVE MyTree AS (
// SELECT * FROM comment WHERE parent_id IS NULL
#[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
#[belongs_to(Post)]
-#[table_name="comment"]
+#[table_name = "comment"]
pub struct Comment {
pub id: i32,
pub creator_id: i32,
}
#[derive(Insertable, AsChangeset, Clone)]
-#[table_name="comment"]
+#[table_name = "comment"]
pub struct CommentForm {
pub creator_id: i32,
pub post_id: i32,
impl Crud<CommentForm> for Comment {
fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
use crate::schema::comment::dsl::*;
- comment.find(comment_id)
- .first::<Self>(conn)
+ comment.find(comment_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, comment_id: i32) -> Result<usize, Error> {
use crate::schema::comment::dsl::*;
- diesel::delete(comment.find(comment_id))
- .execute(conn)
+ diesel::delete(comment.find(comment_id)).execute(conn)
}
fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
.get_result::<Self>(conn)
}
- fn update(conn: &PgConnection, comment_id: i32, comment_form: &CommentForm) -> Result<Self, Error> {
+ fn update(
+ conn: &PgConnection,
+ comment_id: i32,
+ comment_form: &CommentForm,
+ ) -> Result<Self, Error> {
use crate::schema::comment::dsl::*;
diesel::update(comment.find(comment_id))
.set(comment_form)
}
#[derive(Insertable, AsChangeset, Clone)]
-#[table_name="comment_like"]
+#[table_name = "comment_like"]
pub struct CommentLikeForm {
pub user_id: i32,
pub comment_id: i32,
pub post_id: i32,
- pub score: i16
+ pub score: i16,
}
-impl Likeable <CommentLikeForm> for CommentLike {
+impl Likeable<CommentLikeForm> for CommentLike {
fn read(conn: &PgConnection, comment_id_from: i32) -> Result<Vec<Self>, Error> {
use crate::schema::comment_like::dsl::*;
comment_like
.filter(comment_id.eq(comment_id_from))
- .load::<Self>(conn)
+ .load::<Self>(conn)
}
fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
use crate::schema::comment_like::dsl::*;
diesel::delete(
comment_like
- .filter(comment_id.eq(comment_like_form.comment_id))
- .filter(user_id.eq(comment_like_form.user_id)))
- .execute(conn)
+ .filter(comment_id.eq(comment_like_form.comment_id))
+ .filter(user_id.eq(comment_like_form.user_id)),
+ )
+ .execute(conn)
}
}
use crate::schema::comment_like::dsl::*;
comment_like
.filter(post_id.eq(post_id_from))
- .load::<Self>(conn)
+ .load::<Self>(conn)
}
}
}
#[derive(Insertable, AsChangeset, Clone)]
-#[table_name="comment_saved"]
+#[table_name = "comment_saved"]
pub struct CommentSavedForm {
pub comment_id: i32,
pub user_id: i32,
}
-impl Saveable <CommentSavedForm> for CommentSaved {
+impl Saveable<CommentSavedForm> for CommentSaved {
fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
use crate::schema::comment_saved::dsl::*;
insert_into(comment_saved)
}
fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
use crate::schema::comment_saved::dsl::*;
- diesel::delete(comment_saved
- .filter(comment_id.eq(comment_saved_form.comment_id))
- .filter(user_id.eq(comment_saved_form.user_id)))
- .execute(conn)
+ diesel::delete(
+ comment_saved
+ .filter(comment_id.eq(comment_saved_form.comment_id))
+ .filter(user_id.eq(comment_saved_form.user_id)),
+ )
+ .execute(conn)
}
}
#[cfg(test)]
mod tests {
- use super::*;
- use super::super::post::*;
use super::super::community::*;
+ use super::super::post::*;
use super::super::user::*;
- #[test]
+ use super::*;
+ #[test]
fn test_crud() {
let conn = establish_connection();
};
let inserted_community = Community::create(&conn, &new_community).unwrap();
-
+
let new_post = PostForm {
name: "A test post".into(),
creator_id: inserted_user.id,
deleted: None,
read: None,
parent_id: None,
- updated: None
+ updated: None,
};
let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
read: false,
parent_id: None,
published: inserted_comment.published,
- updated: None
+ updated: None,
};
-
+
let child_comment_form = CommentForm {
content: "A child comment".into(),
creator_id: inserted_user.id,
removed: None,
deleted: None,
read: None,
- updated: None
+ updated: None,
};
let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
comment_id: inserted_comment.id,
post_id: inserted_post.id,
user_id: inserted_user.id,
- score: 1
+ score: 1,
};
let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
post_id: inserted_post.id,
user_id: inserted_user.id,
published: inserted_comment_like.published,
- score: 1
+ score: 1,
};
-
+
// Comment Saved
let comment_saved_form = CommentSavedForm {
comment_id: inserted_comment.id,
assert_eq!(expected_comment, updated_comment);
assert_eq!(expected_comment_like, inserted_comment_like);
assert_eq!(expected_comment_saved, inserted_comment_saved);
- assert_eq!(expected_comment.id, inserted_child_comment.parent_id.unwrap());
+ assert_eq!(
+ expected_comment.id,
+ inserted_child_comment.parent_id.unwrap()
+ );
assert_eq!(1, like_removed);
assert_eq!(1, saved_removed);
assert_eq!(1, num_deleted);
-
}
}
}
}
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="comment_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "comment_view"]
pub struct CommentView {
pub id: i32,
pub creator_id: i32,
}
impl CommentView {
-
- pub fn list(conn: &PgConnection,
- sort: &SortType,
- for_post_id: Option<i32>,
- for_creator_id: Option<i32>,
- search_term: Option<String>,
- my_user_id: Option<i32>,
- saved_only: bool,
- page: Option<i64>,
- limit: Option<i64>,
- ) -> Result<Vec<Self>, Error> {
+ pub fn list(
+ conn: &PgConnection,
+ sort: &SortType,
+ for_post_id: Option<i32>,
+ for_creator_id: Option<i32>,
+ search_term: Option<String>,
+ my_user_id: Option<i32>,
+ saved_only: bool,
+ page: Option<i64>,
+ limit: Option<i64>,
+ ) -> Result<Vec<Self>, Error> {
use super::comment_view::comment_view::dsl::*;
let (limit, offset) = limit_and_offset(page, limit);
if let Some(search_term) = search_term {
query = query.filter(content.ilike(fuzzy_search(&search_term)));
};
-
+
if saved_only {
query = query.filter(saved.eq(true));
}
SortType::TopYear => query
.filter(published.gt(now - 1.years()))
.order_by(score.desc()),
- SortType::TopMonth => query
- .filter(published.gt(now - 1.months()))
- .order_by(score.desc()),
- SortType::TopWeek => query
- .filter(published.gt(now - 1.weeks()))
- .order_by(score.desc()),
- SortType::TopDay => query
- .filter(published.gt(now - 1.days()))
- .order_by(score.desc()),
- _ => query.order_by(published.desc())
+ SortType::TopMonth => query
+ .filter(published.gt(now - 1.months()))
+ .order_by(score.desc()),
+ SortType::TopWeek => query
+ .filter(published.gt(now - 1.weeks()))
+ .order_by(score.desc()),
+ SortType::TopDay => query
+ .filter(published.gt(now - 1.days()))
+ .order_by(score.desc()),
+ _ => query.order_by(published.desc()),
};
// Note: deleted and removed comments are done on the front side
- query
- .limit(limit)
- .offset(offset)
- .load::<Self>(conn)
+ query.limit(limit).offset(offset).load::<Self>(conn)
}
- pub fn read(conn: &PgConnection, from_comment_id: i32, my_user_id: Option<i32>) -> Result<Self, Error> {
+ pub fn read(
+ conn: &PgConnection,
+ from_comment_id: i32,
+ my_user_id: Option<i32>,
+ ) -> Result<Self, Error> {
use super::comment_view::comment_view::dsl::*;
let mut query = comment_view.into_boxed();
query = query.filter(user_id.is_null());
}
- query = query.filter(id.eq(from_comment_id)).order_by(published.desc());
+ query = query
+ .filter(id.eq(from_comment_id))
+ .order_by(published.desc());
- query.first::<Self>(conn)
+ query.first::<Self>(conn)
}
-
}
-
// The faked schema since diesel doesn't do views
table! {
reply_view (id) {
}
}
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="reply_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "reply_view"]
pub struct ReplyView {
pub id: i32,
pub creator_id: i32,
}
impl ReplyView {
-
- pub fn get_replies(conn: &PgConnection,
- for_user_id: i32,
- sort: &SortType,
- unread_only: bool,
- page: Option<i64>,
- limit: Option<i64>,
- ) -> Result<Vec<Self>, Error> {
+ pub fn get_replies(
+ conn: &PgConnection,
+ for_user_id: i32,
+ sort: &SortType,
+ unread_only: bool,
+ page: Option<i64>,
+ limit: Option<i64>,
+ ) -> Result<Vec<Self>, Error> {
use super::comment_view::reply_view::dsl::*;
let (limit, offset) = limit_and_offset(page, limit);
SortType::TopYear => query
.filter(published.gt(now - 1.years()))
.order_by(score.desc()),
- SortType::TopMonth => query
- .filter(published.gt(now - 1.months()))
- .order_by(score.desc()),
- SortType::TopWeek => query
- .filter(published.gt(now - 1.weeks()))
- .order_by(score.desc()),
- SortType::TopDay => query
- .filter(published.gt(now - 1.days()))
- .order_by(score.desc()),
- _ => query.order_by(published.desc())
+ SortType::TopMonth => query
+ .filter(published.gt(now - 1.months()))
+ .order_by(score.desc()),
+ SortType::TopWeek => query
+ .filter(published.gt(now - 1.weeks()))
+ .order_by(score.desc()),
+ SortType::TopDay => query
+ .filter(published.gt(now - 1.days()))
+ .order_by(score.desc()),
+ _ => query.order_by(published.desc()),
};
- query
- .limit(limit)
- .offset(offset)
- .load::<Self>(conn)
+ query.limit(limit).offset(offset).load::<Self>(conn)
}
-
}
#[cfg(test)]
mod tests {
- use super::*;
- use super::super::post::*;
+ use super::super::comment::*;
use super::super::community::*;
+ use super::super::post::*;
use super::super::user::*;
- use super::super::comment::*;
- #[test]
+ use super::*;
+ #[test]
fn test_crud() {
let conn = establish_connection();
};
let inserted_community = Community::create(&conn, &new_community).unwrap();
-
+
let new_post = PostForm {
name: "A test post 2".into(),
creator_id: inserted_user.id,
removed: None,
deleted: None,
read: None,
- updated: None
+ updated: None,
};
let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
comment_id: inserted_comment.id,
post_id: inserted_post.id,
user_id: inserted_user.id,
- score: 1
+ score: 1,
};
let _inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
};
let read_comment_views_no_user = CommentView::list(
- &conn,
- &SortType::New,
- Some(inserted_post.id),
- None,
- None,
+ &conn,
+ &SortType::New,
+ Some(inserted_post.id),
+ None,
None,
- false,
- None,
- None).unwrap();
+ None,
+ false,
+ None,
+ None,
+ )
+ .unwrap();
let read_comment_views_with_user = CommentView::list(
- &conn,
- &SortType::New,
- Some(inserted_post.id),
- None,
+ &conn,
+ &SortType::New,
+ Some(inserted_post.id),
+ None,
None,
- Some(inserted_user.id),
- false,
- None,
- None).unwrap();
+ Some(inserted_user.id),
+ false,
+ None,
+ None,
+ )
+ .unwrap();
let like_removed = CommentLike::remove(&conn, &comment_like_form).unwrap();
let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
Post::delete(&conn, inserted_post.id).unwrap();
User_::delete(&conn, inserted_user.id).unwrap();
assert_eq!(expected_comment_view_no_user, read_comment_views_no_user[0]);
- assert_eq!(expected_comment_view_with_user, read_comment_views_with_user[0]);
+ assert_eq!(
+ expected_comment_view_with_user,
+ read_comment_views_with_user[0]
+ );
assert_eq!(1, num_deleted);
assert_eq!(1, like_removed);
}
}
-
-use crate::schema::{community, community_moderator, community_follower, community_user_ban, site};
use super::*;
+use crate::schema::{community, community_follower, community_moderator, community_user_ban, site};
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
-#[table_name="community"]
+#[table_name = "community"]
pub struct Community {
pub id: i32,
pub name: String,
}
#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
-#[table_name="community"]
+#[table_name = "community"]
pub struct CommunityForm {
pub name: String,
pub title: String,
impl Crud<CommunityForm> for Community {
fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
use crate::schema::community::dsl::*;
- community.find(community_id)
- .first::<Self>(conn)
+ community.find(community_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
use crate::schema::community::dsl::*;
- diesel::delete(community.find(community_id))
- .execute(conn)
+ diesel::delete(community.find(community_id)).execute(conn)
}
fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
use crate::schema::community::dsl::*;
- insert_into(community)
- .values(new_community)
- .get_result::<Self>(conn)
+ insert_into(community)
+ .values(new_community)
+ .get_result::<Self>(conn)
}
- fn update(conn: &PgConnection, community_id: i32, new_community: &CommunityForm) -> Result<Self, Error> {
+ fn update(
+ conn: &PgConnection,
+ community_id: i32,
+ new_community: &CommunityForm,
+ ) -> Result<Self, Error> {
use crate::schema::community::dsl::*;
diesel::update(community.find(community_id))
.set(new_community)
impl Community {
pub fn read_from_name(conn: &PgConnection, community_name: String) -> Result<Self, Error> {
use crate::schema::community::dsl::*;
- community.filter(name.eq(community_name))
+ community
+ .filter(name.eq(community_name))
.first::<Self>(conn)
}
}
}
#[derive(Insertable, AsChangeset, Clone)]
-#[table_name="community_moderator"]
+#[table_name = "community_moderator"]
pub struct CommunityModeratorForm {
pub community_id: i32,
pub user_id: i32,
}
impl Joinable<CommunityModeratorForm> for CommunityModerator {
- fn join(conn: &PgConnection, community_user_form: &CommunityModeratorForm) -> Result<Self, Error> {
+ fn join(
+ conn: &PgConnection,
+ community_user_form: &CommunityModeratorForm,
+ ) -> Result<Self, Error> {
use crate::schema::community_moderator::dsl::*;
insert_into(community_moderator)
.values(community_user_form)
.get_result::<Self>(conn)
}
- fn leave(conn: &PgConnection, community_user_form: &CommunityModeratorForm) -> Result<usize, Error> {
+ fn leave(
+ conn: &PgConnection,
+ community_user_form: &CommunityModeratorForm,
+ ) -> Result<usize, Error> {
use crate::schema::community_moderator::dsl::*;
- diesel::delete(community_moderator
- .filter(community_id.eq(community_user_form.community_id))
- .filter(user_id.eq(community_user_form.user_id)))
- .execute(conn)
+ diesel::delete(
+ community_moderator
+ .filter(community_id.eq(community_user_form.community_id))
+ .filter(user_id.eq(community_user_form.user_id)),
+ )
+ .execute(conn)
}
}
impl CommunityModerator {
pub fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
use crate::schema::community_moderator::dsl::*;
- diesel::delete(
- community_moderator
- .filter(community_id.eq(for_community_id)))
- .execute(conn)
+ diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
}
}
}
#[derive(Insertable, AsChangeset, Clone)]
-#[table_name="community_user_ban"]
+#[table_name = "community_user_ban"]
pub struct CommunityUserBanForm {
pub community_id: i32,
pub user_id: i32,
}
impl Bannable<CommunityUserBanForm> for CommunityUserBan {
- fn ban(conn: &PgConnection, community_user_ban_form: &CommunityUserBanForm) -> Result<Self, Error> {
+ fn ban(
+ conn: &PgConnection,
+ community_user_ban_form: &CommunityUserBanForm,
+ ) -> Result<Self, Error> {
use crate::schema::community_user_ban::dsl::*;
insert_into(community_user_ban)
.values(community_user_ban_form)
.get_result::<Self>(conn)
}
- fn unban(conn: &PgConnection, community_user_ban_form: &CommunityUserBanForm) -> Result<usize, Error> {
+ fn unban(
+ conn: &PgConnection,
+ community_user_ban_form: &CommunityUserBanForm,
+ ) -> Result<usize, Error> {
use crate::schema::community_user_ban::dsl::*;
- diesel::delete(community_user_ban
- .filter(community_id.eq(community_user_ban_form.community_id))
- .filter(user_id.eq(community_user_ban_form.user_id)))
- .execute(conn)
+ diesel::delete(
+ community_user_ban
+ .filter(community_id.eq(community_user_ban_form.community_id))
+ .filter(user_id.eq(community_user_ban_form.user_id)),
+ )
+ .execute(conn)
}
}
}
#[derive(Insertable, AsChangeset, Clone)]
-#[table_name="community_follower"]
+#[table_name = "community_follower"]
pub struct CommunityFollowerForm {
pub community_id: i32,
pub user_id: i32,
}
impl Followable<CommunityFollowerForm> for CommunityFollower {
- fn follow(conn: &PgConnection, community_follower_form: &CommunityFollowerForm) -> Result<Self, Error> {
+ fn follow(
+ conn: &PgConnection,
+ community_follower_form: &CommunityFollowerForm,
+ ) -> Result<Self, Error> {
use crate::schema::community_follower::dsl::*;
insert_into(community_follower)
.values(community_follower_form)
.get_result::<Self>(conn)
}
- fn ignore(conn: &PgConnection, community_follower_form: &CommunityFollowerForm) -> Result<usize, Error> {
+ fn ignore(
+ conn: &PgConnection,
+ community_follower_form: &CommunityFollowerForm,
+ ) -> Result<usize, Error> {
use crate::schema::community_follower::dsl::*;
- diesel::delete(community_follower
- .filter(community_id.eq(&community_follower_form.community_id))
- .filter(user_id.eq(&community_follower_form.user_id)))
- .execute(conn)
+ diesel::delete(
+ community_follower
+ .filter(community_id.eq(&community_follower_form.community_id))
+ .filter(user_id.eq(&community_follower_form.user_id)),
+ )
+ .execute(conn)
}
}
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
-#[table_name="site"]
+#[table_name = "site"]
pub struct Site {
pub id: i32,
pub name: String,
pub description: Option<String>,
pub creator_id: i32,
pub published: chrono::NaiveDateTime,
- pub updated: Option<chrono::NaiveDateTime>
+ pub updated: Option<chrono::NaiveDateTime>,
}
#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
-#[table_name="site"]
+#[table_name = "site"]
pub struct SiteForm {
pub name: String,
pub description: Option<String>,
pub creator_id: i32,
- pub updated: Option<chrono::NaiveDateTime>
+ pub updated: Option<chrono::NaiveDateTime>,
}
impl Crud<SiteForm> for Site {
fn delete(conn: &PgConnection, site_id: i32) -> Result<usize, Error> {
use crate::schema::site::dsl::*;
- diesel::delete(site.find(site_id))
- .execute(conn)
+ diesel::delete(site.find(site_id)).execute(conn)
}
fn create(conn: &PgConnection, new_site: &SiteForm) -> Result<Self, Error> {
use crate::schema::site::dsl::*;
- insert_into(site)
- .values(new_site)
- .get_result::<Self>(conn)
+ insert_into(site).values(new_site).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, site_id: i32, new_site: &SiteForm) -> Result<Self, Error> {
#[cfg(test)]
mod tests {
- use super::*;
use super::super::user::*;
- #[test]
+ use super::*;
+ #[test]
fn test_crud() {
let conn = establish_connection();
removed: false,
deleted: false,
published: inserted_community.published,
- updated: None
+ updated: None,
};
let community_follower_form = CommunityFollowerForm {
community_id: inserted_community.id,
- user_id: inserted_user.id
+ user_id: inserted_user.id,
};
- let inserted_community_follower = CommunityFollower::follow(&conn, &community_follower_form).unwrap();
-
+ let inserted_community_follower =
+ CommunityFollower::follow(&conn, &community_follower_form).unwrap();
let expected_community_follower = CommunityFollower {
id: inserted_community_follower.id,
community_id: inserted_community.id,
user_id: inserted_user.id,
- published: inserted_community_follower.published
+ published: inserted_community_follower.published,
};
-
+
let community_user_form = CommunityModeratorForm {
community_id: inserted_community.id,
- user_id: inserted_user.id
+ user_id: inserted_user.id,
};
let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
id: inserted_community_user.id,
community_id: inserted_community.id,
user_id: inserted_user.id,
- published: inserted_community_user.published
+ published: inserted_community_user.published,
};
let community_user_ban_form = CommunityUserBanForm {
community_id: inserted_community.id,
- user_id: inserted_user.id
+ user_id: inserted_user.id,
};
- let inserted_community_user_ban = CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
+ let inserted_community_user_ban =
+ CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
let expected_community_user_ban = CommunityUserBan {
id: inserted_community_user_ban.id,
community_id: inserted_community.id,
user_id: inserted_user.id,
- published: inserted_community_user_ban.published
+ published: inserted_community_user_ban.published,
};
let read_community = Community::read(&conn, inserted_community.id).unwrap();
- let updated_community = Community::update(&conn, inserted_community.id, &new_community).unwrap();
+ let updated_community =
+ Community::update(&conn, inserted_community.id, &new_community).unwrap();
let ignored_community = CommunityFollower::ignore(&conn, &community_follower_form).unwrap();
let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
let unban = CommunityUserBan::unban(&conn, &community_user_ban_form).unwrap();
assert_eq!(1, unban);
// assert_eq!(2, loaded_count);
assert_eq!(1, num_deleted);
-
}
}
}
}
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="community_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "community_view"]
pub struct CommunityView {
pub id: i32,
pub name: String,
}
impl CommunityView {
- pub fn read(conn: &PgConnection, from_community_id: i32, from_user_id: Option<i32>) -> Result<Self, Error> {
+ pub fn read(
+ conn: &PgConnection,
+ from_community_id: i32,
+ from_user_id: Option<i32>,
+ ) -> Result<Self, Error> {
use super::community_view::community_view::dsl::*;
let mut query = community_view.into_boxed();
}
pub fn list(
- conn: &PgConnection,
- sort: &SortType,
- from_user_id: Option<i32>,
+ conn: &PgConnection,
+ sort: &SortType,
+ from_user_id: Option<i32>,
show_nsfw: bool,
search_term: Option<String>,
page: Option<i64>,
limit: Option<i64>,
- ) -> Result<Vec<Self>, Error> {
+ ) -> Result<Vec<Self>, Error> {
use super::community_view::community_view::dsl::*;
let mut query = community_view.into_boxed();
// The view lets you pass a null user_id, if you're not logged in
match sort {
- SortType::Hot => query = query.order_by(hot_rank.desc())
- .then_order_by(number_of_subscribers.desc())
- .filter(user_id.is_null()),
- SortType::New => query = query.order_by(published.desc()).filter(user_id.is_null()),
- SortType::TopAll => {
- match from_user_id {
- Some(from_user_id) => query = query.filter(user_id.eq(from_user_id)).order_by((subscribed.asc(), number_of_subscribers.desc())),
- None => query = query.order_by(number_of_subscribers.desc()).filter(user_id.is_null())
- }
+ SortType::Hot => {
+ query = query
+ .order_by(hot_rank.desc())
+ .then_order_by(number_of_subscribers.desc())
+ .filter(user_id.is_null())
+ }
+ SortType::New => query = query.order_by(published.desc()).filter(user_id.is_null()),
+ SortType::TopAll => match from_user_id {
+ Some(from_user_id) => {
+ query = query
+ .filter(user_id.eq(from_user_id))
+ .order_by((subscribed.asc(), number_of_subscribers.desc()))
}
- _ => ()
+ None => {
+ query = query
+ .order_by(number_of_subscribers.desc())
+ .filter(user_id.is_null())
+ }
+ },
+ _ => (),
};
if !show_nsfw {
.offset(offset)
.filter(removed.eq(false))
.filter(deleted.eq(false))
- .load::<Self>(conn)
+ .load::<Self>(conn)
}
}
-
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="community_moderator_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "community_moderator_view"]
pub struct CommunityModeratorView {
pub id: i32,
pub community_id: i32,
pub user_id: i32,
pub published: chrono::NaiveDateTime,
- pub user_name : String,
+ pub user_name: String,
pub community_name: String,
}
impl CommunityModeratorView {
pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
use super::community_view::community_moderator_view::dsl::*;
- community_moderator_view.filter(community_id.eq(from_community_id)).load::<Self>(conn)
+ community_moderator_view
+ .filter(community_id.eq(from_community_id))
+ .load::<Self>(conn)
}
pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
use super::community_view::community_moderator_view::dsl::*;
- community_moderator_view.filter(user_id.eq(from_user_id)).load::<Self>(conn)
+ community_moderator_view
+ .filter(user_id.eq(from_user_id))
+ .load::<Self>(conn)
}
}
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="community_follower_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "community_follower_view"]
pub struct CommunityFollowerView {
pub id: i32,
pub community_id: i32,
pub user_id: i32,
pub published: chrono::NaiveDateTime,
- pub user_name : String,
+ pub user_name: String,
pub community_name: String,
}
impl CommunityFollowerView {
pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
use super::community_view::community_follower_view::dsl::*;
- community_follower_view.filter(community_id.eq(from_community_id)).load::<Self>(conn)
+ community_follower_view
+ .filter(community_id.eq(from_community_id))
+ .load::<Self>(conn)
}
pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
use super::community_view::community_follower_view::dsl::*;
- community_follower_view.filter(user_id.eq(from_user_id)).load::<Self>(conn)
+ community_follower_view
+ .filter(user_id.eq(from_user_id))
+ .load::<Self>(conn)
}
}
-
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="community_user_ban_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "community_user_ban_view"]
pub struct CommunityUserBanView {
pub id: i32,
pub community_id: i32,
pub user_id: i32,
pub published: chrono::NaiveDateTime,
- pub user_name : String,
+ pub user_name: String,
pub community_name: String,
}
impl CommunityUserBanView {
pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
use super::community_view::community_user_ban_view::dsl::*;
- community_user_ban_view.filter(community_id.eq(from_community_id)).load::<Self>(conn)
+ community_user_ban_view
+ .filter(community_id.eq(from_community_id))
+ .load::<Self>(conn)
}
pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
use super::community_view::community_user_ban_view::dsl::*;
- community_user_ban_view.filter(user_id.eq(from_user_id)).load::<Self>(conn)
+ community_user_ban_view
+ .filter(user_id.eq(from_user_id))
+ .load::<Self>(conn)
}
- pub fn get(conn: &PgConnection, from_user_id: i32, from_community_id: i32) -> Result<Self, Error> {
+ pub fn get(
+ conn: &PgConnection,
+ from_user_id: i32,
+ from_community_id: i32,
+ ) -> Result<Self, Error> {
use super::community_view::community_user_ban_view::dsl::*;
community_user_ban_view
.filter(user_id.eq(from_user_id))
}
}
-
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="site_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "site_view"]
pub struct SiteView {
pub id: i32,
pub name: String,
-use diesel::*;
+use crate::Settings;
use diesel::dsl::*;
use diesel::result::Error;
-use crate::{Settings};
+use diesel::*;
use serde::{Deserialize, Serialize};
-pub mod user;
-pub mod community;
-pub mod post;
+pub mod category;
pub mod comment;
-pub mod post_view;
pub mod comment_view;
-pub mod category;
+pub mod community;
pub mod community_view;
-pub mod user_view;
pub mod moderator;
pub mod moderator_views;
+pub mod post;
+pub mod post_view;
+pub mod user;
+pub mod user_view;
pub trait Crud<T> {
- fn create(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
- fn read(conn: &PgConnection, id: i32) -> Result<Self, Error> where Self: Sized;
- fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error> where Self: Sized;
- fn delete(conn: &PgConnection, id: i32) -> Result<usize, Error> where Self: Sized;
+ fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
+ where
+ Self: Sized;
+ fn read(conn: &PgConnection, id: i32) -> Result<Self, Error>
+ where
+ Self: Sized;
+ fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error>
+ where
+ Self: Sized;
+ fn delete(conn: &PgConnection, id: i32) -> Result<usize, Error>
+ where
+ Self: Sized;
}
pub trait Followable<T> {
- fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
- fn ignore(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
+ fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error>
+ where
+ Self: Sized;
+ fn ignore(conn: &PgConnection, form: &T) -> Result<usize, Error>
+ where
+ Self: Sized;
}
pub trait Joinable<T> {
- fn join(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
- fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
+ fn join(conn: &PgConnection, form: &T) -> Result<Self, Error>
+ where
+ Self: Sized;
+ fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error>
+ where
+ Self: Sized;
}
pub trait Likeable<T> {
- fn read(conn: &PgConnection, id: i32) -> Result<Vec<Self>, Error> where Self: Sized;
- fn like(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
- fn remove(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
+ fn read(conn: &PgConnection, id: i32) -> Result<Vec<Self>, Error>
+ where
+ Self: Sized;
+ fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
+ where
+ Self: Sized;
+ fn remove(conn: &PgConnection, form: &T) -> Result<usize, Error>
+ where
+ Self: Sized;
}
pub trait Bannable<T> {
- fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
- fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
+ fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error>
+ where
+ Self: Sized;
+ fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error>
+ where
+ Self: Sized;
}
pub trait Saveable<T> {
- fn save(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
- fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
+ fn save(conn: &PgConnection, form: &T) -> Result<Self, Error>
+ where
+ Self: Sized;
+ fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error>
+ where
+ Self: Sized;
}
pub trait Readable<T> {
- fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
- fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
+ fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error>
+ where
+ Self: Sized;
+ fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error>
+ where
+ Self: Sized;
}
pub fn establish_connection() -> PgConnection {
let db_url = Settings::get().db_url;
- PgConnection::establish(&db_url)
- .expect(&format!("Error connecting to {}", db_url))
+ PgConnection::establish(&db_url).expect(&format!("Error connecting to {}", db_url))
}
-#[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
+#[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
pub enum SortType {
- Hot, New, TopDay, TopWeek, TopMonth, TopYear, TopAll
+ Hot,
+ New,
+ TopDay,
+ TopWeek,
+ TopMonth,
+ TopYear,
+ TopAll,
}
-#[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
+#[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
pub enum SearchType {
- All, Comments, Posts, Communities, Users, Url
+ All,
+ Comments,
+ Posts,
+ Communities,
+ Users,
+ Url,
}
pub fn fuzzy_search(q: &str) -> String {
}
pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
- let page = page.unwrap_or(1);
- let limit = limit.unwrap_or(10);
- let offset = limit * (page - 1);
- (limit, offset)
+ let page = page.unwrap_or(1);
+ let limit = limit.unwrap_or(10);
+ let offset = limit * (page - 1);
+ (limit, offset)
}
#[cfg(test)]
mod tests {
use super::fuzzy_search;
- #[test] fn test_fuzzy_search() {
+ #[test]
+ fn test_fuzzy_search() {
let test = "This is a fuzzy search";
assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
}
}
-
-use crate::schema::{mod_remove_post, mod_lock_post, mod_remove_comment, mod_remove_community, mod_ban_from_community, mod_ban, mod_add_community, mod_add};
use super::*;
+use crate::schema::{
+ mod_add, mod_add_community, mod_ban, mod_ban_from_community, mod_lock_post, mod_remove_comment,
+ mod_remove_community, mod_remove_post,
+};
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
-#[table_name="mod_remove_post"]
+#[table_name = "mod_remove_post"]
pub struct ModRemovePost {
pub id: i32,
pub mod_user_id: i32,
}
#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
-#[table_name="mod_remove_post"]
+#[table_name = "mod_remove_post"]
pub struct ModRemovePostForm {
pub mod_user_id: i32,
pub post_id: i32,
impl Crud<ModRemovePostForm> for ModRemovePost {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_remove_post::dsl::*;
- mod_remove_post.find(from_id)
- .first::<Self>(conn)
+ mod_remove_post.find(from_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_remove_post::dsl::*;
- diesel::delete(mod_remove_post.find(from_id))
- .execute(conn)
+ diesel::delete(mod_remove_post.find(from_id)).execute(conn)
}
fn create(conn: &PgConnection, form: &ModRemovePostForm) -> Result<Self, Error> {
use crate::schema::mod_remove_post::dsl::*;
- insert_into(mod_remove_post)
- .values(form)
- .get_result::<Self>(conn)
+ insert_into(mod_remove_post)
+ .values(form)
+ .get_result::<Self>(conn)
}
fn update(conn: &PgConnection, from_id: i32, form: &ModRemovePostForm) -> Result<Self, Error> {
}
}
-
-
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
-#[table_name="mod_lock_post"]
+#[table_name = "mod_lock_post"]
pub struct ModLockPost {
pub id: i32,
pub mod_user_id: i32,
}
#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
-#[table_name="mod_lock_post"]
+#[table_name = "mod_lock_post"]
pub struct ModLockPostForm {
pub mod_user_id: i32,
pub post_id: i32,
impl Crud<ModLockPostForm> for ModLockPost {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_lock_post::dsl::*;
- mod_lock_post.find(from_id)
- .first::<Self>(conn)
+ mod_lock_post.find(from_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_lock_post::dsl::*;
- diesel::delete(mod_lock_post.find(from_id))
- .execute(conn)
+ diesel::delete(mod_lock_post.find(from_id)).execute(conn)
}
fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result<Self, Error> {
use crate::schema::mod_lock_post::dsl::*;
- insert_into(mod_lock_post)
- .values(form)
- .get_result::<Self>(conn)
+ insert_into(mod_lock_post)
+ .values(form)
+ .get_result::<Self>(conn)
}
fn update(conn: &PgConnection, from_id: i32, form: &ModLockPostForm) -> Result<Self, Error> {
}
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
-#[table_name="mod_remove_comment"]
+#[table_name = "mod_remove_comment"]
pub struct ModRemoveComment {
pub id: i32,
pub mod_user_id: i32,
}
#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
-#[table_name="mod_remove_comment"]
+#[table_name = "mod_remove_comment"]
pub struct ModRemoveCommentForm {
pub mod_user_id: i32,
pub comment_id: i32,
impl Crud<ModRemoveCommentForm> for ModRemoveComment {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_remove_comment::dsl::*;
- mod_remove_comment.find(from_id)
- .first::<Self>(conn)
+ mod_remove_comment.find(from_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_remove_comment::dsl::*;
- diesel::delete(mod_remove_comment.find(from_id))
- .execute(conn)
+ diesel::delete(mod_remove_comment.find(from_id)).execute(conn)
}
fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result<Self, Error> {
use crate::schema::mod_remove_comment::dsl::*;
- insert_into(mod_remove_comment)
- .values(form)
- .get_result::<Self>(conn)
+ insert_into(mod_remove_comment)
+ .values(form)
+ .get_result::<Self>(conn)
}
fn update(conn: &PgConnection, from_id: i32, form: &ModRemoveCommentForm) -> Result<Self, Error> {
}
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
-#[table_name="mod_remove_community"]
+#[table_name = "mod_remove_community"]
pub struct ModRemoveCommunity {
pub id: i32,
pub mod_user_id: i32,
}
#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
-#[table_name="mod_remove_community"]
+#[table_name = "mod_remove_community"]
pub struct ModRemoveCommunityForm {
pub mod_user_id: i32,
pub community_id: i32,
impl Crud<ModRemoveCommunityForm> for ModRemoveCommunity {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_remove_community::dsl::*;
- mod_remove_community.find(from_id)
- .first::<Self>(conn)
+ mod_remove_community.find(from_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_remove_community::dsl::*;
- diesel::delete(mod_remove_community.find(from_id))
- .execute(conn)
+ diesel::delete(mod_remove_community.find(from_id)).execute(conn)
}
fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_remove_community::dsl::*;
- insert_into(mod_remove_community)
- .values(form)
- .get_result::<Self>(conn)
+ insert_into(mod_remove_community)
+ .values(form)
+ .get_result::<Self>(conn)
}
- fn update(conn: &PgConnection, from_id: i32, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
+ fn update(
+ conn: &PgConnection,
+ from_id: i32,
+ form: &ModRemoveCommunityForm,
+ ) -> Result<Self, Error> {
use crate::schema::mod_remove_community::dsl::*;
diesel::update(mod_remove_community.find(from_id))
.set(form)
}
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
-#[table_name="mod_ban_from_community"]
+#[table_name = "mod_ban_from_community"]
pub struct ModBanFromCommunity {
pub id: i32,
pub mod_user_id: i32,
}
#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
-#[table_name="mod_ban_from_community"]
+#[table_name = "mod_ban_from_community"]
pub struct ModBanFromCommunityForm {
pub mod_user_id: i32,
pub other_user_id: i32,
impl Crud<ModBanFromCommunityForm> for ModBanFromCommunity {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_ban_from_community::dsl::*;
- mod_ban_from_community.find(from_id)
- .first::<Self>(conn)
+ mod_ban_from_community.find(from_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_ban_from_community::dsl::*;
- diesel::delete(mod_ban_from_community.find(from_id))
- .execute(conn)
+ diesel::delete(mod_ban_from_community.find(from_id)).execute(conn)
}
fn create(conn: &PgConnection, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_ban_from_community::dsl::*;
- insert_into(mod_ban_from_community)
- .values(form)
- .get_result::<Self>(conn)
+ insert_into(mod_ban_from_community)
+ .values(form)
+ .get_result::<Self>(conn)
}
- fn update(conn: &PgConnection, from_id: i32, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
+ fn update(
+ conn: &PgConnection,
+ from_id: i32,
+ form: &ModBanFromCommunityForm,
+ ) -> Result<Self, Error> {
use crate::schema::mod_ban_from_community::dsl::*;
diesel::update(mod_ban_from_community.find(from_id))
.set(form)
}
}
-
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
-#[table_name="mod_ban"]
+#[table_name = "mod_ban"]
pub struct ModBan {
pub id: i32,
pub mod_user_id: i32,
}
#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
-#[table_name="mod_ban"]
+#[table_name = "mod_ban"]
pub struct ModBanForm {
pub mod_user_id: i32,
pub other_user_id: i32,
impl Crud<ModBanForm> for ModBan {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_ban::dsl::*;
- mod_ban.find(from_id)
- .first::<Self>(conn)
+ mod_ban.find(from_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_ban::dsl::*;
- diesel::delete(mod_ban.find(from_id))
- .execute(conn)
+ diesel::delete(mod_ban.find(from_id)).execute(conn)
}
fn create(conn: &PgConnection, form: &ModBanForm) -> Result<Self, Error> {
use crate::schema::mod_ban::dsl::*;
- insert_into(mod_ban)
- .values(form)
- .get_result::<Self>(conn)
+ insert_into(mod_ban).values(form).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, from_id: i32, form: &ModBanForm) -> Result<Self, Error> {
}
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
-#[table_name="mod_add_community"]
+#[table_name = "mod_add_community"]
pub struct ModAddCommunity {
pub id: i32,
pub mod_user_id: i32,
}
#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
-#[table_name="mod_add_community"]
+#[table_name = "mod_add_community"]
pub struct ModAddCommunityForm {
pub mod_user_id: i32,
pub other_user_id: i32,
impl Crud<ModAddCommunityForm> for ModAddCommunity {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_add_community::dsl::*;
- mod_add_community.find(from_id)
- .first::<Self>(conn)
+ mod_add_community.find(from_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_add_community::dsl::*;
- diesel::delete(mod_add_community.find(from_id))
- .execute(conn)
+ diesel::delete(mod_add_community.find(from_id)).execute(conn)
}
fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_add_community::dsl::*;
- insert_into(mod_add_community)
- .values(form)
- .get_result::<Self>(conn)
+ insert_into(mod_add_community)
+ .values(form)
+ .get_result::<Self>(conn)
}
fn update(conn: &PgConnection, from_id: i32, form: &ModAddCommunityForm) -> Result<Self, Error> {
}
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
-#[table_name="mod_add"]
+#[table_name = "mod_add"]
pub struct ModAdd {
pub id: i32,
pub mod_user_id: i32,
}
#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
-#[table_name="mod_add"]
+#[table_name = "mod_add"]
pub struct ModAddForm {
pub mod_user_id: i32,
pub other_user_id: i32,
impl Crud<ModAddForm> for ModAdd {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_add::dsl::*;
- mod_add.find(from_id)
- .first::<Self>(conn)
+ mod_add.find(from_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_add::dsl::*;
- diesel::delete(mod_add.find(from_id))
- .execute(conn)
+ diesel::delete(mod_add.find(from_id)).execute(conn)
}
fn create(conn: &PgConnection, form: &ModAddForm) -> Result<Self, Error> {
use crate::schema::mod_add::dsl::*;
- insert_into(mod_add)
- .values(form)
- .get_result::<Self>(conn)
+ insert_into(mod_add).values(form).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
#[cfg(test)]
mod tests {
- use super::*;
- use super::super::user::*;
- use super::super::post::*;
- use super::super::community::*;
use super::super::comment::*;
+ use super::super::community::*;
+ use super::super::post::*;
+ use super::super::user::*;
+ use super::*;
// use Crud;
- #[test]
+ #[test]
fn test_crud() {
let conn = establish_connection();
};
let inserted_community = Community::create(&conn, &new_community).unwrap();
-
+
let new_post = PostForm {
name: "A test post thweep".into(),
url: None,
deleted: None,
read: None,
parent_id: None,
- updated: None
+ updated: None,
};
let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
removed: None,
};
let inserted_mod_remove_post = ModRemovePost::create(&conn, &mod_remove_post_form).unwrap();
- let read_moderator_remove_post = ModRemovePost::read(&conn, inserted_mod_remove_post.id).unwrap();
+ let read_moderator_remove_post =
+ ModRemovePost::read(&conn, inserted_mod_remove_post.id).unwrap();
let expected_moderator_remove_post = ModRemovePost {
id: inserted_mod_remove_post.id,
post_id: inserted_post.id,
reason: None,
removed: None,
};
- let inserted_mod_remove_comment = ModRemoveComment::create(&conn, &mod_remove_comment_form).unwrap();
- let read_moderator_remove_comment = ModRemoveComment::read(&conn, inserted_mod_remove_comment.id).unwrap();
+ let inserted_mod_remove_comment =
+ ModRemoveComment::create(&conn, &mod_remove_comment_form).unwrap();
+ let read_moderator_remove_comment =
+ ModRemoveComment::read(&conn, inserted_mod_remove_comment.id).unwrap();
let expected_moderator_remove_comment = ModRemoveComment {
id: inserted_mod_remove_comment.id,
comment_id: inserted_comment.id,
removed: None,
expires: None,
};
- let inserted_mod_remove_community = ModRemoveCommunity::create(&conn, &mod_remove_community_form).unwrap();
- let read_moderator_remove_community = ModRemoveCommunity::read(&conn, inserted_mod_remove_community.id).unwrap();
+ let inserted_mod_remove_community =
+ ModRemoveCommunity::create(&conn, &mod_remove_community_form).unwrap();
+ let read_moderator_remove_community =
+ ModRemoveCommunity::read(&conn, inserted_mod_remove_community.id).unwrap();
let expected_moderator_remove_community = ModRemoveCommunity {
id: inserted_mod_remove_community.id,
community_id: inserted_community.id,
banned: None,
expires: None,
};
- let inserted_mod_ban_from_community = ModBanFromCommunity::create(&conn, &mod_ban_from_community_form).unwrap();
- let read_moderator_ban_from_community = ModBanFromCommunity::read(&conn, inserted_mod_ban_from_community.id).unwrap();
+ let inserted_mod_ban_from_community =
+ ModBanFromCommunity::create(&conn, &mod_ban_from_community_form).unwrap();
+ let read_moderator_ban_from_community =
+ ModBanFromCommunity::read(&conn, inserted_mod_ban_from_community.id).unwrap();
let expected_moderator_ban_from_community = ModBanFromCommunity {
id: inserted_mod_ban_from_community.id,
community_id: inserted_community.id,
community_id: inserted_community.id,
removed: None,
};
- let inserted_mod_add_community = ModAddCommunity::create(&conn, &mod_add_community_form).unwrap();
- let read_moderator_add_community = ModAddCommunity::read(&conn, inserted_mod_add_community.id).unwrap();
+ let inserted_mod_add_community =
+ ModAddCommunity::create(&conn, &mod_add_community_form).unwrap();
+ let read_moderator_add_community =
+ ModAddCommunity::read(&conn, inserted_mod_add_community.id).unwrap();
let expected_moderator_add_community = ModAddCommunity {
id: inserted_mod_add_community.id,
community_id: inserted_community.id,
assert_eq!(expected_moderator_remove_post, read_moderator_remove_post);
assert_eq!(expected_moderator_lock_post, read_moderator_lock_post);
- assert_eq!(expected_moderator_remove_comment, read_moderator_remove_comment);
- assert_eq!(expected_moderator_remove_community, read_moderator_remove_community);
- assert_eq!(expected_moderator_ban_from_community, read_moderator_ban_from_community);
+ assert_eq!(
+ expected_moderator_remove_comment,
+ read_moderator_remove_comment
+ );
+ assert_eq!(
+ expected_moderator_remove_community,
+ read_moderator_remove_community
+ );
+ assert_eq!(
+ expected_moderator_ban_from_community,
+ read_moderator_ban_from_community
+ );
assert_eq!(expected_moderator_ban, read_moderator_ban);
- assert_eq!(expected_moderator_add_community, read_moderator_add_community);
+ assert_eq!(
+ expected_moderator_add_community,
+ read_moderator_add_community
+ );
assert_eq!(expected_moderator_add, read_moderator_add);
}
}
}
}
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="mod_remove_post_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "mod_remove_post_view"]
pub struct ModRemovePostView {
pub id: i32,
pub mod_user_id: i32,
}
impl ModRemovePostView {
- pub fn list(conn: &PgConnection,
- from_community_id: Option<i32>,
- from_mod_user_id: Option<i32>,
- page: Option<i64>,
- limit: Option<i64>,
- ) -> Result<Vec<Self>, Error> {
+ pub fn list(
+ conn: &PgConnection,
+ from_community_id: Option<i32>,
+ from_mod_user_id: Option<i32>,
+ page: Option<i64>,
+ limit: Option<i64>,
+ ) -> Result<Vec<Self>, Error> {
use super::moderator_views::mod_remove_post_view::dsl::*;
let mut query = mod_remove_post_view.into_boxed();
query = query.filter(mod_user_id.eq(from_mod_user_id));
};
- query.limit(limit).offset(offset).order_by(when_.desc()).load::<Self>(conn)
+ query
+ .limit(limit)
+ .offset(offset)
+ .order_by(when_.desc())
+ .load::<Self>(conn)
}
}
}
}
-
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="mod_lock_post_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "mod_lock_post_view"]
pub struct ModLockPostView {
pub id: i32,
pub mod_user_id: i32,
}
impl ModLockPostView {
- pub fn list(conn: &PgConnection,
- from_community_id: Option<i32>,
- from_mod_user_id: Option<i32>,
- page: Option<i64>,
- limit: Option<i64>,
- ) -> Result<Vec<Self>, Error> {
+ pub fn list(
+ conn: &PgConnection,
+ from_community_id: Option<i32>,
+ from_mod_user_id: Option<i32>,
+ page: Option<i64>,
+ limit: Option<i64>,
+ ) -> Result<Vec<Self>, Error> {
use super::moderator_views::mod_lock_post_view::dsl::*;
let mut query = mod_lock_post_view.into_boxed();
query = query.filter(mod_user_id.eq(from_mod_user_id));
};
- query.limit(limit).offset(offset).order_by(when_.desc()).load::<Self>(conn)
+ query
+ .limit(limit)
+ .offset(offset)
+ .order_by(when_.desc())
+ .load::<Self>(conn)
}
}
}
}
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="mod_remove_comment_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "mod_remove_comment_view"]
pub struct ModRemoveCommentView {
pub id: i32,
pub mod_user_id: i32,
}
impl ModRemoveCommentView {
- pub fn list(conn: &PgConnection,
- from_community_id: Option<i32>,
- from_mod_user_id: Option<i32>,
- page: Option<i64>,
- limit: Option<i64>,
- ) -> Result<Vec<Self>, Error> {
+ pub fn list(
+ conn: &PgConnection,
+ from_community_id: Option<i32>,
+ from_mod_user_id: Option<i32>,
+ page: Option<i64>,
+ limit: Option<i64>,
+ ) -> Result<Vec<Self>, Error> {
use super::moderator_views::mod_remove_comment_view::dsl::*;
let mut query = mod_remove_comment_view.into_boxed();
query = query.filter(mod_user_id.eq(from_mod_user_id));
};
- query.limit(limit).offset(offset).order_by(when_.desc()).load::<Self>(conn)
+ query
+ .limit(limit)
+ .offset(offset)
+ .order_by(when_.desc())
+ .load::<Self>(conn)
}
}
}
}
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="mod_remove_community_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "mod_remove_community_view"]
pub struct ModRemoveCommunityView {
pub id: i32,
pub mod_user_id: i32,
}
impl ModRemoveCommunityView {
- pub fn list(conn: &PgConnection,
- from_mod_user_id: Option<i32>,
- page: Option<i64>,
- limit: Option<i64>,
- ) -> Result<Vec<Self>, Error> {
+ pub fn list(
+ conn: &PgConnection,
+ from_mod_user_id: Option<i32>,
+ page: Option<i64>,
+ limit: Option<i64>,
+ ) -> Result<Vec<Self>, Error> {
use super::moderator_views::mod_remove_community_view::dsl::*;
let mut query = mod_remove_community_view.into_boxed();
query = query.filter(mod_user_id.eq(from_mod_user_id));
};
- query.limit(limit).offset(offset).order_by(when_.desc()).load::<Self>(conn)
+ query
+ .limit(limit)
+ .offset(offset)
+ .order_by(when_.desc())
+ .load::<Self>(conn)
}
}
-
table! {
mod_ban_from_community_view (id) {
id -> Int4,
}
}
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="mod_ban_from_community_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "mod_ban_from_community_view"]
pub struct ModBanFromCommunityView {
pub id: i32,
pub mod_user_id: i32,
}
impl ModBanFromCommunityView {
- pub fn list(conn: &PgConnection,
- from_community_id: Option<i32>,
- from_mod_user_id: Option<i32>,
- page: Option<i64>,
- limit: Option<i64>,
- ) -> Result<Vec<Self>, Error> {
+ pub fn list(
+ conn: &PgConnection,
+ from_community_id: Option<i32>,
+ from_mod_user_id: Option<i32>,
+ page: Option<i64>,
+ limit: Option<i64>,
+ ) -> Result<Vec<Self>, Error> {
use super::moderator_views::mod_ban_from_community_view::dsl::*;
let mut query = mod_ban_from_community_view.into_boxed();
query = query.filter(mod_user_id.eq(from_mod_user_id));
};
- query.limit(limit).offset(offset).order_by(when_.desc()).load::<Self>(conn)
+ query
+ .limit(limit)
+ .offset(offset)
+ .order_by(when_.desc())
+ .load::<Self>(conn)
}
}
}
}
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="mod_ban_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "mod_ban_view"]
pub struct ModBanView {
pub id: i32,
pub mod_user_id: i32,
}
impl ModBanView {
- pub fn list(conn: &PgConnection,
- from_mod_user_id: Option<i32>,
- page: Option<i64>,
- limit: Option<i64>,
- ) -> Result<Vec<Self>, Error> {
+ pub fn list(
+ conn: &PgConnection,
+ from_mod_user_id: Option<i32>,
+ page: Option<i64>,
+ limit: Option<i64>,
+ ) -> Result<Vec<Self>, Error> {
use super::moderator_views::mod_ban_view::dsl::*;
let mut query = mod_ban_view.into_boxed();
query = query.filter(mod_user_id.eq(from_mod_user_id));
};
- query.limit(limit).offset(offset).order_by(when_.desc()).load::<Self>(conn)
+ query
+ .limit(limit)
+ .offset(offset)
+ .order_by(when_.desc())
+ .load::<Self>(conn)
}
}
}
}
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="mod_add_community_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "mod_add_community_view"]
pub struct ModAddCommunityView {
pub id: i32,
pub mod_user_id: i32,
}
impl ModAddCommunityView {
- pub fn list(conn: &PgConnection,
- from_community_id: Option<i32>,
- from_mod_user_id: Option<i32>,
- page: Option<i64>,
- limit: Option<i64>,
- ) -> Result<Vec<Self>, Error> {
+ pub fn list(
+ conn: &PgConnection,
+ from_community_id: Option<i32>,
+ from_mod_user_id: Option<i32>,
+ page: Option<i64>,
+ limit: Option<i64>,
+ ) -> Result<Vec<Self>, Error> {
use super::moderator_views::mod_add_community_view::dsl::*;
let mut query = mod_add_community_view.into_boxed();
query = query.filter(mod_user_id.eq(from_mod_user_id));
};
- query.limit(limit).offset(offset).order_by(when_.desc()).load::<Self>(conn)
+ query
+ .limit(limit)
+ .offset(offset)
+ .order_by(when_.desc())
+ .load::<Self>(conn)
}
}
}
}
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="mod_add_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "mod_add_view"]
pub struct ModAddView {
pub id: i32,
pub mod_user_id: i32,
}
impl ModAddView {
- pub fn list(conn: &PgConnection,
- from_mod_user_id: Option<i32>,
- page: Option<i64>,
- limit: Option<i64>,
- ) -> Result<Vec<Self>, Error> {
+ pub fn list(
+ conn: &PgConnection,
+ from_mod_user_id: Option<i32>,
+ page: Option<i64>,
+ limit: Option<i64>,
+ ) -> Result<Vec<Self>, Error> {
use super::moderator_views::mod_add_view::dsl::*;
let mut query = mod_add_view.into_boxed();
query = query.filter(mod_user_id.eq(from_mod_user_id));
};
- query.limit(limit).offset(offset).order_by(when_.desc()).load::<Self>(conn)
+ query
+ .limit(limit)
+ .offset(offset)
+ .order_by(when_.desc())
+ .load::<Self>(conn)
}
}
-use crate::schema::{post, post_like, post_saved, post_read};
use super::*;
+use crate::schema::{post, post_like, post_read, post_saved};
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
-#[table_name="post"]
+#[table_name = "post"]
pub struct Post {
pub id: i32,
pub name: String,
}
#[derive(Insertable, AsChangeset, Clone)]
-#[table_name="post"]
+#[table_name = "post"]
pub struct PostForm {
pub name: String,
pub url: Option<String>,
impl Crud<PostForm> for Post {
fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
- post.find(post_id)
- .first::<Self>(conn)
+ post.find(post_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> {
use crate::schema::post::dsl::*;
- diesel::delete(post.find(post_id))
- .execute(conn)
+ diesel::delete(post.find(post_id)).execute(conn)
}
fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
- insert_into(post)
- .values(new_post)
- .get_result::<Self>(conn)
+ insert_into(post).values(new_post).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
}
#[derive(Insertable, AsChangeset, Clone)]
-#[table_name="post_like"]
+#[table_name = "post_like"]
pub struct PostLikeForm {
pub post_id: i32,
pub user_id: i32,
- pub score: i16
+ pub score: i16,
}
-impl Likeable <PostLikeForm> for PostLike {
+impl Likeable<PostLikeForm> for PostLike {
fn read(conn: &PgConnection, post_id_from: i32) -> Result<Vec<Self>, Error> {
use crate::schema::post_like::dsl::*;
post_like
.filter(post_id.eq(post_id_from))
- .load::<Self>(conn)
+ .load::<Self>(conn)
}
fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
use crate::schema::post_like::dsl::*;
}
fn remove(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<usize, Error> {
use crate::schema::post_like::dsl::*;
- diesel::delete(post_like
- .filter(post_id.eq(post_like_form.post_id))
- .filter(user_id.eq(post_like_form.user_id)))
- .execute(conn)
+ diesel::delete(
+ post_like
+ .filter(post_id.eq(post_like_form.post_id))
+ .filter(user_id.eq(post_like_form.user_id)),
+ )
+ .execute(conn)
}
}
}
#[derive(Insertable, AsChangeset, Clone)]
-#[table_name="post_saved"]
+#[table_name = "post_saved"]
pub struct PostSavedForm {
pub post_id: i32,
pub user_id: i32,
}
-impl Saveable <PostSavedForm> for PostSaved {
+impl Saveable<PostSavedForm> for PostSaved {
fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
use crate::schema::post_saved::dsl::*;
insert_into(post_saved)
}
fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
use crate::schema::post_saved::dsl::*;
- diesel::delete(post_saved
- .filter(post_id.eq(post_saved_form.post_id))
- .filter(user_id.eq(post_saved_form.user_id)))
- .execute(conn)
+ diesel::delete(
+ post_saved
+ .filter(post_id.eq(post_saved_form.post_id))
+ .filter(user_id.eq(post_saved_form.user_id)),
+ )
+ .execute(conn)
}
}
}
#[derive(Insertable, AsChangeset, Clone)]
-#[table_name="post_read"]
+#[table_name = "post_read"]
pub struct PostReadForm {
pub post_id: i32,
pub user_id: i32,
}
-impl Readable <PostReadForm> for PostRead {
+impl Readable<PostReadForm> for PostRead {
fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
use crate::schema::post_read::dsl::*;
insert_into(post_read)
}
fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
use crate::schema::post_read::dsl::*;
- diesel::delete(post_read
- .filter(post_id.eq(post_read_form.post_id))
- .filter(user_id.eq(post_read_form.user_id)))
- .execute(conn)
+ diesel::delete(
+ post_read
+ .filter(post_id.eq(post_read_form.post_id))
+ .filter(user_id.eq(post_read_form.user_id)),
+ )
+ .execute(conn)
}
}
#[cfg(test)]
mod tests {
- use super::*;
use super::super::community::*;
use super::super::user::*;
- #[test]
+ use super::*;
+ #[test]
fn test_crud() {
let conn = establish_connection();
};
let inserted_community = Community::create(&conn, &new_community).unwrap();
-
+
let new_post = PostForm {
name: "A test post".into(),
url: None,
deleted: None,
locked: None,
nsfw: false,
- updated: None
+ updated: None,
};
let inserted_post = Post::create(&conn, &new_post).unwrap();
locked: false,
nsfw: false,
deleted: false,
- updated: None
+ updated: None,
};
// Post Like
let post_like_form = PostLikeForm {
post_id: inserted_post.id,
user_id: inserted_user.id,
- score: 1
+ score: 1,
};
let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
post_id: inserted_post.id,
user_id: inserted_user.id,
published: inserted_post_like.published,
- score: 1
+ score: 1,
};
// Post Save
user_id: inserted_user.id,
published: inserted_post_saved.published,
};
-
+
// Post Read
let post_read_form = PostReadForm {
post_id: inserted_post.id,
user_id: inserted_user.id,
published: inserted_post_read.published,
};
-
+
let read_post = Post::read(&conn, inserted_post.id).unwrap();
let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
assert_eq!(1, saved_removed);
assert_eq!(1, read_removed);
assert_eq!(1, num_deleted);
-
}
}
use super::*;
-#[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
+#[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
pub enum PostListingType {
- All, Subscribed, Community
+ All,
+ Subscribed,
+ Community,
}
// The faked schema since diesel doesn't do views
}
}
-
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="post_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "post_view"]
pub struct PostView {
pub id: i32,
pub name: String,
impl PostView {
pub fn list(
- conn: &PgConnection,
- type_: PostListingType,
- sort: &SortType,
- for_community_id: Option<i32>,
- for_creator_id: Option<i32>,
+ conn: &PgConnection,
+ type_: PostListingType,
+ sort: &SortType,
+ for_community_id: Option<i32>,
+ for_creator_id: Option<i32>,
search_term: Option<String>,
url_search: Option<String>,
- my_user_id: Option<i32>,
+ my_user_id: Option<i32>,
show_nsfw: bool,
saved_only: bool,
unread_only: bool,
page: Option<i64>,
limit: Option<i64>,
- ) -> Result<Vec<Self>, Error> {
+ ) -> Result<Vec<Self>, Error> {
use super::post_view::post_view::dsl::*;
let (limit, offset) = limit_and_offset(page, limit);
};
match type_ {
- PostListingType::Subscribed => {
+ PostListingType::Subscribed => {
query = query.filter(subscribed.eq(true));
- },
+ }
_ => {}
};
};
query = match sort {
- SortType::Hot => query.order_by(hot_rank.desc())
+ SortType::Hot => query
+ .order_by(hot_rank.desc())
.then_order_by(published.desc()),
SortType::New => query.order_by(published.desc()),
SortType::TopAll => query.order_by(score.desc()),
SortType::TopYear => query
.filter(published.gt(now - 1.years()))
.order_by(score.desc()),
- SortType::TopMonth => query
- .filter(published.gt(now - 1.months()))
- .order_by(score.desc()),
- SortType::TopWeek => query
- .filter(published.gt(now - 1.weeks()))
- .order_by(score.desc()),
- SortType::TopDay => query
- .filter(published.gt(now - 1.days()))
- .order_by(score.desc())
+ SortType::TopMonth => query
+ .filter(published.gt(now - 1.months()))
+ .order_by(score.desc()),
+ SortType::TopWeek => query
+ .filter(published.gt(now - 1.weeks()))
+ .order_by(score.desc()),
+ SortType::TopDay => query
+ .filter(published.gt(now - 1.days()))
+ .order_by(score.desc()),
};
query = query
.filter(community_removed.eq(false))
.filter(community_deleted.eq(false));
- query.load::<Self>(conn)
+ query.load::<Self>(conn)
}
-
- pub fn read(conn: &PgConnection, from_post_id: i32, my_user_id: Option<i32>) -> Result<Self, Error> {
-
+ pub fn read(
+ conn: &PgConnection,
+ from_post_id: i32,
+ my_user_id: Option<i32>,
+ ) -> Result<Self, Error> {
use super::post_view::post_view::dsl::*;
use diesel::prelude::*;
}
}
-
-
#[cfg(test)]
mod tests {
- use super::*;
use super::super::community::*;
- use super::super::user::*;
use super::super::post::*;
+ use super::super::user::*;
+ use super::*;
#[test]
fn test_crud() {
let conn = establish_connection();
let post_like_form = PostLikeForm {
post_id: inserted_post.id,
user_id: inserted_user.id,
- score: 1
+ score: 1,
};
let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
post_id: inserted_post.id,
user_id: inserted_user.id,
published: inserted_post_like.published,
- score: 1
+ score: 1,
};
let post_like_form = PostLikeForm {
post_id: inserted_post.id,
user_id: inserted_user.id,
- score: 1
+ score: 1,
};
// the non user version
nsfw: false,
};
-
let read_post_listings_with_user = PostView::list(
- &conn,
- PostListingType::Community,
- &SortType::New,
- Some(inserted_community.id),
- None,
+ &conn,
+ PostListingType::Community,
+ &SortType::New,
+ Some(inserted_community.id),
None,
None,
- Some(inserted_user.id),
+ None,
+ Some(inserted_user.id),
+ false,
false,
- false,
- false,
- None,
- None).unwrap();
+ false,
+ None,
+ None,
+ )
+ .unwrap();
let read_post_listings_no_user = PostView::list(
- &conn,
- PostListingType::Community,
- &SortType::New,
- Some(inserted_community.id),
- None,
- None,
+ &conn,
+ PostListingType::Community,
+ &SortType::New,
+ Some(inserted_community.id),
None,
None,
+ None,
+ None,
+ false,
false,
- false,
- false,
- None,
- None).unwrap();
+ false,
+ None,
+ None,
+ )
+ .unwrap();
let read_post_listing_no_user = PostView::read(&conn, inserted_post.id, None).unwrap();
- let read_post_listing_with_user = PostView::read(&conn, inserted_post.id, Some(inserted_user.id)).unwrap();
+ let read_post_listing_with_user =
+ PostView::read(&conn, inserted_post.id, Some(inserted_user.id)).unwrap();
let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
User_::delete(&conn, inserted_user.id).unwrap();
// The with user
- assert_eq!(expected_post_listing_with_user, read_post_listings_with_user[0]);
+ assert_eq!(
+ expected_post_listing_with_user,
+ read_post_listings_with_user[0]
+ );
assert_eq!(expected_post_listing_with_user, read_post_listing_with_user);
assert_eq!(1, read_post_listings_with_user.len());
+use super::*;
use crate::schema::user_;
use crate::schema::user_::dsl::*;
-use super::*;
-use crate::{Settings, is_email_regex};
-use jsonwebtoken::{encode, decode, Header, Validation, TokenData};
-use bcrypt::{DEFAULT_COST, hash};
+use crate::{is_email_regex, Settings};
+use bcrypt::{hash, DEFAULT_COST};
+use jsonwebtoken::{decode, encode, Header, TokenData, Validation};
#[derive(Queryable, Identifiable, PartialEq, Debug)]
-#[table_name="user_"]
+#[table_name = "user_"]
pub struct User_ {
pub id: i32,
pub name: String,
}
#[derive(Insertable, AsChangeset, Clone)]
-#[table_name="user_"]
+#[table_name = "user_"]
pub struct UserForm {
- pub name: String,
- pub fedi_name: String,
- pub preferred_username: Option<String>,
- pub password_encrypted: String,
- pub admin: bool,
- pub banned: bool,
- pub email: Option<String>,
- pub updated: Option<chrono::NaiveDateTime>,
- pub show_nsfw: bool,
+ pub name: String,
+ pub fedi_name: String,
+ pub preferred_username: Option<String>,
+ pub password_encrypted: String,
+ pub admin: bool,
+ pub banned: bool,
+ pub email: Option<String>,
+ pub updated: Option<chrono::NaiveDateTime>,
+ pub show_nsfw: bool,
}
impl Crud<UserForm> for User_ {
fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
use crate::schema::user_::dsl::*;
- user_.find(user_id)
- .first::<Self>(conn)
+ user_.find(user_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, user_id: i32) -> Result<usize, Error> {
- diesel::delete(user_.find(user_id))
- .execute(conn)
+ diesel::delete(user_.find(user_id)).execute(conn)
}
fn create(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
- insert_into(user_)
- .values(form)
- .get_result::<Self>(conn)
+ insert_into(user_).values(form).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, user_id: i32, form: &UserForm) -> Result<Self, Error> {
diesel::update(user_.find(user_id))
impl User_ {
pub fn register(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
let mut edited_user = form.clone();
- let password_hash = hash(&form.password_encrypted, DEFAULT_COST)
- .expect("Couldn't hash password");
+ let password_hash =
+ hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
edited_user.password_encrypted = password_hash;
Self::create(&conn, &edited_user)
-
}
pub fn read_from_name(conn: &PgConnection, from_user_name: String) -> Result<Self, Error> {
- user_.filter(name.eq(from_user_name))
- .first::<Self>(conn)
+ user_.filter(name.eq(from_user_name)).first::<Self>(conn)
}
}
iss: self.fedi_name.to_owned(),
show_nsfw: self.show_nsfw,
};
- encode(&Header::default(), &my_claims, Settings::get().jwt_secret.as_ref()).unwrap()
+ encode(
+ &Header::default(),
+ &my_claims,
+ Settings::get().jwt_secret.as_ref(),
+ )
+ .unwrap()
}
- pub fn find_by_email_or_username(conn: &PgConnection, username_or_email: &str) -> Result<Self, Error> {
+ pub fn find_by_email_or_username(
+ conn: &PgConnection,
+ username_or_email: &str,
+ ) -> Result<Self, Error> {
if is_email_regex(username_or_email) {
- user_.filter(email.eq(username_or_email))
+ user_
+ .filter(email.eq(username_or_email))
.first::<User_>(conn)
} else {
- user_.filter(name.eq(username_or_email))
+ user_
+ .filter(name.eq(username_or_email))
.first::<User_>(conn)
}
}
let claims: Claims = Claims::decode(&jwt).expect("Invalid token").claims;
Self::read(&conn, claims.id)
}
-
}
-
#[cfg(test)]
mod tests {
use super::*;
- #[test]
+ #[test]
fn test_crud() {
let conn = establish_connection();
-
+
let new_user = UserForm {
name: "thommy".into(),
fedi_name: "rrf".into(),
updated: None,
show_nsfw: false,
};
-
+
let read_user = User_::read(&conn, inserted_user.id).unwrap();
let updated_user = User_::update(&conn, inserted_user.id, &new_user).unwrap();
let num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
}
}
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
-#[table_name="user_view"]
+#[derive(
+ Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
+)]
+#[table_name = "user_view"]
pub struct UserView {
pub id: i32,
pub name: String,
}
impl UserView {
-
- pub fn list(conn: &PgConnection,
- sort: &SortType,
- search_term: Option<String>,
- page: Option<i64>,
- limit: Option<i64>,
- ) -> Result<Vec<Self>, Error> {
+ pub fn list(
+ conn: &PgConnection,
+ sort: &SortType,
+ search_term: Option<String>,
+ page: Option<i64>,
+ limit: Option<i64>,
+ ) -> Result<Vec<Self>, Error> {
use super::user_view::user_view::dsl::*;
let (limit, offset) = limit_and_offset(page, limit);
};
query = match sort {
- SortType::Hot => query.order_by(comment_score.desc())
+ SortType::Hot => query
+ .order_by(comment_score.desc())
.then_order_by(published.desc()),
SortType::New => query.order_by(published.desc()),
SortType::TopAll => query.order_by(comment_score.desc()),
SortType::TopYear => query
.filter(published.gt(now - 1.years()))
.order_by(comment_score.desc()),
- SortType::TopMonth => query
- .filter(published.gt(now - 1.months()))
- .order_by(comment_score.desc()),
- SortType::TopWeek => query
- .filter(published.gt(now - 1.weeks()))
- .order_by(comment_score.desc()),
- SortType::TopDay => query
- .filter(published.gt(now - 1.days()))
- .order_by(comment_score.desc())
+ SortType::TopMonth => query
+ .filter(published.gt(now - 1.months()))
+ .order_by(comment_score.desc()),
+ SortType::TopWeek => query
+ .filter(published.gt(now - 1.weeks()))
+ .order_by(comment_score.desc()),
+ SortType::TopDay => query
+ .filter(published.gt(now - 1.days()))
+ .order_by(comment_score.desc()),
};
- query = query
- .limit(limit)
- .offset(offset);
+ query = query.limit(limit).offset(offset);
- query.load::<Self>(conn)
+ query.load::<Self>(conn)
}
pub fn read(conn: &PgConnection, from_user_id: i32) -> Result<Self, Error> {
use super::user_view::user_view::dsl::*;
- user_view.find(from_user_id)
- .first::<Self>(conn)
+ user_view.find(from_user_id).first::<Self>(conn)
}
pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
use super::user_view::user_view::dsl::*;
- user_view.filter(admin.eq(true))
- .load::<Self>(conn)
+ user_view.filter(admin.eq(true)).load::<Self>(conn)
}
pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
use super::user_view::user_view::dsl::*;
- user_view.filter(banned.eq(true))
- .load::<Self>(conn)
+ user_view.filter(banned.eq(true)).load::<Self>(conn)
}
}
-
#![recursion_limit = "512"]
-#[macro_use] pub extern crate strum_macros;
-#[macro_use] pub extern crate lazy_static;
-#[macro_use] pub extern crate failure;
-#[macro_use] pub extern crate diesel;
-pub extern crate dotenv;
-pub extern crate chrono;
-pub extern crate serde;
-pub extern crate serde_json;
+#[macro_use]
+pub extern crate strum_macros;
+#[macro_use]
+pub extern crate lazy_static;
+#[macro_use]
+pub extern crate failure;
+#[macro_use]
+pub extern crate diesel;
pub extern crate actix;
pub extern crate actix_web;
-pub extern crate rand;
-pub extern crate strum;
-pub extern crate jsonwebtoken;
pub extern crate bcrypt;
+pub extern crate chrono;
+pub extern crate dotenv;
+pub extern crate jsonwebtoken;
+pub extern crate rand;
pub extern crate regex;
+pub extern crate serde;
+pub extern crate serde_json;
+pub extern crate strum;
-pub mod schema;
pub mod api;
pub mod apub;
pub mod db;
+pub mod schema;
pub mod websocket;
+use chrono::{DateTime, NaiveDateTime, Utc};
use dotenv::dotenv;
-use std::env;
use regex::Regex;
-use chrono::{DateTime, NaiveDateTime, Utc};
+use std::env;
pub struct Settings {
db_url: String,
fn get() -> Self {
dotenv().ok();
Settings {
- db_url: env::var("DATABASE_URL")
- .expect("DATABASE_URL must be set"),
- hostname: env::var("HOSTNAME").unwrap_or("rrr".to_string()),
- jwt_secret: env::var("JWT_SECRET").unwrap_or("changeme".to_string()),
+ db_url: env::var("DATABASE_URL").expect("DATABASE_URL must be set"),
+ hostname: env::var("HOSTNAME").unwrap_or("rrr".to_string()),
+ jwt_secret: env::var("JWT_SECRET").unwrap_or("changeme".to_string()),
}
}
fn api_endpoint(&self) -> String {
chrono::prelude::Utc::now().naive_utc()
}
-pub fn naive_from_unix(time: i64) -> NaiveDateTime {
+pub fn naive_from_unix(time: i64) -> NaiveDateTime {
NaiveDateTime::from_timestamp(time, 0)
}
#[cfg(test)]
mod tests {
- use crate::{Settings, is_email_regex, remove_slurs, has_slurs};
+ use crate::{has_slurs, is_email_regex, remove_slurs, Settings};
#[test]
fn test_api() {
assert_eq!(Settings::get().api_endpoint(), "rrr/api/v1");
}
- #[test] fn test_email() {
+ #[test]
+ fn test_email() {
assert!(is_email_regex("gush@gmail.com"));
assert!(!is_email_regex("nada_neutho"));
- }
+ }
- #[test] fn test_slur_filter() {
+ #[test]
+ fn test_slur_filter() {
let test = "coons test dindu ladyboy tranny. This is a bunch of other safe text.".to_string();
let slur_free = "No slurs here";
- assert_eq!(remove_slurs(&test), "*removed* test *removed* *removed* *removed*. This is a bunch of other safe text.".to_string());
+ assert_eq!(
+ remove_slurs(&test),
+ "*removed* test *removed* *removed* *removed*. This is a bunch of other safe text."
+ .to_string()
+ );
assert!(has_slurs(&test));
assert!(!has_slurs(slur_free));
- }
+ }
}
req: HttpRequest,
stream: web::Payload,
chat_server: web::Data<Addr<ChatServer>>,
- ) -> Result<HttpResponse, Error> {
+) -> Result<HttpResponse, Error> {
ws::start(
WSSession {
cs_addr: chat_server.get_ref().to_owned(),
},
&req,
stream,
- )
+ )
}
struct WSSession {
// before processing any other events.
// across all routes within application
let addr = ctx.address();
- self.cs_addr
+ self
+ .cs_addr
.send(Connect {
addr: addr.recipient(),
ip: self.ip.to_owned(),
})
- .into_actor(self)
+ .into_actor(self)
.then(|res, act, ctx| {
match res {
Ok(res) => act.id = res,
}
fut::ok(())
})
- .wait(ctx);
+ .wait(ctx);
}
fn stopping(&mut self, _ctx: &mut Self::Context) -> Running {
let m = text.trim().to_owned();
println!("WEBSOCKET MESSAGE: {:?} from id: {}", &m, self.id);
- self.cs_addr
+ self
+ .cs_addr
.send(StandardMessage {
id: self.id,
msg: m,
})
- .into_actor(self)
+ .into_actor(self)
.then(|res, _, ctx| {
match res {
Ok(res) => ctx.text(res),
}
fut::ok(())
})
- .wait(ctx);
+ .wait(ctx);
}
ws::Message::Binary(_bin) => println!("Unexpected binary"),
ws::Message::Close(_) => {
.service(actix_files::Files::new("/static", front_end_dir()))
})
.bind("0.0.0.0:8536")
- .unwrap()
- .start();
+ .unwrap()
+ .start();
println!("Started http server: 0.0.0.0:8536");
let _ = sys.run();
joinable!(user_ban -> user_ (user_id));
allow_tables_to_appear_in_same_query!(
- category,
- comment,
- comment_like,
- comment_saved,
- community,
- community_follower,
- community_moderator,
- community_user_ban,
- mod_add,
- mod_add_community,
- mod_ban,
- mod_ban_from_community,
- mod_lock_post,
- mod_remove_comment,
- mod_remove_community,
- mod_remove_post,
- post,
- post_like,
- post_read,
- post_saved,
- site,
- user_,
- user_ban,
+ category,
+ comment,
+ comment_like,
+ comment_saved,
+ community,
+ community_follower,
+ community_moderator,
+ community_user_ban,
+ mod_add,
+ mod_add_community,
+ mod_ban,
+ mod_ban_from_community,
+ mod_lock_post,
+ mod_remove_comment,
+ mod_remove_community,
+ mod_remove_post,
+ post,
+ post_like,
+ post_read,
+ post_saved,
+ site,
+ user_,
+ user_ban,
);
//! room through `ChatServer`.
use actix::prelude::*;
+use failure::Error;
use rand::{rngs::ThreadRng, Rng};
-use std::collections::{HashMap, HashSet};
use serde::{Deserialize, Serialize};
-use serde_json::{Value};
+use serde_json::Value;
+use std::collections::{HashMap, HashSet};
use std::str::FromStr;
-use failure::Error;
-use std::time::{SystemTime};
+use std::time::SystemTime;
-use crate::api::*;
-use crate::api::user::*;
+use crate::api::comment::*;
use crate::api::community::*;
use crate::api::post::*;
-use crate::api::comment::*;
use crate::api::site::*;
+use crate::api::user::*;
+use crate::api::*;
const RATE_LIMIT_MESSAGE: i32 = 30;
const RATE_LIMIT_MESSAGES_PER_SECOND: i32 = 60;
const RATE_LIMIT_POST: i32 = 1;
-const RATE_LIMIT_POSTS_PER_SECOND: i32 = 60*10;
+const RATE_LIMIT_POSTS_PER_SECOND: i32 = 60 * 10;
const RATE_LIMIT_REGISTER: i32 = 1;
-const RATE_LIMIT_REGISTER_PER_SECOND: i32 = 60*60;
-
+const RATE_LIMIT_REGISTER_PER_SECOND: i32 = 60 * 60;
/// Chat server sends this messages to session
#[derive(Message)]
#[derive(Debug)]
pub struct RateLimitBucket {
last_checked: SystemTime,
- allowance: f64
+ allowance: f64,
}
pub struct SessionInfo {
&self.rooms.get_mut(&room_id).unwrap().insert(id);
}
- fn send_community_message(&self, community_id: &i32, message: &str, skip_id: usize) -> Result<(), Error> {
- use crate::db::*;
+ fn send_community_message(
+ &self,
+ community_id: &i32,
+ message: &str,
+ skip_id: usize,
+ ) -> Result<(), Error> {
use crate::db::post_view::*;
+ use crate::db::*;
let conn = establish_connection();
let posts = PostView::list(
&conn,
- PostListingType::Community,
- &SortType::New,
- Some(*community_id),
+ PostListingType::Community,
+ &SortType::New,
+ Some(*community_id),
+ None,
None,
- None,
None,
None,
false,
false,
false,
None,
- Some(9999))?;
+ Some(9999),
+ )?;
for post in posts {
self.send_room_message(&post.id, message, skip_id);
}
}
if rate_limit.allowance < 1.0 {
- println!("Rate limited IP: {}, time_passed: {}, allowance: {}", &info.ip, time_passed, rate_limit.allowance);
+ println!(
+ "Rate limited IP: {}, time_passed: {}, allowance: {}",
+ &info.ip, time_passed, rate_limit.allowance
+ );
Err(APIError {
- op: "Rate Limit".to_string(),
+ op: "Rate Limit".to_string(),
message: format!("Too many requests. {} per {} seconds", rate, per),
})?
} else {
}
}
-
/// Make actor from `ChatServer`
impl Actor for ChatServer {
/// We are going to use simple Context, we just need ability to communicate
type Result = usize;
fn handle(&mut self, msg: Connect, _ctx: &mut Context<Self>) -> Self::Result {
-
// notify all users in same room
// self.send_room_message(&"Main".to_owned(), "Someone joined", 0);
let id = self.rng.gen::<usize>();
println!("{} joined", &msg.ip);
- self.sessions.insert(id, SessionInfo {
- addr: msg.addr,
- ip: msg.ip.to_owned(),
- });
+ self.sessions.insert(
+ id,
+ SessionInfo {
+ addr: msg.addr,
+ ip: msg.ip.to_owned(),
+ },
+ );
if self.rate_limits.get(&msg.ip).is_none() {
- self.rate_limits.insert(msg.ip, RateLimitBucket {
- last_checked: SystemTime::now(),
- allowance: -2f64,
- });
+ self.rate_limits.insert(
+ msg.ip,
+ RateLimitBucket {
+ last_checked: SystemTime::now(),
+ allowance: -2f64,
+ },
+ );
}
id
type Result = ();
fn handle(&mut self, msg: Disconnect, _: &mut Context<Self>) {
-
// let mut rooms: Vec<i32> = Vec::new();
// remove address
impl Handler<StandardMessage> for ChatServer {
type Result = MessageResult<StandardMessage>;
-
fn handle(&mut self, msg: StandardMessage, _: &mut Context<Self>) -> Self::Result {
-
let msg_out = match parse_json_message(self, msg) {
Ok(m) => m,
- Err(e) => e.to_string()
+ Err(e) => e.to_string(),
};
MessageResult(msg_out)
}
fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<String, Error> {
-
let json: Value = serde_json::from_str(&msg.msg)?;
let data = &json["data"].to_string();
let op = &json["op"].as_str().ok_or(APIError {
- op: "Unknown op type".to_string(),
+ op: "Unknown op type".to_string(),
message: format!("Unknown op type"),
})?;
let login: Login = serde_json::from_str(data)?;
let res = Oper::new(user_operation, login).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::Register => {
chat.check_rate_limit_register(msg.id)?;
let register: Register = serde_json::from_str(data)?;
let res = Oper::new(user_operation, register).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::GetUserDetails => {
let get_user_details: GetUserDetails = serde_json::from_str(data)?;
let res = Oper::new(user_operation, get_user_details).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::SaveUserSettings => {
let save_user_settings: SaveUserSettings = serde_json::from_str(data)?;
let res = Oper::new(user_operation, save_user_settings).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::AddAdmin => {
let add_admin: AddAdmin = serde_json::from_str(data)?;
let res = Oper::new(user_operation, add_admin).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::BanUser => {
let ban_user: BanUser = serde_json::from_str(data)?;
let res = Oper::new(user_operation, ban_user).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::GetReplies => {
let get_replies: GetReplies = serde_json::from_str(data)?;
let res = Oper::new(user_operation, get_replies).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::MarkAllAsRead => {
let mark_all_as_read: MarkAllAsRead = serde_json::from_str(data)?;
let res = Oper::new(user_operation, mark_all_as_read).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::GetCommunity => {
let get_community: GetCommunity = serde_json::from_str(data)?;
let res = Oper::new(user_operation, get_community).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::ListCommunities => {
let list_communities: ListCommunities = serde_json::from_str(data)?;
let res = Oper::new(user_operation, list_communities).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::CreateCommunity => {
chat.check_rate_limit_register(msg.id)?;
let create_community: CreateCommunity = serde_json::from_str(data)?;
let res = Oper::new(user_operation, create_community).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::EditCommunity => {
let edit_community: EditCommunity = serde_json::from_str(data)?;
let res = Oper::new(user_operation, edit_community).perform()?;
let community_sent_str = serde_json::to_string(&community_sent)?;
chat.send_community_message(&community_sent.community.id, &community_sent_str, msg.id)?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::FollowCommunity => {
let follow_community: FollowCommunity = serde_json::from_str(data)?;
let res = Oper::new(user_operation, follow_community).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::GetFollowedCommunities => {
let followed_communities: GetFollowedCommunities = serde_json::from_str(data)?;
let res = Oper::new(user_operation, followed_communities).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::BanFromCommunity => {
let ban_from_community: BanFromCommunity = serde_json::from_str(data)?;
let community_id = ban_from_community.community_id;
let res_str = serde_json::to_string(&res)?;
chat.send_community_message(&community_id, &res_str, msg.id)?;
Ok(res_str)
- },
+ }
UserOperation::AddModToCommunity => {
let mod_add_to_community: AddModToCommunity = serde_json::from_str(data)?;
let community_id = mod_add_to_community.community_id;
let res_str = serde_json::to_string(&res)?;
chat.send_community_message(&community_id, &res_str, msg.id)?;
Ok(res_str)
- },
+ }
UserOperation::ListCategories => {
let list_categories: ListCategories = ListCategories;
let res = Oper::new(user_operation, list_categories).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::CreatePost => {
chat.check_rate_limit_post(msg.id)?;
let create_post: CreatePost = serde_json::from_str(data)?;
let res = Oper::new(user_operation, create_post).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::GetPost => {
let get_post: GetPost = serde_json::from_str(data)?;
chat.join_room(get_post.id, msg.id);
let res = Oper::new(user_operation, get_post).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::GetPosts => {
let get_posts: GetPosts = serde_json::from_str(data)?;
let res = Oper::new(user_operation, get_posts).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::CreatePostLike => {
chat.check_rate_limit_message(msg.id)?;
let create_post_like: CreatePostLike = serde_json::from_str(data)?;
let res = Oper::new(user_operation, create_post_like).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::EditPost => {
let edit_post: EditPost = serde_json::from_str(data)?;
let res = Oper::new(user_operation, edit_post).perform()?;
let post_sent_str = serde_json::to_string(&post_sent)?;
chat.send_room_message(&post_sent.post.id, &post_sent_str, msg.id);
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::SavePost => {
let save_post: SavePost = serde_json::from_str(data)?;
let res = Oper::new(user_operation, save_post).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::CreateComment => {
chat.check_rate_limit_message(msg.id)?;
let create_comment: CreateComment = serde_json::from_str(data)?;
let comment_sent_str = serde_json::to_string(&comment_sent)?;
chat.send_room_message(&post_id, &comment_sent_str, msg.id);
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::EditComment => {
let edit_comment: EditComment = serde_json::from_str(data)?;
let post_id = edit_comment.post_id;
let comment_sent_str = serde_json::to_string(&comment_sent)?;
chat.send_room_message(&post_id, &comment_sent_str, msg.id);
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::SaveComment => {
let save_comment: SaveComment = serde_json::from_str(data)?;
let res = Oper::new(user_operation, save_comment).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::CreateCommentLike => {
chat.check_rate_limit_message(msg.id)?;
let create_comment_like: CreateCommentLike = serde_json::from_str(data)?;
let comment_sent_str = serde_json::to_string(&comment_sent)?;
chat.send_room_message(&post_id, &comment_sent_str, msg.id);
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::GetModlog => {
let get_modlog: GetModlog = serde_json::from_str(data)?;
let res = Oper::new(user_operation, get_modlog).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::CreateSite => {
let create_site: CreateSite = serde_json::from_str(data)?;
let res = Oper::new(user_operation, create_site).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::EditSite => {
let edit_site: EditSite = serde_json::from_str(data)?;
let res = Oper::new(user_operation, edit_site).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::GetSite => {
let get_site: GetSite = serde_json::from_str(data)?;
let res = Oper::new(user_operation, get_site).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::Search => {
let search: Search = serde_json::from_str(data)?;
let res = Oper::new(user_operation, search).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::TransferCommunity => {
let transfer_community: TransferCommunity = serde_json::from_str(data)?;
let res = Oper::new(user_operation, transfer_community).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
UserOperation::TransferSite => {
let transfer_site: TransferSite = serde_json::from_str(data)?;
let res = Oper::new(user_operation, transfer_site).perform()?;
Ok(serde_json::to_string(&res)?)
- },
+ }
}
}