2022-06-21 21:42:29 +00:00
|
|
|
import { None, Option, Right, Some } from "@sniptt/monads";
|
2021-07-17 20:42:55 +00:00
|
|
|
import autosize from "autosize";
|
2021-08-12 01:06:54 +00:00
|
|
|
import { Component, createRef, linkEvent, RefObject } 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-08-20 02:56:18 +00:00
|
|
|
BlockPersonResponse,
|
2022-07-30 13:28:08 +00:00
|
|
|
CommentNode as CommentNodeI,
|
2021-09-28 10:38:59 +00:00
|
|
|
CommentReportResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
CommentResponse,
|
2022-07-30 13:28:08 +00:00
|
|
|
CommentSortType,
|
2021-07-17 20:42:55 +00:00
|
|
|
CommunityResponse,
|
2022-07-30 13:28:08 +00:00
|
|
|
GetComments,
|
|
|
|
GetCommentsResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
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-09-28 10:38:59 +00:00
|
|
|
PostReportResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
PostResponse,
|
|
|
|
PostView,
|
2022-06-23 19:44:05 +00:00
|
|
|
PurgeItemResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
Search,
|
|
|
|
SearchResponse,
|
|
|
|
SearchType,
|
|
|
|
SortType,
|
2022-08-04 14:28:58 +00:00
|
|
|
toOption,
|
2021-07-17 20:42:55 +00:00
|
|
|
UserOperation,
|
2022-06-21 21:42:29 +00:00
|
|
|
wsJsonToRes,
|
|
|
|
wsUserOp,
|
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";
|
2022-07-30 13:28:08 +00:00
|
|
|
import { CommentViewType, InitialFetchRequest } from "../../interfaces";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { UserService, WebSocketService } from "../../services";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2022-06-21 21:42:29 +00:00
|
|
|
auth,
|
2021-07-17 20:42:55 +00:00
|
|
|
buildCommentsTree,
|
|
|
|
commentsToFlatNodes,
|
2022-07-30 13:28:08 +00:00
|
|
|
commentTreeMaxDepth,
|
2020-09-06 16:15:25 +00:00
|
|
|
createCommentLikeRes,
|
|
|
|
createPostLikeRes,
|
2021-11-16 14:46:12 +00:00
|
|
|
debounce,
|
2021-07-17 20:42:55 +00:00
|
|
|
editCommentRes,
|
2022-06-21 21:42:29 +00:00
|
|
|
enableDownvotes,
|
|
|
|
enableNsfw,
|
2020-09-09 00:48:17 +00:00
|
|
|
getCommentIdFromProps,
|
2022-07-30 13:28:08 +00:00
|
|
|
getCommentParentId,
|
|
|
|
getDepthFromComment,
|
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-02-02 18:36:59 +00:00
|
|
|
restoreScrollPosition,
|
2021-07-17 20:42:55 +00:00
|
|
|
saveCommentRes,
|
|
|
|
saveScrollPosition,
|
|
|
|
setIsoData,
|
|
|
|
setupTippy,
|
|
|
|
toast,
|
2022-06-21 21:42:29 +00:00
|
|
|
trendingFetchLimit,
|
2021-08-20 02:56:18 +00:00
|
|
|
updatePersonBlock,
|
2021-07-17 20:42:55 +00:00
|
|
|
wsClient,
|
|
|
|
wsSubscribe,
|
|
|
|
} 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
|
|
|
|
2021-11-16 14:46:12 +00:00
|
|
|
const commentsShownInterval = 15;
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
interface PostState {
|
2022-07-30 13:28:08 +00:00
|
|
|
postId: Option<number>;
|
|
|
|
commentId: Option<number>;
|
2022-06-21 21:42:29 +00:00
|
|
|
postRes: Option<GetPostResponse>;
|
2022-07-30 13:28:08 +00:00
|
|
|
commentsRes: Option<GetCommentsResponse>;
|
2021-02-08 04:23:31 +00:00
|
|
|
commentTree: CommentNodeI[];
|
2020-09-06 16:15:25 +00:00
|
|
|
commentSort: CommentSortType;
|
|
|
|
commentViewType: CommentViewType;
|
|
|
|
scrolled?: boolean;
|
|
|
|
loading: boolean;
|
2022-06-21 21:42:29 +00:00
|
|
|
crossPosts: Option<PostView[]>;
|
2020-09-06 16:15:25 +00:00
|
|
|
siteRes: GetSiteResponse;
|
2021-08-12 01:00:26 +00:00
|
|
|
commentSectionRef?: RefObject<HTMLDivElement>;
|
2021-07-18 03:56:33 +00:00
|
|
|
showSidebarMobile: boolean;
|
2021-11-16 14:46:12 +00:00
|
|
|
maxCommentsShown: number;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Post extends Component<any, PostState> {
|
|
|
|
private subscription: Subscription;
|
2022-07-30 13:28:08 +00:00
|
|
|
private isoData = setIsoData(
|
|
|
|
this.context,
|
|
|
|
GetPostResponse,
|
|
|
|
GetCommentsResponse
|
|
|
|
);
|
2021-11-16 15:37:40 +00:00
|
|
|
private commentScrollDebounced: () => void;
|
2020-09-06 16:15:25 +00:00
|
|
|
private emptyState: PostState = {
|
2022-06-21 21:42:29 +00:00
|
|
|
postRes: None,
|
2022-07-30 13:28:08 +00:00
|
|
|
commentsRes: None,
|
2020-09-09 00:48:17 +00:00
|
|
|
postId: getIdFromProps(this.props),
|
|
|
|
commentId: getCommentIdFromProps(this.props),
|
2022-07-30 13:28:08 +00:00
|
|
|
commentTree: [],
|
|
|
|
commentSort: CommentSortType[CommentSortType.Hot],
|
2020-09-06 16:15:25 +00:00
|
|
|
commentViewType: CommentViewType.Tree,
|
|
|
|
scrolled: false,
|
|
|
|
loading: true,
|
2022-06-21 21:42:29 +00:00
|
|
|
crossPosts: None,
|
2020-12-24 01:58:27 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
2021-08-12 01:00:26 +00:00
|
|
|
commentSectionRef: null,
|
2021-07-18 03:56:33 +00:00
|
|
|
showSidebarMobile: false,
|
2021-11-16 14:46:12 +00:00
|
|
|
maxCommentsShown: commentsShownInterval,
|
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
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = { ...this.state, commentSectionRef: createRef() };
|
|
|
|
|
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) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
postRes: Some(this.isoData.routeData[0] as GetPostResponse),
|
|
|
|
commentsRes: Some(this.isoData.routeData[1] as GetCommentsResponse),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.state.commentsRes.isSome()) {
|
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
commentTree: buildCommentsTree(
|
|
|
|
this.state.commentsRes.unwrap().comments,
|
2022-07-30 13:28:08 +00:00
|
|
|
this.state.commentId.isSome()
|
2022-09-22 15:03:35 +00:00
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.state = { ...this.state, loading: false };
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2021-01-24 01:41:23 +00:00
|
|
|
if (isBrowser()) {
|
2022-06-21 21:42:29 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.communityJoin({
|
|
|
|
community_id:
|
|
|
|
this.state.postRes.unwrap().community_view.community.id,
|
|
|
|
})
|
|
|
|
);
|
2022-07-30 13:28:08 +00:00
|
|
|
|
|
|
|
this.state.postId.match({
|
|
|
|
some: post_id =>
|
|
|
|
WebSocketService.Instance.send(wsClient.postJoin({ post_id })),
|
|
|
|
none: void 0,
|
|
|
|
});
|
2022-06-21 21:42:29 +00:00
|
|
|
|
2021-01-24 01:41:23 +00:00
|
|
|
this.fetchCrossPosts();
|
2021-08-12 01:06:54 +00:00
|
|
|
|
|
|
|
if (this.checkScrollIntoCommentsParam) {
|
2021-08-12 01:00:26 +00:00
|
|
|
this.scrollIntoCommentSection();
|
2021-01-24 01:41:23 +00:00
|
|
|
}
|
2020-09-09 00:48:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.fetchPost();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchPost() {
|
2022-07-30 13:28:08 +00:00
|
|
|
this.setState({ commentsRes: None });
|
|
|
|
let postForm = new GetPost({
|
2020-09-09 00:48:17 +00:00
|
|
|
id: this.state.postId,
|
2022-07-30 13:28:08 +00:00
|
|
|
comment_id: this.state.commentId,
|
|
|
|
auth: auth(false).ok(),
|
|
|
|
});
|
|
|
|
WebSocketService.Instance.send(wsClient.getPost(postForm));
|
|
|
|
|
|
|
|
let commentsForm = new GetComments({
|
|
|
|
post_id: this.state.postId,
|
|
|
|
parent_id: this.state.commentId,
|
|
|
|
max_depth: Some(commentTreeMaxDepth),
|
|
|
|
page: None,
|
|
|
|
limit: None,
|
|
|
|
sort: Some(this.state.commentSort),
|
|
|
|
type_: Some(ListingType.All),
|
|
|
|
community_name: None,
|
|
|
|
community_id: None,
|
|
|
|
saved_only: Some(false),
|
2022-06-21 21:42:29 +00:00
|
|
|
auth: auth(false).ok(),
|
|
|
|
});
|
2022-07-30 13:28:08 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.getComments(commentsForm));
|
2020-09-09 00:48:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 01:41:23 +00:00
|
|
|
fetchCrossPosts() {
|
2022-06-21 21:42:29 +00:00
|
|
|
this.state.postRes
|
|
|
|
.andThen(r => r.post_view.post.url)
|
|
|
|
.match({
|
|
|
|
some: url => {
|
|
|
|
let form = new Search({
|
|
|
|
q: url,
|
|
|
|
type_: Some(SearchType.Url),
|
|
|
|
sort: Some(SortType.TopAll),
|
|
|
|
listing_type: Some(ListingType.All),
|
|
|
|
page: Some(1),
|
|
|
|
limit: Some(trendingFetchLimit),
|
|
|
|
community_id: None,
|
|
|
|
community_name: None,
|
|
|
|
creator_id: None,
|
|
|
|
auth: auth(false).ok(),
|
|
|
|
});
|
|
|
|
WebSocketService.Instance.send(wsClient.search(form));
|
|
|
|
},
|
|
|
|
none: void 0,
|
|
|
|
});
|
2021-01-24 01:41:23 +00:00
|
|
|
}
|
|
|
|
|
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("/");
|
2022-07-30 13:28:08 +00:00
|
|
|
let promises: Promise<any>[] = [];
|
2020-09-09 00:48:17 +00:00
|
|
|
|
2022-07-30 13:28:08 +00:00
|
|
|
let pathType = pathSplit[1];
|
2020-09-09 00:48:17 +00:00
|
|
|
let id = Number(pathSplit[2]);
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
let postForm = new GetPost({
|
2022-07-30 13:28:08 +00:00
|
|
|
id: None,
|
|
|
|
comment_id: None,
|
|
|
|
auth: req.auth,
|
|
|
|
});
|
|
|
|
|
|
|
|
let commentsForm = new GetComments({
|
|
|
|
post_id: None,
|
|
|
|
parent_id: None,
|
|
|
|
max_depth: Some(commentTreeMaxDepth),
|
|
|
|
page: None,
|
|
|
|
limit: None,
|
|
|
|
sort: Some(CommentSortType.Hot),
|
|
|
|
type_: Some(ListingType.All),
|
|
|
|
community_name: None,
|
|
|
|
community_id: None,
|
|
|
|
saved_only: Some(false),
|
2022-06-21 21:42:29 +00:00
|
|
|
auth: req.auth,
|
|
|
|
});
|
2020-09-09 00:48:17 +00:00
|
|
|
|
2022-07-30 13:28:08 +00:00
|
|
|
// Set the correct id based on the path type
|
|
|
|
if (pathType == "post") {
|
|
|
|
postForm.id = Some(id);
|
|
|
|
commentsForm.post_id = Some(id);
|
|
|
|
} else {
|
|
|
|
postForm.comment_id = Some(id);
|
|
|
|
commentsForm.parent_id = Some(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
promises.push(req.client.getPost(postForm));
|
|
|
|
promises.push(req.client.getComments(commentsForm));
|
|
|
|
|
|
|
|
return promises;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
2021-11-16 15:37:40 +00:00
|
|
|
document.removeEventListener("scroll", this.commentScrollDebounced);
|
|
|
|
|
2021-02-02 18:36:59 +00:00
|
|
|
saveScrollPosition(this.context);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2021-02-22 02:39:04 +00:00
|
|
|
autosize(document.querySelectorAll("textarea"));
|
2021-11-16 14:46:12 +00:00
|
|
|
|
2021-11-16 15:37:40 +00:00
|
|
|
this.commentScrollDebounced = debounce(this.trackCommentsBoxScrolling, 100);
|
|
|
|
document.addEventListener("scroll", this.commentScrollDebounced);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 19:10:11 +00:00
|
|
|
componentDidUpdate(_lastProps: any) {
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 01:06:54 +00:00
|
|
|
get checkScrollIntoCommentsParam() {
|
|
|
|
return Boolean(
|
|
|
|
new URLSearchParams(this.props.location.search).get("scrollToComments")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-12 01:00:26 +00:00
|
|
|
scrollIntoCommentSection() {
|
|
|
|
this.state.commentSectionRef.current?.scrollIntoView();
|
|
|
|
}
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
isBottom(el: Element): boolean {
|
2021-11-22 20:04:20 +00:00
|
|
|
return el?.getBoundingClientRect().bottom <= window.innerHeight;
|
2021-11-16 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows new comments when scrolling to the bottom of the comments div
|
|
|
|
*/
|
|
|
|
trackCommentsBoxScrolling = () => {
|
|
|
|
const wrappedElement = document.getElementsByClassName("comments")[0];
|
2021-11-22 20:04:20 +00:00
|
|
|
if (wrappedElement && this.isBottom(wrappedElement)) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({
|
|
|
|
maxCommentsShown: this.state.maxCommentsShown + commentsShownInterval,
|
|
|
|
});
|
2021-11-16 14:46:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
get documentTitle(): string {
|
2022-06-21 21:42:29 +00:00
|
|
|
return this.state.postRes.match({
|
|
|
|
some: res =>
|
2022-11-09 19:53:07 +00:00
|
|
|
`${res.post_view.post.name} - ${this.state.siteRes.site_view.site.name}`,
|
2022-06-21 21:42:29 +00:00
|
|
|
none: "",
|
|
|
|
});
|
2020-09-11 18:09:21 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
get imageTag(): Option<string> {
|
|
|
|
return this.state.postRes.match({
|
|
|
|
some: res =>
|
|
|
|
res.post_view.post.thumbnail_url.or(
|
|
|
|
res.post_view.post.url.match({
|
|
|
|
some: url => (isImage(url) ? Some(url) : None),
|
|
|
|
none: None,
|
|
|
|
})
|
|
|
|
),
|
|
|
|
none: None,
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
get descriptionTag(): Option<string> {
|
|
|
|
return this.state.postRes.andThen(r => r.post_view.post.body);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2022-10-03 18:16:36 +00:00
|
|
|
<div className="container-lg">
|
2020-09-06 16:15:25 +00:00
|
|
|
{this.state.loading ? (
|
|
|
|
<h5>
|
2021-07-17 20:21:31 +00:00
|
|
|
<Spinner large />
|
2020-09-06 16:15:25 +00:00
|
|
|
</h5>
|
|
|
|
) : (
|
2022-06-21 21:42:29 +00:00
|
|
|
this.state.postRes.match({
|
|
|
|
some: res => (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="row">
|
|
|
|
<div className="col-12 col-md-8 mb-3">
|
2022-06-21 21:42:29 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
image={this.imageTag}
|
|
|
|
description={this.descriptionTag}
|
|
|
|
/>
|
|
|
|
<PostListing
|
|
|
|
post_view={res.post_view}
|
|
|
|
duplicates={this.state.crossPosts}
|
|
|
|
showBody
|
|
|
|
showCommunity
|
|
|
|
moderators={Some(res.moderators)}
|
|
|
|
admins={Some(this.state.siteRes.admins)}
|
|
|
|
enableDownvotes={enableDownvotes(this.state.siteRes)}
|
|
|
|
enableNsfw={enableNsfw(this.state.siteRes)}
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages={this.state.siteRes.all_languages}
|
2021-07-18 03:56:33 +00:00
|
|
|
/>
|
2022-06-21 21:42:29 +00:00
|
|
|
<div ref={this.state.commentSectionRef} className="mb-2" />
|
|
|
|
<CommentForm
|
2022-07-30 13:28:08 +00:00
|
|
|
node={Right(res.post_view.post.id)}
|
2022-06-21 21:42:29 +00:00
|
|
|
disabled={res.post_view.post.locked}
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages={this.state.siteRes.all_languages}
|
2022-06-21 21:42:29 +00:00
|
|
|
/>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="d-block d-md-none">
|
2022-06-21 21:42:29 +00:00
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-secondary d-inline-block mb-2 mr-3"
|
2022-06-21 21:42:29 +00:00
|
|
|
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>
|
2022-07-30 13:28:08 +00:00
|
|
|
{this.sortRadios()}
|
2022-06-21 21:42:29 +00:00
|
|
|
{this.state.commentViewType == CommentViewType.Tree &&
|
|
|
|
this.commentsTree()}
|
2022-07-30 13:28:08 +00:00
|
|
|
{this.state.commentViewType == CommentViewType.Flat &&
|
2022-06-21 21:42:29 +00:00
|
|
|
this.commentsFlat()}
|
|
|
|
</div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="d-none d-md-block col-md-4">
|
|
|
|
{this.sidebar()}
|
|
|
|
</div>
|
2021-07-18 03:56:33 +00:00
|
|
|
</div>
|
2022-06-21 21:42:29 +00:00
|
|
|
),
|
|
|
|
none: <></>,
|
|
|
|
})
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sortRadios() {
|
|
|
|
return (
|
|
|
|
<>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
|
2020-09-06 16:15:25 +00:00
|
|
|
<label
|
|
|
|
className={`btn btn-outline-secondary pointer ${
|
2022-07-30 13:28:08 +00:00
|
|
|
CommentSortType[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 ${
|
2022-07-30 13:28:08 +00:00
|
|
|
CommentSortType[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 ${
|
2022-07-30 13:28:08 +00:00
|
|
|
CommentSortType[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 ${
|
2022-07-30 13:28:08 +00:00
|
|
|
CommentSortType[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>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="btn-group btn-group-toggle flex-wrap mb-2">
|
2020-09-06 16:15:25 +00:00
|
|
|
<label
|
|
|
|
className={`btn btn-outline-secondary pointer ${
|
2022-07-30 13:28:08 +00:00
|
|
|
this.state.commentViewType === CommentViewType.Flat && "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"
|
2022-07-30 13:28:08 +00:00
|
|
|
value={CommentViewType.Flat}
|
|
|
|
checked={this.state.commentViewType === CommentViewType.Flat}
|
2020-09-06 16:15:25 +00:00
|
|
|
onChange={linkEvent(this, this.handleCommentViewTypeChange)}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
commentsFlat() {
|
2021-02-08 04:23:31 +00:00
|
|
|
// These are already sorted by new
|
2022-07-30 13:28:08 +00:00
|
|
|
return this.state.commentsRes.match({
|
|
|
|
some: commentsRes =>
|
|
|
|
this.state.postRes.match({
|
|
|
|
some: postRes => (
|
|
|
|
<div>
|
|
|
|
<CommentNodes
|
|
|
|
nodes={commentsToFlatNodes(commentsRes.comments)}
|
|
|
|
viewType={this.state.commentViewType}
|
|
|
|
maxCommentsShown={Some(this.state.maxCommentsShown)}
|
|
|
|
noIndent
|
|
|
|
locked={postRes.post_view.post.locked}
|
|
|
|
moderators={Some(postRes.moderators)}
|
|
|
|
admins={Some(this.state.siteRes.admins)}
|
|
|
|
enableDownvotes={enableDownvotes(this.state.siteRes)}
|
|
|
|
showContext
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages={this.state.siteRes.all_languages}
|
2022-07-30 13:28:08 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
none: <></>,
|
|
|
|
}),
|
2022-06-21 21:42:29 +00:00
|
|
|
none: <></>,
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sidebar() {
|
2022-06-21 21:42:29 +00:00
|
|
|
return this.state.postRes.match({
|
|
|
|
some: res => (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="mb-3">
|
2022-06-21 21:42:29 +00:00
|
|
|
<Sidebar
|
|
|
|
community_view={res.community_view}
|
|
|
|
moderators={res.moderators}
|
|
|
|
admins={this.state.siteRes.admins}
|
|
|
|
online={res.online}
|
|
|
|
enableNsfw={enableNsfw(this.state.siteRes)}
|
|
|
|
showIcon
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
none: <></>,
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleCommentSortChange(i: Post, event: any) {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({
|
|
|
|
commentSort: CommentSortType[event.target.value],
|
|
|
|
commentViewType: CommentViewType.Tree,
|
|
|
|
});
|
2022-07-30 13:28:08 +00:00
|
|
|
i.fetchPost();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleCommentViewTypeChange(i: Post, event: any) {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({
|
|
|
|
commentViewType: Number(event.target.value),
|
|
|
|
commentSort: CommentSortType.New,
|
|
|
|
commentTree: buildCommentsTree(
|
|
|
|
i.state.commentsRes.map(r => r.comments).unwrapOr([]),
|
|
|
|
i.state.commentId.isSome()
|
|
|
|
),
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 03:56:33 +00:00
|
|
|
handleShowSidebarMobile(i: Post) {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ showSidebarMobile: !i.state.showSidebarMobile });
|
2021-07-18 03:56:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 13:28:08 +00:00
|
|
|
handleViewPost(i: Post) {
|
|
|
|
i.state.postRes.match({
|
|
|
|
some: res =>
|
|
|
|
i.context.router.history.push(`/post/${res.post_view.post.id}`),
|
|
|
|
none: void 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleViewContext(i: Post) {
|
|
|
|
i.state.commentsRes.match({
|
|
|
|
some: res =>
|
|
|
|
i.context.router.history.push(
|
|
|
|
`/comment/${getCommentParentId(res.comments[0].comment).unwrap()}`
|
|
|
|
),
|
|
|
|
none: void 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
commentsTree() {
|
2022-08-04 14:28:58 +00:00
|
|
|
let showContextButton = toOption(this.state.commentTree[0]).match({
|
|
|
|
some: comment => getDepthFromComment(comment.comment_view.comment) > 0,
|
|
|
|
none: false,
|
|
|
|
});
|
2022-07-30 13:28:08 +00:00
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
return this.state.postRes.match({
|
|
|
|
some: res => (
|
|
|
|
<div>
|
2022-07-30 13:28:08 +00:00
|
|
|
{this.state.commentId.isSome() && (
|
|
|
|
<>
|
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="pl-0 d-block btn btn-link text-muted"
|
2022-07-30 13:28:08 +00:00
|
|
|
onClick={linkEvent(this, this.handleViewPost)}
|
|
|
|
>
|
|
|
|
{i18n.t("view_all_comments")} ➔
|
|
|
|
</button>
|
|
|
|
{showContextButton && (
|
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="pl-0 d-block btn btn-link text-muted"
|
2022-07-30 13:28:08 +00:00
|
|
|
onClick={linkEvent(this, this.handleViewContext)}
|
|
|
|
>
|
|
|
|
{i18n.t("show_context")} ➔
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
2022-06-21 21:42:29 +00:00
|
|
|
<CommentNodes
|
|
|
|
nodes={this.state.commentTree}
|
2022-07-30 13:28:08 +00:00
|
|
|
viewType={this.state.commentViewType}
|
2022-06-21 21:42:29 +00:00
|
|
|
maxCommentsShown={Some(this.state.maxCommentsShown)}
|
|
|
|
locked={res.post_view.post.locked}
|
|
|
|
moderators={Some(res.moderators)}
|
|
|
|
admins={Some(this.state.siteRes.admins)}
|
|
|
|
enableDownvotes={enableDownvotes(this.state.siteRes)}
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages={this.state.siteRes.all_languages}
|
2022-06-21 21:42:29 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
none: <></>,
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2022-07-30 13:28:08 +00:00
|
|
|
this.state.postRes.match({
|
|
|
|
some: res => {
|
|
|
|
let postId = res.post_view.post.id;
|
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.postJoin({ post_id: postId })
|
|
|
|
);
|
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.getPost({
|
|
|
|
id: Some(postId),
|
|
|
|
comment_id: None,
|
|
|
|
auth: auth(false).ok(),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
|
|
|
none: void 0,
|
|
|
|
});
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.GetPost) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<GetPostResponse>(msg, GetPostResponse);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ postRes: Some(data) });
|
2022-06-21 21:42:29 +00:00
|
|
|
|
|
|
|
// join the rooms
|
|
|
|
WebSocketService.Instance.send(
|
2022-07-30 13:28:08 +00:00
|
|
|
wsClient.postJoin({ post_id: data.post_view.post.id })
|
2022-06-21 21:42:29 +00:00
|
|
|
);
|
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.communityJoin({
|
|
|
|
community_id: data.community_view.community.id,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
// Get cross-posts
|
2022-07-30 13:28:08 +00:00
|
|
|
// TODO move this into initial fetch and refetch
|
2021-01-24 01:41:23 +00:00
|
|
|
this.fetchCrossPosts();
|
2020-09-06 16:15:25 +00:00
|
|
|
setupTippy();
|
2022-07-30 13:28:08 +00:00
|
|
|
if (this.state.commentId.isNone()) restoreScrollPosition(this.context);
|
2021-08-12 01:06:54 +00:00
|
|
|
|
|
|
|
if (this.checkScrollIntoCommentsParam) {
|
2021-08-12 01:00:26 +00:00
|
|
|
this.scrollIntoCommentSection();
|
|
|
|
}
|
2022-07-30 13:28:08 +00:00
|
|
|
} else if (op == UserOperation.GetComments) {
|
|
|
|
let data = wsJsonToRes<GetCommentsResponse>(msg, GetCommentsResponse);
|
|
|
|
// You might need to append here, since this could be building more comments from a tree fetch
|
|
|
|
this.state.commentsRes.match({
|
|
|
|
some: res => {
|
|
|
|
// Remove the first comment, since it is the parent
|
|
|
|
let newComments = data.comments;
|
|
|
|
newComments.shift();
|
|
|
|
res.comments.push(...newComments);
|
|
|
|
},
|
2022-09-22 15:03:35 +00:00
|
|
|
none: () => this.setState({ commentsRes: Some(data) }),
|
2022-07-30 13:28:08 +00:00
|
|
|
});
|
|
|
|
// this.state.commentsRes = Some(data);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({
|
|
|
|
commentTree: buildCommentsTree(
|
|
|
|
this.state.commentsRes.map(r => r.comments).unwrapOr([]),
|
|
|
|
this.state.commentId.isSome()
|
|
|
|
),
|
|
|
|
loading: false,
|
|
|
|
});
|
2022-07-30 13:28:08 +00:00
|
|
|
this.setState(this.state);
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.CreateComment) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2021-12-02 16:46:41 +00:00
|
|
|
// Don't get comments from the post room, if the creator is blocked
|
2022-06-21 21:42:29 +00:00
|
|
|
let creatorBlocked = UserService.Instance.myUserInfo
|
|
|
|
.map(m => m.person_blocks)
|
|
|
|
.unwrapOr([])
|
2021-12-02 16:46:41 +00:00
|
|
|
.map(pb => pb.target.id)
|
|
|
|
.includes(data.comment_view.creator.id);
|
|
|
|
|
2021-01-13 17:06:56 +00:00
|
|
|
// Necessary since it might be a user reply, which has the recipients, to avoid double
|
2021-12-02 16:46:41 +00:00
|
|
|
if (data.recipient_ids.length == 0 && !creatorBlocked) {
|
2022-06-21 21:42:29 +00:00
|
|
|
this.state.postRes.match({
|
2022-07-30 13:28:08 +00:00
|
|
|
some: postRes =>
|
|
|
|
this.state.commentsRes.match({
|
|
|
|
some: commentsRes => {
|
|
|
|
commentsRes.comments.unshift(data.comment_view);
|
|
|
|
insertCommentIntoTree(
|
|
|
|
this.state.commentTree,
|
|
|
|
data.comment_view,
|
|
|
|
this.state.commentId.isSome()
|
|
|
|
);
|
|
|
|
postRes.post_view.counts.comments++;
|
|
|
|
},
|
|
|
|
none: void 0,
|
|
|
|
}),
|
2022-06-21 21:42:29 +00:00
|
|
|
none: void 0,
|
|
|
|
});
|
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
|
|
|
) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
|
|
|
|
editCommentRes(
|
|
|
|
data.comment_view,
|
2022-07-30 13:28:08 +00:00
|
|
|
this.state.commentsRes.map(r => r.comments).unwrapOr([])
|
2022-06-21 21:42:29 +00:00
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
2022-08-17 23:26:50 +00:00
|
|
|
setupTippy();
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.SaveComment) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
|
|
|
|
saveCommentRes(
|
|
|
|
data.comment_view,
|
2022-07-30 13:28:08 +00:00
|
|
|
this.state.commentsRes.map(r => r.comments).unwrapOr([])
|
2022-06-21 21:42:29 +00:00
|
|
|
);
|
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) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
|
|
|
|
createCommentLikeRes(
|
|
|
|
data.comment_view,
|
2022-07-30 13:28:08 +00:00
|
|
|
this.state.commentsRes.map(r => r.comments).unwrapOr([])
|
2022-06-21 21:42:29 +00:00
|
|
|
);
|
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) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<PostResponse>(msg, PostResponse);
|
|
|
|
this.state.postRes.match({
|
|
|
|
some: res => createPostLikeRes(data.post_view, res.post_view),
|
|
|
|
none: void 0,
|
|
|
|
});
|
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
|
|
|
) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<PostResponse>(msg, PostResponse);
|
|
|
|
this.state.postRes.match({
|
|
|
|
some: res => (res.post_view = data.post_view),
|
|
|
|
none: void 0,
|
|
|
|
});
|
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
|
|
|
) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
|
|
|
|
this.state.postRes.match({
|
|
|
|
some: res => {
|
|
|
|
res.community_view = data.community_view;
|
|
|
|
res.post_view.community = data.community_view.community;
|
|
|
|
this.setState(this.state);
|
|
|
|
},
|
|
|
|
none: void 0,
|
|
|
|
});
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.BanFromCommunity) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<BanFromCommunityResponse>(
|
|
|
|
msg,
|
|
|
|
BanFromCommunityResponse
|
|
|
|
);
|
|
|
|
this.state.postRes.match({
|
2022-07-30 13:28:08 +00:00
|
|
|
some: postRes =>
|
|
|
|
this.state.commentsRes.match({
|
|
|
|
some: commentsRes => {
|
|
|
|
commentsRes.comments
|
|
|
|
.filter(c => c.creator.id == data.person_view.person.id)
|
|
|
|
.forEach(c => (c.creator_banned_from_community = data.banned));
|
|
|
|
if (postRes.post_view.creator.id == data.person_view.person.id) {
|
|
|
|
postRes.post_view.creator_banned_from_community = data.banned;
|
|
|
|
}
|
|
|
|
this.setState(this.state);
|
|
|
|
},
|
|
|
|
none: void 0,
|
|
|
|
}),
|
2022-06-21 21:42:29 +00:00
|
|
|
none: void 0,
|
|
|
|
});
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.AddModToCommunity) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<AddModToCommunityResponse>(
|
|
|
|
msg,
|
|
|
|
AddModToCommunityResponse
|
|
|
|
);
|
|
|
|
this.state.postRes.match({
|
|
|
|
some: res => {
|
|
|
|
res.moderators = data.moderators;
|
|
|
|
this.setState(this.state);
|
|
|
|
},
|
|
|
|
none: void 0,
|
|
|
|
});
|
2021-03-15 18:09:31 +00:00
|
|
|
} else if (op == UserOperation.BanPerson) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<BanPersonResponse>(msg, BanPersonResponse);
|
|
|
|
this.state.postRes.match({
|
2022-07-30 13:28:08 +00:00
|
|
|
some: postRes =>
|
|
|
|
this.state.commentsRes.match({
|
|
|
|
some: commentsRes => {
|
|
|
|
commentsRes.comments
|
|
|
|
.filter(c => c.creator.id == data.person_view.person.id)
|
|
|
|
.forEach(c => (c.creator.banned = data.banned));
|
|
|
|
if (postRes.post_view.creator.id == data.person_view.person.id) {
|
|
|
|
postRes.post_view.creator.banned = data.banned;
|
|
|
|
}
|
|
|
|
this.setState(this.state);
|
|
|
|
},
|
|
|
|
none: void 0,
|
|
|
|
}),
|
2022-06-21 21:42:29 +00:00
|
|
|
none: void 0,
|
|
|
|
});
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.AddAdmin) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<AddAdminResponse>(msg, AddAdminResponse);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState(s => ((s.siteRes.admins = data.admins), s));
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.Search) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<SearchResponse>(msg, SearchResponse);
|
|
|
|
let xPosts = 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
|
|
|
);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ crossPosts: xPosts.length > 0 ? Some(xPosts) : None });
|
2022-02-14 17:27:43 +00:00
|
|
|
} else if (op == UserOperation.LeaveAdmin) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<GetSiteResponse>(msg, GetSiteResponse);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ siteRes: data });
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.TransferCommunity) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<GetCommunityResponse>(msg, GetCommunityResponse);
|
|
|
|
this.state.postRes.match({
|
|
|
|
some: res => {
|
|
|
|
res.community_view = data.community_view;
|
|
|
|
res.post_view.community = data.community_view.community;
|
|
|
|
res.moderators = data.moderators;
|
|
|
|
this.setState(this.state);
|
|
|
|
},
|
|
|
|
none: void 0,
|
|
|
|
});
|
2021-08-20 02:56:18 +00:00
|
|
|
} else if (op == UserOperation.BlockPerson) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<BlockPersonResponse>(msg, BlockPersonResponse);
|
2021-08-20 02:56:18 +00:00
|
|
|
updatePersonBlock(data);
|
2021-09-28 10:38:59 +00:00
|
|
|
} else if (op == UserOperation.CreatePostReport) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<PostReportResponse>(msg, PostReportResponse);
|
2021-09-28 10:38:59 +00:00
|
|
|
if (data) {
|
|
|
|
toast(i18n.t("report_created"));
|
|
|
|
}
|
|
|
|
} else if (op == UserOperation.CreateCommentReport) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<CommentReportResponse>(msg, CommentReportResponse);
|
2021-09-28 10:38:59 +00:00
|
|
|
if (data) {
|
|
|
|
toast(i18n.t("report_created"));
|
|
|
|
}
|
2022-06-23 19:44:05 +00:00
|
|
|
} else if (
|
|
|
|
op == UserOperation.PurgePerson ||
|
|
|
|
op == UserOperation.PurgePost ||
|
|
|
|
op == UserOperation.PurgeComment ||
|
|
|
|
op == UserOperation.PurgeCommunity
|
|
|
|
) {
|
|
|
|
let data = wsJsonToRes<PurgeItemResponse>(msg, PurgeItemResponse);
|
|
|
|
if (data.success) {
|
|
|
|
toast(i18n.t("purge_success"));
|
|
|
|
this.context.router.history.push(`/`);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|