lemmy-ui/src/shared/components/post.tsx

572 lines
18 KiB
TypeScript
Raw Normal View History

2021-02-22 02:39:04 +00:00
import { Component, linkEvent } from "inferno";
import { HtmlTags } from "./html-tags";
import { Spinner } from "./icon";
import { Subscription } from "rxjs";
import {
UserOperation,
2020-12-24 01:58:27 +00:00
PostView,
GetPostResponse,
PostResponse,
2020-12-24 01:58:27 +00:00
MarkCommentAsRead,
CommentResponse,
CommunityResponse,
BanFromCommunityResponse,
2021-03-15 18:09:31 +00:00
BanPersonResponse,
AddModToCommunityResponse,
AddAdminResponse,
SearchType,
SortType,
2020-12-24 01:58:27 +00:00
Search,
GetPost,
SearchResponse,
GetSiteResponse,
GetCommunityResponse,
2021-02-22 02:39:04 +00:00
} from "lemmy-js-client";
import {
CommentSortType,
CommentViewType,
InitialFetchRequest,
2020-12-24 01:58:27 +00:00
CommentNode as CommentNodeI,
2021-02-22 02:39:04 +00:00
} from "../interfaces";
import { WebSocketService, UserService } from "../services";
import {
wsJsonToRes,
toast,
editCommentRes,
saveCommentRes,
createCommentLikeRes,
createPostLikeRes,
commentsToFlatNodes,
setupTippy,
2020-09-09 00:48:17 +00:00
setIsoData,
getIdFromProps,
getCommentIdFromProps,
wsSubscribe,
isBrowser,
2020-09-11 18:09:21 +00:00
previewLines,
isImage,
2020-12-24 01:58:27 +00:00
wsUserOp,
wsClient,
authField,
setOptionalAuth,
saveScrollPosition,
restoreScrollPosition,
buildCommentsTree,
insertCommentIntoTree,
2021-02-22 02:39:04 +00:00
} from "../utils";
import { PostListing } from "./post-listing";
import { Sidebar } from "./sidebar";
import { CommentForm } from "./comment-form";
import { CommentNodes } from "./comment-nodes";
import autosize from "autosize";
import { i18n } from "../i18next";
interface PostState {
2020-09-09 00:48:17 +00:00
postRes: GetPostResponse;
postId: number;
commentTree: CommentNodeI[];
2020-09-09 00:48:17 +00:00
commentId?: number;
commentSort: CommentSortType;
commentViewType: CommentViewType;
scrolled?: boolean;
loading: boolean;
2020-12-24 01:58:27 +00:00
crossPosts: PostView[];
siteRes: GetSiteResponse;
}
export class Post extends Component<any, PostState> {
private subscription: Subscription;
2020-09-09 00:48:17 +00:00
private isoData = setIsoData(this.context);
private emptyState: PostState = {
2020-09-09 00:48:17 +00:00
postRes: null,
postId: getIdFromProps(this.props),
commentTree: [],
2020-09-09 00:48:17 +00:00
commentId: getCommentIdFromProps(this.props),
commentSort: CommentSortType.Hot,
commentViewType: CommentViewType.Tree,
scrolled: false,
loading: true,
crossPosts: [],
2020-12-24 01:58:27 +00:00
siteRes: this.isoData.site_res,
};
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-09 00:48:17 +00:00
// Only fetch the data if coming from another route
if (this.isoData.path == this.context.router.route.match.url) {
this.state.postRes = this.isoData.routeData[0];
this.state.commentTree = buildCommentsTree(
this.state.postRes.comments,
this.state.commentSort
);
2020-09-09 00:48:17 +00:00
this.state.loading = false;
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,
auth: authField(false),
};
WebSocketService.Instance.send(wsClient.getPost(form));
2020-09-09 00:48:17 +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,
page: 1,
limit: 6,
auth: authField(false),
};
WebSocketService.Instance.send(wsClient.search(form));
}
}
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,
};
setOptionalAuth(postForm, req.auth);
2020-09-09 00:48:17 +00:00
promises.push(req.client.getPost(postForm));
2020-09-09 00:48:17 +00:00
return promises;
}
componentWillUnmount() {
this.subscription.unsubscribe();
window.isoData.path = undefined;
saveScrollPosition(this.context);
}
componentDidMount() {
WebSocketService.Instance.send(
wsClient.postJoin({ post_id: this.state.postId })
);
2021-02-22 02:39:04 +00:00
autosize(document.querySelectorAll("textarea"));
}
componentDidUpdate(_lastProps: any, lastState: PostState) {
if (
2020-09-09 00:48:17 +00:00
this.state.commentId &&
!this.state.scrolled &&
2020-09-09 00:48:17 +00:00
lastState.postRes &&
lastState.postRes.comments.length > 0
) {
2020-09-09 00:48:17 +00:00
this.scrollCommentIntoView();
}
// 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.
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
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;
if (
2021-03-15 18:09:31 +00:00
UserService.Instance.localUserView &&
UserService.Instance.localUserView.person.id == parent_person_id
) {
2020-12-24 01:58:27 +00:00
let form: MarkCommentAsRead = {
2021-01-06 21:07:13 +00:00
comment_id: found.comment.id,
read: true,
auth: authField(),
};
WebSocketService.Instance.send(wsClient.markCommentAsRead(form));
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-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;
}
render() {
let pv = this.state.postRes?.post_view;
return (
<div class="container">
{this.state.loading ? (
<h5>
2021-02-11 20:35:27 +00:00
<Spinner />
</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}
/>
<PostListing
2020-12-24 01:58:27 +00:00
post_view={pv}
duplicates={this.state.crossPosts}
showBody
showCommunity
2020-09-09 00:48:17 +00:00
moderators={this.state.postRes.moderators}
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}
/>
<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-09 00:48:17 +00:00
{this.state.postRes.comments.length > 0 && this.sortRadios()}
{this.state.commentViewType == CommentViewType.Tree &&
this.commentsTree()}
{this.state.commentViewType == CommentViewType.Chat &&
this.commentsFlat()}
</div>
<div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
</div>
)}
</div>
);
}
sortRadios() {
return (
<>
<div class="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
<label
className={`btn btn-outline-secondary pointer ${
2021-02-22 02:39:04 +00:00
this.state.commentSort === CommentSortType.Hot && "active"
}`}
>
2021-02-22 02:39:04 +00:00
{i18n.t("hot")}
<input
type="radio"
value={CommentSortType.Hot}
checked={this.state.commentSort === CommentSortType.Hot}
onChange={linkEvent(this, this.handleCommentSortChange)}
/>
</label>
<label
className={`btn btn-outline-secondary pointer ${
2021-02-22 02:39:04 +00:00
this.state.commentSort === CommentSortType.Top && "active"
}`}
>
2021-02-22 02:39:04 +00:00
{i18n.t("top")}
<input
type="radio"
value={CommentSortType.Top}
checked={this.state.commentSort === CommentSortType.Top}
onChange={linkEvent(this, this.handleCommentSortChange)}
/>
</label>
<label
className={`btn btn-outline-secondary pointer ${
2021-02-22 02:39:04 +00:00
this.state.commentSort === CommentSortType.New && "active"
}`}
>
2021-02-22 02:39:04 +00:00
{i18n.t("new")}
<input
type="radio"
value={CommentSortType.New}
checked={this.state.commentSort === CommentSortType.New}
onChange={linkEvent(this, this.handleCommentSortChange)}
/>
</label>
<label
className={`btn btn-outline-secondary pointer ${
2021-02-22 02:39:04 +00:00
this.state.commentSort === CommentSortType.Old && "active"
}`}
>
2021-02-22 02:39:04 +00:00
{i18n.t("old")}
<input
type="radio"
value={CommentSortType.Old}
checked={this.state.commentSort === CommentSortType.Old}
onChange={linkEvent(this, this.handleCommentSortChange)}
/>
</label>
</div>
<div class="btn-group btn-group-toggle flex-wrap mb-2">
<label
className={`btn btn-outline-secondary pointer ${
2021-02-22 02:39:04 +00:00
this.state.commentViewType === CommentViewType.Chat && "active"
}`}
>
2021-02-22 02:39:04 +00:00
{i18n.t("chat")}
<input
type="radio"
value={CommentViewType.Chat}
checked={this.state.commentViewType === CommentViewType.Chat}
onChange={linkEvent(this, this.handleCommentViewTypeChange)}
/>
</label>
</div>
</>
);
}
commentsFlat() {
// These are already sorted by new
return (
<div>
<CommentNodes
2020-09-09 00:48:17 +00:00
nodes={commentsToFlatNodes(this.state.postRes.comments)}
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}
admins={this.state.siteRes.admins}
2020-12-24 01:58:27 +00:00
postCreatorId={this.state.postRes.post_view.creator.id}
showContext
2020-12-24 01:58:27 +00:00
enableDownvotes={this.state.siteRes.site_view.site.enable_downvotes}
/>
</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}
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}
showIcon
/>
</div>
);
}
handleCommentSortChange(i: Post, event: any) {
i.state.commentSort = Number(event.target.value);
i.state.commentViewType = CommentViewType.Tree;
i.state.commentTree = buildCommentsTree(
i.state.postRes.comments,
i.state.commentSort
);
i.setState(i.state);
}
handleCommentViewTypeChange(i: Post, event: any) {
i.state.commentViewType = Number(event.target.value);
i.state.commentSort = CommentSortType.New;
i.state.commentTree = buildCommentsTree(
i.state.postRes.comments,
i.state.commentSort
);
i.setState(i.state);
}
commentsTree() {
return (
<div>
<CommentNodes
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}
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}
/>
</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);
if (msg.error) {
2021-02-22 02:39:04 +00:00
toast(i18n.t(msg.error), "danger");
return;
} else if (msg.reconnect) {
let postId = Number(this.props.match.params.id);
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;
this.state.commentTree = buildCommentsTree(
this.state.postRes.comments,
this.state.commentSort
);
this.state.loading = false;
// Get cross-posts
this.fetchCrossPosts();
this.setState(this.state);
setupTippy();
restoreScrollPosition(this.context);
2020-12-24 01:58:27 +00:00
} else if (op == UserOperation.CreateComment) {
let data = wsJsonToRes<CommentResponse>(msg).data;
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);
insertCommentIntoTree(this.state.commentTree, data.comment_view);
2021-01-15 17:42:05 +00:00
this.state.postRes.post_view.counts.comments++;
this.setState(this.state);
setupTippy();
}
} else if (
2020-12-24 01:58:27 +00:00
op == UserOperation.EditComment ||
op == UserOperation.DeleteComment ||
op == UserOperation.RemoveComment
) {
2020-12-24 01:58:27 +00:00
let data = wsJsonToRes<CommentResponse>(msg).data;
editCommentRes(data.comment_view, this.state.postRes.comments);
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);
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);
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);
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-12-24 01:58:27 +00:00
let data = wsJsonToRes<PostResponse>(msg).data;
this.state.postRes.post_view = data.post_view;
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-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;
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;
}
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;
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;
}
this.setState(this.state);
2020-12-24 01:58:27 +00:00
} else if (op == UserOperation.AddAdmin) {
let data = wsJsonToRes<AddAdminResponse>(msg).data;
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;
this.state.crossPosts = data.posts.filter(
2020-12-24 01:58:27 +00:00
p => p.post.id != Number(this.props.match.params.id)
);
this.setState(this.state);
2020-12-24 01:58:27 +00:00
} else if (op == UserOperation.TransferSite) {
let data = wsJsonToRes<GetSiteResponse>(msg).data;
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);
}
}
}