1 import { Component, linkEvent } from "inferno";
2 import { Subscription } from "rxjs";
3 import { DataType, InitialFetchRequest } from "../interfaces";
15 AddModToCommunityResponse,
16 BanFromCommunityResponse,
22 } from "lemmy-js-client";
23 import { UserService, WebSocketService } from "../services";
24 import { PostListings } from "./post-listings";
25 import { CommentNodes } from "./comment-nodes";
26 import { HtmlTags } from "./html-tags";
27 import { SortSelect } from "./sort-select";
28 import { DataTypeSelect } from "./data-type-select";
29 import { Sidebar } from "./sidebar";
30 import { CommunityLink } from "./community-link";
31 import { BannerIconHeader } from "./banner-icon-header";
32 import { Icon, Spinner } from "./icon";
43 createPostLikeFindRes,
56 restoreScrollPosition,
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[];
75 interface CommunityProps {
87 export class Community extends Component<any, State> {
88 private isoData = setIsoData(this.context);
89 private subscription: Subscription;
90 private emptyState: State = {
91 communityRes: undefined,
92 communityId: Number(this.props.match.params.id),
93 communityName: this.props.match.params.name,
94 communityLoading: true,
96 commentsLoading: true,
99 dataType: getDataTypeFromProps(this.props),
100 sort: getSortTypeFromProps(this.props),
101 page: getPageFromProps(this.props),
102 siteRes: this.isoData.site_res,
105 constructor(props: any, context: any) {
106 super(props, context);
108 this.state = this.emptyState;
109 this.handleSortChange = this.handleSortChange.bind(this);
110 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
112 this.parseMessage = this.parseMessage.bind(this);
113 this.subscription = wsSubscribe(this.parseMessage);
115 // Only fetch the data if coming from another route
116 if (this.isoData.path == this.context.router.route.match.url) {
117 this.state.communityRes = this.isoData.routeData[0];
118 if (this.state.dataType == DataType.Post) {
119 this.state.posts = this.isoData.routeData[1].posts;
121 this.state.comments = this.isoData.routeData[1].comments;
123 this.state.communityLoading = false;
124 this.state.postsLoading = false;
125 this.state.commentsLoading = false;
127 this.fetchCommunity();
134 let form: GetCommunity = {
135 id: this.state.communityId ? this.state.communityId : null,
136 name: this.state.communityName ? this.state.communityName : null,
137 auth: authField(false),
139 WebSocketService.Instance.send(wsClient.getCommunity(form));
142 componentWillUnmount() {
143 saveScrollPosition(this.context);
144 this.subscription.unsubscribe();
145 window.isoData.path = undefined;
148 static getDerivedStateFromProps(props: any): CommunityProps {
150 dataType: getDataTypeFromProps(props),
151 sort: getSortTypeFromProps(props),
152 page: getPageFromProps(props),
156 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
157 let pathSplit = req.path.split("/");
158 let promises: Promise<any>[] = [];
160 // It can be /c/main, or /c/1
161 let idOrName = pathSplit[2];
164 if (isNaN(Number(idOrName))) {
167 id = Number(idOrName);
170 let communityForm: GetCommunity = id ? { id } : { name: name_ };
171 setOptionalAuth(communityForm, req.auth);
172 promises.push(req.client.getCommunity(communityForm));
174 let dataType: DataType = pathSplit[4]
175 ? DataType[pathSplit[4]]
178 let sort: SortType = pathSplit[6]
179 ? SortType[pathSplit[6]]
180 : UserService.Instance.user
181 ? Object.values(SortType)[UserService.Instance.user.default_sort_type]
184 let page = pathSplit[8] ? Number(pathSplit[8]) : 1;
186 if (dataType == DataType.Post) {
187 let getPostsForm: GetPosts = {
191 type_: ListingType.Community,
193 setOptionalAuth(getPostsForm, req.auth);
194 this.setIdOrName(getPostsForm, id, name_);
195 promises.push(req.client.getPosts(getPostsForm));
197 let getCommentsForm: GetComments = {
201 type_: ListingType.Community,
203 setOptionalAuth(getCommentsForm, req.auth);
204 this.setIdOrName(getCommentsForm, id, name_);
205 promises.push(req.client.getComments(getCommentsForm));
211 static setIdOrName(obj: any, id: number, name_: string) {
213 obj.community_id = id;
215 obj.community_name = name_;
219 componentDidUpdate(_: any, lastState: State) {
221 lastState.dataType !== this.state.dataType ||
222 lastState.sort !== this.state.sort ||
223 lastState.page !== this.state.page
225 this.setState({ postsLoading: true, commentsLoading: true });
230 get documentTitle(): string {
231 return `${this.state.communityRes.community_view.community.title} - ${this.state.siteRes.site_view.site.name}`;
235 let cv = this.state.communityRes?.community_view;
237 <div class="container">
238 {this.state.communityLoading ? (
244 <div class="col-12 col-md-8">
246 title={this.documentTitle}
247 path={this.context.router.route.match.url}
248 description={cv.community.description}
249 image={cv.community.icon}
251 {this.communityInfo()}
256 <div class="col-12 col-md-4">
259 moderators={this.state.communityRes.moderators}
260 admins={this.state.siteRes.admins}
261 online={this.state.communityRes.online}
262 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
272 let site = this.state.siteRes.site_view.site;
273 return this.state.dataType == DataType.Post ? (
274 this.state.postsLoading ? (
280 posts={this.state.posts}
282 enableDownvotes={site.enable_downvotes}
283 enableNsfw={site.enable_nsfw}
286 ) : this.state.commentsLoading ? (
292 nodes={commentsToFlatNodes(this.state.comments)}
295 enableDownvotes={site.enable_downvotes}
301 let community = this.state.communityRes.community_view.community;
304 <BannerIconHeader banner={community.banner} icon={community.icon} />
305 <h5 class="mb-0">{community.title}</h5>
307 community={community}
323 type_={this.state.dataType}
324 onChange={this.handleDataTypeChange}
328 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
331 href={communityRSSUrl(
332 this.state.communityRes.community_view.community.actor_id,
338 <Icon icon="rss" classes="text-muted small" />
347 {this.state.page > 1 && (
349 class="btn btn-secondary mr-1"
350 onClick={linkEvent(this, this.prevPage)}
355 {this.state.posts.length > 0 && (
357 class="btn btn-secondary"
358 onClick={linkEvent(this, this.nextPage)}
367 nextPage(i: Community) {
368 i.updateUrl({ page: i.state.page + 1 });
369 window.scrollTo(0, 0);
372 prevPage(i: Community) {
373 i.updateUrl({ page: i.state.page - 1 });
374 window.scrollTo(0, 0);
377 handleSortChange(val: SortType) {
378 this.updateUrl({ sort: val, page: 1 });
379 window.scrollTo(0, 0);
382 handleDataTypeChange(val: DataType) {
383 this.updateUrl({ dataType: DataType[val], page: 1 });
384 window.scrollTo(0, 0);
387 updateUrl(paramUpdates: UrlParams) {
388 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
389 const sortStr = paramUpdates.sort || this.state.sort;
390 const page = paramUpdates.page || this.state.page;
392 let typeView = this.state.communityName
393 ? `/c/${this.state.communityName}`
394 : `/community/${this.state.communityId}`;
396 this.props.history.push(
397 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
402 if (this.state.dataType == DataType.Post) {
403 let form: GetPosts = {
404 page: this.state.page,
406 sort: this.state.sort,
407 type_: ListingType.Community,
408 community_id: this.state.communityId,
409 community_name: this.state.communityName,
410 auth: authField(false),
412 WebSocketService.Instance.send(wsClient.getPosts(form));
414 let form: GetComments = {
415 page: this.state.page,
417 sort: this.state.sort,
418 type_: ListingType.Community,
419 community_id: this.state.communityId,
420 community_name: this.state.communityName,
421 auth: authField(false),
423 WebSocketService.Instance.send(wsClient.getComments(form));
427 parseMessage(msg: any) {
428 let op = wsUserOp(msg);
430 toast(i18n.t(msg.error), "danger");
431 this.context.router.history.push("/");
433 } else if (msg.reconnect) {
434 WebSocketService.Instance.send(
435 wsClient.communityJoin({
436 community_id: this.state.communityRes.community_view.community.id,
440 } else if (op == UserOperation.GetCommunity) {
441 let data = wsJsonToRes<GetCommunityResponse>(msg).data;
442 this.state.communityRes = data;
443 this.state.communityLoading = false;
444 this.setState(this.state);
445 // TODO why is there no auth in this form?
446 WebSocketService.Instance.send(
447 wsClient.communityJoin({
448 community_id: data.community_view.community.id,
452 op == UserOperation.EditCommunity ||
453 op == UserOperation.DeleteCommunity ||
454 op == UserOperation.RemoveCommunity
456 let data = wsJsonToRes<CommunityResponse>(msg).data;
457 this.state.communityRes.community_view = data.community_view;
458 this.setState(this.state);
459 } else if (op == UserOperation.FollowCommunity) {
460 let data = wsJsonToRes<CommunityResponse>(msg).data;
461 this.state.communityRes.community_view.subscribed =
462 data.community_view.subscribed;
463 this.state.communityRes.community_view.counts.subscribers =
464 data.community_view.counts.subscribers;
465 this.setState(this.state);
466 } else if (op == UserOperation.GetPosts) {
467 let data = wsJsonToRes<GetPostsResponse>(msg).data;
468 this.state.posts = data.posts;
469 this.state.postsLoading = false;
470 this.setState(this.state);
471 restoreScrollPosition(this.context);
474 op == UserOperation.EditPost ||
475 op == UserOperation.DeletePost ||
476 op == UserOperation.RemovePost ||
477 op == UserOperation.LockPost ||
478 op == UserOperation.StickyPost ||
479 op == UserOperation.SavePost
481 let data = wsJsonToRes<PostResponse>(msg).data;
482 editPostFindRes(data.post_view, this.state.posts);
483 this.setState(this.state);
484 } else if (op == UserOperation.CreatePost) {
485 let data = wsJsonToRes<PostResponse>(msg).data;
486 this.state.posts.unshift(data.post_view);
487 notifyPost(data.post_view, this.context.router);
488 this.setState(this.state);
489 } else if (op == UserOperation.CreatePostLike) {
490 let data = wsJsonToRes<PostResponse>(msg).data;
491 createPostLikeFindRes(data.post_view, this.state.posts);
492 this.setState(this.state);
493 } else if (op == UserOperation.AddModToCommunity) {
494 let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
495 this.state.communityRes.moderators = data.moderators;
496 this.setState(this.state);
497 } else if (op == UserOperation.BanFromCommunity) {
498 let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
500 // TODO this might be incorrect
502 .filter(p => p.creator.id == data.user_view.user.id)
503 .forEach(p => (p.creator_banned_from_community = data.banned));
505 this.setState(this.state);
506 } else if (op == UserOperation.GetComments) {
507 let data = wsJsonToRes<GetCommentsResponse>(msg).data;
508 this.state.comments = data.comments;
509 this.state.commentsLoading = false;
510 this.setState(this.state);
512 op == UserOperation.EditComment ||
513 op == UserOperation.DeleteComment ||
514 op == UserOperation.RemoveComment
516 let data = wsJsonToRes<CommentResponse>(msg).data;
517 editCommentRes(data.comment_view, this.state.comments);
518 this.setState(this.state);
519 } else if (op == UserOperation.CreateComment) {
520 let data = wsJsonToRes<CommentResponse>(msg).data;
522 // Necessary since it might be a user reply
524 this.state.comments.unshift(data.comment_view);
525 this.setState(this.state);
527 } else if (op == UserOperation.SaveComment) {
528 let data = wsJsonToRes<CommentResponse>(msg).data;
529 saveCommentRes(data.comment_view, this.state.comments);
530 this.setState(this.state);
531 } else if (op == UserOperation.CreateCommentLike) {
532 let data = wsJsonToRes<CommentResponse>(msg).data;
533 createCommentLikeRes(data.comment_view, this.state.comments);
534 this.setState(this.state);