1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { DataType, InitialFetchRequest } from '../interfaces';
15 AddModToCommunityResponse,
16 BanFromCommunityResponse,
23 ListCategoriesResponse,
24 } from 'lemmy-js-client';
25 import { UserService, WebSocketService } from '../services';
26 import { PostListings } from './post-listings';
27 import { CommentNodes } from './comment-nodes';
28 import { HtmlTags } from './html-tags';
29 import { SortSelect } from './sort-select';
30 import { DataTypeSelect } from './data-type-select';
31 import { Sidebar } from './sidebar';
32 import { CommunityLink } from './community-link';
33 import { BannerIconHeader } from './banner-icon-header';
44 createPostLikeFindRes,
58 import { i18n } from '../i18next';
61 communityRes: GetCommunityResponse;
62 siteRes: GetSiteResponse;
64 communityName: string;
65 communityLoading: boolean;
66 postsLoading: boolean;
67 commentsLoading: boolean;
69 comments: CommentView[];
73 categories: Category[];
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,
107 constructor(props: any, context: any) {
108 super(props, context);
110 this.state = this.emptyState;
111 this.handleSortChange = this.handleSortChange.bind(this);
112 this.handleDataTypeChange = this.handleDataTypeChange.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.categories = this.isoData.routeData[2].categories;
126 this.state.communityLoading = false;
127 this.state.postsLoading = false;
128 this.state.commentsLoading = false;
130 this.fetchCommunity();
132 WebSocketService.Instance.send(wsClient.listCategories());
138 let form: GetCommunity = {
139 id: this.state.communityId ? this.state.communityId : null,
140 name: this.state.communityName ? this.state.communityName : null,
141 auth: authField(false),
143 WebSocketService.Instance.send(wsClient.getCommunity(form));
146 componentWillUnmount() {
148 this.subscription.unsubscribe();
149 window.isoData.path = undefined;
153 static getDerivedStateFromProps(props: any): CommunityProps {
155 dataType: getDataTypeFromProps(props),
156 sort: getSortTypeFromProps(props),
157 page: getPageFromProps(props),
161 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
162 let pathSplit = req.path.split('/');
163 let promises: Promise<any>[] = [];
165 // It can be /c/main, or /c/1
166 let idOrName = pathSplit[2];
169 if (isNaN(Number(idOrName))) {
172 id = Number(idOrName);
175 let communityForm: GetCommunity = id ? { id } : { name: name_ };
176 setOptionalAuth(communityForm, req.auth);
177 promises.push(req.client.getCommunity(communityForm));
179 let dataType: DataType = pathSplit[4]
180 ? DataType[pathSplit[4]]
183 let sort: SortType = pathSplit[6]
184 ? SortType[pathSplit[6]]
185 : UserService.Instance.user
186 ? Object.values(SortType)[UserService.Instance.user.default_sort_type]
189 let page = pathSplit[8] ? Number(pathSplit[8]) : 1;
191 if (dataType == DataType.Post) {
192 let getPostsForm: GetPosts = {
196 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,
208 setOptionalAuth(getCommentsForm, req.auth);
209 this.setIdOrName(getCommentsForm, id, name_);
210 promises.push(req.client.getComments(getCommentsForm));
213 promises.push(req.client.listCategories());
218 static setIdOrName(obj: any, id: number, name_: string) {
220 obj.community_id = id;
222 obj.community_name = name_;
226 componentDidUpdate(_: any, lastState: State) {
228 lastState.dataType !== this.state.dataType ||
229 lastState.sort !== this.state.sort ||
230 lastState.page !== this.state.page
232 this.setState({ postsLoading: true, commentsLoading: true });
237 get documentTitle(): string {
238 return `${this.state.communityRes.community_view.community.title} - ${this.state.siteRes.site_view.site.name}`;
242 let cv = this.state.communityRes?.community_view;
244 <div class="container">
245 {this.state.communityLoading ? (
247 <svg class="icon icon-spinner spin">
248 <use xlinkHref="#icon-spinner"></use>
253 <div class="col-12 col-md-8">
255 title={this.documentTitle}
256 path={this.context.router.route.match.url}
257 description={cv.community.description}
258 image={cv.community.icon}
260 {this.communityInfo()}
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}
272 categories={this.state.categories}
282 let site = this.state.siteRes.site_view.site;
283 return this.state.dataType == DataType.Post ? (
284 this.state.postsLoading ? (
286 <svg class="icon icon-spinner spin">
287 <use xlinkHref="#icon-spinner"></use>
292 posts={this.state.posts}
294 sort={this.state.sort}
295 enableDownvotes={site.enable_downvotes}
296 enableNsfw={site.enable_nsfw}
299 ) : this.state.commentsLoading ? (
301 <svg class="icon icon-spinner spin">
302 <use xlinkHref="#icon-spinner"></use>
307 nodes={commentsToFlatNodes(this.state.comments)}
309 sortType={this.state.sort}
311 enableDownvotes={site.enable_downvotes}
317 let community = this.state.communityRes.community_view.community;
320 <BannerIconHeader banner={community.banner} icon={community.icon} />
321 <h5 class="mb-0">{community.title}</h5>
323 community={community}
339 type_={this.state.dataType}
340 onChange={this.handleDataTypeChange}
344 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
347 href={communityRSSUrl(
348 this.state.communityRes.community_view.community.actor_id,
355 <svg class="icon text-muted small">
356 <use xlinkHref="#icon-rss">#</use>
366 {this.state.page > 1 && (
368 class="btn btn-secondary mr-1"
369 onClick={linkEvent(this, this.prevPage)}
374 {this.state.posts.length > 0 && (
376 class="btn btn-secondary"
377 onClick={linkEvent(this, this.nextPage)}
386 nextPage(i: Community) {
387 i.updateUrl({ page: i.state.page + 1 });
388 window.scrollTo(0, 0);
391 prevPage(i: Community) {
392 i.updateUrl({ page: i.state.page - 1 });
393 window.scrollTo(0, 0);
396 handleSortChange(val: SortType) {
397 this.updateUrl({ sort: val, page: 1 });
398 window.scrollTo(0, 0);
401 handleDataTypeChange(val: DataType) {
402 this.updateUrl({ dataType: DataType[val], page: 1 });
403 window.scrollTo(0, 0);
406 updateUrl(paramUpdates: UrlParams) {
407 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
408 const sortStr = paramUpdates.sort || this.state.sort;
409 const page = paramUpdates.page || this.state.page;
410 this.props.history.push(
411 `/c/${this.state.communityRes.community_view.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
416 if (this.state.dataType == DataType.Post) {
417 let form: GetPosts = {
418 page: this.state.page,
420 sort: this.state.sort,
421 type_: ListingType.Community,
422 community_id: this.state.communityId,
423 community_name: this.state.communityName,
424 auth: authField(false),
426 WebSocketService.Instance.send(wsClient.getPosts(form));
428 let form: GetComments = {
429 page: this.state.page,
431 sort: this.state.sort,
432 type_: ListingType.Community,
433 community_id: this.state.communityId,
434 community_name: this.state.communityName,
435 auth: authField(false),
437 WebSocketService.Instance.send(wsClient.getComments(form));
441 parseMessage(msg: any) {
442 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);
487 op == UserOperation.EditPost ||
488 op == UserOperation.DeletePost ||
489 op == UserOperation.RemovePost ||
490 op == UserOperation.LockPost ||
491 op == UserOperation.StickyPost ||
492 op == UserOperation.SavePost
494 let data = wsJsonToRes<PostResponse>(msg).data;
495 editPostFindRes(data.post_view, this.state.posts);
496 this.setState(this.state);
497 } else if (op == UserOperation.CreatePost) {
498 let data = wsJsonToRes<PostResponse>(msg).data;
499 this.state.posts.unshift(data.post_view);
500 notifyPost(data.post_view, this.context.router);
501 this.setState(this.state);
502 } else if (op == UserOperation.CreatePostLike) {
503 let data = wsJsonToRes<PostResponse>(msg).data;
504 createPostLikeFindRes(data.post_view, this.state.posts);
505 this.setState(this.state);
506 } else if (op == UserOperation.AddModToCommunity) {
507 let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
508 this.state.communityRes.moderators = data.moderators;
509 this.setState(this.state);
510 } else if (op == UserOperation.BanFromCommunity) {
511 let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
513 // TODO this might be incorrect
515 .filter(p => p.creator.id == data.user_view.user.id)
516 .forEach(p => (p.creator_banned_from_community = data.banned));
518 this.setState(this.state);
519 } else if (op == UserOperation.GetComments) {
520 let data = wsJsonToRes<GetCommentsResponse>(msg).data;
521 this.state.comments = data.comments;
522 this.state.commentsLoading = false;
523 this.setState(this.state);
525 op == UserOperation.EditComment ||
526 op == UserOperation.DeleteComment ||
527 op == UserOperation.RemoveComment
529 let data = wsJsonToRes<CommentResponse>(msg).data;
530 editCommentRes(data.comment_view, this.state.comments);
531 this.setState(this.state);
532 } else if (op == UserOperation.CreateComment) {
533 let data = wsJsonToRes<CommentResponse>(msg).data;
535 // Necessary since it might be a user reply
536 if (data.recipient_ids.length == 0) {
537 this.state.comments.unshift(data.comment_view);
538 this.setState(this.state);
540 } else if (op == UserOperation.SaveComment) {
541 let data = wsJsonToRes<CommentResponse>(msg).data;
542 saveCommentRes(data.comment_view, this.state.comments);
543 this.setState(this.state);
544 } else if (op == UserOperation.CreateCommentLike) {
545 let data = wsJsonToRes<CommentResponse>(msg).data;
546 createCommentLikeRes(data.comment_view, this.state.comments);
547 this.setState(this.state);
548 } else if (op == UserOperation.ListCategories) {
549 let data = wsJsonToRes<ListCategoriesResponse>(msg).data;
550 this.state.categories = data.categories;
551 this.setState(this.state);