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,
|
2020-07-21 01:37:44 +00:00
|
|
|
MarkCommentAsReadForm,
|
2019-10-19 00:20:27 +00:00
|
|
|
CommentResponse,
|
|
|
|
CommentSortType,
|
2020-07-12 04:58:42 +00:00
|
|
|
CommentViewType,
|
2019-10-19 00:20:27 +00:00
|
|
|
CommunityUser,
|
|
|
|
CommunityResponse,
|
|
|
|
CommentNode as CommentNodeI,
|
|
|
|
BanFromCommunityResponse,
|
|
|
|
BanUserResponse,
|
|
|
|
AddModToCommunityResponse,
|
|
|
|
AddAdminResponse,
|
|
|
|
SearchType,
|
|
|
|
SortType,
|
|
|
|
SearchForm,
|
2020-01-28 02:04:30 +00:00
|
|
|
GetPostForm,
|
2019-10-19 00:20:27 +00:00
|
|
|
SearchResponse,
|
|
|
|
GetSiteResponse,
|
|
|
|
GetCommunityResponse,
|
2020-01-19 05:38:45 +00:00
|
|
|
WebSocketJsonResponse,
|
2019-10-19 00:20:27 +00:00
|
|
|
} from '../interfaces';
|
2019-05-19 16:15:08 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
2020-02-09 04:20:11 +00:00
|
|
|
import {
|
|
|
|
wsJsonToRes,
|
|
|
|
toast,
|
|
|
|
editCommentRes,
|
|
|
|
saveCommentRes,
|
|
|
|
createCommentLikeRes,
|
|
|
|
createPostLikeRes,
|
|
|
|
commentsToFlatNodes,
|
2020-03-05 03:35:55 +00:00
|
|
|
setupTippy,
|
2020-02-09 04:20:11 +00:00
|
|
|
} from '../utils';
|
2019-04-03 06:49:32 +00:00
|
|
|
import { PostListing } from './post-listing';
|
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';
|
2020-01-06 16:22:51 +00:00
|
|
|
import autosize from 'autosize';
|
2019-08-10 00:14:43 +00:00
|
|
|
import { i18n } from '../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;
|
2020-07-12 04:58:42 +00:00
|
|
|
commentViewType: CommentViewType;
|
2019-04-03 23:01:20 +00:00
|
|
|
community: Community;
|
2019-04-04 20:53:32 +00:00
|
|
|
moderators: Array<CommunityUser>;
|
2020-02-01 01:02:20 +00:00
|
|
|
online: number;
|
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>;
|
2020-07-08 05:02:14 +00:00
|
|
|
siteRes: GetSiteResponse;
|
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,
|
2020-07-12 04:58:42 +00:00
|
|
|
commentViewType: CommentViewType.Tree,
|
2019-04-03 23:01:20 +00:00
|
|
|
community: null,
|
2019-04-05 20:22:59 +00:00
|
|
|
moderators: [],
|
2020-02-01 01:02:20 +00:00
|
|
|
online: null,
|
2019-10-19 00:20:27 +00:00
|
|
|
scrolled: false,
|
2019-08-22 05:17:15 +00:00
|
|
|
loading: true,
|
|
|
|
crossPosts: [],
|
2020-07-08 05:02:14 +00:00
|
|
|
siteRes: {
|
|
|
|
admins: [],
|
|
|
|
banned: [],
|
|
|
|
site: {
|
|
|
|
id: undefined,
|
|
|
|
name: undefined,
|
|
|
|
creator_id: undefined,
|
|
|
|
published: undefined,
|
|
|
|
creator_name: undefined,
|
|
|
|
number_of_users: undefined,
|
|
|
|
number_of_posts: undefined,
|
|
|
|
number_of_comments: undefined,
|
|
|
|
number_of_communities: undefined,
|
|
|
|
enable_downvotes: undefined,
|
|
|
|
open_registration: undefined,
|
|
|
|
enable_nsfw: undefined,
|
|
|
|
},
|
|
|
|
online: null,
|
2020-07-21 13:20:23 +00:00
|
|
|
version: null,
|
2020-07-08 05:02:14 +00:00
|
|
|
},
|
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
|
2020-01-06 16:22:51 +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')
|
|
|
|
);
|
|
|
|
|
2020-01-28 02:04:30 +00:00
|
|
|
let form: GetPostForm = {
|
|
|
|
id: postId,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.getPost(form);
|
2020-07-08 05:02:14 +00:00
|
|
|
WebSocketService.Instance.getSite();
|
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
|
|
|
|
) {
|
2020-07-21 01:37:44 +00:00
|
|
|
let form: MarkCommentAsReadForm = {
|
2019-05-19 16:15:08 +00:00
|
|
|
edit_id: found.id,
|
|
|
|
read: true,
|
2019-10-19 00:20:27 +00:00
|
|
|
auth: null,
|
2019-05-19 16:15:08 +00:00
|
|
|
};
|
2020-07-21 01:37:44 +00:00
|
|
|
WebSocketService.Instance.markCommentAsRead(form);
|
2020-07-27 13:23:08 +00:00
|
|
|
UserService.Instance.unreadCountSub.next(
|
|
|
|
UserService.Instance.unreadCountSub.value - 1
|
|
|
|
);
|
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
|
|
|
|
moderators={this.state.moderators}
|
2020-07-08 05:02:14 +00:00
|
|
|
admins={this.state.siteRes.admins}
|
|
|
|
enableDownvotes={this.state.siteRes.site.enable_downvotes}
|
|
|
|
enableNsfw={this.state.siteRes.site.enable_nsfw}
|
2019-04-20 07:24:59 +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}
|
|
|
|
/>
|
2020-01-19 21:47:54 +00:00
|
|
|
{this.state.comments.length > 0 && this.sortRadios()}
|
2020-07-12 04:58:42 +00:00
|
|
|
{this.state.commentViewType == CommentViewType.Tree &&
|
|
|
|
this.commentsTree()}
|
|
|
|
{this.state.commentViewType == CommentViewType.Chat &&
|
|
|
|
this.commentsFlat()}
|
2019-04-03 06:49:32 +00:00
|
|
|
</div>
|
2020-07-12 04:58:42 +00:00
|
|
|
<div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</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 (
|
2020-07-12 04:58:42 +00:00
|
|
|
<>
|
2020-07-12 18:10:38 +00:00
|
|
|
<div class="btn-group btn-group-toggle mr-3 mb-2">
|
2020-07-12 04:58:42 +00:00
|
|
|
<label
|
2020-07-23 06:10:22 +00:00
|
|
|
className={`btn btn-outline-secondary pointer ${
|
2020-07-12 04:58:42 +00:00
|
|
|
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
|
2020-07-23 06:10:22 +00:00
|
|
|
className={`btn btn-outline-secondary pointer ${
|
2020-07-12 04:58:42 +00:00
|
|
|
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
|
2020-07-23 06:10:22 +00:00
|
|
|
className={`btn btn-outline-secondary pointer ${
|
2020-07-12 04:58:42 +00:00
|
|
|
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
|
2020-07-23 06:10:22 +00:00
|
|
|
className={`btn btn-outline-secondary pointer ${
|
2020-07-12 04:58:42 +00:00
|
|
|
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>
|
2020-07-12 18:10:38 +00:00
|
|
|
<div class="btn-group btn-group-toggle mb-2">
|
2020-07-12 04:58:42 +00:00
|
|
|
<label
|
2020-07-23 06:10:22 +00:00
|
|
|
className={`btn btn-outline-secondary pointer ${
|
2020-07-12 04:58:42 +00:00
|
|
|
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>
|
|
|
|
</>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-03-30 06:08:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-12 04:58:42 +00:00
|
|
|
commentsFlat() {
|
2019-03-27 16:59:21 +00:00
|
|
|
return (
|
2020-07-12 04:58:42 +00:00
|
|
|
<div>
|
|
|
|
<CommentNodes
|
|
|
|
nodes={commentsToFlatNodes(this.state.comments)}
|
|
|
|
noIndent
|
|
|
|
locked={this.state.post.locked}
|
|
|
|
moderators={this.state.moderators}
|
|
|
|
admins={this.state.siteRes.admins}
|
|
|
|
postCreatorId={this.state.post.creator_id}
|
|
|
|
showContext
|
|
|
|
enableDownvotes={this.state.siteRes.site.enable_downvotes}
|
|
|
|
sort={this.state.commentSort}
|
|
|
|
/>
|
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}
|
2020-07-08 05:02:14 +00:00
|
|
|
admins={this.state.siteRes.admins}
|
2020-02-01 01:02:20 +00:00
|
|
|
online={this.state.online}
|
2020-07-08 05:02:14 +00:00
|
|
|
enableNsfw={this.state.siteRes.site.enable_nsfw}
|
2019-04-21 20:52:55 +00:00
|
|
|
/>
|
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);
|
2020-07-13 19:20:46 +00:00
|
|
|
i.state.commentViewType = CommentViewType.Tree;
|
2019-03-30 06:08:02 +00:00
|
|
|
i.setState(i.state);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-12 04:58:42 +00:00
|
|
|
handleCommentViewTypeChange(i: Post, event: any) {
|
|
|
|
i.state.commentViewType = Number(event.target.value);
|
2020-07-13 19:20:46 +00:00
|
|
|
i.state.commentSort = CommentSortType.New;
|
2020-07-12 04:58:42 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2020-03-10 01:38:10 +00:00
|
|
|
buildCommentsTree(): Array<CommentNodeI> {
|
2019-03-30 06:08:02 +00:00
|
|
|
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) {
|
2020-03-05 20:10:46 +00:00
|
|
|
let child = map.get(comment.id);
|
2019-10-19 00:20:27 +00:00
|
|
|
if (comment.parent_id) {
|
2020-03-05 20:10:46 +00:00
|
|
|
let parent_ = map.get(comment.parent_id);
|
|
|
|
parent_.children.push(child);
|
2019-10-19 00:20:27 +00:00
|
|
|
} else {
|
2020-03-05 20:10:46 +00:00
|
|
|
tree.push(child);
|
2019-03-30 06:08:02 +00:00
|
|
|
}
|
2020-03-05 20:10:46 +00:00
|
|
|
|
|
|
|
this.setDepth(child);
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
2019-03-30 06:08:02 +00:00
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:10:46 +00:00
|
|
|
setDepth(node: CommentNodeI, i: number = 0): void {
|
|
|
|
for (let child of node.children) {
|
|
|
|
child.comment.depth = i;
|
|
|
|
this.setDepth(child, i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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}
|
2020-07-08 05:02:14 +00:00
|
|
|
admins={this.state.siteRes.admins}
|
2019-09-13 15:37:12 +00:00
|
|
|
postCreatorId={this.state.post.creator_id}
|
2020-02-09 16:44:24 +00:00
|
|
|
sort={this.state.commentSort}
|
2020-07-08 05:02:14 +00:00
|
|
|
enableDownvotes={this.state.siteRes.site.enable_downvotes}
|
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
|
|
|
}
|
|
|
|
|
2020-01-19 05:38:45 +00:00
|
|
|
parseMessage(msg: WebSocketJsonResponse) {
|
2019-03-26 18:00:18 +00:00
|
|
|
console.log(msg);
|
2020-01-19 04:54:10 +00:00
|
|
|
let res = wsJsonToRes(msg);
|
2020-01-25 14:58:53 +00:00
|
|
|
if (msg.error) {
|
2020-01-23 03:29:11 +00:00
|
|
|
toast(i18n.t(msg.error), 'danger');
|
2019-03-26 18:00:18 +00:00
|
|
|
return;
|
2020-02-04 16:19:05 +00:00
|
|
|
} else if (msg.reconnect) {
|
|
|
|
WebSocketService.Instance.getPost({
|
|
|
|
id: Number(this.props.match.params.id),
|
|
|
|
});
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.GetPost) {
|
|
|
|
let data = res.data as GetPostResponse;
|
|
|
|
this.state.post = data.post;
|
|
|
|
this.state.comments = data.comments;
|
|
|
|
this.state.community = data.community;
|
|
|
|
this.state.moderators = data.moderators;
|
2020-02-01 01:02:20 +00:00
|
|
|
this.state.online = data.online;
|
2019-04-08 21:46:09 +00:00
|
|
|
this.state.loading = false;
|
2020-07-08 05:02:14 +00:00
|
|
|
document.title = `${this.state.post.name} - ${this.state.siteRes.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);
|
2020-03-05 03:35:55 +00:00
|
|
|
setupTippy();
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.CreateComment) {
|
|
|
|
let data = res.data as CommentResponse;
|
2020-02-02 15:09:01 +00:00
|
|
|
|
2020-02-02 17:45:41 +00:00
|
|
|
// Necessary since it might be a user reply
|
|
|
|
if (data.recipient_ids.length == 0) {
|
2020-02-02 15:09:01 +00:00
|
|
|
this.state.comments.unshift(data.comment);
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
2020-07-21 01:37:44 +00:00
|
|
|
} else if (
|
|
|
|
res.op == UserOperation.EditComment ||
|
|
|
|
res.op == UserOperation.DeleteComment ||
|
|
|
|
res.op == UserOperation.RemoveComment
|
|
|
|
) {
|
2020-01-19 04:54:10 +00:00
|
|
|
let data = res.data as CommentResponse;
|
2020-02-09 04:20:11 +00:00
|
|
|
editCommentRes(data, this.state.comments);
|
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.SaveComment) {
|
|
|
|
let data = res.data as CommentResponse;
|
2020-02-09 04:20:11 +00:00
|
|
|
saveCommentRes(data, this.state.comments);
|
|
|
|
this.setState(this.state);
|
2020-03-05 03:35:55 +00:00
|
|
|
setupTippy();
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.CreateCommentLike) {
|
|
|
|
let data = res.data as CommentResponse;
|
2020-02-09 04:20:11 +00:00
|
|
|
createCommentLikeRes(data, this.state.comments);
|
2019-03-28 19:32:08 +00:00
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.CreatePostLike) {
|
2020-02-02 17:45:41 +00:00
|
|
|
let data = res.data as PostResponse;
|
2020-02-09 04:20:11 +00:00
|
|
|
createPostLikeRes(data, this.state.post);
|
2019-04-03 06:49:32 +00:00
|
|
|
this.setState(this.state);
|
2020-07-21 03:46:36 +00:00
|
|
|
} else if (
|
|
|
|
res.op == UserOperation.EditPost ||
|
|
|
|
res.op == UserOperation.DeletePost ||
|
|
|
|
res.op == UserOperation.RemovePost ||
|
|
|
|
res.op == UserOperation.LockPost ||
|
|
|
|
res.op == UserOperation.StickyPost
|
|
|
|
) {
|
2020-01-19 04:54:10 +00:00
|
|
|
let data = res.data as PostResponse;
|
|
|
|
this.state.post = data.post;
|
2019-04-03 20:59:37 +00:00
|
|
|
this.setState(this.state);
|
2020-03-05 03:35:55 +00:00
|
|
|
setupTippy();
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.SavePost) {
|
|
|
|
let data = res.data as PostResponse;
|
|
|
|
this.state.post = data.post;
|
2019-04-20 04:06:25 +00:00
|
|
|
this.setState(this.state);
|
2020-03-05 03:35:55 +00:00
|
|
|
setupTippy();
|
2020-07-20 17:37:39 +00:00
|
|
|
} else if (
|
|
|
|
res.op == UserOperation.EditCommunity ||
|
|
|
|
res.op == UserOperation.DeleteCommunity ||
|
|
|
|
res.op == UserOperation.RemoveCommunity
|
|
|
|
) {
|
2020-01-19 04:54:10 +00:00
|
|
|
let data = res.data as CommunityResponse;
|
|
|
|
this.state.community = data.community;
|
|
|
|
this.state.post.community_id = data.community.id;
|
|
|
|
this.state.post.community_name = data.community.name;
|
2019-04-04 22:29:14 +00:00
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.FollowCommunity) {
|
|
|
|
let data = res.data as CommunityResponse;
|
|
|
|
this.state.community.subscribed = data.community.subscribed;
|
2019-10-19 00:20:27 +00:00
|
|
|
this.state.community.number_of_subscribers =
|
2020-01-19 04:54:10 +00:00
|
|
|
data.community.number_of_subscribers;
|
2019-04-05 06:26:38 +00:00
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.BanFromCommunity) {
|
|
|
|
let data = res.data as BanFromCommunityResponse;
|
2019-10-19 00:20:27 +00:00
|
|
|
this.state.comments
|
2020-01-19 04:54:10 +00:00
|
|
|
.filter(c => c.creator_id == data.user.id)
|
|
|
|
.forEach(c => (c.banned_from_community = data.banned));
|
|
|
|
if (this.state.post.creator_id == data.user.id) {
|
|
|
|
this.state.post.banned_from_community = data.banned;
|
2019-09-06 00:18:48 +00:00
|
|
|
}
|
2019-04-15 23:12:06 +00:00
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.AddModToCommunity) {
|
|
|
|
let data = res.data as AddModToCommunityResponse;
|
|
|
|
this.state.moderators = data.moderators;
|
2019-04-15 23:12:06 +00:00
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.BanUser) {
|
|
|
|
let data = res.data as BanUserResponse;
|
2019-10-19 00:20:27 +00:00
|
|
|
this.state.comments
|
2020-01-19 04:54:10 +00:00
|
|
|
.filter(c => c.creator_id == data.user.id)
|
|
|
|
.forEach(c => (c.banned = data.banned));
|
|
|
|
if (this.state.post.creator_id == data.user.id) {
|
|
|
|
this.state.post.banned = data.banned;
|
2019-09-06 00:18:48 +00:00
|
|
|
}
|
2019-04-20 04:06:25 +00:00
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.AddAdmin) {
|
|
|
|
let data = res.data as AddAdminResponse;
|
2020-07-08 05:02:14 +00:00
|
|
|
this.state.siteRes.admins = data.admins;
|
2019-04-20 04:06:25 +00:00
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.Search) {
|
|
|
|
let data = res.data as SearchResponse;
|
|
|
|
this.state.crossPosts = data.posts.filter(
|
2020-04-05 14:11:16 +00:00
|
|
|
p => p.id != Number(this.props.match.params.id)
|
2020-01-19 04:54:10 +00:00
|
|
|
);
|
2020-05-18 13:50:05 +00:00
|
|
|
if (this.state.crossPosts.length) {
|
|
|
|
this.state.post.duplicates = this.state.crossPosts;
|
|
|
|
}
|
2019-08-22 05:17:15 +00:00
|
|
|
this.setState(this.state);
|
2020-07-08 05:02:14 +00:00
|
|
|
} else if (
|
|
|
|
res.op == UserOperation.TransferSite ||
|
|
|
|
res.op == UserOperation.GetSite
|
|
|
|
) {
|
2020-01-19 04:54:10 +00:00
|
|
|
let data = res.data as GetSiteResponse;
|
2020-07-08 05:02:14 +00:00
|
|
|
this.state.siteRes = data;
|
2019-08-24 02:40:41 +00:00
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.TransferCommunity) {
|
|
|
|
let data = res.data as GetCommunityResponse;
|
|
|
|
this.state.community = data.community;
|
|
|
|
this.state.moderators = data.moderators;
|
2019-08-24 02:40:41 +00:00
|
|
|
this.setState(this.state);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
}
|