1 import { Component, linkEvent } from "inferno";
3 AddModToCommunityResponse,
4 BanFromCommunityResponse,
23 } from "lemmy-js-client";
24 import { Subscription } from "rxjs";
25 import { i18n } from "../../i18next";
26 import { DataType, InitialFetchRequest } from "../../interfaces";
27 import { UserService, WebSocketService } from "../../services";
33 createPostLikeFindRes,
42 restoreScrollPosition,
55 import { CommentNodes } from "../comment/comment-nodes";
56 import { BannerIconHeader } from "../common/banner-icon-header";
57 import { DataTypeSelect } from "../common/data-type-select";
58 import { HtmlTags } from "../common/html-tags";
59 import { Icon, Spinner } from "../common/icon";
60 import { Paginator } from "../common/paginator";
61 import { SortSelect } from "../common/sort-select";
62 import { Sidebar } from "../community/sidebar";
63 import { PostListings } from "../post/post-listings";
64 import { CommunityLink } from "./community-link";
67 communityRes: GetCommunityResponse;
68 siteRes: GetSiteResponse;
69 communityName: string;
70 communityLoading: boolean;
71 postsLoading: boolean;
72 commentsLoading: boolean;
74 comments: CommentView[];
78 showSidebarMobile: boolean;
81 interface CommunityProps {
93 export class Community extends Component<any, State> {
94 private isoData = setIsoData(this.context);
95 private subscription: Subscription;
96 private emptyState: State = {
97 communityRes: undefined,
98 communityName: this.props.match.params.name,
99 communityLoading: true,
101 commentsLoading: true,
104 dataType: getDataTypeFromProps(this.props),
105 sort: getSortTypeFromProps(this.props),
106 page: getPageFromProps(this.props),
107 siteRes: this.isoData.site_res,
108 showSidebarMobile: false,
111 constructor(props: any, context: any) {
112 super(props, context);
114 this.state = this.emptyState;
115 this.handleSortChange = this.handleSortChange.bind(this);
116 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
117 this.handlePageChange = this.handlePageChange.bind(this);
119 this.parseMessage = this.parseMessage.bind(this);
120 this.subscription = wsSubscribe(this.parseMessage);
122 // Only fetch the data if coming from another route
123 if (this.isoData.path == this.context.router.route.match.url) {
124 this.state.communityRes = this.isoData.routeData[0];
125 if (this.state.dataType == DataType.Post) {
126 this.state.posts = this.isoData.routeData[1].posts;
128 this.state.comments = this.isoData.routeData[1].comments;
130 this.state.communityLoading = false;
131 this.state.postsLoading = false;
132 this.state.commentsLoading = false;
134 this.fetchCommunity();
140 let form: GetCommunity = {
141 name: this.state.communityName ? this.state.communityName : null,
142 auth: authField(false),
144 WebSocketService.Instance.send(wsClient.getCommunity(form));
147 componentDidMount() {
151 componentWillUnmount() {
152 saveScrollPosition(this.context);
153 this.subscription.unsubscribe();
154 window.isoData.path = undefined;
157 static getDerivedStateFromProps(props: any): CommunityProps {
159 dataType: getDataTypeFromProps(props),
160 sort: getSortTypeFromProps(props),
161 page: getPageFromProps(props),
165 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
166 let pathSplit = req.path.split("/");
167 let promises: Promise<any>[] = [];
169 // It can be /c/main, or /c/1
170 let idOrName = pathSplit[2];
173 if (isNaN(Number(idOrName))) {
176 id = Number(idOrName);
179 let communityForm: GetCommunity = id ? { id } : { name: name_ };
180 setOptionalAuth(communityForm, req.auth);
181 promises.push(req.client.getCommunity(communityForm));
183 let dataType: DataType = pathSplit[4]
184 ? DataType[pathSplit[4]]
187 let sort: SortType = pathSplit[6]
188 ? SortType[pathSplit[6]]
189 : UserService.Instance.myUserInfo
190 ? Object.values(SortType)[
191 UserService.Instance.myUserInfo.local_user_view.local_user
196 let page = pathSplit[8] ? Number(pathSplit[8]) : 1;
198 if (dataType == DataType.Post) {
199 let getPostsForm: GetPosts = {
203 type_: ListingType.Community,
206 setOptionalAuth(getPostsForm, req.auth);
207 this.setName(getPostsForm, name_);
208 promises.push(req.client.getPosts(getPostsForm));
210 let getCommentsForm: GetComments = {
214 type_: ListingType.Community,
217 this.setName(getCommentsForm, name_);
218 setOptionalAuth(getCommentsForm, req.auth);
219 promises.push(req.client.getComments(getCommentsForm));
225 static setName(obj: any, name_: string) {
226 obj.community_name = name_;
229 componentDidUpdate(_: any, lastState: State) {
231 lastState.dataType !== this.state.dataType ||
232 lastState.sort !== this.state.sort ||
233 lastState.page !== this.state.page
235 this.setState({ postsLoading: true, commentsLoading: true });
240 get documentTitle(): string {
241 return `${this.state.communityRes.community_view.community.title} - ${this.state.siteRes.site_view.site.name}`;
245 let cv = this.state.communityRes?.community_view;
247 <div class="container">
248 {this.state.communityLoading ? (
255 title={this.documentTitle}
256 path={this.context.router.route.match.url}
257 description={cv.community.description}
258 image={cv.community.icon}
262 <div class="col-12 col-md-8">
263 {this.communityInfo()}
264 <div class="d-block d-md-none">
266 class="btn btn-secondary d-inline-block mb-2 mr-3"
267 onClick={linkEvent(this, this.handleShowSidebarMobile)}
269 {i18n.t("sidebar")}{" "}
272 this.state.showSidebarMobile
276 classes="icon-inline"
279 {this.state.showSidebarMobile && (
282 moderators={this.state.communityRes.moderators}
283 admins={this.state.siteRes.admins}
284 online={this.state.communityRes.online}
285 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
292 page={this.state.page}
293 onChange={this.handlePageChange}
296 <div class="d-none d-md-block col-md-4">
299 moderators={this.state.communityRes.moderators}
300 admins={this.state.siteRes.admins}
301 online={this.state.communityRes.online}
302 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
313 let site = this.state.siteRes.site_view.site;
314 return this.state.dataType == DataType.Post ? (
315 this.state.postsLoading ? (
321 posts={this.state.posts}
323 enableDownvotes={site.enable_downvotes}
324 enableNsfw={site.enable_nsfw}
327 ) : this.state.commentsLoading ? (
333 nodes={commentsToFlatNodes(this.state.comments)}
336 enableDownvotes={site.enable_downvotes}
342 let community = this.state.communityRes.community_view.community;
345 <BannerIconHeader banner={community.banner} icon={community.icon} />
346 <h5 class="mb-0 overflow-wrap-anywhere">{community.title}</h5>
348 community={community}
359 let communityRss = communityRSSUrl(
360 this.state.communityRes.community_view.community.actor_id,
367 type_={this.state.dataType}
368 onChange={this.handleDataTypeChange}
372 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
374 <a href={communityRss} title="RSS" rel={relTags}>
375 <Icon icon="rss" classes="text-muted small" />
377 <link rel="alternate" type="application/atom+xml" href={communityRss} />
382 handlePageChange(page: number) {
383 this.updateUrl({ page });
384 window.scrollTo(0, 0);
387 handleSortChange(val: SortType) {
388 this.updateUrl({ sort: val, page: 1 });
389 window.scrollTo(0, 0);
392 handleDataTypeChange(val: DataType) {
393 this.updateUrl({ dataType: DataType[val], page: 1 });
394 window.scrollTo(0, 0);
397 handleShowSidebarMobile(i: Community) {
398 i.state.showSidebarMobile = !i.state.showSidebarMobile;
402 updateUrl(paramUpdates: UrlParams) {
403 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
404 const sortStr = paramUpdates.sort || this.state.sort;
405 const page = paramUpdates.page || this.state.page;
407 let typeView = `/c/${this.state.communityName}`;
409 this.props.history.push(
410 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
415 if (this.state.dataType == DataType.Post) {
416 let form: GetPosts = {
417 page: this.state.page,
419 sort: this.state.sort,
420 type_: ListingType.Community,
421 community_name: this.state.communityName,
423 auth: authField(false),
425 WebSocketService.Instance.send(wsClient.getPosts(form));
427 let form: GetComments = {
428 page: this.state.page,
430 sort: this.state.sort,
431 type_: ListingType.Community,
432 community_name: this.state.communityName,
434 auth: authField(false),
436 WebSocketService.Instance.send(wsClient.getComments(form));
440 parseMessage(msg: any) {
441 let op = wsUserOp(msg);
444 toast(i18n.t(msg.error), "danger");
445 this.context.router.history.push("/");
447 } else if (msg.reconnect) {
448 WebSocketService.Instance.send(
449 wsClient.communityJoin({
450 community_id: this.state.communityRes.community_view.community.id,
454 } else if (op == UserOperation.GetCommunity) {
455 let data = wsJsonToRes<GetCommunityResponse>(msg).data;
456 this.state.communityRes = data;
457 this.state.communityLoading = false;
458 this.setState(this.state);
459 // TODO why is there no auth in this form?
460 WebSocketService.Instance.send(
461 wsClient.communityJoin({
462 community_id: data.community_view.community.id,
466 op == UserOperation.EditCommunity ||
467 op == UserOperation.DeleteCommunity ||
468 op == UserOperation.RemoveCommunity
470 let data = wsJsonToRes<CommunityResponse>(msg).data;
471 this.state.communityRes.community_view = data.community_view;
472 this.setState(this.state);
473 } else if (op == UserOperation.FollowCommunity) {
474 let data = wsJsonToRes<CommunityResponse>(msg).data;
475 this.state.communityRes.community_view.subscribed =
476 data.community_view.subscribed;
477 this.state.communityRes.community_view.counts.subscribers =
478 data.community_view.counts.subscribers;
479 this.setState(this.state);
480 } else if (op == UserOperation.GetPosts) {
481 let data = wsJsonToRes<GetPostsResponse>(msg).data;
482 this.state.posts = data.posts;
483 this.state.postsLoading = false;
484 this.setState(this.state);
485 restoreScrollPosition(this.context);
488 op == UserOperation.EditPost ||
489 op == UserOperation.DeletePost ||
490 op == UserOperation.RemovePost ||
491 op == UserOperation.LockPost ||
492 op == UserOperation.StickyPost ||
493 op == UserOperation.SavePost
495 let data = wsJsonToRes<PostResponse>(msg).data;
496 editPostFindRes(data.post_view, this.state.posts);
497 this.setState(this.state);
498 } else if (op == UserOperation.CreatePost) {
499 let data = wsJsonToRes<PostResponse>(msg).data;
500 this.state.posts.unshift(data.post_view);
502 UserService.Instance.myUserInfo?.local_user_view.local_user
503 .show_new_post_notifs
505 notifyPost(data.post_view, this.context.router);
507 this.setState(this.state);
508 } else if (op == UserOperation.CreatePostLike) {
509 let data = wsJsonToRes<PostResponse>(msg).data;
510 createPostLikeFindRes(data.post_view, this.state.posts);
511 this.setState(this.state);
512 } else if (op == UserOperation.AddModToCommunity) {
513 let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
514 this.state.communityRes.moderators = data.moderators;
515 this.setState(this.state);
516 } else if (op == UserOperation.BanFromCommunity) {
517 let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
519 // TODO this might be incorrect
521 .filter(p => p.creator.id == data.person_view.person.id)
522 .forEach(p => (p.creator_banned_from_community = data.banned));
524 this.setState(this.state);
525 } else if (op == UserOperation.GetComments) {
526 let data = wsJsonToRes<GetCommentsResponse>(msg).data;
527 this.state.comments = data.comments;
528 this.state.commentsLoading = false;
529 this.setState(this.state);
531 op == UserOperation.EditComment ||
532 op == UserOperation.DeleteComment ||
533 op == UserOperation.RemoveComment
535 let data = wsJsonToRes<CommentResponse>(msg).data;
536 editCommentRes(data.comment_view, this.state.comments);
537 this.setState(this.state);
538 } else if (op == UserOperation.CreateComment) {
539 let data = wsJsonToRes<CommentResponse>(msg).data;
541 // Necessary since it might be a user reply
543 this.state.comments.unshift(data.comment_view);
544 this.setState(this.state);
546 } else if (op == UserOperation.SaveComment) {
547 let data = wsJsonToRes<CommentResponse>(msg).data;
548 saveCommentRes(data.comment_view, this.state.comments);
549 this.setState(this.state);
550 } else if (op == UserOperation.CreateCommentLike) {
551 let data = wsJsonToRes<CommentResponse>(msg).data;
552 createCommentLikeRes(data.comment_view, this.state.comments);
553 this.setState(this.state);
554 } else if (op == UserOperation.BlockPerson) {
555 let data = wsJsonToRes<BlockPersonResponse>(msg).data;
556 updatePersonBlock(data);
557 } else if (op == UserOperation.CreatePostReport) {
558 let data = wsJsonToRes<PostReportResponse>(msg).data;
560 toast(i18n.t("report_created"));
562 } else if (op == UserOperation.CreateCommentReport) {
563 let data = wsJsonToRes<CommentReportResponse>(msg).data;
565 toast(i18n.t("report_created"));