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,
55 import { i18n } from '../i18next';
58 communityRes: GetCommunityResponse;
59 siteRes: GetSiteResponse;
61 communityName: string;
62 communityLoading: boolean;
63 postsLoading: boolean;
64 commentsLoading: boolean;
66 comments: CommentView[];
70 categories: Category[];
73 interface CommunityProps {
85 export class Community extends Component<any, State> {
86 private isoData = setIsoData(this.context);
87 private subscription: Subscription;
88 private emptyState: State = {
89 communityRes: undefined,
90 communityId: Number(this.props.match.params.id),
91 communityName: this.props.match.params.name,
92 communityLoading: true,
94 commentsLoading: true,
97 dataType: getDataTypeFromProps(this.props),
98 sort: getSortTypeFromProps(this.props),
99 page: getPageFromProps(this.props),
100 siteRes: this.isoData.site_res,
104 constructor(props: any, context: any) {
105 super(props, context);
107 this.state = this.emptyState;
108 this.handleSortChange = this.handleSortChange.bind(this);
109 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
111 this.parseMessage = this.parseMessage.bind(this);
112 this.subscription = wsSubscribe(this.parseMessage);
114 // Only fetch the data if coming from another route
115 if (this.isoData.path == this.context.router.route.match.url) {
116 this.state.communityRes = this.isoData.routeData[0];
117 if (this.state.dataType == DataType.Post) {
118 this.state.posts = this.isoData.routeData[1].posts;
120 this.state.comments = this.isoData.routeData[1].comments;
122 this.state.categories = this.isoData.routeData[2].categories;
123 this.state.communityLoading = false;
124 this.state.postsLoading = false;
125 this.state.commentsLoading = false;
127 this.fetchCommunity();
129 WebSocketService.Instance.client.listCategories();
135 let form: GetCommunity = {
136 id: this.state.communityId ? this.state.communityId : null,
137 name: this.state.communityName ? this.state.communityName : null,
138 auth: UserService.Instance.authField(false),
140 WebSocketService.Instance.client.getCommunity(form);
143 componentWillUnmount() {
145 this.subscription.unsubscribe();
146 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 communityForm.auth = 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.user
183 ? Object.values(SortType)[UserService.Instance.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 this.setIdOrName(getPostsForm, id, name_);
197 promises.push(req.client.getPosts(getPostsForm));
199 let getCommentsForm: GetComments = {
203 type_: ListingType.Community,
206 this.setIdOrName(getCommentsForm, id, name_);
207 promises.push(req.client.getComments(getCommentsForm));
210 promises.push(req.client.listCategories());
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 ? (
244 <svg class="icon icon-spinner spin">
245 <use xlinkHref="#icon-spinner"></use>
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()}
262 <div class="col-12 col-md-4">
265 moderators={this.state.communityRes.moderators}
266 admins={this.state.siteRes.admins}
267 online={this.state.communityRes.online}
268 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
269 categories={this.state.categories}
279 let site = this.state.siteRes.site_view.site;
280 return this.state.dataType == DataType.Post ? (
281 this.state.postsLoading ? (
283 <svg class="icon icon-spinner spin">
284 <use xlinkHref="#icon-spinner"></use>
289 posts={this.state.posts}
291 sort={this.state.sort}
292 enableDownvotes={site.enable_downvotes}
293 enableNsfw={site.enable_nsfw}
296 ) : this.state.commentsLoading ? (
298 <svg class="icon icon-spinner spin">
299 <use xlinkHref="#icon-spinner"></use>
304 nodes={commentsToFlatNodes(this.state.comments)}
306 sortType={this.state.sort}
308 enableDownvotes={site.enable_downvotes}
314 let community = this.state.communityRes.community_view.community;
317 <BannerIconHeader banner={community.banner} icon={community.icon} />
318 <h5 class="mb-0">{community.title}</h5>
320 community={community}
336 type_={this.state.dataType}
337 onChange={this.handleDataTypeChange}
341 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
344 href={communityRSSUrl(
345 this.state.communityRes.community_view.community.actor_id,
352 <svg class="icon text-muted small">
353 <use xlinkHref="#icon-rss">#</use>
363 {this.state.page > 1 && (
365 class="btn btn-secondary mr-1"
366 onClick={linkEvent(this, this.prevPage)}
371 {this.state.posts.length > 0 && (
373 class="btn btn-secondary"
374 onClick={linkEvent(this, this.nextPage)}
383 nextPage(i: Community) {
384 i.updateUrl({ page: i.state.page + 1 });
385 window.scrollTo(0, 0);
388 prevPage(i: Community) {
389 i.updateUrl({ page: i.state.page - 1 });
390 window.scrollTo(0, 0);
393 handleSortChange(val: SortType) {
394 this.updateUrl({ sort: val, page: 1 });
395 window.scrollTo(0, 0);
398 handleDataTypeChange(val: DataType) {
399 this.updateUrl({ dataType: DataType[val], page: 1 });
400 window.scrollTo(0, 0);
403 updateUrl(paramUpdates: UrlParams) {
404 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
405 const sortStr = paramUpdates.sort || this.state.sort;
406 const page = paramUpdates.page || this.state.page;
407 this.props.history.push(
408 `/c/${this.state.communityRes.community_view.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
413 if (this.state.dataType == DataType.Post) {
414 let form: GetPosts = {
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: UserService.Instance.authField(false),
423 WebSocketService.Instance.client.getPosts(form);
425 let form: GetComments = {
426 page: this.state.page,
428 sort: this.state.sort,
429 type_: ListingType.Community,
430 community_id: this.state.communityId,
431 community_name: this.state.communityName,
432 auth: UserService.Instance.authField(false),
434 WebSocketService.Instance.client.getComments(form);
438 parseMessage(msg: any) {
439 let op = wsUserOp(msg);
441 toast(i18n.t(msg.error), 'danger');
442 this.context.router.history.push('/');
444 } else if (msg.reconnect) {
445 WebSocketService.Instance.client.communityJoin({
446 community_id: this.state.communityRes.community_view.community.id,
449 } else if (op == UserOperation.GetCommunity) {
450 let data = wsJsonToRes<GetCommunityResponse>(msg).data;
451 this.state.communityRes = data;
452 this.state.communityLoading = false;
453 this.setState(this.state);
454 // TODO why is there no auth in this form?
455 WebSocketService.Instance.client.communityJoin({
456 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);
480 op == UserOperation.EditPost ||
481 op == UserOperation.DeletePost ||
482 op == UserOperation.RemovePost ||
483 op == UserOperation.LockPost ||
484 op == UserOperation.StickyPost ||
485 op == UserOperation.SavePost
487 let data = wsJsonToRes<PostResponse>(msg).data;
488 editPostFindRes(data.post_view, this.state.posts);
489 this.setState(this.state);
490 } else if (op == UserOperation.CreatePost) {
491 let data = wsJsonToRes<PostResponse>(msg).data;
492 this.state.posts.unshift(data.post_view);
493 notifyPost(data.post_view, this.context.router);
494 this.setState(this.state);
495 } else if (op == UserOperation.CreatePostLike) {
496 let data = wsJsonToRes<PostResponse>(msg).data;
497 createPostLikeFindRes(data.post_view, this.state.posts);
498 this.setState(this.state);
499 } else if (op == UserOperation.AddModToCommunity) {
500 let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
501 this.state.communityRes.moderators = data.moderators;
502 this.setState(this.state);
503 } else if (op == UserOperation.BanFromCommunity) {
504 let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
506 // TODO this might be incorrect
508 .filter(p => p.creator.id == data.user_view.user.id)
509 .forEach(p => (p.creator_banned_from_community = data.banned));
511 this.setState(this.state);
512 } else if (op == UserOperation.GetComments) {
513 let data = wsJsonToRes<GetCommentsResponse>(msg).data;
514 this.state.comments = data.comments;
515 this.state.commentsLoading = false;
516 this.setState(this.state);
518 op == UserOperation.EditComment ||
519 op == UserOperation.DeleteComment ||
520 op == UserOperation.RemoveComment
522 let data = wsJsonToRes<CommentResponse>(msg).data;
523 editCommentRes(data.comment_view, this.state.comments);
524 this.setState(this.state);
525 } else if (op == UserOperation.CreateComment) {
526 let data = wsJsonToRes<CommentResponse>(msg).data;
528 // Necessary since it might be a user reply
529 if (data.recipient_ids.length == 0) {
530 this.state.comments.unshift(data.comment_view);
531 this.setState(this.state);
533 } else if (op == UserOperation.SaveComment) {
534 let data = wsJsonToRes<CommentResponse>(msg).data;
535 saveCommentRes(data.comment_view, this.state.comments);
536 this.setState(this.state);
537 } else if (op == UserOperation.CreateCommentLike) {
538 let data = wsJsonToRes<CommentResponse>(msg).data;
539 createCommentLikeRes(data.comment_view, this.state.comments);
540 this.setState(this.state);
541 } else if (op == UserOperation.ListCategories) {
542 let data = wsJsonToRes<ListCategoriesResponse>(msg).data;
543 this.state.categories = data.categories;
544 this.setState(this.state);