2021-07-17 20:42:55 +00:00
|
|
|
import autosize from "autosize";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2021-07-17 20:42:55 +00:00
|
|
|
AddAdminResponse,
|
|
|
|
AddModToCommunityResponse,
|
2020-09-06 16:15:25 +00:00
|
|
|
BanFromCommunityResponse,
|
2021-03-15 18:09:31 +00:00
|
|
|
BanPersonResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
CommentResponse,
|
|
|
|
CommunityResponse,
|
|
|
|
GetCommunityResponse,
|
2020-12-24 01:58:27 +00:00
|
|
|
GetPost,
|
2021-07-17 20:42:55 +00:00
|
|
|
GetPostResponse,
|
2020-09-06 16:15:25 +00:00
|
|
|
GetSiteResponse,
|
2021-04-23 21:48:31 +00:00
|
|
|
ListingType,
|
2021-07-17 20:42:55 +00:00
|
|
|
MarkCommentAsRead,
|
|
|
|
PostResponse,
|
|
|
|
PostView,
|
|
|
|
Search,
|
|
|
|
SearchResponse,
|
|
|
|
SearchType,
|
|
|
|
SortType,
|
|
|
|
UserOperation,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { i18n } from "../../i18next";
|
2020-11-12 21:56:46 +00:00
|
|
|
import {
|
2021-07-17 20:42:55 +00:00
|
|
|
CommentNode as CommentNodeI,
|
2020-11-12 21:56:46 +00:00
|
|
|
CommentSortType,
|
|
|
|
CommentViewType,
|
|
|
|
InitialFetchRequest,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../interfaces";
|
|
|
|
import { UserService, WebSocketService } from "../../services";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2021-07-17 20:42:55 +00:00
|
|
|
authField,
|
|
|
|
buildCommentsTree,
|
|
|
|
commentsToFlatNodes,
|
2020-09-06 16:15:25 +00:00
|
|
|
createCommentLikeRes,
|
|
|
|
createPostLikeRes,
|
2021-07-17 20:42:55 +00:00
|
|
|
editCommentRes,
|
2020-09-09 00:48:17 +00:00
|
|
|
getCommentIdFromProps,
|
2021-07-17 20:42:55 +00:00
|
|
|
getIdFromProps,
|
|
|
|
insertCommentIntoTree,
|
2020-09-09 00:48:17 +00:00
|
|
|
isBrowser,
|
2020-09-11 18:09:21 +00:00
|
|
|
isImage,
|
2021-07-17 20:42:55 +00:00
|
|
|
previewLines,
|
2021-02-02 18:36:59 +00:00
|
|
|
restoreScrollPosition,
|
2021-07-17 20:42:55 +00:00
|
|
|
saveCommentRes,
|
|
|
|
saveScrollPosition,
|
|
|
|
setIsoData,
|
|
|
|
setOptionalAuth,
|
|
|
|
setupTippy,
|
|
|
|
toast,
|
|
|
|
wsClient,
|
|
|
|
wsJsonToRes,
|
|
|
|
wsSubscribe,
|
|
|
|
wsUserOp,
|
|
|
|
} from "../../utils";
|
|
|
|
import { CommentForm } from "../comment/comment-form";
|
|
|
|
import { CommentNodes } from "../comment/comment-nodes";
|
|
|
|
import { HtmlTags } from "../common/html-tags";
|
2021-07-18 03:56:33 +00:00
|
|
|
import { Icon, Spinner } from "../common/icon";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Sidebar } from "../community/sidebar";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { PostListing } from "./post-listing";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface PostState {
|
2020-09-09 00:48:17 +00:00
|
|
|
postRes: GetPostResponse;
|
|
|
|
postId: number;
|
2021-02-08 04:23:31 +00:00
|
|
|
commentTree: CommentNodeI[];
|
2020-09-09 00:48:17 +00:00
|
|
|
commentId?: number;
|
2020-09-06 16:15:25 +00:00
|
|
|
commentSort: CommentSortType;
|
|
|
|
commentViewType: CommentViewType;
|
|
|
|
scrolled?: boolean;
|
|
|
|
loading: boolean;
|
2020-12-24 01:58:27 +00:00
|
|
|
crossPosts: PostView[];
|
2020-09-06 16:15:25 +00:00
|
|
|
siteRes: GetSiteResponse;
|
2021-07-18 03:56:33 +00:00
|
|
|
showSidebarMobile: boolean;
|
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),
|
2021-02-08 04:23:31 +00:00
|
|
|
commentTree: [],
|
2020-09-09 00:48:17 +00:00
|
|
|
commentId: getCommentIdFromProps(this.props),
|
2020-09-06 16:15:25 +00:00
|
|
|
commentSort: CommentSortType.Hot,
|
|
|
|
commentViewType: CommentViewType.Tree,
|
|
|
|
scrolled: false,
|
|
|
|
loading: true,
|
|
|
|
crossPosts: [],
|
2020-12-24 01:58:27 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
2021-07-18 03:56:33 +00:00
|
|
|
showSidebarMobile: false,
|
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];
|
2021-02-08 04:23:31 +00:00
|
|
|
this.state.commentTree = buildCommentsTree(
|
|
|
|
this.state.postRes.comments,
|
|
|
|
this.state.commentSort
|
|
|
|
);
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.loading = false;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2021-01-24 01:41:23 +00:00
|
|
|
if (isBrowser()) {
|
|
|
|
this.fetchCrossPosts();
|
|
|
|
if (this.state.commentId) {
|
|
|
|
this.scrollCommentIntoView();
|
|
|
|
}
|
2020-09-09 00:48:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.fetchPost();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchPost() {
|
2020-12-24 01:58:27 +00:00
|
|
|
let form: GetPost = {
|
2020-09-09 00:48:17 +00:00
|
|
|
id: this.state.postId,
|
2020-12-24 22:05:57 +00:00
|
|
|
auth: authField(false),
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.getPost(form));
|
2020-09-09 00:48:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 01:41:23 +00:00
|
|
|
fetchCrossPosts() {
|
|
|
|
if (this.state.postRes.post_view.post.url) {
|
|
|
|
let form: Search = {
|
|
|
|
q: this.state.postRes.post_view.post.url,
|
|
|
|
type_: SearchType.Url,
|
|
|
|
sort: SortType.TopAll,
|
2021-04-23 21:48:31 +00:00
|
|
|
listing_type: ListingType.All,
|
2021-01-24 01:41:23 +00:00
|
|
|
page: 1,
|
|
|
|
limit: 6,
|
|
|
|
auth: authField(false),
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.send(wsClient.search(form));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:56:46 +00:00
|
|
|
static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
|
2021-02-22 02:39:04 +00:00
|
|
|
let pathSplit = req.path.split("/");
|
2020-09-09 00:48:17 +00:00
|
|
|
let promises: Promise<any>[] = [];
|
|
|
|
|
|
|
|
let id = Number(pathSplit[2]);
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
let postForm: GetPost = {
|
2020-09-09 00:48:17 +00:00
|
|
|
id,
|
|
|
|
};
|
2020-12-24 22:05:57 +00:00
|
|
|
setOptionalAuth(postForm, req.auth);
|
2020-09-09 00:48:17 +00:00
|
|
|
|
2020-11-12 21:56:46 +00:00
|
|
|
promises.push(req.client.getPost(postForm));
|
2020-09-09 00:48:17 +00:00
|
|
|
|
|
|
|
return promises;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
2020-11-05 23:41:21 +00:00
|
|
|
window.isoData.path = undefined;
|
2021-02-02 18:36:59 +00:00
|
|
|
saveScrollPosition(this.context);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.postJoin({ post_id: this.state.postId })
|
|
|
|
);
|
2021-02-22 02:39:04 +00:00
|
|
|
autosize(document.querySelectorAll("textarea"));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:24:09 +00:00
|
|
|
componentDidUpdate(_lastProps: any, lastState: PostState) {
|
2020-09-06 16:15:25 +00:00
|
|
|
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.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();
|
2021-02-22 02:39:04 +00:00
|
|
|
elmnt.classList.add("mark");
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.scrolled = true;
|
|
|
|
this.markScrolledAsRead(this.state.commentId);
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
// TODO this needs some re-work
|
2020-09-06 16:15:25 +00:00
|
|
|
markScrolledAsRead(commentId: number) {
|
2020-12-24 01:58:27 +00:00
|
|
|
let found = this.state.postRes.comments.find(
|
|
|
|
c => c.comment.id == commentId
|
|
|
|
);
|
|
|
|
let parent = this.state.postRes.comments.find(
|
|
|
|
c => found.comment.parent_id == c.comment.id
|
|
|
|
);
|
2021-03-15 18:09:31 +00:00
|
|
|
let parent_person_id = parent
|
2020-12-24 01:58:27 +00:00
|
|
|
? parent.creator.id
|
|
|
|
: this.state.postRes.post_view.creator.id;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
if (
|
2021-03-15 18:09:31 +00:00
|
|
|
UserService.Instance.localUserView &&
|
|
|
|
UserService.Instance.localUserView.person.id == parent_person_id
|
2020-09-06 16:15:25 +00:00
|
|
|
) {
|
2020-12-24 01:58:27 +00:00
|
|
|
let form: MarkCommentAsRead = {
|
2021-01-06 21:07:13 +00:00
|
|
|
comment_id: found.comment.id,
|
2020-09-06 16:15:25 +00:00
|
|
|
read: true,
|
2020-12-24 22:05:57 +00:00
|
|
|
auth: authField(),
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.markCommentAsRead(form));
|
2020-09-06 16:15:25 +00:00
|
|
|
UserService.Instance.unreadCountSub.next(
|
|
|
|
UserService.Instance.unreadCountSub.value - 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2020-12-24 01:58:27 +00:00
|
|
|
return `${this.state.postRes.post_view.post.name} - ${this.state.siteRes.site_view.site.name}`;
|
2020-09-11 18:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get imageTag(): string {
|
2020-12-24 01:58:27 +00:00
|
|
|
let post = this.state.postRes.post_view.post;
|
2020-09-11 18:09:21 +00:00
|
|
|
return (
|
2020-12-24 01:58:27 +00:00
|
|
|
post.thumbnail_url ||
|
|
|
|
(post.url ? (isImage(post.url) ? post.url : undefined) : undefined)
|
2020-09-11 18:09:21 +00:00
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 18:09:21 +00:00
|
|
|
get descriptionTag(): string {
|
2020-12-24 01:58:27 +00:00
|
|
|
let body = this.state.postRes.post_view.post.body;
|
|
|
|
return body ? previewLines(body) : undefined;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-12-24 22:05:57 +00:00
|
|
|
let pv = this.state.postRes?.post_view;
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
|
|
|
<div class="container">
|
|
|
|
{this.state.loading ? (
|
|
|
|
<h5>
|
2021-07-17 20:21:31 +00:00
|
|
|
<Spinner large />
|
2020-09-06 16:15:25 +00:00
|
|
|
</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-12-24 01:58:27 +00:00
|
|
|
post_view={pv}
|
|
|
|
duplicates={this.state.crossPosts}
|
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}
|
2020-12-24 01:58:27 +00:00
|
|
|
enableDownvotes={
|
|
|
|
this.state.siteRes.site_view.site.enable_downvotes
|
|
|
|
}
|
|
|
|
enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
<div className="mb-2" />
|
|
|
|
<CommentForm
|
2020-09-09 00:48:17 +00:00
|
|
|
postId={this.state.postId}
|
2020-12-24 01:58:27 +00:00
|
|
|
disabled={pv.post.locked}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
2021-07-18 03:56:33 +00:00
|
|
|
<div class="d-block d-md-none">
|
|
|
|
<button
|
|
|
|
class="btn btn-secondary d-inline-block mb-2 mr-3"
|
|
|
|
onClick={linkEvent(this, this.handleShowSidebarMobile)}
|
|
|
|
>
|
|
|
|
{i18n.t("sidebar")}{" "}
|
|
|
|
<Icon
|
|
|
|
icon={
|
|
|
|
this.state.showSidebarMobile
|
|
|
|
? `minus-square`
|
|
|
|
: `plus-square`
|
|
|
|
}
|
|
|
|
classes="icon-inline"
|
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
{this.state.showSidebarMobile && this.sidebar()}
|
|
|
|
</div>
|
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>
|
2021-07-18 03:56:33 +00:00
|
|
|
<div class="d-none d-md-block col-md-4">{this.sidebar()}</div>
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sortRadios() {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div class="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
|
|
|
|
<label
|
|
|
|
className={`btn btn-outline-secondary pointer ${
|
2021-02-22 02:39:04 +00:00
|
|
|
this.state.commentSort === CommentSortType.Hot && "active"
|
2020-09-06 16:15:25 +00:00
|
|
|
}`}
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("hot")}
|
2020-09-06 16:15:25 +00:00
|
|
|
<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 ${
|
2021-02-22 02:39:04 +00:00
|
|
|
this.state.commentSort === CommentSortType.Top && "active"
|
2020-09-06 16:15:25 +00:00
|
|
|
}`}
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("top")}
|
2020-09-06 16:15:25 +00:00
|
|
|
<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 ${
|
2021-02-22 02:39:04 +00:00
|
|
|
this.state.commentSort === CommentSortType.New && "active"
|
2020-09-06 16:15:25 +00:00
|
|
|
}`}
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("new")}
|
2020-09-06 16:15:25 +00:00
|
|
|
<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 ${
|
2021-02-22 02:39:04 +00:00
|
|
|
this.state.commentSort === CommentSortType.Old && "active"
|
2020-09-06 16:15:25 +00:00
|
|
|
}`}
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("old")}
|
2020-09-06 16:15:25 +00:00
|
|
|
<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 ${
|
2021-02-22 02:39:04 +00:00
|
|
|
this.state.commentViewType === CommentViewType.Chat && "active"
|
2020-09-06 16:15:25 +00:00
|
|
|
}`}
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("chat")}
|
2020-09-06 16:15:25 +00:00
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
value={CommentViewType.Chat}
|
|
|
|
checked={this.state.commentViewType === CommentViewType.Chat}
|
|
|
|
onChange={linkEvent(this, this.handleCommentViewTypeChange)}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
commentsFlat() {
|
2021-02-08 04:23:31 +00:00
|
|
|
// These are already sorted by new
|
2020-09-06 16:15:25 +00:00
|
|
|
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-12-24 01:58:27 +00:00
|
|
|
locked={this.state.postRes.post_view.post.locked}
|
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}
|
2020-12-24 01:58:27 +00:00
|
|
|
postCreatorId={this.state.postRes.post_view.creator.id}
|
2020-09-06 16:15:25 +00:00
|
|
|
showContext
|
2020-12-24 01:58:27 +00:00
|
|
|
enableDownvotes={this.state.siteRes.site_view.site.enable_downvotes}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sidebar() {
|
|
|
|
return (
|
|
|
|
<div class="mb-3">
|
|
|
|
<Sidebar
|
2020-12-24 01:58:27 +00:00
|
|
|
community_view={this.state.postRes.community_view}
|
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}
|
2020-09-09 00:48:17 +00:00
|
|
|
online={this.state.postRes.online}
|
2020-12-24 01:58:27 +00:00
|
|
|
enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
|
2020-09-06 16:15:25 +00:00
|
|
|
showIcon
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCommentSortChange(i: Post, event: any) {
|
|
|
|
i.state.commentSort = Number(event.target.value);
|
|
|
|
i.state.commentViewType = CommentViewType.Tree;
|
2021-02-08 04:23:31 +00:00
|
|
|
i.state.commentTree = buildCommentsTree(
|
|
|
|
i.state.postRes.comments,
|
|
|
|
i.state.commentSort
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCommentViewTypeChange(i: Post, event: any) {
|
|
|
|
i.state.commentViewType = Number(event.target.value);
|
|
|
|
i.state.commentSort = CommentSortType.New;
|
2021-02-08 04:23:31 +00:00
|
|
|
i.state.commentTree = buildCommentsTree(
|
|
|
|
i.state.postRes.comments,
|
|
|
|
i.state.commentSort
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2021-07-18 03:56:33 +00:00
|
|
|
handleShowSidebarMobile(i: Post) {
|
|
|
|
i.state.showSidebarMobile = !i.state.showSidebarMobile;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
commentsTree() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<CommentNodes
|
2021-02-08 04:23:31 +00:00
|
|
|
nodes={this.state.commentTree}
|
2020-12-24 01:58:27 +00:00
|
|
|
locked={this.state.postRes.post_view.post.locked}
|
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}
|
2020-12-24 01:58:27 +00:00
|
|
|
postCreatorId={this.state.postRes.post_view.creator.id}
|
|
|
|
enableDownvotes={this.state.siteRes.site_view.site.enable_downvotes}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
|
|
|
let op = wsUserOp(msg);
|
2021-01-13 17:06:56 +00:00
|
|
|
console.log(msg);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t(msg.error), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
return;
|
|
|
|
} else if (msg.reconnect) {
|
2020-09-25 16:47:29 +00:00
|
|
|
let postId = Number(this.props.match.params.id);
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.postJoin({ post_id: postId }));
|
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.getPost({
|
|
|
|
id: postId,
|
|
|
|
auth: authField(false),
|
|
|
|
})
|
|
|
|
);
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.GetPost) {
|
|
|
|
let data = wsJsonToRes<GetPostResponse>(msg).data;
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.postRes = data;
|
2021-02-08 04:23:31 +00:00
|
|
|
this.state.commentTree = buildCommentsTree(
|
|
|
|
this.state.postRes.comments,
|
|
|
|
this.state.commentSort
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.loading = false;
|
|
|
|
|
|
|
|
// Get cross-posts
|
2021-01-24 01:41:23 +00:00
|
|
|
this.fetchCrossPosts();
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
setupTippy();
|
2021-03-25 15:38:37 +00:00
|
|
|
if (!this.state.commentId) restoreScrollPosition(this.context);
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.CreateComment) {
|
|
|
|
let data = wsJsonToRes<CommentResponse>(msg).data;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2021-01-13 17:06:56 +00:00
|
|
|
// Necessary since it might be a user reply, which has the recipients, to avoid double
|
|
|
|
if (data.recipient_ids.length == 0) {
|
2020-12-24 01:58:27 +00:00
|
|
|
this.state.postRes.comments.unshift(data.comment_view);
|
2021-02-08 04:23:31 +00:00
|
|
|
insertCommentIntoTree(this.state.commentTree, data.comment_view);
|
2021-01-15 17:42:05 +00:00
|
|
|
this.state.postRes.post_view.counts.comments++;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
2021-01-23 21:40:24 +00:00
|
|
|
setupTippy();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
} else if (
|
2020-12-24 01:58:27 +00:00
|
|
|
op == UserOperation.EditComment ||
|
|
|
|
op == UserOperation.DeleteComment ||
|
|
|
|
op == UserOperation.RemoveComment
|
2020-09-06 16:15:25 +00:00
|
|
|
) {
|
2020-12-24 01:58:27 +00:00
|
|
|
let data = wsJsonToRes<CommentResponse>(msg).data;
|
|
|
|
editCommentRes(data.comment_view, this.state.postRes.comments);
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.SaveComment) {
|
|
|
|
let data = wsJsonToRes<CommentResponse>(msg).data;
|
|
|
|
saveCommentRes(data.comment_view, this.state.postRes.comments);
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
setupTippy();
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.CreateCommentLike) {
|
|
|
|
let data = wsJsonToRes<CommentResponse>(msg).data;
|
|
|
|
createCommentLikeRes(data.comment_view, this.state.postRes.comments);
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.CreatePostLike) {
|
|
|
|
let data = wsJsonToRes<PostResponse>(msg).data;
|
|
|
|
createPostLikeRes(data.post_view, this.state.postRes.post_view);
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (
|
2020-12-24 01:58:27 +00:00
|
|
|
op == UserOperation.EditPost ||
|
|
|
|
op == UserOperation.DeletePost ||
|
|
|
|
op == UserOperation.RemovePost ||
|
|
|
|
op == UserOperation.LockPost ||
|
|
|
|
op == UserOperation.StickyPost ||
|
|
|
|
op == UserOperation.SavePost
|
2020-09-06 16:15:25 +00:00
|
|
|
) {
|
2020-12-24 01:58:27 +00:00
|
|
|
let data = wsJsonToRes<PostResponse>(msg).data;
|
|
|
|
this.state.postRes.post_view = data.post_view;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
setupTippy();
|
|
|
|
} else if (
|
2020-12-24 01:58:27 +00:00
|
|
|
op == UserOperation.EditCommunity ||
|
|
|
|
op == UserOperation.DeleteCommunity ||
|
|
|
|
op == UserOperation.RemoveCommunity ||
|
|
|
|
op == UserOperation.FollowCommunity
|
2020-09-06 16:15:25 +00:00
|
|
|
) {
|
2020-12-24 01:58:27 +00:00
|
|
|
let data = wsJsonToRes<CommunityResponse>(msg).data;
|
|
|
|
this.state.postRes.community_view = data.community_view;
|
|
|
|
this.state.postRes.post_view.community = data.community_view.community;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
this.setState(this.state);
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.BanFromCommunity) {
|
|
|
|
let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.postRes.comments
|
2021-03-15 18:09:31 +00:00
|
|
|
.filter(c => c.creator.id == data.person_view.person.id)
|
2020-12-24 01:58:27 +00:00
|
|
|
.forEach(c => (c.creator_banned_from_community = data.banned));
|
2021-03-15 18:09:31 +00:00
|
|
|
if (
|
|
|
|
this.state.postRes.post_view.creator.id == data.person_view.person.id
|
|
|
|
) {
|
2020-12-24 01:58:27 +00:00
|
|
|
this.state.postRes.post_view.creator_banned_from_community =
|
|
|
|
data.banned;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
this.setState(this.state);
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.AddModToCommunity) {
|
|
|
|
let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
|
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);
|
2021-03-15 18:09:31 +00:00
|
|
|
} else if (op == UserOperation.BanPerson) {
|
|
|
|
let data = wsJsonToRes<BanPersonResponse>(msg).data;
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.postRes.comments
|
2021-03-15 18:09:31 +00:00
|
|
|
.filter(c => c.creator.id == data.person_view.person.id)
|
2020-12-24 01:58:27 +00:00
|
|
|
.forEach(c => (c.creator.banned = data.banned));
|
2021-03-15 18:09:31 +00:00
|
|
|
if (
|
|
|
|
this.state.postRes.post_view.creator.id == data.person_view.person.id
|
|
|
|
) {
|
2020-12-24 01:58:27 +00:00
|
|
|
this.state.postRes.post_view.creator.banned = data.banned;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
this.setState(this.state);
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.AddAdmin) {
|
|
|
|
let data = wsJsonToRes<AddAdminResponse>(msg).data;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.siteRes.admins = data.admins;
|
|
|
|
this.setState(this.state);
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.Search) {
|
|
|
|
let data = wsJsonToRes<SearchResponse>(msg).data;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.crossPosts = data.posts.filter(
|
2020-12-24 01:58:27 +00:00
|
|
|
p => p.post.id != Number(this.props.match.params.id)
|
2020-09-06 16:15:25 +00:00
|
|
|
);
|
|
|
|
this.setState(this.state);
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.TransferSite) {
|
|
|
|
let data = wsJsonToRes<GetSiteResponse>(msg).data;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.siteRes = data;
|
|
|
|
this.setState(this.state);
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.TransferCommunity) {
|
|
|
|
let data = wsJsonToRes<GetCommunityResponse>(msg).data;
|
|
|
|
this.state.postRes.community_view = data.community_view;
|
|
|
|
this.state.postRes.post_view.community = data.community_view.community;
|
2020-09-09 00:48:17 +00:00
|
|
|
this.state.postRes.moderators = data.moderators;
|
|
|
|
this.setState(this.state);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|