2020-09-06 16:15:25 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2020-09-11 18:09:21 +00:00
|
|
|
import { HtmlTags } from './html-tags';
|
2020-09-06 16:15:25 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
import {
|
|
|
|
UserOperation,
|
|
|
|
Post as PostI,
|
|
|
|
GetPostResponse,
|
|
|
|
PostResponse,
|
|
|
|
MarkCommentAsReadForm,
|
|
|
|
CommentResponse,
|
|
|
|
CommunityResponse,
|
|
|
|
CommentNode as CommentNodeI,
|
|
|
|
BanFromCommunityResponse,
|
|
|
|
BanUserResponse,
|
|
|
|
AddModToCommunityResponse,
|
|
|
|
AddAdminResponse,
|
|
|
|
SearchType,
|
|
|
|
SortType,
|
|
|
|
SearchForm,
|
|
|
|
GetPostForm,
|
|
|
|
SearchResponse,
|
|
|
|
GetSiteResponse,
|
|
|
|
GetCommunityResponse,
|
|
|
|
WebSocketJsonResponse,
|
2020-09-09 00:48:17 +00:00
|
|
|
ListCategoriesResponse,
|
|
|
|
Category,
|
2020-09-06 16:15:25 +00:00
|
|
|
} from 'lemmy-js-client';
|
|
|
|
import { CommentSortType, CommentViewType } from '../interfaces';
|
|
|
|
import { WebSocketService, UserService } from '../services';
|
|
|
|
import {
|
|
|
|
wsJsonToRes,
|
|
|
|
toast,
|
|
|
|
editCommentRes,
|
|
|
|
saveCommentRes,
|
|
|
|
createCommentLikeRes,
|
|
|
|
createPostLikeRes,
|
|
|
|
commentsToFlatNodes,
|
|
|
|
setupTippy,
|
2020-09-09 00:48:17 +00:00
|
|
|
setIsoData,
|
|
|
|
getIdFromProps,
|
|
|
|
getCommentIdFromProps,
|
|
|
|
wsSubscribe,
|
|
|
|
setAuth,
|
|
|
|
lemmyHttp,
|
|
|
|
isBrowser,
|
2020-09-11 18:09:21 +00:00
|
|
|
previewLines,
|
|
|
|
isImage,
|
2020-09-06 16:15:25 +00:00
|
|
|
} from '../utils';
|
|
|
|
import { PostListing } from './post-listing';
|
|
|
|
import { Sidebar } from './sidebar';
|
|
|
|
import { CommentForm } from './comment-form';
|
|
|
|
import { CommentNodes } from './comment-nodes';
|
|
|
|
import autosize from 'autosize';
|
|
|
|
import { i18n } from '../i18next';
|
|
|
|
|
|
|
|
interface PostState {
|
2020-09-09 00:48:17 +00:00
|
|
|
postRes: GetPostResponse;
|
|
|
|
postId: number;
|
|
|
|
commentId?: number;
|
2020-09-06 16:15:25 +00:00
|
|
|
commentSort: CommentSortType;
|
|
|
|
commentViewType: CommentViewType;
|
|
|
|
scrolled?: boolean;
|
|
|
|
loading: boolean;
|
|
|
|
crossPosts: PostI[];
|
|
|
|
siteRes: GetSiteResponse;
|
2020-09-09 00:48:17 +00:00
|
|
|
categories: Category[];
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Post extends Component<any, PostState> {
|
|
|
|
private subscription: Subscription;
|
2020-09-09 00:48:17 +00:00
|
|
|
private isoData = setIsoData(this.context);
|
2020-09-06 16:15:25 +00:00
|
|
|
private emptyState: PostState = {
|
2020-09-09 00:48:17 +00:00
|
|
|
postRes: null,
|
|
|
|
postId: getIdFromProps(this.props),
|
|
|
|
commentId: getCommentIdFromProps(this.props),
|
2020-09-06 16:15:25 +00:00
|
|
|
commentSort: CommentSortType.Hot,
|
|
|
|
commentViewType: CommentViewType.Tree,
|
|
|
|
scrolled: false,
|
|
|
|
loading: true,
|
|
|
|
crossPosts: [],
|
2020-09-09 00:48:17 +00:00
|
|
|
siteRes: this.isoData.site,
|
|
|
|
categories: [],
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
|
|
|
|
2020-09-09 00:48:17 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-09 00:48:17 +00:00
|
|
|
// Only fetch the data if coming from another route
|
|
|
|
if (this.isoData.path == this.context.router.route.match.url) {
|
|
|
|
this.state.postRes = this.isoData.routeData[0];
|
|
|
|
this.state.categories = this.isoData.routeData[1].categories;
|
|
|
|
this.state.loading = false;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-09 00:48:17 +00:00
|
|
|
if (isBrowser() && this.state.commentId) {
|
|
|
|
this.scrollCommentIntoView();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.fetchPost();
|
|
|
|
WebSocketService.Instance.listCategories();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchPost() {
|
2020-09-06 16:15:25 +00:00
|
|
|
let form: GetPostForm = {
|
2020-09-09 00:48:17 +00:00
|
|
|
id: this.state.postId,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
WebSocketService.Instance.getPost(form);
|
2020-09-09 00:48:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static fetchInitialData(auth: string, path: string): Promise<any>[] {
|
|
|
|
let pathSplit = path.split('/');
|
|
|
|
let promises: Promise<any>[] = [];
|
|
|
|
|
|
|
|
let id = Number(pathSplit[2]);
|
|
|
|
|
|
|
|
let postForm: GetPostForm = {
|
|
|
|
id,
|
|
|
|
};
|
|
|
|
setAuth(postForm, auth);
|
|
|
|
|
|
|
|
promises.push(lemmyHttp.getPost(postForm));
|
|
|
|
promises.push(lemmyHttp.listCategories());
|
|
|
|
|
|
|
|
return promises;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-09-13 15:37:23 +00:00
|
|
|
WebSocketService.Instance.postJoin({ post_id: this.state.postId });
|
2020-09-06 16:15:25 +00:00
|
|
|
autosize(document.querySelectorAll('textarea'));
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
|
|
|
|
if (
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.commentId &&
|
2020-09-06 16:15:25 +00:00
|
|
|
!this.state.scrolled &&
|
2020-09-09 00:48:17 +00:00
|
|
|
lastState.postRes &&
|
|
|
|
lastState.postRes.comments.length > 0
|
2020-09-06 16:15:25 +00:00
|
|
|
) {
|
2020-09-09 00:48:17 +00:00
|
|
|
this.scrollCommentIntoView();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Necessary if you are on a post and you click another post (same route)
|
|
|
|
if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
|
2020-09-11 18:09:21 +00:00
|
|
|
// TODO Couldnt get a refresh working. This does for now.
|
2020-09-06 16:15:25 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:48:17 +00:00
|
|
|
scrollCommentIntoView() {
|
|
|
|
var elmnt = document.getElementById(`comment-${this.state.commentId}`);
|
|
|
|
elmnt.scrollIntoView();
|
|
|
|
elmnt.classList.add('mark');
|
|
|
|
this.state.scrolled = true;
|
|
|
|
this.markScrolledAsRead(this.state.commentId);
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
markScrolledAsRead(commentId: number) {
|
2020-09-09 00:48:17 +00:00
|
|
|
let found = this.state.postRes.comments.find(c => c.id == commentId);
|
|
|
|
let parent = this.state.postRes.comments.find(c => found.parent_id == c.id);
|
2020-09-06 16:15:25 +00:00
|
|
|
let parent_user_id = parent
|
|
|
|
? parent.creator_id
|
2020-09-09 00:48:17 +00:00
|
|
|
: this.state.postRes.post.creator_id;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
UserService.Instance.user &&
|
|
|
|
UserService.Instance.user.id == parent_user_id
|
|
|
|
) {
|
|
|
|
let form: MarkCommentAsReadForm = {
|
|
|
|
edit_id: found.id,
|
|
|
|
read: true,
|
|
|
|
auth: null,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.markCommentAsRead(form);
|
|
|
|
UserService.Instance.unreadCountSub.next(
|
|
|
|
UserService.Instance.unreadCountSub.value - 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2020-09-11 18:09:21 +00:00
|
|
|
return `${this.state.postRes.post.name} - ${this.state.siteRes.site.name}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
get imageTag(): string {
|
|
|
|
return (
|
|
|
|
this.state.postRes.post.thumbnail_url ||
|
|
|
|
(this.state.postRes.post.url
|
|
|
|
? isImage(this.state.postRes.post.url)
|
|
|
|
? this.state.postRes.post.url
|
|
|
|
: undefined
|
|
|
|
: undefined)
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 18:09:21 +00:00
|
|
|
get descriptionTag(): string {
|
|
|
|
return this.state.postRes.post.body
|
|
|
|
? previewLines(this.state.postRes.post.body)
|
|
|
|
: undefined;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
|
|
|
{this.state.loading ? (
|
|
|
|
<h5>
|
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
|
|
|
</h5>
|
|
|
|
) : (
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-12 col-md-8 mb-3">
|
2020-09-11 18:09:21 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
image={this.imageTag}
|
|
|
|
description={this.descriptionTag}
|
|
|
|
/>
|
2020-09-06 16:15:25 +00:00
|
|
|
<PostListing
|
2020-09-09 00:48:17 +00:00
|
|
|
post={this.state.postRes.post}
|
2020-09-06 16:15:25 +00:00
|
|
|
showBody
|
|
|
|
showCommunity
|
2020-09-09 00:48:17 +00:00
|
|
|
moderators={this.state.postRes.moderators}
|
2020-09-06 16:15:25 +00:00
|
|
|
admins={this.state.siteRes.admins}
|
|
|
|
enableDownvotes={this.state.siteRes.site.enable_downvotes}
|
|
|
|
enableNsfw={this.state.siteRes.site.enable_nsfw}
|
|
|
|
/>
|
|
|
|
<div className="mb-2" />
|
|
|
|
<CommentForm
|
2020-09-09 00:48:17 +00:00
|
|
|
postId={this.state.postId}
|
|
|
|
disabled={this.state.postRes.post.locked}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
2020-09-09 00:48:17 +00:00
|
|
|
{this.state.postRes.comments.length > 0 && this.sortRadios()}
|
2020-09-06 16:15:25 +00:00
|
|
|
{this.state.commentViewType == CommentViewType.Tree &&
|
|
|
|
this.commentsTree()}
|
|
|
|
{this.state.commentViewType == CommentViewType.Chat &&
|
|
|
|
this.commentsFlat()}
|
|
|
|
</div>
|
|
|
|
<div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sortRadios() {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div class="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
|
|
|
|
<label
|
|
|
|
className={`btn btn-outline-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)}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
<label
|
|
|
|
className={`btn btn-outline-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)}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
<label
|
|
|
|
className={`btn btn-outline-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)}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
<label
|
|
|
|
className={`btn btn-outline-secondary pointer ${
|
|
|
|
this.state.commentSort === CommentSortType.Old && 'active'
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{i18n.t('old')}
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
value={CommentSortType.Old}
|
|
|
|
checked={this.state.commentSort === CommentSortType.Old}
|
|
|
|
onChange={linkEvent(this, this.handleCommentSortChange)}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
<div class="btn-group btn-group-toggle flex-wrap mb-2">
|
|
|
|
<label
|
|
|
|
className={`btn btn-outline-secondary pointer ${
|
|
|
|
this.state.commentViewType === CommentViewType.Chat && 'active'
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{i18n.t('chat')}
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
value={CommentViewType.Chat}
|
|
|
|
checked={this.state.commentViewType === CommentViewType.Chat}
|
|
|
|
onChange={linkEvent(this, this.handleCommentViewTypeChange)}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
commentsFlat() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<CommentNodes
|
2020-09-09 00:48:17 +00:00
|
|
|
nodes={commentsToFlatNodes(this.state.postRes.comments)}
|
2020-09-06 16:15:25 +00:00
|
|
|
noIndent
|
2020-09-09 00:48:17 +00:00
|
|
|
locked={this.state.postRes.post.locked}
|
|
|
|
moderators={this.state.postRes.moderators}
|
2020-09-06 16:15:25 +00:00
|
|
|
admins={this.state.siteRes.admins}
|
2020-09-09 00:48:17 +00:00
|
|
|
postCreatorId={this.state.postRes.post.creator_id}
|
2020-09-06 16:15:25 +00:00
|
|
|
showContext
|
|
|
|
enableDownvotes={this.state.siteRes.site.enable_downvotes}
|
|
|
|
sort={this.state.commentSort}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sidebar() {
|
|
|
|
return (
|
|
|
|
<div class="mb-3">
|
|
|
|
<Sidebar
|
2020-09-09 00:48:17 +00:00
|
|
|
community={this.state.postRes.community}
|
|
|
|
moderators={this.state.postRes.moderators}
|
2020-09-06 16:15:25 +00:00
|
|
|
admins={this.state.siteRes.admins}
|
2020-09-09 00:48:17 +00:00
|
|
|
online={this.state.postRes.online}
|
2020-09-06 16:15:25 +00:00
|
|
|
enableNsfw={this.state.siteRes.site.enable_nsfw}
|
|
|
|
showIcon
|
2020-09-09 00:48:17 +00:00
|
|
|
categories={this.state.categories}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCommentSortChange(i: Post, event: any) {
|
|
|
|
i.state.commentSort = Number(event.target.value);
|
|
|
|
i.state.commentViewType = CommentViewType.Tree;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCommentViewTypeChange(i: Post, event: any) {
|
|
|
|
i.state.commentViewType = Number(event.target.value);
|
|
|
|
i.state.commentSort = CommentSortType.New;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
buildCommentsTree(): CommentNodeI[] {
|
|
|
|
let map = new Map<number, CommentNodeI>();
|
2020-09-09 00:48:17 +00:00
|
|
|
for (let comment of this.state.postRes.comments) {
|
2020-09-06 16:15:25 +00:00
|
|
|
let node: CommentNodeI = {
|
|
|
|
comment: comment,
|
|
|
|
children: [],
|
|
|
|
};
|
|
|
|
map.set(comment.id, { ...node });
|
|
|
|
}
|
|
|
|
let tree: CommentNodeI[] = [];
|
2020-09-09 00:48:17 +00:00
|
|
|
for (let comment of this.state.postRes.comments) {
|
2020-09-06 16:15:25 +00:00
|
|
|
let child = map.get(comment.id);
|
|
|
|
if (comment.parent_id) {
|
|
|
|
let parent_ = map.get(comment.parent_id);
|
|
|
|
parent_.children.push(child);
|
|
|
|
} else {
|
|
|
|
tree.push(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setDepth(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
setDepth(node: CommentNodeI, i: number = 0): void {
|
|
|
|
for (let child of node.children) {
|
|
|
|
child.comment.depth = i;
|
|
|
|
this.setDepth(child, i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commentsTree() {
|
|
|
|
let nodes = this.buildCommentsTree();
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<CommentNodes
|
|
|
|
nodes={nodes}
|
2020-09-09 00:48:17 +00:00
|
|
|
locked={this.state.postRes.post.locked}
|
|
|
|
moderators={this.state.postRes.moderators}
|
2020-09-06 16:15:25 +00:00
|
|
|
admins={this.state.siteRes.admins}
|
2020-09-09 00:48:17 +00:00
|
|
|
postCreatorId={this.state.postRes.post.creator_id}
|
2020-09-06 16:15:25 +00:00
|
|
|
sort={this.state.commentSort}
|
|
|
|
enableDownvotes={this.state.siteRes.site.enable_downvotes}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
parseMessage(msg: WebSocketJsonResponse) {
|
|
|
|
console.log(msg);
|
|
|
|
let res = wsJsonToRes(msg);
|
|
|
|
if (msg.error) {
|
|
|
|
toast(i18n.t(msg.error), 'danger');
|
|
|
|
return;
|
|
|
|
} else if (msg.reconnect) {
|
|
|
|
WebSocketService.Instance.getPost({
|
|
|
|
id: Number(this.props.match.params.id),
|
|
|
|
});
|
|
|
|
} else if (res.op == UserOperation.GetPost) {
|
|
|
|
let data = res.data as GetPostResponse;
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.postRes = data;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.loading = false;
|
|
|
|
|
|
|
|
// Get cross-posts
|
2020-09-09 00:48:17 +00:00
|
|
|
if (this.state.postRes.post.url) {
|
2020-09-06 16:15:25 +00:00
|
|
|
let form: SearchForm = {
|
2020-09-09 00:48:17 +00:00
|
|
|
q: this.state.postRes.post.url,
|
2020-09-06 16:15:25 +00:00
|
|
|
type_: SearchType.Url,
|
|
|
|
sort: SortType.TopAll,
|
|
|
|
page: 1,
|
|
|
|
limit: 6,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.search(form);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState(this.state);
|
|
|
|
setupTippy();
|
|
|
|
} else if (res.op == UserOperation.CreateComment) {
|
|
|
|
let data = res.data as CommentResponse;
|
|
|
|
|
|
|
|
// Necessary since it might be a user reply
|
|
|
|
if (data.recipient_ids.length == 0) {
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.postRes.comments.unshift(data.comment);
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
} else if (
|
|
|
|
res.op == UserOperation.EditComment ||
|
|
|
|
res.op == UserOperation.DeleteComment ||
|
|
|
|
res.op == UserOperation.RemoveComment
|
|
|
|
) {
|
|
|
|
let data = res.data as CommentResponse;
|
2020-09-09 00:48:17 +00:00
|
|
|
editCommentRes(data, this.state.postRes.comments);
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.SaveComment) {
|
|
|
|
let data = res.data as CommentResponse;
|
2020-09-09 00:48:17 +00:00
|
|
|
saveCommentRes(data, this.state.postRes.comments);
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
setupTippy();
|
|
|
|
} else if (res.op == UserOperation.CreateCommentLike) {
|
|
|
|
let data = res.data as CommentResponse;
|
2020-09-09 00:48:17 +00:00
|
|
|
createCommentLikeRes(data, this.state.postRes.comments);
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.CreatePostLike) {
|
|
|
|
let data = res.data as PostResponse;
|
2020-09-09 00:48:17 +00:00
|
|
|
createPostLikeRes(data, this.state.postRes.post);
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (
|
|
|
|
res.op == UserOperation.EditPost ||
|
|
|
|
res.op == UserOperation.DeletePost ||
|
|
|
|
res.op == UserOperation.RemovePost ||
|
|
|
|
res.op == UserOperation.LockPost ||
|
|
|
|
res.op == UserOperation.StickyPost
|
|
|
|
) {
|
|
|
|
let data = res.data as PostResponse;
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.postRes.post = data.post;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
setupTippy();
|
|
|
|
} else if (res.op == UserOperation.SavePost) {
|
|
|
|
let data = res.data as PostResponse;
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.postRes.post = data.post;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
setupTippy();
|
|
|
|
} else if (
|
|
|
|
res.op == UserOperation.EditCommunity ||
|
|
|
|
res.op == UserOperation.DeleteCommunity ||
|
|
|
|
res.op == UserOperation.RemoveCommunity
|
|
|
|
) {
|
|
|
|
let data = res.data as CommunityResponse;
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.postRes.community = data.community;
|
|
|
|
this.state.postRes.post.community_id = data.community.id;
|
|
|
|
this.state.postRes.post.community_name = data.community.name;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.FollowCommunity) {
|
|
|
|
let data = res.data as CommunityResponse;
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.postRes.community.subscribed = data.community.subscribed;
|
|
|
|
this.state.postRes.community.number_of_subscribers =
|
2020-09-06 16:15:25 +00:00
|
|
|
data.community.number_of_subscribers;
|
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.BanFromCommunity) {
|
|
|
|
let data = res.data as BanFromCommunityResponse;
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.postRes.comments
|
2020-09-06 16:15:25 +00:00
|
|
|
.filter(c => c.creator_id == data.user.id)
|
|
|
|
.forEach(c => (c.banned_from_community = data.banned));
|
2020-09-09 00:48:17 +00:00
|
|
|
if (this.state.postRes.post.creator_id == data.user.id) {
|
|
|
|
this.state.postRes.post.banned_from_community = data.banned;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.AddModToCommunity) {
|
|
|
|
let data = res.data as AddModToCommunityResponse;
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.postRes.moderators = data.moderators;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.BanUser) {
|
|
|
|
let data = res.data as BanUserResponse;
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.postRes.comments
|
2020-09-06 16:15:25 +00:00
|
|
|
.filter(c => c.creator_id == data.user.id)
|
|
|
|
.forEach(c => (c.banned = data.banned));
|
2020-09-09 00:48:17 +00:00
|
|
|
if (this.state.postRes.post.creator_id == data.user.id) {
|
|
|
|
this.state.postRes.post.banned = data.banned;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.AddAdmin) {
|
|
|
|
let data = res.data as AddAdminResponse;
|
|
|
|
this.state.siteRes.admins = data.admins;
|
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.Search) {
|
|
|
|
let data = res.data as SearchResponse;
|
|
|
|
this.state.crossPosts = data.posts.filter(
|
|
|
|
p => p.id != Number(this.props.match.params.id)
|
|
|
|
);
|
|
|
|
if (this.state.crossPosts.length) {
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.postRes.post.duplicates = this.state.crossPosts;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
this.setState(this.state);
|
2020-09-09 00:48:17 +00:00
|
|
|
} else if (res.op == UserOperation.TransferSite) {
|
2020-09-06 16:15:25 +00:00
|
|
|
let data = res.data as GetSiteResponse;
|
|
|
|
this.state.siteRes = data;
|
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.TransferCommunity) {
|
|
|
|
let data = res.data as GetCommunityResponse;
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.postRes.community = data.community;
|
|
|
|
this.state.postRes.moderators = data.moderators;
|
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.ListCategories) {
|
|
|
|
let data = res.data as ListCategoriesResponse;
|
|
|
|
this.state.categories = data.categories;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|