From e14e6e53cd969039087df17bc4407d1b7444e05d Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sat, 20 Apr 2019 00:24:59 -0700 Subject: [PATCH] More moderation fixed --- server/src/actions/comment.rs | 6 ++-- server/src/actions/comment_view.rs | 6 ++-- server/src/actions/community.rs | 4 +-- server/src/actions/moderator.rs | 6 ++-- server/src/actions/post.rs | 10 +++--- server/src/actions/post_view.rs | 6 ++-- server/src/websocket_server/server.rs | 44 ++++++++++++++++----------- ui/src/components/comment-node.tsx | 7 ++--- ui/src/components/post-listing.tsx | 41 ++++++++++++++++++++----- ui/src/components/post.tsx | 9 +++++- ui/src/interfaces.ts | 6 ++-- 11 files changed, 93 insertions(+), 52 deletions(-) diff --git a/server/src/actions/comment.rs b/server/src/actions/comment.rs index c3aa0107..36da90c6 100644 --- a/server/src/actions/comment.rs +++ b/server/src/actions/comment.rs @@ -184,7 +184,7 @@ mod tests { description: None, category_id: 1, creator_id: inserted_user.id, - removed: false, + removed: None, updated: None }; @@ -196,8 +196,8 @@ mod tests { url: None, body: None, community_id: inserted_community.id, - removed: false, - locked: false, + removed: None, + locked: None, updated: None }; diff --git a/server/src/actions/comment_view.rs b/server/src/actions/comment_view.rs index 36043716..4e3d99b7 100644 --- a/server/src/actions/comment_view.rs +++ b/server/src/actions/comment_view.rs @@ -168,7 +168,7 @@ mod tests { description: None, category_id: 1, creator_id: inserted_user.id, - removed: false, + removed: None, updated: None }; @@ -180,8 +180,8 @@ mod tests { url: None, body: None, community_id: inserted_community.id, - removed: false, - locked: false, + removed: None, + locked: None, updated: None }; diff --git a/server/src/actions/community.rs b/server/src/actions/community.rs index 594518ba..7a69c807 100644 --- a/server/src/actions/community.rs +++ b/server/src/actions/community.rs @@ -27,7 +27,7 @@ pub struct CommunityForm { pub description: Option, pub category_id: i32, pub creator_id: i32, - pub removed: bool, + pub removed: Option, pub updated: Option } @@ -236,7 +236,7 @@ mod tests { title: "nada".to_owned(), description: None, category_id: 1, - removed: false, + removed: None, updated: None, }; diff --git a/server/src/actions/moderator.rs b/server/src/actions/moderator.rs index e0d885ce..a97b2120 100644 --- a/server/src/actions/moderator.rs +++ b/server/src/actions/moderator.rs @@ -441,7 +441,7 @@ mod tests { description: None, category_id: 1, creator_id: inserted_user.id, - removed: false, + removed: None, updated: None }; @@ -453,8 +453,8 @@ mod tests { body: None, creator_id: inserted_user.id, community_id: inserted_community.id, - removed: false, - locked: false, + removed: None, + locked: None, updated: None }; diff --git a/server/src/actions/post.rs b/server/src/actions/post.rs index 0fd0e5c5..4dd4561d 100644 --- a/server/src/actions/post.rs +++ b/server/src/actions/post.rs @@ -28,8 +28,8 @@ pub struct PostForm { pub body: Option, pub creator_id: i32, pub community_id: i32, - pub removed: bool, - pub locked: bool, + pub removed: Option, + pub locked: Option, pub updated: Option } @@ -198,7 +198,7 @@ mod tests { description: None, category_id: 1, creator_id: inserted_user.id, - removed: false, + removed: None, updated: None }; @@ -210,8 +210,8 @@ mod tests { body: None, creator_id: inserted_user.id, community_id: inserted_community.id, - removed: false, - locked: false, + removed: None, + locked: None, updated: None }; diff --git a/server/src/actions/post_view.rs b/server/src/actions/post_view.rs index 78fcef63..28e5fb98 100644 --- a/server/src/actions/post_view.rs +++ b/server/src/actions/post_view.rs @@ -200,7 +200,7 @@ mod tests { description: None, creator_id: inserted_user.id, category_id: 1, - removed: false, + removed: None, updated: None }; @@ -212,8 +212,8 @@ mod tests { body: None, creator_id: inserted_user.id, community_id: inserted_community.id, - removed: false, - locked: false, + removed: None, + locked: None, updated: None }; diff --git a/server/src/websocket_server/server.rs b/server/src/websocket_server/server.rs index d1f72109..63d767c2 100644 --- a/server/src/websocket_server/server.rs +++ b/server/src/websocket_server/server.rs @@ -261,8 +261,8 @@ pub struct EditPost { name: String, url: Option, body: Option, - removed: bool, - locked: bool, + removed: Option, + locked: Option, reason: Option, auth: String } @@ -281,7 +281,7 @@ pub struct EditCommunity { title: String, description: Option, category_id: i32, - removed: bool, + removed: Option, reason: Option, expires: Option, auth: String @@ -836,14 +836,13 @@ impl Perform for CreateCommunity { } // When you create a community, make sure the user becomes a moderator and a follower - let community_form = CommunityForm { name: self.name.to_owned(), title: self.title.to_owned(), description: self.description.to_owned(), category_id: self.category_id, creator_id: user_id, - removed: false, + removed: None, updated: None, }; @@ -988,8 +987,8 @@ impl Perform for CreatePost { body: self.body.to_owned(), community_id: self.community_id, creator_id: user_id, - removed: false, - locked: false, + removed: None, + locked: None, updated: None }; @@ -1612,15 +1611,24 @@ impl Perform for EditPost { let user_id = claims.id; - // Verify its the creator or a mod - let mut editors: Vec = CommunityModeratorView::for_community(&conn, self.community_id) + // Verify its the creator or a mod or admin + let mut editors: Vec = vec![self.creator_id]; + editors.append( + &mut CommunityModeratorView::for_community(&conn, self.community_id) .unwrap() .into_iter() .map(|m| m.user_id) - .collect(); - editors.push(self.creator_id); + .collect() + ); + editors.append( + &mut UserView::admins(&conn) + .unwrap() + .into_iter() + .map(|a| a.id) + .collect() + ); if !editors.contains(&user_id) { - return self.error("Not allowed to edit comment."); + return self.error("Not allowed to edit post."); } // Check for a community ban @@ -1652,21 +1660,21 @@ impl Perform for EditPost { }; // Mod tables - if self.removed { + if let Some(removed) = self.removed.to_owned() { let form = ModRemovePostForm { mod_user_id: user_id, post_id: self.edit_id, - removed: Some(self.removed), + removed: Some(removed), reason: self.reason.to_owned(), }; ModRemovePost::create(&conn, &form).unwrap(); } - if self.locked { + if let Some(locked) = self.locked.to_owned() { let form = ModLockPostForm { mod_user_id: user_id, post_id: self.edit_id, - locked: Some(self.locked), + locked: Some(locked), }; ModLockPost::create(&conn, &form).unwrap(); } @@ -1803,7 +1811,7 @@ impl Perform for EditCommunity { }; // Mod tables - if self.removed { + if let Some(removed) = self.removed.to_owned() { let expires = match self.expires { Some(time) => Some(naive_from_unix(time)), None => None @@ -1811,7 +1819,7 @@ impl Perform for EditCommunity { let form = ModRemoveCommunityForm { mod_user_id: user_id, community_id: self.edit_id, - removed: Some(self.removed), + removed: Some(removed), reason: self.reason.to_owned(), expires: expires }; diff --git a/ui/src/components/comment-node.tsx b/ui/src/components/comment-node.tsx index c1fc059b..90cf5a54 100644 --- a/ui/src/components/comment-node.tsx +++ b/ui/src/components/comment-node.tsx @@ -212,15 +212,15 @@ export class CommentNode extends Component { } get isMod(): boolean { - return isMod(this.props.moderators.map(m => m.user_id), this.props.node.comment.creator_id); + return this.props.moderators && isMod(this.props.moderators.map(m => m.user_id), this.props.node.comment.creator_id); } get isAdmin(): boolean { - return isMod(this.props.admins.map(a => a.id), this.props.node.comment.creator_id); + return this.props.admins && isMod(this.props.admins.map(a => a.id), this.props.node.comment.creator_id); } get canAdmin(): boolean { - return canMod(UserService.Instance.user, this.props.admins.map(a => a.id), this.props.node.comment.creator_id); + return this.props.admins && canMod(UserService.Instance.user, this.props.admins.map(a => a.id), this.props.node.comment.creator_id); } handleReplyClick(i: CommentNode) { @@ -240,7 +240,6 @@ export class CommentNode extends Component { creator_id: i.props.node.comment.creator_id, post_id: i.props.node.comment.post_id, parent_id: i.props.node.comment.parent_id, - removed: i.props.node.comment.removed, auth: null }; WebSocketService.Instance.editComment(deleteForm); diff --git a/ui/src/components/post-listing.tsx b/ui/src/components/post-listing.tsx index da375aee..7103a8cf 100644 --- a/ui/src/components/post-listing.tsx +++ b/ui/src/components/post-listing.tsx @@ -1,10 +1,10 @@ import { Component, linkEvent } from 'inferno'; import { Link } from 'inferno-router'; import { WebSocketService, UserService } from '../services'; -import { Post, CreatePostLikeForm, PostForm as PostFormI, SavePostForm } from '../interfaces'; +import { Post, CreatePostLikeForm, PostForm as PostFormI, SavePostForm, CommunityUser, UserView } from '../interfaces'; import { MomentTime } from './moment-time'; import { PostForm } from './post-form'; -import { mdToHtml } from '../utils'; +import { mdToHtml, canMod, isMod } from '../utils'; interface PostListingState { showEdit: boolean; @@ -19,6 +19,8 @@ interface PostListingProps { showCommunity?: boolean; showBody?: boolean; viewOnly?: boolean; + moderators?: Array; + admins?: Array; } export class PostListing extends Component { @@ -98,6 +100,12 @@ export class PostListing extends Component {
  • by {post.creator_name} + {this.isMod && + mod + } + {this.isAdmin && + admin + } {this.props.showCommunity && to @@ -135,7 +143,7 @@ export class PostListing extends Component {
  • } - {this.props.post.am_mod && + {this.canMod &&
  • {!this.props.post.removed ? @@ -166,6 +174,29 @@ export class PostListing extends Component { return UserService.Instance.user && this.props.post.creator_id == UserService.Instance.user.id; } + get canMod(): boolean { + + if (this.props.editable) { + let adminsThenMods = this.props.admins.map(a => a.id) + .concat(this.props.moderators.map(m => m.user_id)); + + return canMod(UserService.Instance.user, adminsThenMods, this.props.post.creator_id); + + } else return false; + } + + get isMod(): boolean { + return this.props.moderators && isMod(this.props.moderators.map(m => m.user_id), this.props.post.creator_id); + } + + get isAdmin(): boolean { + return this.props.admins && isMod(this.props.admins.map(a => a.id), this.props.post.creator_id); + } + + get canAdmin(): boolean { + return this.props.admins && canMod(UserService.Instance.user, this.props.admins.map(a => a.id), this.props.post.creator_id); + } + handlePostLike(i: PostListing) { let form: CreatePostLikeForm = { @@ -207,8 +238,6 @@ export class PostListing extends Component { url: '', edit_id: i.props.post.id, creator_id: i.props.post.creator_id, - removed: !i.props.post.removed, - locked: !i.props.post.locked, auth: null }; WebSocketService.Instance.editPost(deleteForm); @@ -242,7 +271,6 @@ export class PostListing extends Component { edit_id: i.props.post.id, creator_id: i.props.post.creator_id, removed: !i.props.post.removed, - locked: !i.props.post.locked, reason: i.state.removeReason, auth: null, }; @@ -258,7 +286,6 @@ export class PostListing extends Component { community_id: i.props.post.community_id, edit_id: i.props.post.id, creator_id: i.props.post.creator_id, - removed: !i.props.post.removed, locked: !i.props.post.locked, auth: null, }; diff --git a/ui/src/components/post.tsx b/ui/src/components/post.tsx index 64f56d88..56b73f6e 100644 --- a/ui/src/components/post.tsx +++ b/ui/src/components/post.tsx @@ -82,7 +82,14 @@ export class Post extends Component {
    :
    - +
    {this.sortRadios()} diff --git a/ui/src/interfaces.ts b/ui/src/interfaces.ts index 4a4ee643..8927a171 100644 --- a/ui/src/interfaces.ts +++ b/ui/src/interfaces.ts @@ -370,8 +370,8 @@ export interface PostForm { updated?: number; edit_id?: number; creator_id: number; - removed: boolean; - locked: boolean; + removed?: boolean; + locked?: boolean; reason?: string; auth: string; } @@ -402,7 +402,7 @@ export interface CommentForm { parent_id?: number; edit_id?: number; creator_id: number; - removed: boolean; + removed?: boolean; reason?: string; auth: string; } -- 2.44.1