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.localUserView
181 ? Object.values(SortType)[
182 UserService.Instance.localUserView.local_user.default_sort_type
186 let page = pathSplit[8] ? Number(pathSplit[8]) : 1;
188 if (dataType == DataType.Post) {
189 let getPostsForm: GetPosts = {
193 type_: ListingType.Community,
196 setOptionalAuth(getPostsForm, req.auth);
197 this.setIdOrName(getPostsForm, id, name_);
198 promises.push(req.client.getPosts(getPostsForm));
200 let getCommentsForm: GetComments = {
204 type_: ListingType.Community,
207 setOptionalAuth(getCommentsForm, req.auth);
208 this.setIdOrName(getCommentsForm, id, name_);
209 promises.push(req.client.getComments(getCommentsForm));
215 static setIdOrName(obj: any, id: number, name_: string) {
217 obj.community_id = id;
219 obj.community_name = name_;
223 componentDidUpdate(_: any, lastState: State) {
225 lastState.dataType !== this.state.dataType ||
226 lastState.sort !== this.state.sort ||
227 lastState.page !== this.state.page
229 this.setState({ postsLoading: true, commentsLoading: true });
234 get documentTitle(): string {
235 return `${this.state.communityRes.community_view.community.title} - ${this.state.siteRes.site_view.site.name}`;
239 let cv = this.state.communityRes?.community_view;
241 <div class="container">
242 {this.state.communityLoading ? (
248 <div class="col-12 col-md-8">
250 title={this.documentTitle}
251 path={this.context.router.route.match.url}
252 description={cv.community.description}
253 image={cv.community.icon}
255 {this.communityInfo()}
260 <div class="col-12 col-md-4">
263 moderators={this.state.communityRes.moderators}
264 admins={this.state.siteRes.admins}
265 online={this.state.communityRes.online}
266 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
276 let site = this.state.siteRes.site_view.site;
277 return this.state.dataType == DataType.Post ? (
278 this.state.postsLoading ? (
284 posts={this.state.posts}
286 enableDownvotes={site.enable_downvotes}
287 enableNsfw={site.enable_nsfw}
290 ) : this.state.commentsLoading ? (
296 nodes={commentsToFlatNodes(this.state.comments)}
299 enableDownvotes={site.enable_downvotes}
305 let community = this.state.communityRes.community_view.community;
308 <BannerIconHeader banner={community.banner} icon={community.icon} />
309 <h5 class="mb-0">{community.title}</h5>
311 community={community}
327 type_={this.state.dataType}
328 onChange={this.handleDataTypeChange}
332 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
335 href={communityRSSUrl(
336 this.state.communityRes.community_view.community.actor_id,
342 <Icon icon="rss" classes="text-muted small" />
351 {this.state.page > 1 && (
353 class="btn btn-secondary mr-1"
354 onClick={linkEvent(this, this.prevPage)}
359 {this.state.posts.length > 0 && (
361 class="btn btn-secondary"
362 onClick={linkEvent(this, this.nextPage)}
371 nextPage(i: Community) {
372 i.updateUrl({ page: i.state.page + 1 });
373 window.scrollTo(0, 0);
376 prevPage(i: Community) {
377 i.updateUrl({ page: i.state.page - 1 });
378 window.scrollTo(0, 0);
381 handleSortChange(val: SortType) {
382 this.updateUrl({ sort: val, page: 1 });
383 window.scrollTo(0, 0);
386 handleDataTypeChange(val: DataType) {
387 this.updateUrl({ dataType: DataType[val], page: 1 });
388 window.scrollTo(0, 0);
391 updateUrl(paramUpdates: UrlParams) {
392 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
393 const sortStr = paramUpdates.sort || this.state.sort;
394 const page = paramUpdates.page || this.state.page;
396 let typeView = this.state.communityName
397 ? `/c/${this.state.communityName}`
398 : `/community/${this.state.communityId}`;
400 this.props.history.push(
401 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
406 if (this.state.dataType == DataType.Post) {
407 let form: GetPosts = {
408 page: this.state.page,
410 sort: this.state.sort,
411 type_: ListingType.Community,
412 community_id: this.state.communityId,
413 community_name: this.state.communityName,
415 auth: authField(false),
417 WebSocketService.Instance.send(wsClient.getPosts(form));
419 let form: GetComments = {
420 page: this.state.page,
422 sort: this.state.sort,
423 type_: ListingType.Community,
424 community_id: this.state.communityId,
425 community_name: this.state.communityName,
427 auth: authField(false),
429 WebSocketService.Instance.send(wsClient.getComments(form));
433 parseMessage(msg: any) {
434 let op = wsUserOp(msg);
437 toast(i18n.t(msg.error), "danger");
438 this.context.router.history.push("/");
440 } else if (msg.reconnect) {
441 WebSocketService.Instance.send(
442 wsClient.communityJoin({
443 community_id: this.state.communityRes.community_view.community.id,
447 } else if (op == UserOperation.GetCommunity) {
448 let data = wsJsonToRes<GetCommunityResponse>(msg).data;
449 this.state.communityRes = data;
450 this.state.communityLoading = false;
451 this.setState(this.state);
452 // TODO why is there no auth in this form?
453 WebSocketService.Instance.send(
454 wsClient.communityJoin({
455 community_id: data.community_view.community.id,
459 op == UserOperation.EditCommunity ||
460 op == UserOperation.DeleteCommunity ||
461 op == UserOperation.RemoveCommunity
463 let data = wsJsonToRes<CommunityResponse>(msg).data;
464 this.state.communityRes.community_view = data.community_view;
465 this.setState(this.state);
466 } else if (op == UserOperation.FollowCommunity) {
467 let data = wsJsonToRes<CommunityResponse>(msg).data;
468 this.state.communityRes.community_view.subscribed =
469 data.community_view.subscribed;
470 this.state.communityRes.community_view.counts.subscribers =
471 data.community_view.counts.subscribers;
472 this.setState(this.state);
473 } else if (op == UserOperation.GetPosts) {
474 let data = wsJsonToRes<GetPostsResponse>(msg).data;
475 this.state.posts = data.posts;
476 this.state.postsLoading = false;
477 this.setState(this.state);
478 restoreScrollPosition(this.context);
481 op == UserOperation.EditPost ||
482 op == UserOperation.DeletePost ||
483 op == UserOperation.RemovePost ||
484 op == UserOperation.LockPost ||
485 op == UserOperation.StickyPost ||
486 op == UserOperation.SavePost
488 let data = wsJsonToRes<PostResponse>(msg).data;
489 editPostFindRes(data.post_view, this.state.posts);
490 this.setState(this.state);
491 } else if (op == UserOperation.CreatePost) {
492 let data = wsJsonToRes<PostResponse>(msg).data;
493 this.state.posts.unshift(data.post_view);
494 notifyPost(data.post_view, this.context.router);
495 this.setState(this.state);
496 } else if (op == UserOperation.CreatePostLike) {
497 let data = wsJsonToRes<PostResponse>(msg).data;
498 createPostLikeFindRes(data.post_view, this.state.posts);
499 this.setState(this.state);
500 } else if (op == UserOperation.AddModToCommunity) {
501 let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
502 this.state.communityRes.moderators = data.moderators;
503 this.setState(this.state);
504 } else if (op == UserOperation.BanFromCommunity) {
505 let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
507 // TODO this might be incorrect
509 .filter(p => p.creator.id == data.person_view.person.id)
510 .forEach(p => (p.creator_banned_from_community = data.banned));
512 this.setState(this.state);
513 } else if (op == UserOperation.GetComments) {
514 let data = wsJsonToRes<GetCommentsResponse>(msg).data;
515 this.state.comments = data.comments;
516 this.state.commentsLoading = false;
517 this.setState(this.state);
519 op == UserOperation.EditComment ||
520 op == UserOperation.DeleteComment ||
521 op == UserOperation.RemoveComment
523 let data = wsJsonToRes<CommentResponse>(msg).data;
524 editCommentRes(data.comment_view, this.state.comments);
525 this.setState(this.state);
526 } else if (op == UserOperation.CreateComment) {
527 let data = wsJsonToRes<CommentResponse>(msg).data;
529 // Necessary since it might be a user reply
531 this.state.comments.unshift(data.comment_view);
532 this.setState(this.state);
534 } else if (op == UserOperation.SaveComment) {
535 let data = wsJsonToRes<CommentResponse>(msg).data;
536 saveCommentRes(data.comment_view, this.state.comments);
537 this.setState(this.state);
538 } else if (op == UserOperation.CreateCommentLike) {
539 let data = wsJsonToRes<CommentResponse>(msg).data;
540 createCommentLikeRes(data.comment_view, this.state.comments);
541 this.setState(this.state);