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 } from '../interfaces';
19 import { WebSocketService } from '../services';
20 import { wsJsonToRes, addTypeInfo, fetchLimit } from '../utils';
21 import { MomentTime } from './moment-time';
22 import moment from 'moment';
23 import { i18n } from '../i18next';
25 interface ModlogState {
37 communityName?: string;
42 export class Modlog extends Component<any, ModlogState> {
43 private subscription: Subscription;
44 private emptyState: ModlogState = {
50 constructor(props: any, context: any) {
51 super(props, context);
53 this.state = this.emptyState;
54 this.state.communityId = this.props.match.params.community_id
55 ? Number(this.props.match.params.community_id)
57 this.subscription = WebSocketService.Instance.subject
58 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
60 msg => this.parseMessage(msg),
61 err => console.error(err),
62 () => console.log('complete')
68 componentWillUnmount() {
69 this.subscription.unsubscribe();
73 document.title = `Modlog - ${WebSocketService.Instance.site.name}`;
76 setCombined(res: GetModlogResponse) {
77 let removed_posts = addTypeInfo(res.removed_posts, 'removed_posts');
78 let locked_posts = addTypeInfo(res.locked_posts, 'locked_posts');
79 let stickied_posts = addTypeInfo(res.stickied_posts, 'stickied_posts');
80 let removed_comments = addTypeInfo(
84 let removed_communities = addTypeInfo(
85 res.removed_communities,
88 let banned_from_community = addTypeInfo(
89 res.banned_from_community,
90 'banned_from_community'
92 let added_to_community = addTypeInfo(
93 res.added_to_community,
96 let added = addTypeInfo(res.added, 'added');
97 let banned = addTypeInfo(res.banned, 'banned');
98 this.state.combined = [];
100 this.state.combined.push(...removed_posts);
101 this.state.combined.push(...locked_posts);
102 this.state.combined.push(...stickied_posts);
103 this.state.combined.push(...removed_comments);
104 this.state.combined.push(...removed_communities);
105 this.state.combined.push(...banned_from_community);
106 this.state.combined.push(...added_to_community);
107 this.state.combined.push(...added);
108 this.state.combined.push(...banned);
110 if (this.state.communityId && this.state.combined.length > 0) {
111 this.state.communityName = (this.state.combined[0]
112 .data as ModRemovePost).community_name;
116 this.state.combined.sort((a, b) =>
117 b.data.when_.localeCompare(a.data.when_)
120 this.setState(this.state);
126 {this.state.combined.map(i => (
129 <MomentTime data={i.data} />
132 <Link to={`/u/${i.data.mod_user_name}`}>
133 {i.data.mod_user_name}
137 {i.type_ == 'removed_posts' && (
139 {(i.data as ModRemovePost).removed ? 'Removed' : 'Restored'}
143 <Link to={`/post/${(i.data as ModRemovePost).post_id}`}>
144 {(i.data as ModRemovePost).post_name}
148 {(i.data as ModRemovePost).reason &&
149 ` reason: ${(i.data as ModRemovePost).reason}`}
153 {i.type_ == 'locked_posts' && (
155 {(i.data as ModLockPost).locked ? 'Locked' : 'Unlocked'}
159 <Link to={`/post/${(i.data as ModLockPost).post_id}`}>
160 {(i.data as ModLockPost).post_name}
165 {i.type_ == 'stickied_posts' && (
167 {(i.data as ModStickyPost).stickied
173 <Link to={`/post/${(i.data as ModStickyPost).post_id}`}>
174 {(i.data as ModStickyPost).post_name}
179 {i.type_ == 'removed_comments' && (
181 {(i.data as ModRemoveComment).removed
189 (i.data as ModRemoveComment).post_id
190 }/comment/${(i.data as ModRemoveComment).comment_id}`}
192 {(i.data as ModRemoveComment).comment_content}
200 (i.data as ModRemoveComment).comment_user_name
203 {(i.data as ModRemoveComment).comment_user_name}
207 {(i.data as ModRemoveComment).reason &&
208 ` reason: ${(i.data as ModRemoveComment).reason}`}
212 {i.type_ == 'removed_communities' && (
214 {(i.data as ModRemoveCommunity).removed
221 to={`/c/${(i.data as ModRemoveCommunity).community_name}`}
223 {(i.data as ModRemoveCommunity).community_name}
227 {(i.data as ModRemoveCommunity).reason &&
228 ` reason: ${(i.data as ModRemoveCommunity).reason}`}
231 {(i.data as ModRemoveCommunity).expires &&
233 .utc((i.data as ModRemoveCommunity).expires)
238 {i.type_ == 'banned_from_community' && (
241 {(i.data as ModBanFromCommunity).banned
248 (i.data as ModBanFromCommunity).other_user_name
251 {(i.data as ModBanFromCommunity).other_user_name}
254 <span> from the community </span>
258 (i.data as ModBanFromCommunity).community_name
261 {(i.data as ModBanFromCommunity).community_name}
265 {(i.data as ModBanFromCommunity).reason &&
266 ` reason: ${(i.data as ModBanFromCommunity).reason}`}
269 {(i.data as ModBanFromCommunity).expires &&
271 .utc((i.data as ModBanFromCommunity).expires)
276 {i.type_ == 'added_to_community' && (
279 {(i.data as ModAddCommunity).removed
285 to={`/u/${(i.data as ModAddCommunity).other_user_name}`}
287 {(i.data as ModAddCommunity).other_user_name}
290 <span> as a mod to the community </span>
293 to={`/c/${(i.data as ModAddCommunity).community_name}`}
295 {(i.data as ModAddCommunity).community_name}
300 {i.type_ == 'banned' && (
303 {(i.data as ModBan).banned ? 'Banned ' : 'Unbanned '}{' '}
306 <Link to={`/u/${(i.data as ModBan).other_user_name}`}>
307 {(i.data as ModBan).other_user_name}
311 {(i.data as ModBan).reason &&
312 ` reason: ${(i.data as ModBan).reason}`}
315 {(i.data as ModBan).expires &&
317 .utc((i.data as ModBan).expires)
322 {i.type_ == 'added' && (
325 {(i.data as ModAdd).removed ? 'Removed ' : 'Appointed '}{' '}
328 <Link to={`/u/${(i.data as ModAdd).other_user_name}`}>
329 {(i.data as ModAdd).other_user_name}
332 <span> as an admin </span>
344 <div class="container">
345 {this.state.loading ? (
347 <svg class="icon icon-spinner spin">
348 <use xlinkHref="#icon-spinner"></use>
354 {this.state.communityName && (
356 className="text-white"
357 to={`/c/${this.state.communityName}`}
359 /c/{this.state.communityName}{' '}
364 <div class="table-responsive">
365 <table id="modlog_table" class="table table-sm table-hover">
366 <thead class="pointer">
386 {this.state.page > 1 && (
388 class="btn btn-sm btn-secondary mr-1"
389 onClick={linkEvent(this, this.prevPage)}
395 class="btn btn-sm btn-secondary"
396 onClick={linkEvent(this, this.nextPage)}
404 nextPage(i: Modlog) {
410 prevPage(i: Modlog) {
417 let modlogForm: GetModlogForm = {
418 community_id: this.state.communityId,
419 page: this.state.page,
422 WebSocketService.Instance.getModlog(modlogForm);
425 parseMessage(msg: WebSocketJsonResponse) {
427 let res = wsJsonToRes(msg);
429 alert(i18n.t(res.error));
431 } else if (res.op == UserOperation.GetModlog) {
432 let data = res.data as GetModlogResponse;
433 this.state.loading = false;
434 window.scrollTo(0, 0);
435 this.setCombined(data);