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 {
|
2023-06-14 12:20:40 +00:00
|
|
|
AddAdmin,
|
|
|
|
AddModToCommunity,
|
2021-07-17 20:42:55 +00:00
|
|
|
AddModToCommunityResponse,
|
2023-06-14 12:20:40 +00:00
|
|
|
BanFromCommunity,
|
2020-09-06 16:15:25 +00:00
|
|
|
BanFromCommunityResponse,
|
2023-06-14 12:20:40 +00:00
|
|
|
BanPerson,
|
2021-03-15 18:09:31 +00:00
|
|
|
BanPersonResponse,
|
2023-06-14 12:20:40 +00:00
|
|
|
BlockCommunity,
|
|
|
|
BlockPerson,
|
|
|
|
CommentId,
|
|
|
|
CommentReplyResponse,
|
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,
|
2023-06-14 12:20:40 +00:00
|
|
|
CreateComment,
|
|
|
|
CreateCommentLike,
|
|
|
|
CreateCommentReport,
|
|
|
|
CreatePostLike,
|
|
|
|
CreatePostReport,
|
|
|
|
DeleteComment,
|
|
|
|
DeleteCommunity,
|
|
|
|
DeletePost,
|
|
|
|
DistinguishComment,
|
|
|
|
EditComment,
|
|
|
|
EditCommunity,
|
|
|
|
EditPost,
|
|
|
|
FeaturePost,
|
|
|
|
FollowCommunity,
|
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,
|
2023-06-14 12:20:40 +00:00
|
|
|
LockPost,
|
|
|
|
MarkCommentReplyAsRead,
|
|
|
|
MarkPersonMentionAsRead,
|
2021-07-17 20:42:55 +00:00
|
|
|
PostResponse,
|
2023-06-14 12:20:40 +00:00
|
|
|
PurgeComment,
|
|
|
|
PurgeCommunity,
|
2022-06-23 19:44:05 +00:00
|
|
|
PurgeItemResponse,
|
2023-06-14 12:20:40 +00:00
|
|
|
PurgePerson,
|
|
|
|
PurgePost,
|
|
|
|
RemoveComment,
|
|
|
|
RemoveCommunity,
|
|
|
|
RemovePost,
|
|
|
|
SaveComment,
|
|
|
|
SavePost,
|
|
|
|
TransferCommunity,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { i18n } from "../../i18next";
|
2023-05-11 18:32:32 +00:00
|
|
|
import {
|
|
|
|
CommentNodeI,
|
|
|
|
CommentViewType,
|
|
|
|
InitialFetchRequest,
|
|
|
|
} from "../../interfaces";
|
2023-06-14 12:20:40 +00:00
|
|
|
import { UserService } from "../../services";
|
|
|
|
import { FirstLoadService } from "../../services/FirstLoadService";
|
|
|
|
import { HttpService, RequestState } from "../../services/HttpService";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2021-07-17 20:42:55 +00:00
|
|
|
buildCommentsTree,
|
|
|
|
commentsToFlatNodes,
|
2022-07-30 13:28:08 +00:00
|
|
|
commentTreeMaxDepth,
|
2023-06-14 12:20:40 +00:00
|
|
|
editComment,
|
|
|
|
editWith,
|
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,
|
2020-09-11 18:09:21 +00:00
|
|
|
isImage,
|
2023-01-04 16:56:24 +00:00
|
|
|
myAuth,
|
2021-02-02 18:36:59 +00:00
|
|
|
restoreScrollPosition,
|
2023-06-16 02:08:14 +00:00
|
|
|
RouteDataResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
saveScrollPosition,
|
|
|
|
setIsoData,
|
|
|
|
setupTippy,
|
|
|
|
toast,
|
2023-06-14 12:20:40 +00:00
|
|
|
updateCommunityBlock,
|
2021-08-20 02:56:18 +00:00
|
|
|
updatePersonBlock,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
2023-06-20 01:48:38 +00:00
|
|
|
import isBrowser from "../../utils/browser/is-browser";
|
|
|
|
import debounce from "../../utils/helpers/debounce";
|
2021-07-17 20:42:55 +00:00
|
|
|
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;
|
|
|
|
|
2023-06-16 02:08:14 +00:00
|
|
|
type PostData = RouteDataResponse<{
|
|
|
|
postRes: GetPostResponse;
|
|
|
|
commentsRes: GetCommentsResponse;
|
|
|
|
}>;
|
2023-05-30 00:40:00 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
interface PostState {
|
2023-01-04 16:56:24 +00:00
|
|
|
postId?: number;
|
|
|
|
commentId?: number;
|
2023-06-14 12:20:40 +00:00
|
|
|
postRes: RequestState<GetPostResponse>;
|
|
|
|
commentsRes: RequestState<GetCommentsResponse>;
|
2020-09-06 16:15:25 +00:00
|
|
|
commentSort: CommentSortType;
|
|
|
|
commentViewType: CommentViewType;
|
|
|
|
scrolled?: boolean;
|
|
|
|
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;
|
2023-06-14 12:20:40 +00:00
|
|
|
finished: Map<CommentId, boolean | undefined>;
|
|
|
|
isIsomorphic: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Post extends Component<any, PostState> {
|
2023-05-30 00:40:00 +00:00
|
|
|
private isoData = setIsoData<PostData>(this.context);
|
2021-11-16 15:37:40 +00:00
|
|
|
private commentScrollDebounced: () => void;
|
2023-01-04 16:56:24 +00:00
|
|
|
state: PostState = {
|
2023-06-14 12:20:40 +00:00
|
|
|
postRes: { state: "empty" },
|
|
|
|
commentsRes: { state: "empty" },
|
2020-09-09 00:48:17 +00:00
|
|
|
postId: getIdFromProps(this.props),
|
|
|
|
commentId: getCommentIdFromProps(this.props),
|
2023-05-11 18:32:32 +00:00
|
|
|
commentSort: "Hot",
|
2020-09-06 16:15:25 +00:00
|
|
|
commentViewType: CommentViewType.Tree,
|
|
|
|
scrolled: false,
|
2020-12-24 01:58:27 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
2021-07-18 03:56:33 +00:00
|
|
|
showSidebarMobile: false,
|
2021-11-16 14:46:12 +00:00
|
|
|
maxCommentsShown: commentsShownInterval,
|
2023-06-14 12:20:40 +00:00
|
|
|
finished: new Map(),
|
|
|
|
isIsomorphic: false,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
this.handleDeleteCommunityClick =
|
|
|
|
this.handleDeleteCommunityClick.bind(this);
|
|
|
|
this.handleEditCommunity = this.handleEditCommunity.bind(this);
|
|
|
|
this.handleFollow = this.handleFollow.bind(this);
|
|
|
|
this.handleModRemoveCommunity = this.handleModRemoveCommunity.bind(this);
|
|
|
|
this.handleCreateComment = this.handleCreateComment.bind(this);
|
|
|
|
this.handleEditComment = this.handleEditComment.bind(this);
|
|
|
|
this.handleSaveComment = this.handleSaveComment.bind(this);
|
|
|
|
this.handleBlockCommunity = this.handleBlockCommunity.bind(this);
|
|
|
|
this.handleBlockPerson = this.handleBlockPerson.bind(this);
|
|
|
|
this.handleDeleteComment = this.handleDeleteComment.bind(this);
|
|
|
|
this.handleRemoveComment = this.handleRemoveComment.bind(this);
|
|
|
|
this.handleCommentVote = this.handleCommentVote.bind(this);
|
|
|
|
this.handleAddModToCommunity = this.handleAddModToCommunity.bind(this);
|
|
|
|
this.handleAddAdmin = this.handleAddAdmin.bind(this);
|
|
|
|
this.handlePurgePerson = this.handlePurgePerson.bind(this);
|
|
|
|
this.handlePurgeComment = this.handlePurgeComment.bind(this);
|
|
|
|
this.handleCommentReport = this.handleCommentReport.bind(this);
|
|
|
|
this.handleDistinguishComment = this.handleDistinguishComment.bind(this);
|
|
|
|
this.handleTransferCommunity = this.handleTransferCommunity.bind(this);
|
|
|
|
this.handleFetchChildren = this.handleFetchChildren.bind(this);
|
|
|
|
this.handleCommentReplyRead = this.handleCommentReplyRead.bind(this);
|
|
|
|
this.handlePersonMentionRead = this.handlePersonMentionRead.bind(this);
|
|
|
|
this.handleBanFromCommunity = this.handleBanFromCommunity.bind(this);
|
|
|
|
this.handleBanPerson = this.handleBanPerson.bind(this);
|
|
|
|
this.handlePostEdit = this.handlePostEdit.bind(this);
|
|
|
|
this.handlePostVote = this.handlePostVote.bind(this);
|
|
|
|
this.handlePostReport = this.handlePostReport.bind(this);
|
|
|
|
this.handleLockPost = this.handleLockPost.bind(this);
|
|
|
|
this.handleDeletePost = this.handleDeletePost.bind(this);
|
|
|
|
this.handleRemovePost = this.handleRemovePost.bind(this);
|
|
|
|
this.handleSavePost = this.handleSavePost.bind(this);
|
|
|
|
this.handlePurgePost = this.handlePurgePost.bind(this);
|
|
|
|
this.handleFeaturePost = this.handleFeaturePost.bind(this);
|
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
|
2023-06-14 12:20:40 +00:00
|
|
|
if (FirstLoadService.isFirstLoad) {
|
2023-06-16 02:08:14 +00:00
|
|
|
const { commentsRes, postRes } = this.isoData.routeData;
|
2023-06-14 12:20:40 +00:00
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
2023-06-14 12:20:40 +00:00
|
|
|
postRes,
|
|
|
|
commentsRes,
|
|
|
|
isIsomorphic: true,
|
2022-09-22 15:03:35 +00:00
|
|
|
};
|
|
|
|
|
2021-01-24 01:41:23 +00:00
|
|
|
if (isBrowser()) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async fetchPost() {
|
|
|
|
this.setState({
|
|
|
|
postRes: { state: "loading" },
|
|
|
|
commentsRes: { state: "loading" },
|
|
|
|
});
|
2022-07-30 13:28:08 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
const auth = myAuth();
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
postRes: await HttpService.client.getPost({
|
|
|
|
id: this.state.postId,
|
|
|
|
comment_id: this.state.commentId,
|
|
|
|
auth,
|
|
|
|
}),
|
|
|
|
commentsRes: await HttpService.client.getComments({
|
|
|
|
post_id: this.state.postId,
|
|
|
|
parent_id: this.state.commentId,
|
|
|
|
max_depth: commentTreeMaxDepth,
|
|
|
|
sort: this.state.commentSort,
|
|
|
|
type_: "All",
|
|
|
|
saved_only: false,
|
|
|
|
auth,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
setupTippy();
|
|
|
|
|
|
|
|
if (!this.state.commentId) restoreScrollPosition(this.context);
|
|
|
|
|
|
|
|
if (this.checkScrollIntoCommentsParam) {
|
|
|
|
this.scrollIntoCommentSection();
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2021-01-24 01:41:23 +00:00
|
|
|
}
|
|
|
|
|
2023-06-16 02:08:14 +00:00
|
|
|
static async fetchInitialData({
|
2023-06-14 12:20:40 +00:00
|
|
|
client,
|
|
|
|
path,
|
2023-06-16 02:08:14 +00:00
|
|
|
auth,
|
|
|
|
}: InitialFetchRequest): Promise<PostData> {
|
2023-06-14 12:20:40 +00:00
|
|
|
const pathSplit = path.split("/");
|
2020-09-09 00:48:17 +00:00
|
|
|
|
2023-06-05 21:31:12 +00:00
|
|
|
const pathType = pathSplit.at(1);
|
|
|
|
const id = pathSplit.at(2) ? Number(pathSplit.at(2)) : undefined;
|
2020-09-09 00:48:17 +00:00
|
|
|
|
2023-06-05 21:31:12 +00:00
|
|
|
const postForm: GetPost = {
|
2023-01-04 16:56:24 +00:00
|
|
|
auth,
|
|
|
|
};
|
2022-07-30 13:28:08 +00:00
|
|
|
|
2023-06-05 21:31:12 +00:00
|
|
|
const commentsForm: GetComments = {
|
2023-01-04 16:56:24 +00:00
|
|
|
max_depth: commentTreeMaxDepth,
|
2023-05-11 18:32:32 +00:00
|
|
|
sort: "Hot",
|
|
|
|
type_: "All",
|
2023-01-04 16:56:24 +00:00
|
|
|
saved_only: false,
|
|
|
|
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
|
2023-06-14 12:20:40 +00:00
|
|
|
if (pathType === "post") {
|
2023-01-04 16:56:24 +00:00
|
|
|
postForm.id = id;
|
|
|
|
commentsForm.post_id = id;
|
2022-07-30 13:28:08 +00:00
|
|
|
} else {
|
2023-01-04 16:56:24 +00:00
|
|
|
postForm.comment_id = id;
|
|
|
|
commentsForm.parent_id = id;
|
2022-07-30 13:28:08 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 00:40:00 +00:00
|
|
|
return {
|
2023-06-16 02:08:14 +00:00
|
|
|
postRes: await client.getPost(postForm),
|
|
|
|
commentsRes: await client.getComments(commentsForm),
|
2023-05-30 00:40:00 +00:00
|
|
|
};
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
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
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async componentDidMount() {
|
|
|
|
if (!this.state.isIsomorphic) {
|
|
|
|
await this.fetchPost();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async 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) {
|
2023-06-14 12:20:40 +00:00
|
|
|
await this.fetchPost();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.state.commentSectionRef?.current?.scrollIntoView();
|
2021-08-12 01:00:26 +00:00
|
|
|
}
|
|
|
|
|
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)) {
|
2023-06-14 12:20:40 +00:00
|
|
|
const commentCount =
|
|
|
|
this.state.commentsRes.state == "success"
|
|
|
|
? this.state.commentsRes.data.comments.length
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
if (this.state.maxCommentsShown < commentCount) {
|
|
|
|
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 {
|
2023-06-05 21:31:12 +00:00
|
|
|
const siteName = this.state.siteRes.site_view.site.name;
|
2023-06-14 12:20:40 +00:00
|
|
|
return this.state.postRes.state == "success"
|
|
|
|
? `${this.state.postRes.data.post_view.post.name} - ${siteName}`
|
|
|
|
: siteName;
|
2020-09-11 18:09:21 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
get imageTag(): string | undefined {
|
2023-06-14 12:20:40 +00:00
|
|
|
if (this.state.postRes.state == "success") {
|
|
|
|
const post = this.state.postRes.data.post_view.post;
|
|
|
|
const thumbnail = post.thumbnail_url;
|
|
|
|
const url = post.url;
|
|
|
|
return thumbnail || (url && isImage(url) ? url : undefined);
|
|
|
|
} else return undefined;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
renderPostRes() {
|
|
|
|
switch (this.state.postRes.state) {
|
|
|
|
case "loading":
|
|
|
|
return (
|
2020-09-06 16:15:25 +00:00
|
|
|
<h5>
|
2021-07-17 20:21:31 +00:00
|
|
|
<Spinner large />
|
2020-09-06 16:15:25 +00:00
|
|
|
</h5>
|
2023-06-14 12:20:40 +00:00
|
|
|
);
|
|
|
|
case "success": {
|
|
|
|
const res = this.state.postRes.data;
|
|
|
|
return (
|
|
|
|
<div className="row">
|
|
|
|
<div className="col-12 col-md-8 mb-3">
|
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
image={this.imageTag}
|
|
|
|
description={res.post_view.post.body}
|
|
|
|
/>
|
|
|
|
<PostListing
|
|
|
|
post_view={res.post_view}
|
|
|
|
crossPosts={res.cross_posts}
|
|
|
|
showBody
|
|
|
|
showCommunity
|
|
|
|
moderators={res.moderators}
|
|
|
|
admins={this.state.siteRes.admins}
|
|
|
|
enableDownvotes={enableDownvotes(this.state.siteRes)}
|
|
|
|
enableNsfw={enableNsfw(this.state.siteRes)}
|
|
|
|
allLanguages={this.state.siteRes.all_languages}
|
|
|
|
siteLanguages={this.state.siteRes.discussion_languages}
|
|
|
|
onBlockPerson={this.handleBlockPerson}
|
|
|
|
onPostEdit={this.handlePostEdit}
|
|
|
|
onPostVote={this.handlePostVote}
|
|
|
|
onPostReport={this.handlePostReport}
|
|
|
|
onLockPost={this.handleLockPost}
|
|
|
|
onDeletePost={this.handleDeletePost}
|
|
|
|
onRemovePost={this.handleRemovePost}
|
|
|
|
onSavePost={this.handleSavePost}
|
|
|
|
onPurgePerson={this.handlePurgePerson}
|
|
|
|
onPurgePost={this.handlePurgePost}
|
|
|
|
onBanPerson={this.handleBanPerson}
|
|
|
|
onBanPersonFromCommunity={this.handleBanFromCommunity}
|
|
|
|
onAddModToCommunity={this.handleAddModToCommunity}
|
|
|
|
onAddAdmin={this.handleAddAdmin}
|
|
|
|
onTransferCommunity={this.handleTransferCommunity}
|
|
|
|
onFeaturePost={this.handleFeaturePost}
|
|
|
|
/>
|
|
|
|
<div ref={this.state.commentSectionRef} className="mb-2" />
|
|
|
|
<CommentForm
|
|
|
|
node={res.post_view.post.id}
|
|
|
|
disabled={res.post_view.post.locked}
|
|
|
|
allLanguages={this.state.siteRes.all_languages}
|
|
|
|
siteLanguages={this.state.siteRes.discussion_languages}
|
2023-06-16 17:10:56 +00:00
|
|
|
containerClass="post-comment-container"
|
2023-06-14 12:20:40 +00:00
|
|
|
onUpsertComment={this.handleCreateComment}
|
|
|
|
finished={this.state.finished.get(0)}
|
|
|
|
/>
|
|
|
|
<div className="d-block d-md-none">
|
|
|
|
<button
|
|
|
|
className="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()}
|
2021-07-18 03:56:33 +00:00
|
|
|
</div>
|
2023-06-14 12:20:40 +00:00
|
|
|
{this.sortRadios()}
|
|
|
|
{this.state.commentViewType == CommentViewType.Tree &&
|
|
|
|
this.commentsTree()}
|
|
|
|
{this.state.commentViewType == CommentViewType.Flat &&
|
|
|
|
this.commentsFlat()}
|
2023-01-04 16:56:24 +00:00
|
|
|
</div>
|
2023-06-14 12:20:40 +00:00
|
|
|
<div className="d-none d-md-block col-md-4">{this.sidebar()}</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <div className="container-lg">{this.renderPostRes()}</div>;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 ${
|
2023-05-11 18:32:32 +00:00
|
|
|
this.state.commentSort === "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"
|
2023-05-11 18:32:32 +00:00
|
|
|
value={"Hot"}
|
|
|
|
checked={this.state.commentSort === "Hot"}
|
2020-09-06 16:15:25 +00:00
|
|
|
onChange={linkEvent(this, this.handleCommentSortChange)}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
<label
|
|
|
|
className={`btn btn-outline-secondary pointer ${
|
2023-05-11 18:32:32 +00:00
|
|
|
this.state.commentSort === "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"
|
2023-05-11 18:32:32 +00:00
|
|
|
value={"Top"}
|
|
|
|
checked={this.state.commentSort === "Top"}
|
2020-09-06 16:15:25 +00:00
|
|
|
onChange={linkEvent(this, this.handleCommentSortChange)}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
<label
|
|
|
|
className={`btn btn-outline-secondary pointer ${
|
2023-05-11 18:32:32 +00:00
|
|
|
this.state.commentSort === "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"
|
2023-05-11 18:32:32 +00:00
|
|
|
value={"New"}
|
|
|
|
checked={this.state.commentSort === "New"}
|
2020-09-06 16:15:25 +00:00
|
|
|
onChange={linkEvent(this, this.handleCommentSortChange)}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
<label
|
|
|
|
className={`btn btn-outline-secondary pointer ${
|
2023-05-11 18:32:32 +00:00
|
|
|
this.state.commentSort === "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"
|
2023-05-11 18:32:32 +00:00
|
|
|
value={"Old"}
|
|
|
|
checked={this.state.commentSort === "Old"}
|
2020-09-06 16:15:25 +00:00
|
|
|
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
|
2023-06-05 21:31:12 +00:00
|
|
|
const commentsRes = this.state.commentsRes;
|
|
|
|
const postRes = this.state.postRes;
|
2023-06-14 12:20:40 +00:00
|
|
|
|
|
|
|
if (commentsRes.state == "success" && postRes.state == "success") {
|
|
|
|
return (
|
2023-01-04 16:56:24 +00:00
|
|
|
<div>
|
|
|
|
<CommentNodes
|
2023-06-14 12:20:40 +00:00
|
|
|
nodes={commentsToFlatNodes(commentsRes.data.comments)}
|
2023-01-04 16:56:24 +00:00
|
|
|
viewType={this.state.commentViewType}
|
|
|
|
maxCommentsShown={this.state.maxCommentsShown}
|
|
|
|
noIndent
|
2023-06-14 12:20:40 +00:00
|
|
|
locked={postRes.data.post_view.post.locked}
|
|
|
|
moderators={postRes.data.moderators}
|
2023-01-04 16:56:24 +00:00
|
|
|
admins={this.state.siteRes.admins}
|
|
|
|
enableDownvotes={enableDownvotes(this.state.siteRes)}
|
|
|
|
showContext
|
2023-06-14 12:20:40 +00:00
|
|
|
finished={this.state.finished}
|
2023-01-04 16:56:24 +00:00
|
|
|
allLanguages={this.state.siteRes.all_languages}
|
|
|
|
siteLanguages={this.state.siteRes.discussion_languages}
|
2023-06-14 12:20:40 +00:00
|
|
|
onSaveComment={this.handleSaveComment}
|
|
|
|
onBlockPerson={this.handleBlockPerson}
|
|
|
|
onDeleteComment={this.handleDeleteComment}
|
|
|
|
onRemoveComment={this.handleRemoveComment}
|
|
|
|
onCommentVote={this.handleCommentVote}
|
|
|
|
onCommentReport={this.handleCommentReport}
|
|
|
|
onDistinguishComment={this.handleDistinguishComment}
|
|
|
|
onAddModToCommunity={this.handleAddModToCommunity}
|
|
|
|
onAddAdmin={this.handleAddAdmin}
|
|
|
|
onTransferCommunity={this.handleTransferCommunity}
|
|
|
|
onFetchChildren={this.handleFetchChildren}
|
|
|
|
onPurgeComment={this.handlePurgeComment}
|
|
|
|
onPurgePerson={this.handlePurgePerson}
|
|
|
|
onCommentReplyRead={this.handleCommentReplyRead}
|
|
|
|
onPersonMentionRead={this.handlePersonMentionRead}
|
|
|
|
onBanPersonFromCommunity={this.handleBanFromCommunity}
|
|
|
|
onBanPerson={this.handleBanPerson}
|
|
|
|
onCreateComment={this.handleCreateComment}
|
|
|
|
onEditComment={this.handleEditComment}
|
2023-01-04 16:56:24 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2023-06-14 12:20:40 +00:00
|
|
|
);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sidebar() {
|
2023-06-05 21:31:12 +00:00
|
|
|
const res = this.state.postRes;
|
2023-06-14 12:20:40 +00:00
|
|
|
if (res.state === "success") {
|
|
|
|
return (
|
2023-06-16 21:56:06 +00:00
|
|
|
<Sidebar
|
|
|
|
community_view={res.data.community_view}
|
|
|
|
moderators={res.data.moderators}
|
|
|
|
admins={this.state.siteRes.admins}
|
|
|
|
enableNsfw={enableNsfw(this.state.siteRes)}
|
|
|
|
showIcon
|
|
|
|
allLanguages={this.state.siteRes.all_languages}
|
|
|
|
siteLanguages={this.state.siteRes.discussion_languages}
|
|
|
|
onDeleteCommunity={this.handleDeleteCommunityClick}
|
|
|
|
onLeaveModTeam={this.handleAddModToCommunity}
|
|
|
|
onFollowCommunity={this.handleFollow}
|
|
|
|
onRemoveCommunity={this.handleModRemoveCommunity}
|
|
|
|
onPurgeCommunity={this.handlePurgeCommunity}
|
|
|
|
onBlockCommunity={this.handleBlockCommunity}
|
|
|
|
onEditCommunity={this.handleEditCommunity}
|
|
|
|
/>
|
2023-06-14 12:20:40 +00:00
|
|
|
);
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2022-07-30 13:28:08 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
commentsTree() {
|
2023-06-05 21:31:12 +00:00
|
|
|
const res = this.state.postRes;
|
2023-06-14 12:20:40 +00:00
|
|
|
const firstComment = this.commentTree().at(0)?.comment_view.comment;
|
2023-06-05 21:31:12 +00:00
|
|
|
const depth = getDepthFromComment(firstComment);
|
|
|
|
const showContextButton = depth ? depth > 0 : false;
|
2022-07-30 13:28:08 +00:00
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
return (
|
2023-06-14 12:20:40 +00:00
|
|
|
res.state == "success" && (
|
2022-06-21 21:42:29 +00:00
|
|
|
<div>
|
2023-01-04 16:56:24 +00:00
|
|
|
{!!this.state.commentId && (
|
2022-07-30 13:28:08 +00:00
|
|
|
<>
|
|
|
|
<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
|
2023-06-14 12:20:40 +00:00
|
|
|
nodes={this.commentTree()}
|
2022-07-30 13:28:08 +00:00
|
|
|
viewType={this.state.commentViewType}
|
2023-01-04 16:56:24 +00:00
|
|
|
maxCommentsShown={this.state.maxCommentsShown}
|
2023-06-14 12:20:40 +00:00
|
|
|
locked={res.data.post_view.post.locked}
|
|
|
|
moderators={res.data.moderators}
|
2023-01-04 16:56:24 +00:00
|
|
|
admins={this.state.siteRes.admins}
|
2022-06-21 21:42:29 +00:00
|
|
|
enableDownvotes={enableDownvotes(this.state.siteRes)}
|
2023-06-14 12:20:40 +00:00
|
|
|
finished={this.state.finished}
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages={this.state.siteRes.all_languages}
|
2022-12-19 15:57:29 +00:00
|
|
|
siteLanguages={this.state.siteRes.discussion_languages}
|
2023-06-14 12:20:40 +00:00
|
|
|
onSaveComment={this.handleSaveComment}
|
|
|
|
onBlockPerson={this.handleBlockPerson}
|
|
|
|
onDeleteComment={this.handleDeleteComment}
|
|
|
|
onRemoveComment={this.handleRemoveComment}
|
|
|
|
onCommentVote={this.handleCommentVote}
|
|
|
|
onCommentReport={this.handleCommentReport}
|
|
|
|
onDistinguishComment={this.handleDistinguishComment}
|
|
|
|
onAddModToCommunity={this.handleAddModToCommunity}
|
|
|
|
onAddAdmin={this.handleAddAdmin}
|
|
|
|
onTransferCommunity={this.handleTransferCommunity}
|
|
|
|
onFetchChildren={this.handleFetchChildren}
|
|
|
|
onPurgeComment={this.handlePurgeComment}
|
|
|
|
onPurgePerson={this.handlePurgePerson}
|
|
|
|
onCommentReplyRead={this.handleCommentReplyRead}
|
|
|
|
onPersonMentionRead={this.handlePersonMentionRead}
|
|
|
|
onBanPersonFromCommunity={this.handleBanFromCommunity}
|
|
|
|
onBanPerson={this.handleBanPerson}
|
|
|
|
onCreateComment={this.handleCreateComment}
|
|
|
|
onEditComment={this.handleEditComment}
|
2022-06-21 21:42:29 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2023-01-04 16:56:24 +00:00
|
|
|
)
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
commentTree(): CommentNodeI[] {
|
|
|
|
if (this.state.commentsRes.state == "success") {
|
|
|
|
return buildCommentsTree(
|
|
|
|
this.state.commentsRes.data.comments,
|
|
|
|
!!this.state.commentId
|
2022-06-21 21:42:29 +00:00
|
|
|
);
|
2023-06-14 12:20:40 +00:00
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 21:42:29 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async handleCommentSortChange(i: Post, event: any) {
|
|
|
|
i.setState({
|
|
|
|
commentSort: event.target.value as CommentSortType,
|
|
|
|
commentViewType: CommentViewType.Tree,
|
|
|
|
commentsRes: { state: "loading" },
|
|
|
|
postRes: { state: "loading" },
|
|
|
|
});
|
|
|
|
await i.fetchPost();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCommentViewTypeChange(i: Post, event: any) {
|
|
|
|
i.setState({
|
|
|
|
commentViewType: Number(event.target.value),
|
|
|
|
commentSort: "New",
|
|
|
|
});
|
|
|
|
}
|
2021-08-12 01:06:54 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
handleShowSidebarMobile(i: Post) {
|
|
|
|
i.setState({ showSidebarMobile: !i.state.showSidebarMobile });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleViewPost(i: Post) {
|
|
|
|
if (i.state.postRes.state == "success") {
|
|
|
|
const id = i.state.postRes.data.post_view.post.id;
|
|
|
|
i.context.router.history.push(`/post/${id}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleViewContext(i: Post) {
|
|
|
|
if (i.state.commentsRes.state == "success") {
|
|
|
|
const parentId = getCommentParentId(
|
|
|
|
i.state.commentsRes.data.comments.at(0)?.comment
|
|
|
|
);
|
|
|
|
if (parentId) {
|
|
|
|
i.context.router.history.push(`/comment/${parentId}`);
|
2021-08-12 01:00:26 +00:00
|
|
|
}
|
2023-06-14 12:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleDeleteCommunityClick(form: DeleteCommunity) {
|
|
|
|
const deleteCommunityRes = await HttpService.client.deleteCommunity(form);
|
|
|
|
this.updateCommunity(deleteCommunityRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleAddModToCommunity(form: AddModToCommunity) {
|
|
|
|
const addModRes = await HttpService.client.addModToCommunity(form);
|
|
|
|
this.updateModerators(addModRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleFollow(form: FollowCommunity) {
|
|
|
|
const followCommunityRes = await HttpService.client.followCommunity(form);
|
|
|
|
this.updateCommunity(followCommunityRes);
|
|
|
|
|
|
|
|
// Update myUserInfo
|
|
|
|
if (followCommunityRes.state === "success") {
|
|
|
|
const communityId = followCommunityRes.data.community_view.community.id;
|
|
|
|
const mui = UserService.Instance.myUserInfo;
|
|
|
|
if (mui) {
|
|
|
|
mui.follows = mui.follows.filter(i => i.community.id != communityId);
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2023-06-14 12:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-04 16:56:24 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async handlePurgeCommunity(form: PurgeCommunity) {
|
|
|
|
const purgeCommunityRes = await HttpService.client.purgeCommunity(form);
|
|
|
|
this.purgeItem(purgeCommunityRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handlePurgePerson(form: PurgePerson) {
|
|
|
|
const purgePersonRes = await HttpService.client.purgePerson(form);
|
|
|
|
this.purgeItem(purgePersonRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handlePurgeComment(form: PurgeComment) {
|
|
|
|
const purgeCommentRes = await HttpService.client.purgeComment(form);
|
|
|
|
this.purgeItem(purgeCommentRes);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async handlePurgePost(form: PurgePost) {
|
|
|
|
const purgeRes = await HttpService.client.purgePost(form);
|
|
|
|
this.purgeItem(purgeRes);
|
|
|
|
}
|
2021-12-02 16:46:41 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async handleBlockCommunity(form: BlockCommunity) {
|
|
|
|
const blockCommunityRes = await HttpService.client.blockCommunity(form);
|
|
|
|
if (blockCommunityRes.state == "success") {
|
|
|
|
updateCommunityBlock(blockCommunityRes.data);
|
2023-06-16 17:01:09 +00:00
|
|
|
this.setState(s => {
|
|
|
|
if (s.postRes.state == "success") {
|
|
|
|
s.postRes.data.community_view.blocked =
|
|
|
|
blockCommunityRes.data.blocked;
|
|
|
|
}
|
|
|
|
});
|
2023-06-14 12:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleBlockPerson(form: BlockPerson) {
|
|
|
|
const blockPersonRes = await HttpService.client.blockPerson(form);
|
|
|
|
if (blockPersonRes.state == "success") {
|
|
|
|
updatePersonBlock(blockPersonRes.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleModRemoveCommunity(form: RemoveCommunity) {
|
|
|
|
const removeCommunityRes = await HttpService.client.removeCommunity(form);
|
|
|
|
this.updateCommunity(removeCommunityRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleEditCommunity(form: EditCommunity) {
|
|
|
|
const res = await HttpService.client.editCommunity(form);
|
|
|
|
this.updateCommunity(res);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleCreateComment(form: CreateComment) {
|
|
|
|
const createCommentRes = await HttpService.client.createComment(form);
|
|
|
|
this.createAndUpdateComments(createCommentRes);
|
|
|
|
|
|
|
|
return createCommentRes;
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleEditComment(form: EditComment) {
|
|
|
|
const editCommentRes = await HttpService.client.editComment(form);
|
|
|
|
this.findAndUpdateComment(editCommentRes);
|
|
|
|
|
|
|
|
return editCommentRes;
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleDeleteComment(form: DeleteComment) {
|
|
|
|
const deleteCommentRes = await HttpService.client.deleteComment(form);
|
|
|
|
this.findAndUpdateComment(deleteCommentRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleDeletePost(form: DeletePost) {
|
|
|
|
const deleteRes = await HttpService.client.deletePost(form);
|
|
|
|
this.updatePost(deleteRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleRemovePost(form: RemovePost) {
|
|
|
|
const removeRes = await HttpService.client.removePost(form);
|
|
|
|
this.updatePost(removeRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleRemoveComment(form: RemoveComment) {
|
|
|
|
const removeCommentRes = await HttpService.client.removeComment(form);
|
|
|
|
this.findAndUpdateComment(removeCommentRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleSaveComment(form: SaveComment) {
|
|
|
|
const saveCommentRes = await HttpService.client.saveComment(form);
|
|
|
|
this.findAndUpdateComment(saveCommentRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleSavePost(form: SavePost) {
|
|
|
|
const saveRes = await HttpService.client.savePost(form);
|
|
|
|
this.updatePost(saveRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleFeaturePost(form: FeaturePost) {
|
|
|
|
const featureRes = await HttpService.client.featurePost(form);
|
|
|
|
this.updatePost(featureRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleCommentVote(form: CreateCommentLike) {
|
|
|
|
const voteRes = await HttpService.client.likeComment(form);
|
|
|
|
this.findAndUpdateComment(voteRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handlePostVote(form: CreatePostLike) {
|
|
|
|
const voteRes = await HttpService.client.likePost(form);
|
|
|
|
this.updatePost(voteRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handlePostEdit(form: EditPost) {
|
|
|
|
const res = await HttpService.client.editPost(form);
|
|
|
|
this.updatePost(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleCommentReport(form: CreateCommentReport) {
|
|
|
|
const reportRes = await HttpService.client.createCommentReport(form);
|
|
|
|
if (reportRes.state == "success") {
|
|
|
|
toast(i18n.t("report_created"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async handlePostReport(form: CreatePostReport) {
|
|
|
|
const reportRes = await HttpService.client.createPostReport(form);
|
|
|
|
if (reportRes.state == "success") {
|
|
|
|
toast(i18n.t("report_created"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleLockPost(form: LockPost) {
|
|
|
|
const lockRes = await HttpService.client.lockPost(form);
|
|
|
|
this.updatePost(lockRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleDistinguishComment(form: DistinguishComment) {
|
|
|
|
const distinguishRes = await HttpService.client.distinguishComment(form);
|
|
|
|
this.findAndUpdateComment(distinguishRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleAddAdmin(form: AddAdmin) {
|
|
|
|
const addAdminRes = await HttpService.client.addAdmin(form);
|
|
|
|
|
|
|
|
if (addAdminRes.state === "success") {
|
|
|
|
this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleTransferCommunity(form: TransferCommunity) {
|
|
|
|
const transferCommunityRes = await HttpService.client.transferCommunity(
|
|
|
|
form
|
|
|
|
);
|
|
|
|
this.updateCommunityFull(transferCommunityRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleFetchChildren(form: GetComments) {
|
|
|
|
const moreCommentsRes = await HttpService.client.getComments(form);
|
|
|
|
if (
|
|
|
|
this.state.commentsRes.state == "success" &&
|
|
|
|
moreCommentsRes.state == "success"
|
2020-09-06 16:15:25 +00:00
|
|
|
) {
|
2023-06-14 12:20:40 +00:00
|
|
|
const newComments = moreCommentsRes.data.comments;
|
|
|
|
// Remove the first comment, since it is the parent
|
|
|
|
newComments.shift();
|
|
|
|
const newRes = this.state.commentsRes;
|
|
|
|
newRes.data.comments.push(...newComments);
|
|
|
|
this.setState({ commentsRes: newRes });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
|
|
|
|
const readRes = await HttpService.client.markCommentReplyAsRead(form);
|
|
|
|
this.findAndUpdateCommentReply(readRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
|
|
|
|
// TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
|
|
|
|
await HttpService.client.markPersonMentionAsRead(form);
|
|
|
|
}
|
2023-01-04 16:56:24 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async handleBanFromCommunity(form: BanFromCommunity) {
|
|
|
|
const banRes = await HttpService.client.banFromCommunity(form);
|
|
|
|
this.updateBan(banRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleBanPerson(form: BanPerson) {
|
|
|
|
const banRes = await HttpService.client.banPerson(form);
|
|
|
|
this.updateBan(banRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateBanFromCommunity(banRes: RequestState<BanFromCommunityResponse>) {
|
|
|
|
// Maybe not necessary
|
|
|
|
if (banRes.state == "success") {
|
|
|
|
this.setState(s => {
|
|
|
|
if (
|
|
|
|
s.postRes.state == "success" &&
|
|
|
|
s.postRes.data.post_view.creator.id ==
|
|
|
|
banRes.data.person_view.person.id
|
|
|
|
) {
|
|
|
|
s.postRes.data.post_view.creator_banned_from_community =
|
|
|
|
banRes.data.banned;
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2023-06-14 12:20:40 +00:00
|
|
|
if (s.commentsRes.state == "success") {
|
|
|
|
s.commentsRes.data.comments
|
|
|
|
.filter(c => c.creator.id == banRes.data.person_view.person.id)
|
|
|
|
.forEach(
|
|
|
|
c => (c.creator_banned_from_community = banRes.data.banned)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateBan(banRes: RequestState<BanPersonResponse>) {
|
|
|
|
// Maybe not necessary
|
|
|
|
if (banRes.state == "success") {
|
|
|
|
this.setState(s => {
|
|
|
|
if (
|
|
|
|
s.postRes.state == "success" &&
|
|
|
|
s.postRes.data.post_view.creator.id ==
|
|
|
|
banRes.data.person_view.person.id
|
|
|
|
) {
|
|
|
|
s.postRes.data.post_view.creator.banned = banRes.data.banned;
|
|
|
|
}
|
|
|
|
if (s.commentsRes.state == "success") {
|
|
|
|
s.commentsRes.data.comments
|
|
|
|
.filter(c => c.creator.id == banRes.data.person_view.person.id)
|
|
|
|
.forEach(c => (c.creator.banned = banRes.data.banned));
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCommunity(communityRes: RequestState<CommunityResponse>) {
|
|
|
|
this.setState(s => {
|
|
|
|
if (s.postRes.state == "success" && communityRes.state == "success") {
|
|
|
|
s.postRes.data.community_view = communityRes.data.community_view;
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2023-06-14 12:20:40 +00:00
|
|
|
return s;
|
|
|
|
});
|
|
|
|
}
|
2023-01-04 16:56:24 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
updateCommunityFull(res: RequestState<GetCommunityResponse>) {
|
|
|
|
this.setState(s => {
|
|
|
|
if (s.postRes.state == "success" && res.state == "success") {
|
|
|
|
s.postRes.data.community_view = res.data.community_view;
|
|
|
|
s.postRes.data.moderators = res.data.moderators;
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2023-06-14 12:20:40 +00:00
|
|
|
return s;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updatePost(post: RequestState<PostResponse>) {
|
|
|
|
this.setState(s => {
|
|
|
|
if (s.postRes.state == "success" && post.state == "success") {
|
|
|
|
s.postRes.data.post_view = post.data.post_view;
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2023-06-14 12:20:40 +00:00
|
|
|
return s;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
purgeItem(purgeRes: RequestState<PurgeItemResponse>) {
|
|
|
|
if (purgeRes.state == "success") {
|
|
|
|
toast(i18n.t("purge_success"));
|
|
|
|
this.context.router.history.push(`/`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
createAndUpdateComments(res: RequestState<CommentResponse>) {
|
|
|
|
this.setState(s => {
|
|
|
|
if (s.commentsRes.state === "success" && res.state === "success") {
|
|
|
|
s.commentsRes.data.comments.unshift(res.data.comment_view);
|
|
|
|
|
|
|
|
// Set finished for the parent
|
|
|
|
s.finished.set(
|
|
|
|
getCommentParentId(res.data.comment_view.comment) ?? 0,
|
|
|
|
true
|
|
|
|
);
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2023-06-14 12:20:40 +00:00
|
|
|
return s;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
findAndUpdateComment(res: RequestState<CommentResponse>) {
|
|
|
|
this.setState(s => {
|
|
|
|
if (s.commentsRes.state == "success" && res.state == "success") {
|
|
|
|
s.commentsRes.data.comments = editComment(
|
|
|
|
res.data.comment_view,
|
|
|
|
s.commentsRes.data.comments
|
|
|
|
);
|
|
|
|
s.finished.set(res.data.comment_view.comment.id, true);
|
2021-09-28 10:38:59 +00:00
|
|
|
}
|
2023-06-14 12:20:40 +00:00
|
|
|
return s;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
findAndUpdateCommentReply(res: RequestState<CommentReplyResponse>) {
|
|
|
|
this.setState(s => {
|
|
|
|
if (s.commentsRes.state == "success" && res.state == "success") {
|
|
|
|
s.commentsRes.data.comments = editWith(
|
|
|
|
res.data.comment_reply_view,
|
|
|
|
s.commentsRes.data.comments
|
|
|
|
);
|
2021-09-28 10:38:59 +00:00
|
|
|
}
|
2023-06-14 12:20:40 +00:00
|
|
|
return s;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateModerators(res: RequestState<AddModToCommunityResponse>) {
|
|
|
|
// Update the moderators
|
|
|
|
this.setState(s => {
|
|
|
|
if (s.postRes.state == "success" && res.state == "success") {
|
|
|
|
s.postRes.data.moderators = res.data.moderators;
|
2022-06-23 19:44:05 +00:00
|
|
|
}
|
2023-06-14 12:20:40 +00:00
|
|
|
return s;
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|