1 import { Component, linkEvent } from 'inferno';
2 import { Helmet } from 'inferno-helmet';
3 import { Link } from 'inferno-router';
4 import { Subscription } from 'rxjs';
5 import { retryWhen, delay, take } from 'rxjs/operators';
19 WebSocketJsonResponse,
22 } from '../interfaces';
23 import { WebSocketService } from '../services';
24 import { wsJsonToRes, addTypeInfo, fetchLimit, toast } from '../utils';
25 import { MomentTime } from './moment-time';
26 import moment from 'moment';
27 import { i18n } from '../i18next';
29 interface ModlogState {
41 communityName?: string;
47 export class Modlog extends Component<any, ModlogState> {
48 private subscription: Subscription;
49 private emptyState: ModlogState = {
56 constructor(props: any, context: any) {
57 super(props, context);
59 this.state = this.emptyState;
60 this.state.communityId = this.props.match.params.community_id
61 ? Number(this.props.match.params.community_id)
63 this.subscription = WebSocketService.Instance.subject
64 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
66 msg => this.parseMessage(msg),
67 err => console.error(err),
68 () => console.log('complete')
72 WebSocketService.Instance.getSite();
75 componentWillUnmount() {
76 this.subscription.unsubscribe();
79 setCombined(res: GetModlogResponse) {
80 let removed_posts = addTypeInfo(res.removed_posts, 'removed_posts');
81 let locked_posts = addTypeInfo(res.locked_posts, 'locked_posts');
82 let stickied_posts = addTypeInfo(res.stickied_posts, 'stickied_posts');
83 let removed_comments = addTypeInfo(
87 let removed_communities = addTypeInfo(
88 res.removed_communities,
91 let banned_from_community = addTypeInfo(
92 res.banned_from_community,
93 'banned_from_community'
95 let added_to_community = addTypeInfo(
96 res.added_to_community,
99 let added = addTypeInfo(res.added, 'added');
100 let banned = addTypeInfo(res.banned, 'banned');
101 this.state.combined = [];
103 this.state.combined.push(...removed_posts);
104 this.state.combined.push(...locked_posts);
105 this.state.combined.push(...stickied_posts);
106 this.state.combined.push(...removed_comments);
107 this.state.combined.push(...removed_communities);
108 this.state.combined.push(...banned_from_community);
109 this.state.combined.push(...added_to_community);
110 this.state.combined.push(...added);
111 this.state.combined.push(...banned);
113 if (this.state.communityId && this.state.combined.length > 0) {
114 this.state.communityName = (this.state.combined[0]
115 .data as ModRemovePost).community_name;
119 this.state.combined.sort((a, b) =>
120 b.data.when_.localeCompare(a.data.when_)
123 this.setState(this.state);
129 {this.state.combined.map(i => (
132 <MomentTime data={i.data} />
135 <Link to={`/u/${i.data.mod_user_name}`}>
136 {i.data.mod_user_name}
140 {i.type_ == 'removed_posts' && (
142 {(i.data as ModRemovePost).removed ? 'Removed' : 'Restored'}
146 <Link to={`/post/${(i.data as ModRemovePost).post_id}`}>
147 {(i.data as ModRemovePost).post_name}
151 {(i.data as ModRemovePost).reason &&
152 ` reason: ${(i.data as ModRemovePost).reason}`}
156 {i.type_ == 'locked_posts' && (
158 {(i.data as ModLockPost).locked ? 'Locked' : 'Unlocked'}
162 <Link to={`/post/${(i.data as ModLockPost).post_id}`}>
163 {(i.data as ModLockPost).post_name}
168 {i.type_ == 'stickied_posts' && (
170 {(i.data as ModStickyPost).stickied
176 <Link to={`/post/${(i.data as ModStickyPost).post_id}`}>
177 {(i.data as ModStickyPost).post_name}
182 {i.type_ == 'removed_comments' && (
184 {(i.data as ModRemoveComment).removed
192 (i.data as ModRemoveComment).post_id
193 }/comment/${(i.data as ModRemoveComment).comment_id}`}
195 {(i.data as ModRemoveComment).comment_content}
203 (i.data as ModRemoveComment).comment_user_name
206 {(i.data as ModRemoveComment).comment_user_name}
210 {(i.data as ModRemoveComment).reason &&
211 ` reason: ${(i.data as ModRemoveComment).reason}`}
215 {i.type_ == 'removed_communities' && (
217 {(i.data as ModRemoveCommunity).removed
224 to={`/c/${(i.data as ModRemoveCommunity).community_name}`}
226 {(i.data as ModRemoveCommunity).community_name}
230 {(i.data as ModRemoveCommunity).reason &&
231 ` reason: ${(i.data as ModRemoveCommunity).reason}`}
234 {(i.data as ModRemoveCommunity).expires &&
236 .utc((i.data as ModRemoveCommunity).expires)
241 {i.type_ == 'banned_from_community' && (
244 {(i.data as ModBanFromCommunity).banned
251 (i.data as ModBanFromCommunity).other_user_name
254 {(i.data as ModBanFromCommunity).other_user_name}
257 <span> from the community </span>
261 (i.data as ModBanFromCommunity).community_name
264 {(i.data as ModBanFromCommunity).community_name}
268 {(i.data as ModBanFromCommunity).reason &&
269 ` reason: ${(i.data as ModBanFromCommunity).reason}`}
272 {(i.data as ModBanFromCommunity).expires &&
274 .utc((i.data as ModBanFromCommunity).expires)
279 {i.type_ == 'added_to_community' && (
282 {(i.data as ModAddCommunity).removed
288 to={`/u/${(i.data as ModAddCommunity).other_user_name}`}
290 {(i.data as ModAddCommunity).other_user_name}
293 <span> as a mod to the community </span>
296 to={`/c/${(i.data as ModAddCommunity).community_name}`}
298 {(i.data as ModAddCommunity).community_name}
303 {i.type_ == 'banned' && (
306 {(i.data as ModBan).banned ? 'Banned ' : 'Unbanned '}{' '}
309 <Link to={`/u/${(i.data as ModBan).other_user_name}`}>
310 {(i.data as ModBan).other_user_name}
314 {(i.data as ModBan).reason &&
315 ` reason: ${(i.data as ModBan).reason}`}
318 {(i.data as ModBan).expires &&
320 .utc((i.data as ModBan).expires)
325 {i.type_ == 'added' && (
328 {(i.data as ModAdd).removed ? 'Removed ' : 'Appointed '}{' '}
331 <Link to={`/u/${(i.data as ModAdd).other_user_name}`}>
332 {(i.data as ModAdd).other_user_name}
335 <span> as an admin </span>
345 get documentTitle(): string {
346 if (this.state.site) {
347 return `Modlog - ${this.state.site.name}`;
355 <div class="container">
356 <Helmet title={this.documentTitle} />
357 {this.state.loading ? (
359 <svg class="icon icon-spinner spin">
360 <use xlinkHref="#icon-spinner"></use>
366 {this.state.communityName && (
368 className="text-body"
369 to={`/c/${this.state.communityName}`}
371 /c/{this.state.communityName}{' '}
374 <span>{i18n.t('modlog')}</span>
376 <div class="table-responsive">
377 <table id="modlog_table" class="table table-sm table-hover">
378 <thead class="pointer">
380 <th> {i18n.t('time')}</th>
381 <th>{i18n.t('mod')}</th>
382 <th>{i18n.t('action')}</th>
398 {this.state.page > 1 && (
400 class="btn btn-secondary mr-1"
401 onClick={linkEvent(this, this.prevPage)}
407 class="btn btn-secondary"
408 onClick={linkEvent(this, this.nextPage)}
416 nextPage(i: Modlog) {
422 prevPage(i: Modlog) {
429 let modlogForm: GetModlogForm = {
430 community_id: this.state.communityId,
431 page: this.state.page,
434 WebSocketService.Instance.getModlog(modlogForm);
437 parseMessage(msg: WebSocketJsonResponse) {
439 let res = wsJsonToRes(msg);
441 toast(i18n.t(msg.error), 'danger');
443 } else if (res.op == UserOperation.GetModlog) {
444 let data = res.data as GetModlogResponse;
445 this.state.loading = false;
446 window.scrollTo(0, 0);
447 this.setCombined(data);
448 } else if (res.op == UserOperation.GetSite) {
449 let data = res.data as GetSiteResponse;
450 this.state.site = data.site;
451 this.setState(this.state);