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,
19 } from '../interfaces';
20 import { WebSocketService } from '../services';
21 import { wsJsonToRes, addTypeInfo, fetchLimit, toast } from '../utils';
22 import { MomentTime } from './moment-time';
23 import moment from 'moment';
24 import { i18n } from '../i18next';
26 interface ModlogState {
38 communityName?: string;
43 export class Modlog extends Component<any, ModlogState> {
44 private subscription: Subscription;
45 private emptyState: ModlogState = {
51 constructor(props: any, context: any) {
52 super(props, context);
54 this.state = this.emptyState;
55 this.state.communityId = this.props.match.params.community_id
56 ? Number(this.props.match.params.community_id)
58 this.subscription = WebSocketService.Instance.subject
59 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
61 msg => this.parseMessage(msg),
62 err => console.error(err),
63 () => console.log('complete')
69 componentWillUnmount() {
70 this.subscription.unsubscribe();
74 document.title = `Modlog - ${WebSocketService.Instance.site.name}`;
77 setCombined(res: GetModlogResponse) {
78 let removed_posts = addTypeInfo(res.removed_posts, 'removed_posts');
79 let locked_posts = addTypeInfo(res.locked_posts, 'locked_posts');
80 let stickied_posts = addTypeInfo(res.stickied_posts, 'stickied_posts');
81 let removed_comments = addTypeInfo(
85 let removed_communities = addTypeInfo(
86 res.removed_communities,
89 let banned_from_community = addTypeInfo(
90 res.banned_from_community,
91 'banned_from_community'
93 let added_to_community = addTypeInfo(
94 res.added_to_community,
97 let added = addTypeInfo(res.added, 'added');
98 let banned = addTypeInfo(res.banned, 'banned');
99 this.state.combined = [];
101 this.state.combined.push(...removed_posts);
102 this.state.combined.push(...locked_posts);
103 this.state.combined.push(...stickied_posts);
104 this.state.combined.push(...removed_comments);
105 this.state.combined.push(...removed_communities);
106 this.state.combined.push(...banned_from_community);
107 this.state.combined.push(...added_to_community);
108 this.state.combined.push(...added);
109 this.state.combined.push(...banned);
111 if (this.state.communityId && this.state.combined.length > 0) {
112 this.state.communityName = (this.state.combined[0]
113 .data as ModRemovePost).community_name;
117 this.state.combined.sort((a, b) =>
118 b.data.when_.localeCompare(a.data.when_)
121 this.setState(this.state);
127 {this.state.combined.map(i => (
130 <MomentTime data={i.data} />
133 <Link to={`/u/${i.data.mod_user_name}`}>
134 {i.data.mod_user_name}
138 {i.type_ == 'removed_posts' && (
140 {(i.data as ModRemovePost).removed ? 'Removed' : 'Restored'}
144 <Link to={`/post/${(i.data as ModRemovePost).post_id}`}>
145 {(i.data as ModRemovePost).post_name}
149 {(i.data as ModRemovePost).reason &&
150 ` reason: ${(i.data as ModRemovePost).reason}`}
154 {i.type_ == 'locked_posts' && (
156 {(i.data as ModLockPost).locked ? 'Locked' : 'Unlocked'}
160 <Link to={`/post/${(i.data as ModLockPost).post_id}`}>
161 {(i.data as ModLockPost).post_name}
166 {i.type_ == 'stickied_posts' && (
168 {(i.data as ModStickyPost).stickied
174 <Link to={`/post/${(i.data as ModStickyPost).post_id}`}>
175 {(i.data as ModStickyPost).post_name}
180 {i.type_ == 'removed_comments' && (
182 {(i.data as ModRemoveComment).removed
190 (i.data as ModRemoveComment).post_id
191 }/comment/${(i.data as ModRemoveComment).comment_id}`}
193 {(i.data as ModRemoveComment).comment_content}
201 (i.data as ModRemoveComment).comment_user_name
204 {(i.data as ModRemoveComment).comment_user_name}
208 {(i.data as ModRemoveComment).reason &&
209 ` reason: ${(i.data as ModRemoveComment).reason}`}
213 {i.type_ == 'removed_communities' && (
215 {(i.data as ModRemoveCommunity).removed
222 to={`/c/${(i.data as ModRemoveCommunity).community_name}`}
224 {(i.data as ModRemoveCommunity).community_name}
228 {(i.data as ModRemoveCommunity).reason &&
229 ` reason: ${(i.data as ModRemoveCommunity).reason}`}
232 {(i.data as ModRemoveCommunity).expires &&
234 .utc((i.data as ModRemoveCommunity).expires)
239 {i.type_ == 'banned_from_community' && (
242 {(i.data as ModBanFromCommunity).banned
249 (i.data as ModBanFromCommunity).other_user_name
252 {(i.data as ModBanFromCommunity).other_user_name}
255 <span> from the community </span>
259 (i.data as ModBanFromCommunity).community_name
262 {(i.data as ModBanFromCommunity).community_name}
266 {(i.data as ModBanFromCommunity).reason &&
267 ` reason: ${(i.data as ModBanFromCommunity).reason}`}
270 {(i.data as ModBanFromCommunity).expires &&
272 .utc((i.data as ModBanFromCommunity).expires)
277 {i.type_ == 'added_to_community' && (
280 {(i.data as ModAddCommunity).removed
286 to={`/u/${(i.data as ModAddCommunity).other_user_name}`}
288 {(i.data as ModAddCommunity).other_user_name}
291 <span> as a mod to the community </span>
294 to={`/c/${(i.data as ModAddCommunity).community_name}`}
296 {(i.data as ModAddCommunity).community_name}
301 {i.type_ == 'banned' && (
304 {(i.data as ModBan).banned ? 'Banned ' : 'Unbanned '}{' '}
307 <Link to={`/u/${(i.data as ModBan).other_user_name}`}>
308 {(i.data as ModBan).other_user_name}
312 {(i.data as ModBan).reason &&
313 ` reason: ${(i.data as ModBan).reason}`}
316 {(i.data as ModBan).expires &&
318 .utc((i.data as ModBan).expires)
323 {i.type_ == 'added' && (
326 {(i.data as ModAdd).removed ? 'Removed ' : 'Appointed '}{' '}
329 <Link to={`/u/${(i.data as ModAdd).other_user_name}`}>
330 {(i.data as ModAdd).other_user_name}
333 <span> as an admin </span>
345 <div class="container">
346 {this.state.loading ? (
348 <svg class="icon icon-spinner spin">
349 <use xlinkHref="#icon-spinner"></use>
355 {this.state.communityName && (
357 className="text-white"
358 to={`/c/${this.state.communityName}`}
360 /c/{this.state.communityName}{' '}
363 <span>{i18n.t('modlog')}</span>
365 <div class="table-responsive">
366 <table id="modlog_table" class="table table-sm table-hover">
367 <thead class="pointer">
369 <th> {i18n.t('time')}</th>
370 <th>{i18n.t('mod')}</th>
371 <th>{i18n.t('action')}</th>
387 {this.state.page > 1 && (
389 class="btn btn-sm btn-secondary mr-1"
390 onClick={linkEvent(this, this.prevPage)}
396 class="btn btn-sm btn-secondary"
397 onClick={linkEvent(this, this.nextPage)}
405 nextPage(i: Modlog) {
411 prevPage(i: Modlog) {
418 let modlogForm: GetModlogForm = {
419 community_id: this.state.communityId,
420 page: this.state.page,
423 WebSocketService.Instance.getModlog(modlogForm);
426 parseMessage(msg: WebSocketJsonResponse) {
428 let res = wsJsonToRes(msg);
430 toast(i18n.t(msg.error), 'danger');
432 } else if (res.op == UserOperation.GetModlog) {
433 let data = res.data as GetModlogResponse;
434 this.state.loading = false;
435 window.scrollTo(0, 0);
436 this.setCombined(data);