1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
18 WebSocketJsonResponse,
20 } from '../interfaces';
21 import { WebSocketService } from '../services';
22 import { wsJsonToRes, addTypeInfo, fetchLimit, toast } from '../utils';
23 import { MomentTime } from './moment-time';
24 import moment from 'moment';
25 import { i18n } from '../i18next';
27 interface ModlogState {
39 communityName?: string;
44 export class Modlog extends Component<any, ModlogState> {
45 private subscription: Subscription;
46 private emptyState: ModlogState = {
52 constructor(props: any, context: any) {
53 super(props, context);
55 this.state = this.emptyState;
56 this.state.communityId = this.props.match.params.community_id
57 ? Number(this.props.match.params.community_id)
59 this.subscription = WebSocketService.Instance.subject
60 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
62 msg => this.parseMessage(msg),
63 err => console.error(err),
64 () => console.log('complete')
68 WebSocketService.Instance.getSite();
71 componentWillUnmount() {
72 this.subscription.unsubscribe();
75 setCombined(res: GetModlogResponse) {
76 let removed_posts = addTypeInfo(res.removed_posts, 'removed_posts');
77 let locked_posts = addTypeInfo(res.locked_posts, 'locked_posts');
78 let stickied_posts = addTypeInfo(res.stickied_posts, 'stickied_posts');
79 let removed_comments = addTypeInfo(
83 let removed_communities = addTypeInfo(
84 res.removed_communities,
87 let banned_from_community = addTypeInfo(
88 res.banned_from_community,
89 'banned_from_community'
91 let added_to_community = addTypeInfo(
92 res.added_to_community,
95 let added = addTypeInfo(res.added, 'added');
96 let banned = addTypeInfo(res.banned, 'banned');
97 this.state.combined = [];
99 this.state.combined.push(...removed_posts);
100 this.state.combined.push(...locked_posts);
101 this.state.combined.push(...stickied_posts);
102 this.state.combined.push(...removed_comments);
103 this.state.combined.push(...removed_communities);
104 this.state.combined.push(...banned_from_community);
105 this.state.combined.push(...added_to_community);
106 this.state.combined.push(...added);
107 this.state.combined.push(...banned);
109 if (this.state.communityId && this.state.combined.length > 0) {
110 this.state.communityName = (this.state.combined[0]
111 .data as ModRemovePost).community_name;
115 this.state.combined.sort((a, b) =>
116 b.data.when_.localeCompare(a.data.when_)
119 this.setState(this.state);
125 {this.state.combined.map(i => (
128 <MomentTime data={i.data} />
131 <Link to={`/u/${i.data.mod_user_name}`}>
132 {i.data.mod_user_name}
136 {i.type_ == 'removed_posts' && (
138 {(i.data as ModRemovePost).removed ? 'Removed' : 'Restored'}
142 <Link to={`/post/${(i.data as ModRemovePost).post_id}`}>
143 {(i.data as ModRemovePost).post_name}
147 {(i.data as ModRemovePost).reason &&
148 ` reason: ${(i.data as ModRemovePost).reason}`}
152 {i.type_ == 'locked_posts' && (
154 {(i.data as ModLockPost).locked ? 'Locked' : 'Unlocked'}
158 <Link to={`/post/${(i.data as ModLockPost).post_id}`}>
159 {(i.data as ModLockPost).post_name}
164 {i.type_ == 'stickied_posts' && (
166 {(i.data as ModStickyPost).stickied
172 <Link to={`/post/${(i.data as ModStickyPost).post_id}`}>
173 {(i.data as ModStickyPost).post_name}
178 {i.type_ == 'removed_comments' && (
180 {(i.data as ModRemoveComment).removed
188 (i.data as ModRemoveComment).post_id
189 }/comment/${(i.data as ModRemoveComment).comment_id}`}
191 {(i.data as ModRemoveComment).comment_content}
199 (i.data as ModRemoveComment).comment_user_name
202 {(i.data as ModRemoveComment).comment_user_name}
206 {(i.data as ModRemoveComment).reason &&
207 ` reason: ${(i.data as ModRemoveComment).reason}`}
211 {i.type_ == 'removed_communities' && (
213 {(i.data as ModRemoveCommunity).removed
220 to={`/c/${(i.data as ModRemoveCommunity).community_name}`}
222 {(i.data as ModRemoveCommunity).community_name}
226 {(i.data as ModRemoveCommunity).reason &&
227 ` reason: ${(i.data as ModRemoveCommunity).reason}`}
230 {(i.data as ModRemoveCommunity).expires &&
232 .utc((i.data as ModRemoveCommunity).expires)
237 {i.type_ == 'banned_from_community' && (
240 {(i.data as ModBanFromCommunity).banned
247 (i.data as ModBanFromCommunity).other_user_name
250 {(i.data as ModBanFromCommunity).other_user_name}
253 <span> from the community </span>
257 (i.data as ModBanFromCommunity).community_name
260 {(i.data as ModBanFromCommunity).community_name}
264 {(i.data as ModBanFromCommunity).reason &&
265 ` reason: ${(i.data as ModBanFromCommunity).reason}`}
268 {(i.data as ModBanFromCommunity).expires &&
270 .utc((i.data as ModBanFromCommunity).expires)
275 {i.type_ == 'added_to_community' && (
278 {(i.data as ModAddCommunity).removed
284 to={`/u/${(i.data as ModAddCommunity).other_user_name}`}
286 {(i.data as ModAddCommunity).other_user_name}
289 <span> as a mod to the community </span>
292 to={`/c/${(i.data as ModAddCommunity).community_name}`}
294 {(i.data as ModAddCommunity).community_name}
299 {i.type_ == 'banned' && (
302 {(i.data as ModBan).banned ? 'Banned ' : 'Unbanned '}{' '}
305 <Link to={`/u/${(i.data as ModBan).other_user_name}`}>
306 {(i.data as ModBan).other_user_name}
310 {(i.data as ModBan).reason &&
311 ` reason: ${(i.data as ModBan).reason}`}
314 {(i.data as ModBan).expires &&
316 .utc((i.data as ModBan).expires)
321 {i.type_ == 'added' && (
324 {(i.data as ModAdd).removed ? 'Removed ' : 'Appointed '}{' '}
327 <Link to={`/u/${(i.data as ModAdd).other_user_name}`}>
328 {(i.data as ModAdd).other_user_name}
331 <span> as an admin </span>
343 <div class="container">
344 {this.state.loading ? (
346 <svg class="icon icon-spinner spin">
347 <use xlinkHref="#icon-spinner"></use>
353 {this.state.communityName && (
355 className="text-body"
356 to={`/c/${this.state.communityName}`}
358 /c/{this.state.communityName}{' '}
361 <span>{i18n.t('modlog')}</span>
363 <div class="table-responsive">
364 <table id="modlog_table" class="table table-sm table-hover">
365 <thead class="pointer">
367 <th> {i18n.t('time')}</th>
368 <th>{i18n.t('mod')}</th>
369 <th>{i18n.t('action')}</th>
385 {this.state.page > 1 && (
387 class="btn btn-secondary mr-1"
388 onClick={linkEvent(this, this.prevPage)}
394 class="btn btn-secondary"
395 onClick={linkEvent(this, this.nextPage)}
403 nextPage(i: Modlog) {
409 prevPage(i: Modlog) {
416 let modlogForm: GetModlogForm = {
417 community_id: this.state.communityId,
418 page: this.state.page,
421 WebSocketService.Instance.getModlog(modlogForm);
424 parseMessage(msg: WebSocketJsonResponse) {
426 let res = wsJsonToRes(msg);
428 toast(i18n.t(msg.error), 'danger');
430 } else if (res.op == UserOperation.GetModlog) {
431 let data = res.data as GetModlogResponse;
432 this.state.loading = false;
433 window.scrollTo(0, 0);
434 this.setCombined(data);
435 } else if (res.op == UserOperation.GetSite) {
436 let data = res.data as GetSiteResponse;
437 document.title = `Modlog - ${data.site.name}`;