2019-03-26 18:00:18 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2019-10-19 00:20:27 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
2019-03-26 18:00:18 +00:00
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-10-19 00:20:27 +00:00
|
|
|
import {
|
|
|
|
UserOperation,
|
|
|
|
Community,
|
|
|
|
Post as PostI,
|
|
|
|
GetPostResponse,
|
|
|
|
PostResponse,
|
|
|
|
Comment,
|
|
|
|
CommentForm as CommentFormI,
|
|
|
|
CommentResponse,
|
|
|
|
CommentSortType,
|
|
|
|
CreatePostLikeResponse,
|
|
|
|
CommunityUser,
|
|
|
|
CommunityResponse,
|
|
|
|
CommentNode as CommentNodeI,
|
|
|
|
BanFromCommunityResponse,
|
|
|
|
BanUserResponse,
|
|
|
|
AddModToCommunityResponse,
|
|
|
|
AddAdminResponse,
|
|
|
|
UserView,
|
|
|
|
SearchType,
|
|
|
|
SortType,
|
|
|
|
SearchForm,
|
|
|
|
SearchResponse,
|
|
|
|
GetSiteResponse,
|
|
|
|
GetCommunityResponse,
|
|
|
|
} from '../interfaces';
|
2019-05-19 16:15:08 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
2019-04-08 05:19:02 +00:00
|
|
|
import { msgOp, hotRank } from '../utils';
|
2019-04-03 06:49:32 +00:00
|
|
|
import { PostListing } from './post-listing';
|
2019-08-22 05:17:15 +00:00
|
|
|
import { PostListings } from './post-listings';
|
2019-04-03 23:01:20 +00:00
|
|
|
import { Sidebar } from './sidebar';
|
2019-04-08 05:19:02 +00:00
|
|
|
import { CommentForm } from './comment-form';
|
|
|
|
import { CommentNodes } from './comment-nodes';
|
2019-03-31 00:38:54 +00:00
|
|
|
import * as autosize from 'autosize';
|
2019-08-10 00:14:43 +00:00
|
|
|
import { i18n } from '../i18next';
|
|
|
|
import { T } from 'inferno-i18next';
|
2019-03-26 18:00:18 +00:00
|
|
|
|
2019-04-03 23:01:20 +00:00
|
|
|
interface PostState {
|
2019-03-26 18:00:18 +00:00
|
|
|
post: PostI;
|
|
|
|
comments: Array<Comment>;
|
2019-03-30 06:08:02 +00:00
|
|
|
commentSort: CommentSortType;
|
2019-04-03 23:01:20 +00:00
|
|
|
community: Community;
|
2019-04-04 20:53:32 +00:00
|
|
|
moderators: Array<CommunityUser>;
|
2019-04-20 04:06:25 +00:00
|
|
|
admins: Array<UserView>;
|
2019-04-08 05:19:02 +00:00
|
|
|
scrolled?: boolean;
|
|
|
|
scrolled_comment_id?: number;
|
2019-04-08 21:46:09 +00:00
|
|
|
loading: boolean;
|
2019-08-22 05:17:15 +00:00
|
|
|
crossPosts: Array<PostI>;
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 23:01:20 +00:00
|
|
|
export class Post extends Component<any, PostState> {
|
2019-03-26 18:00:18 +00:00
|
|
|
private subscription: Subscription;
|
2019-04-03 23:01:20 +00:00
|
|
|
private emptyState: PostState = {
|
2019-04-03 06:49:32 +00:00
|
|
|
post: null,
|
2019-03-30 06:08:02 +00:00
|
|
|
comments: [],
|
2019-04-03 23:01:20 +00:00
|
|
|
commentSort: CommentSortType.Hot,
|
|
|
|
community: null,
|
2019-04-05 20:22:59 +00:00
|
|
|
moderators: [],
|
2019-04-20 04:06:25 +00:00
|
|
|
admins: [],
|
2019-10-19 00:20:27 +00:00
|
|
|
scrolled: false,
|
2019-08-22 05:17:15 +00:00
|
|
|
loading: true,
|
|
|
|
crossPosts: [],
|
2019-10-19 00:20:27 +00:00
|
|
|
};
|
2019-03-26 18:00:18 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
constructor(props: any, context: any) {
|
2019-03-26 18:00:18 +00:00
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
2019-03-27 16:59:21 +00:00
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
let postId = Number(this.props.match.params.id);
|
2019-04-08 05:19:02 +00:00
|
|
|
if (this.props.match.params.comment_id) {
|
|
|
|
this.state.scrolled_comment_id = this.props.match.params.comment_id;
|
|
|
|
}
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
this.subscription = WebSocketService.Instance.subject
|
2019-10-19 00:20:27 +00:00
|
|
|
.pipe(
|
|
|
|
retryWhen(errors =>
|
|
|
|
errors.pipe(
|
|
|
|
delay(3000),
|
|
|
|
take(10)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2019-03-26 18:00:18 +00:00
|
|
|
.subscribe(
|
2019-10-19 00:20:27 +00:00
|
|
|
msg => this.parseMessage(msg),
|
|
|
|
err => console.error(err),
|
2019-03-26 18:00:18 +00:00
|
|
|
() => console.log('complete')
|
|
|
|
);
|
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
WebSocketService.Instance.getPost(postId);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2019-03-31 00:38:54 +00:00
|
|
|
componentDidMount() {
|
|
|
|
autosize(document.querySelectorAll('textarea'));
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
|
2019-10-19 00:20:27 +00:00
|
|
|
if (
|
|
|
|
this.state.scrolled_comment_id &&
|
|
|
|
!this.state.scrolled &&
|
|
|
|
lastState.comments.length > 0
|
|
|
|
) {
|
|
|
|
var elmnt = document.getElementById(
|
|
|
|
`comment-${this.state.scrolled_comment_id}`
|
|
|
|
);
|
|
|
|
elmnt.scrollIntoView();
|
|
|
|
elmnt.classList.add('mark');
|
2019-04-05 20:22:59 +00:00
|
|
|
this.state.scrolled = true;
|
2019-05-19 16:15:08 +00:00
|
|
|
this.markScrolledAsRead(this.state.scrolled_comment_id);
|
|
|
|
}
|
2019-08-22 23:13:26 +00:00
|
|
|
|
|
|
|
// Necessary if you are on a post and you click another post (same route)
|
|
|
|
if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
|
|
|
|
// Couldnt get a refresh working. This does for now.
|
|
|
|
location.reload();
|
|
|
|
|
|
|
|
// let currentId = this.props.match.params.id;
|
|
|
|
// WebSocketService.Instance.getPost(currentId);
|
|
|
|
// this.context.router.history.push('/sponsors');
|
|
|
|
// this.context.refresh();
|
|
|
|
// this.context.router.history.push(_lastProps.location.pathname);
|
|
|
|
}
|
2019-05-19 16:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
markScrolledAsRead(commentId: number) {
|
|
|
|
let found = this.state.comments.find(c => c.id == commentId);
|
|
|
|
let parent = this.state.comments.find(c => found.parent_id == c.id);
|
2019-10-19 00:20:27 +00:00
|
|
|
let parent_user_id = parent
|
|
|
|
? parent.creator_id
|
|
|
|
: this.state.post.creator_id;
|
|
|
|
|
|
|
|
if (
|
|
|
|
UserService.Instance.user &&
|
|
|
|
UserService.Instance.user.id == parent_user_id
|
|
|
|
) {
|
2019-05-19 16:15:08 +00:00
|
|
|
let form: CommentFormI = {
|
|
|
|
content: found.content,
|
|
|
|
edit_id: found.id,
|
|
|
|
creator_id: found.creator_id,
|
|
|
|
post_id: found.post_id,
|
|
|
|
parent_id: found.parent_id,
|
|
|
|
read: true,
|
2019-10-19 00:20:27 +00:00
|
|
|
auth: null,
|
2019-05-19 16:15:08 +00:00
|
|
|
};
|
|
|
|
WebSocketService.Instance.editComment(form);
|
2019-04-05 20:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.loading ? (
|
|
|
|
<h5>
|
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
|
|
|
</h5>
|
|
|
|
) : (
|
|
|
|
<div class="row">
|
2019-08-25 17:42:10 +00:00
|
|
|
<div class="col-12 col-md-8 mb-3">
|
2019-10-19 00:20:27 +00:00
|
|
|
<PostListing
|
|
|
|
post={this.state.post}
|
|
|
|
showBody
|
|
|
|
showCommunity
|
|
|
|
editable
|
|
|
|
moderators={this.state.moderators}
|
2019-04-20 07:24:59 +00:00
|
|
|
admins={this.state.admins}
|
|
|
|
/>
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.crossPosts.length > 0 && (
|
2019-08-22 05:17:15 +00:00
|
|
|
<>
|
2019-10-19 00:20:27 +00:00
|
|
|
<div class="my-1 text-muted small font-weight-bold">
|
|
|
|
<T i18nKey="cross_posts">#</T>
|
|
|
|
</div>
|
2019-08-22 05:17:15 +00:00
|
|
|
<PostListings showCommunity posts={this.state.crossPosts} />
|
|
|
|
</>
|
2019-10-19 00:20:27 +00:00
|
|
|
)}
|
2019-04-03 06:49:32 +00:00
|
|
|
<div className="mb-2" />
|
2019-10-19 00:20:27 +00:00
|
|
|
<CommentForm
|
|
|
|
postId={this.state.post.id}
|
|
|
|
disabled={this.state.post.locked}
|
|
|
|
/>
|
2019-04-03 06:49:32 +00:00
|
|
|
{this.sortRadios()}
|
|
|
|
{this.commentsTree()}
|
|
|
|
</div>
|
2019-08-25 17:42:10 +00:00
|
|
|
<div class="col-12 col-sm-12 col-md-4">
|
|
|
|
{this.state.comments.length > 0 && this.newComments()}
|
2019-09-12 00:20:57 +00:00
|
|
|
{this.sidebar()}
|
2019-04-03 06:49:32 +00:00
|
|
|
</div>
|
2019-03-27 16:59:21 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
)}
|
2019-03-27 19:54:55 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
2019-03-30 06:08:02 +00:00
|
|
|
sortRadios() {
|
|
|
|
return (
|
|
|
|
<div class="btn-group btn-group-toggle mb-3">
|
2019-10-19 00:20:27 +00:00
|
|
|
<label
|
|
|
|
className={`btn btn-sm btn-secondary pointer ${this.state
|
|
|
|
.commentSort === CommentSortType.Hot && 'active'}`}
|
|
|
|
>
|
|
|
|
{i18n.t('hot')}
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
value={CommentSortType.Hot}
|
|
|
|
checked={this.state.commentSort === CommentSortType.Hot}
|
|
|
|
onChange={linkEvent(this, this.handleCommentSortChange)}
|
|
|
|
/>
|
2019-03-30 06:08:02 +00:00
|
|
|
</label>
|
2019-10-19 00:20:27 +00:00
|
|
|
<label
|
|
|
|
className={`btn btn-sm btn-secondary pointer ${this.state
|
|
|
|
.commentSort === CommentSortType.Top && 'active'}`}
|
|
|
|
>
|
|
|
|
{i18n.t('top')}
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
value={CommentSortType.Top}
|
|
|
|
checked={this.state.commentSort === CommentSortType.Top}
|
|
|
|
onChange={linkEvent(this, this.handleCommentSortChange)}
|
|
|
|
/>
|
2019-03-30 06:08:02 +00:00
|
|
|
</label>
|
2019-10-19 00:20:27 +00:00
|
|
|
<label
|
|
|
|
className={`btn btn-sm btn-secondary pointer ${this.state
|
|
|
|
.commentSort === CommentSortType.New && 'active'}`}
|
|
|
|
>
|
|
|
|
{i18n.t('new')}
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
value={CommentSortType.New}
|
|
|
|
checked={this.state.commentSort === CommentSortType.New}
|
|
|
|
onChange={linkEvent(this, this.handleCommentSortChange)}
|
|
|
|
/>
|
2019-03-30 06:08:02 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-03-30 06:08:02 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
newComments() {
|
|
|
|
return (
|
2019-09-12 00:20:57 +00:00
|
|
|
<div class="d-none d-md-block new-comments mb-3 card border-secondary">
|
2019-08-25 17:42:10 +00:00
|
|
|
<div class="card-body small">
|
2019-10-19 00:20:27 +00:00
|
|
|
<h6>
|
|
|
|
<T i18nKey="recent_comments">#</T>
|
|
|
|
</h6>
|
|
|
|
{this.state.comments.map(comment => (
|
|
|
|
<CommentNodes
|
|
|
|
nodes={[{ comment: comment }]}
|
|
|
|
noIndent
|
|
|
|
locked={this.state.post.locked}
|
|
|
|
moderators={this.state.moderators}
|
2019-08-25 17:42:10 +00:00
|
|
|
admins={this.state.admins}
|
2019-09-13 15:37:12 +00:00
|
|
|
postCreatorId={this.state.post.creator_id}
|
2019-08-25 17:42:10 +00:00
|
|
|
/>
|
2019-10-19 00:20:27 +00:00
|
|
|
))}
|
2019-08-25 17:42:10 +00:00
|
|
|
</div>
|
2019-03-26 18:00:18 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
2019-03-27 16:59:21 +00:00
|
|
|
|
|
|
|
sidebar() {
|
2019-10-19 00:20:27 +00:00
|
|
|
return (
|
2019-08-25 17:42:10 +00:00
|
|
|
<div class="mb-3">
|
2019-10-19 00:20:27 +00:00
|
|
|
<Sidebar
|
|
|
|
community={this.state.community}
|
|
|
|
moderators={this.state.moderators}
|
2019-04-21 20:52:55 +00:00
|
|
|
admins={this.state.admins}
|
|
|
|
/>
|
2019-03-26 18:00:18 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2019-10-19 00:20:27 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleCommentSortChange(i: Post, event: any) {
|
2019-03-30 06:08:02 +00:00
|
|
|
i.state.commentSort = Number(event.target.value);
|
|
|
|
i.setState(i.state);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-30 06:08:02 +00:00
|
|
|
private buildCommentsTree(): Array<CommentNodeI> {
|
|
|
|
let map = new Map<number, CommentNodeI>();
|
|
|
|
for (let comment of this.state.comments) {
|
2019-03-27 16:59:21 +00:00
|
|
|
let node: CommentNodeI = {
|
2019-03-30 06:08:02 +00:00
|
|
|
comment: comment,
|
2019-10-19 00:20:27 +00:00
|
|
|
children: [],
|
2019-03-27 16:59:21 +00:00
|
|
|
};
|
2019-03-30 06:08:02 +00:00
|
|
|
map.set(comment.id, { ...node });
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
let tree: Array<CommentNodeI> = [];
|
2019-03-30 06:08:02 +00:00
|
|
|
for (let comment of this.state.comments) {
|
2019-10-19 00:20:27 +00:00
|
|
|
if (comment.parent_id) {
|
2019-03-30 06:08:02 +00:00
|
|
|
map.get(comment.parent_id).children.push(map.get(comment.id));
|
2019-10-19 00:20:27 +00:00
|
|
|
} else {
|
2019-03-30 06:08:02 +00:00
|
|
|
tree.push(map.get(comment.id));
|
|
|
|
}
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
2019-03-30 06:08:02 +00:00
|
|
|
|
|
|
|
this.sortTree(tree);
|
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
2019-03-30 06:08:02 +00:00
|
|
|
sortTree(tree: Array<CommentNodeI>) {
|
2019-10-13 19:53:41 +00:00
|
|
|
// First, put removed and deleted comments at the bottom, then do your other sorts
|
2019-03-30 06:08:02 +00:00
|
|
|
if (this.state.commentSort == CommentSortType.Top) {
|
2019-10-19 00:20:27 +00:00
|
|
|
tree.sort(
|
|
|
|
(a, b) =>
|
|
|
|
+a.comment.removed - +b.comment.removed ||
|
|
|
|
+a.comment.deleted - +b.comment.deleted ||
|
|
|
|
b.comment.score - a.comment.score
|
|
|
|
);
|
2019-03-30 06:08:02 +00:00
|
|
|
} else if (this.state.commentSort == CommentSortType.New) {
|
2019-10-19 00:20:27 +00:00
|
|
|
tree.sort(
|
|
|
|
(a, b) =>
|
|
|
|
+a.comment.removed - +b.comment.removed ||
|
|
|
|
+a.comment.deleted - +b.comment.deleted ||
|
|
|
|
b.comment.published.localeCompare(a.comment.published)
|
|
|
|
);
|
2019-03-30 06:08:02 +00:00
|
|
|
} else if (this.state.commentSort == CommentSortType.Hot) {
|
2019-10-19 00:20:27 +00:00
|
|
|
tree.sort(
|
|
|
|
(a, b) =>
|
|
|
|
+a.comment.removed - +b.comment.removed ||
|
|
|
|
+a.comment.deleted - +b.comment.deleted ||
|
|
|
|
hotRank(b.comment) - hotRank(a.comment)
|
|
|
|
);
|
2019-03-30 06:08:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let node of tree) {
|
|
|
|
this.sortTree(node.children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
commentsTree() {
|
|
|
|
let nodes = this.buildCommentsTree();
|
|
|
|
return (
|
2019-04-20 04:06:25 +00:00
|
|
|
<div>
|
2019-10-19 00:20:27 +00:00
|
|
|
<CommentNodes
|
|
|
|
nodes={nodes}
|
|
|
|
locked={this.state.post.locked}
|
|
|
|
moderators={this.state.moderators}
|
2019-04-20 04:06:25 +00:00
|
|
|
admins={this.state.admins}
|
2019-09-13 15:37:12 +00:00
|
|
|
postCreatorId={this.state.post.creator_id}
|
2019-04-20 04:06:25 +00:00
|
|
|
/>
|
2019-03-27 16:59:21 +00:00
|
|
|
</div>
|
|
|
|
);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parseMessage(msg: any) {
|
|
|
|
console.log(msg);
|
|
|
|
let op: UserOperation = msgOp(msg);
|
|
|
|
if (msg.error) {
|
2019-08-10 00:14:43 +00:00
|
|
|
alert(i18n.t(msg.error));
|
2019-03-26 18:00:18 +00:00
|
|
|
return;
|
|
|
|
} else if (op == UserOperation.GetPost) {
|
2019-04-03 20:59:37 +00:00
|
|
|
let res: GetPostResponse = msg;
|
2019-03-26 18:00:18 +00:00
|
|
|
this.state.post = res.post;
|
2019-03-27 18:37:16 +00:00
|
|
|
this.state.comments = res.comments;
|
2019-04-03 23:01:20 +00:00
|
|
|
this.state.community = res.community;
|
2019-04-04 20:53:32 +00:00
|
|
|
this.state.moderators = res.moderators;
|
2019-04-20 04:06:25 +00:00
|
|
|
this.state.admins = res.admins;
|
2019-04-08 21:46:09 +00:00
|
|
|
this.state.loading = false;
|
2019-06-03 01:35:46 +00:00
|
|
|
document.title = `${this.state.post.name} - ${WebSocketService.Instance.site.name}`;
|
2019-08-22 05:17:15 +00:00
|
|
|
|
2019-10-19 00:20:27 +00:00
|
|
|
// Get cross-posts
|
2019-08-22 23:13:26 +00:00
|
|
|
if (this.state.post.url) {
|
|
|
|
let form: SearchForm = {
|
|
|
|
q: this.state.post.url,
|
|
|
|
type_: SearchType[SearchType.Url],
|
|
|
|
sort: SortType[SortType.TopAll],
|
|
|
|
page: 1,
|
|
|
|
limit: 6,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.search(form);
|
|
|
|
}
|
2019-10-19 00:20:27 +00:00
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (op == UserOperation.CreateComment) {
|
|
|
|
let res: CommentResponse = msg;
|
|
|
|
this.state.comments.unshift(res.comment);
|
2019-03-26 18:00:18 +00:00
|
|
|
this.setState(this.state);
|
2019-03-29 04:56:23 +00:00
|
|
|
} else if (op == UserOperation.EditComment) {
|
|
|
|
let res: CommentResponse = msg;
|
|
|
|
let found = this.state.comments.find(c => c.id == res.comment.id);
|
|
|
|
found.content = res.comment.content;
|
|
|
|
found.updated = res.comment.updated;
|
2019-04-15 23:12:06 +00:00
|
|
|
found.removed = res.comment.removed;
|
2019-04-29 19:14:54 +00:00
|
|
|
found.deleted = res.comment.deleted;
|
2019-04-15 23:12:06 +00:00
|
|
|
found.upvotes = res.comment.upvotes;
|
|
|
|
found.downvotes = res.comment.downvotes;
|
|
|
|
found.score = res.comment.score;
|
2019-05-19 16:15:08 +00:00
|
|
|
found.read = res.comment.read;
|
2019-04-15 23:12:06 +00:00
|
|
|
|
2019-03-29 04:56:23 +00:00
|
|
|
this.setState(this.state);
|
2019-04-20 04:06:25 +00:00
|
|
|
} else if (op == UserOperation.SaveComment) {
|
|
|
|
let res: CommentResponse = msg;
|
|
|
|
let found = this.state.comments.find(c => c.id == res.comment.id);
|
|
|
|
found.saved = res.comment.saved;
|
|
|
|
this.setState(this.state);
|
|
|
|
} else if (op == UserOperation.CreateCommentLike) {
|
2019-04-03 23:01:20 +00:00
|
|
|
let res: CommentResponse = msg;
|
2019-10-19 00:20:27 +00:00
|
|
|
let found: Comment = this.state.comments.find(
|
|
|
|
c => c.id === res.comment.id
|
|
|
|
);
|
2019-03-28 19:32:08 +00:00
|
|
|
found.score = res.comment.score;
|
|
|
|
found.upvotes = res.comment.upvotes;
|
|
|
|
found.downvotes = res.comment.downvotes;
|
2019-10-19 00:20:27 +00:00
|
|
|
if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
|
2019-03-28 19:32:08 +00:00
|
|
|
this.setState(this.state);
|
2019-04-03 06:49:32 +00:00
|
|
|
} else if (op == UserOperation.CreatePostLike) {
|
|
|
|
let res: CreatePostLikeResponse = msg;
|
|
|
|
this.state.post.my_vote = res.post.my_vote;
|
|
|
|
this.state.post.score = res.post.score;
|
|
|
|
this.state.post.upvotes = res.post.upvotes;
|
|
|
|
this.state.post.downvotes = res.post.downvotes;
|
|
|
|
this.setState(this.state);
|
2019-04-03 20:59:37 +00:00
|
|
|
} else if (op == UserOperation.EditPost) {
|
|
|
|
let res: PostResponse = msg;
|
|
|
|
this.state.post = res.post;
|
|
|
|
this.setState(this.state);
|
2019-04-20 04:06:25 +00:00
|
|
|
} else if (op == UserOperation.SavePost) {
|
|
|
|
let res: PostResponse = msg;
|
|
|
|
this.state.post = res.post;
|
|
|
|
this.setState(this.state);
|
2019-04-04 22:29:14 +00:00
|
|
|
} else if (op == UserOperation.EditCommunity) {
|
|
|
|
let res: CommunityResponse = msg;
|
|
|
|
this.state.community = res.community;
|
|
|
|
this.state.post.community_id = res.community.id;
|
|
|
|
this.state.post.community_name = res.community.name;
|
|
|
|
this.setState(this.state);
|
2019-04-05 06:26:38 +00:00
|
|
|
} else if (op == UserOperation.FollowCommunity) {
|
|
|
|
let res: CommunityResponse = msg;
|
|
|
|
this.state.community.subscribed = res.community.subscribed;
|
2019-10-19 00:20:27 +00:00
|
|
|
this.state.community.number_of_subscribers =
|
|
|
|
res.community.number_of_subscribers;
|
2019-04-05 06:26:38 +00:00
|
|
|
this.setState(this.state);
|
2019-04-15 23:12:06 +00:00
|
|
|
} else if (op == UserOperation.BanFromCommunity) {
|
|
|
|
let res: BanFromCommunityResponse = msg;
|
2019-10-19 00:20:27 +00:00
|
|
|
this.state.comments
|
|
|
|
.filter(c => c.creator_id == res.user.id)
|
|
|
|
.forEach(c => (c.banned_from_community = res.banned));
|
|
|
|
if (this.state.post.creator_id == res.user.id) {
|
2019-09-06 00:18:48 +00:00
|
|
|
this.state.post.banned_from_community = res.banned;
|
|
|
|
}
|
2019-04-15 23:12:06 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (op == UserOperation.AddModToCommunity) {
|
|
|
|
let res: AddModToCommunityResponse = msg;
|
|
|
|
this.state.moderators = res.moderators;
|
|
|
|
this.setState(this.state);
|
2019-04-20 04:06:25 +00:00
|
|
|
} else if (op == UserOperation.BanUser) {
|
|
|
|
let res: BanUserResponse = msg;
|
2019-10-19 00:20:27 +00:00
|
|
|
this.state.comments
|
|
|
|
.filter(c => c.creator_id == res.user.id)
|
|
|
|
.forEach(c => (c.banned = res.banned));
|
|
|
|
if (this.state.post.creator_id == res.user.id) {
|
2019-09-06 00:18:48 +00:00
|
|
|
this.state.post.banned = res.banned;
|
|
|
|
}
|
2019-04-20 04:06:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (op == UserOperation.AddAdmin) {
|
|
|
|
let res: AddAdminResponse = msg;
|
|
|
|
this.state.admins = res.admins;
|
|
|
|
this.setState(this.state);
|
2019-08-22 05:17:15 +00:00
|
|
|
} else if (op == UserOperation.Search) {
|
|
|
|
let res: SearchResponse = msg;
|
|
|
|
this.state.crossPosts = res.posts.filter(p => p.id != this.state.post.id);
|
|
|
|
this.setState(this.state);
|
2019-10-19 00:20:27 +00:00
|
|
|
} else if (op == UserOperation.TransferSite) {
|
2019-08-24 02:40:41 +00:00
|
|
|
let res: GetSiteResponse = msg;
|
|
|
|
|
|
|
|
this.state.admins = res.admins;
|
|
|
|
this.setState(this.state);
|
2019-10-19 00:20:27 +00:00
|
|
|
} else if (op == UserOperation.TransferCommunity) {
|
2019-08-24 02:40:41 +00:00
|
|
|
let res: GetCommunityResponse = msg;
|
|
|
|
this.state.community = res.community;
|
|
|
|
this.state.moderators = res.moderators;
|
|
|
|
this.state.admins = res.admins;
|
|
|
|
this.setState(this.state);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
}
|