1 import { Component } from "inferno";
3 AddModToCommunityResponse,
4 BanFromCommunityResponse,
20 } from "lemmy-js-client";
21 import { Subscription } from "rxjs";
22 import { i18n } from "../../i18next";
23 import { DataType, InitialFetchRequest } from "../../interfaces";
24 import { UserService, WebSocketService } from "../../services";
30 createPostLikeFindRes,
38 restoreScrollPosition,
50 import { CommentNodes } from "../comment/comment-nodes";
51 import { BannerIconHeader } from "../common/banner-icon-header";
52 import { DataTypeSelect } from "../common/data-type-select";
53 import { HtmlTags } from "../common/html-tags";
54 import { Icon, Spinner } from "../common/icon";
55 import { Paginator } from "../common/paginator";
56 import { SortSelect } from "../common/sort-select";
57 import { Sidebar } from "../community/sidebar";
58 import { PostListings } from "../post/post-listings";
59 import { CommunityLink } from "./community-link";
62 communityRes: GetCommunityResponse;
63 siteRes: GetSiteResponse;
65 communityName: string;
66 communityLoading: boolean;
67 postsLoading: boolean;
68 commentsLoading: boolean;
70 comments: CommentView[];
76 interface CommunityProps {
88 export class Community extends Component<any, State> {
89 private isoData = setIsoData(this.context);
90 private subscription: Subscription;
91 private emptyState: State = {
92 communityRes: undefined,
93 communityId: Number(this.props.match.params.id),
94 communityName: this.props.match.params.name,
95 communityLoading: true,
97 commentsLoading: true,
100 dataType: getDataTypeFromProps(this.props),
101 sort: getSortTypeFromProps(this.props),
102 page: getPageFromProps(this.props),
103 siteRes: this.isoData.site_res,
106 constructor(props: any, context: any) {
107 super(props, context);
109 this.state = this.emptyState;
110 this.handleSortChange = this.handleSortChange.bind(this);
111 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
112 this.handlePageChange = this.handlePageChange.bind(this);
114 this.parseMessage = this.parseMessage.bind(this);
115 this.subscription = wsSubscribe(this.parseMessage);
117 // Only fetch the data if coming from another route
118 if (this.isoData.path == this.context.router.route.match.url) {
119 this.state.communityRes = this.isoData.routeData[0];
120 if (this.state.dataType == DataType.Post) {
121 this.state.posts = this.isoData.routeData[1].posts;
123 this.state.comments = this.isoData.routeData[1].comments;
125 this.state.communityLoading = false;
126 this.state.postsLoading = false;
127 this.state.commentsLoading = false;
129 this.fetchCommunity();
136 let form: GetCommunity = {
137 id: this.state.communityId ? this.state.communityId : null,
138 name: this.state.communityName ? this.state.communityName : null,
139 auth: authField(false),
141 WebSocketService.Instance.send(wsClient.getCommunity(form));
144 componentWillUnmount() {
145 saveScrollPosition(this.context);
146 this.subscription.unsubscribe();
147 window.isoData.path = undefined;
150 static getDerivedStateFromProps(props: any): CommunityProps {
152 dataType: getDataTypeFromProps(props),
153 sort: getSortTypeFromProps(props),
154 page: getPageFromProps(props),
158 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
159 let pathSplit = req.path.split("/");
160 let promises: Promise<any>[] = [];
162 // It can be /c/main, or /c/1
163 let idOrName = pathSplit[2];
166 if (isNaN(Number(idOrName))) {
169 id = Number(idOrName);
172 let communityForm: GetCommunity = id ? { id } : { name: name_ };
173 setOptionalAuth(communityForm, req.auth);
174 promises.push(req.client.getCommunity(communityForm));
176 let dataType: DataType = pathSplit[4]
177 ? DataType[pathSplit[4]]
180 let sort: SortType = pathSplit[6]
181 ? SortType[pathSplit[6]]
182 : UserService.Instance.localUserView
183 ? Object.values(SortType)[
184 UserService.Instance.localUserView.local_user.default_sort_type
188 let page = pathSplit[8] ? Number(pathSplit[8]) : 1;
190 if (dataType == DataType.Post) {
191 let getPostsForm: GetPosts = {
195 type_: ListingType.Community,
198 setOptionalAuth(getPostsForm, req.auth);
199 this.setIdOrName(getPostsForm, id, name_);
200 promises.push(req.client.getPosts(getPostsForm));
202 let getCommentsForm: GetComments = {
206 type_: ListingType.Community,
209 setOptionalAuth(getCommentsForm, req.auth);
210 this.setIdOrName(getCommentsForm, id, name_);
211 promises.push(req.client.getComments(getCommentsForm));
217 static setIdOrName(obj: any, id: number, name_: string) {
219 obj.community_id = id;
221 obj.community_name = name_;
225 componentDidUpdate(_: any, lastState: State) {
227 lastState.dataType !== this.state.dataType ||
228 lastState.sort !== this.state.sort ||
229 lastState.page !== this.state.page
231 this.setState({ postsLoading: true, commentsLoading: true });
236 get documentTitle(): string {
237 return `${this.state.communityRes.community_view.community.title} - ${this.state.siteRes.site_view.site.name}`;
241 let cv = this.state.communityRes?.community_view;
243 <div class="container">
244 {this.state.communityLoading ? (
250 <div class="col-12 col-md-8">
252 title={this.documentTitle}
253 path={this.context.router.route.match.url}
254 description={cv.community.description}
255 image={cv.community.icon}
257 {this.communityInfo()}
261 page={this.state.page}
262 onChange={this.handlePageChange}
265 <div class="col-12 col-md-4">
268 moderators={this.state.communityRes.moderators}
269 admins={this.state.siteRes.admins}
270 online={this.state.communityRes.online}
271 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
281 let site = this.state.siteRes.site_view.site;
282 return this.state.dataType == DataType.Post ? (
283 this.state.postsLoading ? (
289 posts={this.state.posts}
291 enableDownvotes={site.enable_downvotes}
292 enableNsfw={site.enable_nsfw}
295 ) : this.state.commentsLoading ? (
301 nodes={commentsToFlatNodes(this.state.comments)}
304 enableDownvotes={site.enable_downvotes}
310 let community = this.state.communityRes.community_view.community;
313 <BannerIconHeader banner={community.banner} icon={community.icon} />
314 <h5 class="mb-0">{community.title}</h5>
316 community={community}
332 type_={this.state.dataType}
333 onChange={this.handleDataTypeChange}
337 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
340 href={communityRSSUrl(
341 this.state.communityRes.community_view.community.actor_id,
347 <Icon icon="rss" classes="text-muted small" />
353 handlePageChange(page: number) {
354 this.updateUrl({ page });
355 window.scrollTo(0, 0);
358 handleSortChange(val: SortType) {
359 this.updateUrl({ sort: val, page: 1 });
360 window.scrollTo(0, 0);
363 handleDataTypeChange(val: DataType) {
364 this.updateUrl({ dataType: DataType[val], page: 1 });
365 window.scrollTo(0, 0);
368 updateUrl(paramUpdates: UrlParams) {
369 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
370 const sortStr = paramUpdates.sort || this.state.sort;
371 const page = paramUpdates.page || this.state.page;
373 let typeView = this.state.communityName
374 ? `/c/${this.state.communityName}`
375 : `/community/${this.state.communityId}`;
377 this.props.history.push(
378 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
383 if (this.state.dataType == DataType.Post) {
384 let form: GetPosts = {
385 page: this.state.page,
387 sort: this.state.sort,
388 type_: ListingType.Community,
389 community_id: this.state.communityId,
390 community_name: this.state.communityName,
392 auth: authField(false),
394 WebSocketService.Instance.send(wsClient.getPosts(form));
396 let form: GetComments = {
397 page: this.state.page,
399 sort: this.state.sort,
400 type_: ListingType.Community,
401 community_id: this.state.communityId,
402 community_name: this.state.communityName,
404 auth: authField(false),
406 WebSocketService.Instance.send(wsClient.getComments(form));
410 parseMessage(msg: any) {
411 let op = wsUserOp(msg);
414 toast(i18n.t(msg.error), "danger");
415 this.context.router.history.push("/");
417 } else if (msg.reconnect) {
418 WebSocketService.Instance.send(
419 wsClient.communityJoin({
420 community_id: this.state.communityRes.community_view.community.id,
424 } else if (op == UserOperation.GetCommunity) {
425 let data = wsJsonToRes<GetCommunityResponse>(msg).data;
426 this.state.communityRes = data;
427 this.state.communityLoading = false;
428 this.setState(this.state);
429 // TODO why is there no auth in this form?
430 WebSocketService.Instance.send(
431 wsClient.communityJoin({
432 community_id: data.community_view.community.id,
436 op == UserOperation.EditCommunity ||
437 op == UserOperation.DeleteCommunity ||
438 op == UserOperation.RemoveCommunity
440 let data = wsJsonToRes<CommunityResponse>(msg).data;
441 this.state.communityRes.community_view = data.community_view;
442 this.setState(this.state);
443 } else if (op == UserOperation.FollowCommunity) {
444 let data = wsJsonToRes<CommunityResponse>(msg).data;
445 this.state.communityRes.community_view.subscribed =
446 data.community_view.subscribed;
447 this.state.communityRes.community_view.counts.subscribers =
448 data.community_view.counts.subscribers;
449 this.setState(this.state);
450 } else if (op == UserOperation.GetPosts) {
451 let data = wsJsonToRes<GetPostsResponse>(msg).data;
452 this.state.posts = data.posts;
453 this.state.postsLoading = false;
454 this.setState(this.state);
455 restoreScrollPosition(this.context);
458 op == UserOperation.EditPost ||
459 op == UserOperation.DeletePost ||
460 op == UserOperation.RemovePost ||
461 op == UserOperation.LockPost ||
462 op == UserOperation.StickyPost ||
463 op == UserOperation.SavePost
465 let data = wsJsonToRes<PostResponse>(msg).data;
466 editPostFindRes(data.post_view, this.state.posts);
467 this.setState(this.state);
468 } else if (op == UserOperation.CreatePost) {
469 let data = wsJsonToRes<PostResponse>(msg).data;
470 this.state.posts.unshift(data.post_view);
471 notifyPost(data.post_view, this.context.router);
472 this.setState(this.state);
473 } else if (op == UserOperation.CreatePostLike) {
474 let data = wsJsonToRes<PostResponse>(msg).data;
475 createPostLikeFindRes(data.post_view, this.state.posts);
476 this.setState(this.state);
477 } else if (op == UserOperation.AddModToCommunity) {
478 let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
479 this.state.communityRes.moderators = data.moderators;
480 this.setState(this.state);
481 } else if (op == UserOperation.BanFromCommunity) {
482 let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
484 // TODO this might be incorrect
486 .filter(p => p.creator.id == data.person_view.person.id)
487 .forEach(p => (p.creator_banned_from_community = data.banned));
489 this.setState(this.state);
490 } else if (op == UserOperation.GetComments) {
491 let data = wsJsonToRes<GetCommentsResponse>(msg).data;
492 this.state.comments = data.comments;
493 this.state.commentsLoading = false;
494 this.setState(this.state);
496 op == UserOperation.EditComment ||
497 op == UserOperation.DeleteComment ||
498 op == UserOperation.RemoveComment
500 let data = wsJsonToRes<CommentResponse>(msg).data;
501 editCommentRes(data.comment_view, this.state.comments);
502 this.setState(this.state);
503 } else if (op == UserOperation.CreateComment) {
504 let data = wsJsonToRes<CommentResponse>(msg).data;
506 // Necessary since it might be a user reply
508 this.state.comments.unshift(data.comment_view);
509 this.setState(this.state);
511 } else if (op == UserOperation.SaveComment) {
512 let data = wsJsonToRes<CommentResponse>(msg).data;
513 saveCommentRes(data.comment_view, this.state.comments);
514 this.setState(this.state);
515 } else if (op == UserOperation.CreateCommentLike) {
516 let data = wsJsonToRes<CommentResponse>(msg).data;
517 createCommentLikeRes(data.comment_view, this.state.comments);
518 this.setState(this.state);