]> Untitled Git - lemmy.git/blob - ui/src/components/community.tsx
Merge branch 'ban_user_bug'
[lemmy.git] / ui / src / components / community.tsx
1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import {
5   UserOperation,
6   Community as CommunityI,
7   GetCommunityResponse,
8   CommunityResponse,
9   CommunityUser,
10   UserView,
11   SortType,
12   Post,
13   GetPostsForm,
14   GetCommunityForm,
15   ListingType,
16   DataType,
17   GetPostsResponse,
18   PostResponse,
19   AddModToCommunityResponse,
20   BanFromCommunityResponse,
21   Comment,
22   GetCommentsForm,
23   GetCommentsResponse,
24   CommentResponse,
25   WebSocketJsonResponse,
26 } from '../interfaces';
27 import { WebSocketService } from '../services';
28 import { PostListings } from './post-listings';
29 import { CommentNodes } from './comment-nodes';
30 import { SortSelect } from './sort-select';
31 import { DataTypeSelect } from './data-type-select';
32 import { Sidebar } from './sidebar';
33 import {
34   wsJsonToRes,
35   fetchLimit,
36   toast,
37   getPageFromProps,
38   getSortTypeFromProps,
39   getDataTypeFromProps,
40   editCommentRes,
41   saveCommentRes,
42   createCommentLikeRes,
43   createPostLikeFindRes,
44   editPostFindRes,
45   commentsToFlatNodes,
46   setupTippy,
47 } from '../utils';
48 import { i18n } from '../i18next';
49
50 interface State {
51   community: CommunityI;
52   communityId: number;
53   communityName: string;
54   moderators: Array<CommunityUser>;
55   admins: Array<UserView>;
56   online: number;
57   loading: boolean;
58   posts: Array<Post>;
59   comments: Array<Comment>;
60   dataType: DataType;
61   sort: SortType;
62   page: number;
63 }
64
65 export class Community extends Component<any, State> {
66   private subscription: Subscription;
67   private emptyState: State = {
68     community: {
69       id: null,
70       name: null,
71       title: null,
72       category_id: null,
73       category_name: null,
74       creator_id: null,
75       creator_name: null,
76       number_of_subscribers: null,
77       number_of_posts: null,
78       number_of_comments: null,
79       published: null,
80       removed: null,
81       nsfw: false,
82       deleted: null,
83       local: null,
84       actor_id: null,
85       last_refreshed_at: null,
86       creator_actor_id: null,
87       creator_local: null,
88     },
89     moderators: [],
90     admins: [],
91     communityId: Number(this.props.match.params.id),
92     communityName: this.props.match.params.name,
93     online: null,
94     loading: true,
95     posts: [],
96     comments: [],
97     dataType: getDataTypeFromProps(this.props),
98     sort: getSortTypeFromProps(this.props),
99     page: getPageFromProps(this.props),
100   };
101
102   constructor(props: any, context: any) {
103     super(props, context);
104
105     this.state = this.emptyState;
106     this.handleSortChange = this.handleSortChange.bind(this);
107     this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
108
109     this.subscription = WebSocketService.Instance.subject
110       .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
111       .subscribe(
112         msg => this.parseMessage(msg),
113         err => console.error(err),
114         () => console.log('complete')
115       );
116
117     let form: GetCommunityForm = {
118       id: this.state.communityId ? this.state.communityId : null,
119       name: this.state.communityName ? this.state.communityName : null,
120     };
121     WebSocketService.Instance.getCommunity(form);
122   }
123
124   componentWillUnmount() {
125     this.subscription.unsubscribe();
126   }
127
128   // Necessary for back button for some reason
129   componentWillReceiveProps(nextProps: any) {
130     if (
131       nextProps.history.action == 'POP' ||
132       nextProps.history.action == 'PUSH'
133     ) {
134       this.state.dataType = getDataTypeFromProps(nextProps);
135       this.state.sort = getSortTypeFromProps(nextProps);
136       this.state.page = getPageFromProps(nextProps);
137       this.setState(this.state);
138       this.fetchData();
139     }
140   }
141
142   render() {
143     return (
144       <div class="container">
145         {this.selects()}
146         {this.state.loading ? (
147           <h5>
148             <svg class="icon icon-spinner spin">
149               <use xlinkHref="#icon-spinner"></use>
150             </svg>
151           </h5>
152         ) : (
153           <div class="row">
154             <div class="col-12 col-md-8">
155               <h5>
156                 {this.state.community.title}
157                 {this.state.community.removed && (
158                   <small className="ml-2 text-muted font-italic">
159                     {i18n.t('removed')}
160                   </small>
161                 )}
162                 {this.state.community.nsfw && (
163                   <small className="ml-2 text-muted font-italic">
164                     {i18n.t('nsfw')}
165                   </small>
166                 )}
167               </h5>
168               {this.listings()}
169               {this.paginator()}
170             </div>
171             <div class="col-12 col-md-4">
172               <Sidebar
173                 community={this.state.community}
174                 moderators={this.state.moderators}
175                 admins={this.state.admins}
176                 online={this.state.online}
177               />
178             </div>
179           </div>
180         )}
181       </div>
182     );
183   }
184
185   listings() {
186     return this.state.dataType == DataType.Post ? (
187       <PostListings
188         posts={this.state.posts}
189         removeDuplicates
190         sort={this.state.sort}
191       />
192     ) : (
193       <CommentNodes
194         nodes={commentsToFlatNodes(this.state.comments)}
195         noIndent
196         sortType={this.state.sort}
197         showContext
198       />
199     );
200   }
201
202   selects() {
203     return (
204       <div class="mb-3">
205         <span class="mr-3">
206           <DataTypeSelect
207             type_={this.state.dataType}
208             onChange={this.handleDataTypeChange}
209           />
210         </span>
211         <span class="mr-2">
212           <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
213         </span>
214         <a
215           href={`/feeds/c/${this.state.communityName}.xml?sort=${
216             SortType[this.state.sort]
217           }`}
218           target="_blank"
219           title="RSS"
220           rel="noopener"
221         >
222           <svg class="icon text-muted small">
223             <use xlinkHref="#icon-rss">#</use>
224           </svg>
225         </a>
226       </div>
227     );
228   }
229
230   paginator() {
231     return (
232       <div class="my-2">
233         {this.state.page > 1 && (
234           <button
235             class="btn btn-sm btn-secondary mr-1"
236             onClick={linkEvent(this, this.prevPage)}
237           >
238             {i18n.t('prev')}
239           </button>
240         )}
241         {this.state.posts.length == fetchLimit && (
242           <button
243             class="btn btn-sm btn-secondary"
244             onClick={linkEvent(this, this.nextPage)}
245           >
246             {i18n.t('next')}
247           </button>
248         )}
249       </div>
250     );
251   }
252
253   nextPage(i: Community) {
254     i.state.page++;
255     i.setState(i.state);
256     i.updateUrl();
257     i.fetchData();
258     window.scrollTo(0, 0);
259   }
260
261   prevPage(i: Community) {
262     i.state.page--;
263     i.setState(i.state);
264     i.updateUrl();
265     i.fetchData();
266     window.scrollTo(0, 0);
267   }
268
269   handleSortChange(val: SortType) {
270     this.state.sort = val;
271     this.state.page = 1;
272     this.state.loading = true;
273     this.setState(this.state);
274     this.updateUrl();
275     this.fetchData();
276     window.scrollTo(0, 0);
277   }
278
279   handleDataTypeChange(val: DataType) {
280     this.state.dataType = val;
281     this.state.page = 1;
282     this.state.loading = true;
283     this.setState(this.state);
284     this.updateUrl();
285     this.fetchData();
286     window.scrollTo(0, 0);
287   }
288
289   updateUrl() {
290     let dataTypeStr = DataType[this.state.dataType].toLowerCase();
291     let sortStr = SortType[this.state.sort].toLowerCase();
292     this.props.history.push(
293       `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${this.state.page}`
294     );
295   }
296
297   fetchData() {
298     if (this.state.dataType == DataType.Post) {
299       let getPostsForm: GetPostsForm = {
300         page: this.state.page,
301         limit: fetchLimit,
302         sort: SortType[this.state.sort],
303         type_: ListingType[ListingType.Community],
304         community_id: this.state.community.id,
305       };
306       WebSocketService.Instance.getPosts(getPostsForm);
307     } else {
308       let getCommentsForm: GetCommentsForm = {
309         page: this.state.page,
310         limit: fetchLimit,
311         sort: SortType[this.state.sort],
312         type_: ListingType[ListingType.Community],
313         community_id: this.state.community.id,
314       };
315       WebSocketService.Instance.getComments(getCommentsForm);
316     }
317   }
318
319   parseMessage(msg: WebSocketJsonResponse) {
320     console.log(msg);
321     let res = wsJsonToRes(msg);
322     if (msg.error) {
323       toast(i18n.t(msg.error), 'danger');
324       this.context.router.history.push('/');
325       return;
326     } else if (msg.reconnect) {
327       this.fetchData();
328     } else if (res.op == UserOperation.GetCommunity) {
329       let data = res.data as GetCommunityResponse;
330       this.state.community = data.community;
331       this.state.moderators = data.moderators;
332       this.state.admins = data.admins;
333       this.state.online = data.online;
334       document.title = `/c/${this.state.community.name} - ${WebSocketService.Instance.site.name}`;
335       this.setState(this.state);
336       this.fetchData();
337     } else if (res.op == UserOperation.EditCommunity) {
338       let data = res.data as CommunityResponse;
339       this.state.community = data.community;
340       this.setState(this.state);
341     } else if (res.op == UserOperation.FollowCommunity) {
342       let data = res.data as CommunityResponse;
343       this.state.community.subscribed = data.community.subscribed;
344       this.state.community.number_of_subscribers =
345         data.community.number_of_subscribers;
346       this.setState(this.state);
347     } else if (res.op == UserOperation.GetPosts) {
348       let data = res.data as GetPostsResponse;
349       this.state.posts = data.posts;
350       this.state.loading = false;
351       this.setState(this.state);
352       setupTippy();
353     } else if (res.op == UserOperation.EditPost) {
354       let data = res.data as PostResponse;
355       editPostFindRes(data, this.state.posts);
356       this.setState(this.state);
357     } else if (res.op == UserOperation.CreatePost) {
358       let data = res.data as PostResponse;
359       this.state.posts.unshift(data.post);
360       this.setState(this.state);
361     } else if (res.op == UserOperation.CreatePostLike) {
362       let data = res.data as PostResponse;
363       createPostLikeFindRes(data, this.state.posts);
364       this.setState(this.state);
365     } else if (res.op == UserOperation.AddModToCommunity) {
366       let data = res.data as AddModToCommunityResponse;
367       this.state.moderators = data.moderators;
368       this.setState(this.state);
369     } else if (res.op == UserOperation.BanFromCommunity) {
370       let data = res.data as BanFromCommunityResponse;
371
372       this.state.posts
373         .filter(p => p.creator_id == data.user.id)
374         .forEach(p => (p.banned = data.banned));
375
376       this.setState(this.state);
377     } else if (res.op == UserOperation.GetComments) {
378       let data = res.data as GetCommentsResponse;
379       this.state.comments = data.comments;
380       this.state.loading = false;
381       this.setState(this.state);
382     } else if (res.op == UserOperation.EditComment) {
383       let data = res.data as CommentResponse;
384       editCommentRes(data, this.state.comments);
385       this.setState(this.state);
386     } else if (res.op == UserOperation.CreateComment) {
387       let data = res.data as CommentResponse;
388
389       // Necessary since it might be a user reply
390       if (data.recipient_ids.length == 0) {
391         this.state.comments.unshift(data.comment);
392         this.setState(this.state);
393       }
394     } else if (res.op == UserOperation.SaveComment) {
395       let data = res.data as CommentResponse;
396       saveCommentRes(data, this.state.comments);
397       this.setState(this.state);
398     } else if (res.op == UserOperation.CreateCommentLike) {
399       let data = res.data as CommentResponse;
400       createCommentLikeRes(data, this.state.comments);
401       this.setState(this.state);
402     }
403   }
404 }