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 { msgOp, addTypeInfo, fetchLimit } from '../utils';
21 import { MomentTime } from './moment-time';
22 import * as 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
67 msg => this.parseMessage(msg),
68 err => console.error(err),
69 () => console.log('complete')
75 componentWillUnmount() {
76 this.subscription.unsubscribe();
80 document.title = `Modlog - ${WebSocketService.Instance.site.name}`;
83 setCombined(res: GetModlogResponse) {
84 let removed_posts = addTypeInfo(res.removed_posts, 'removed_posts');
85 let locked_posts = addTypeInfo(res.locked_posts, 'locked_posts');
86 let stickied_posts = addTypeInfo(res.stickied_posts, 'stickied_posts');
87 let removed_comments = addTypeInfo(
91 let removed_communities = addTypeInfo(
92 res.removed_communities,
95 let banned_from_community = addTypeInfo(
96 res.banned_from_community,
97 'banned_from_community'
99 let added_to_community = addTypeInfo(
100 res.added_to_community,
103 let added = addTypeInfo(res.added, 'added');
104 let banned = addTypeInfo(res.banned, 'banned');
105 this.state.combined = [];
107 this.state.combined.push(...removed_posts);
108 this.state.combined.push(...locked_posts);
109 this.state.combined.push(...stickied_posts);
110 this.state.combined.push(...removed_comments);
111 this.state.combined.push(...removed_communities);
112 this.state.combined.push(...banned_from_community);
113 this.state.combined.push(...added_to_community);
114 this.state.combined.push(...added);
115 this.state.combined.push(...banned);
117 if (this.state.communityId && this.state.combined.length > 0) {
118 this.state.communityName = (this.state.combined[0]
119 .data as ModRemovePost).community_name;
123 this.state.combined.sort((a, b) =>
124 b.data.when_.localeCompare(a.data.when_)
127 this.setState(this.state);
133 {this.state.combined.map(i => (
136 <MomentTime data={i.data} />
139 <Link to={`/u/${i.data.mod_user_name}`}>
140 {i.data.mod_user_name}
144 {i.type_ == 'removed_posts' && (
146 {(i.data as ModRemovePost).removed ? 'Removed' : 'Restored'}
150 <Link to={`/post/${(i.data as ModRemovePost).post_id}`}>
151 {(i.data as ModRemovePost).post_name}
155 {(i.data as ModRemovePost).reason &&
156 ` reason: ${(i.data as ModRemovePost).reason}`}
160 {i.type_ == 'locked_posts' && (
162 {(i.data as ModLockPost).locked ? 'Locked' : 'Unlocked'}
166 <Link to={`/post/${(i.data as ModLockPost).post_id}`}>
167 {(i.data as ModLockPost).post_name}
172 {i.type_ == 'stickied_posts' && (
174 {(i.data as ModStickyPost).stickied
180 <Link to={`/post/${(i.data as ModStickyPost).post_id}`}>
181 {(i.data as ModStickyPost).post_name}
186 {i.type_ == 'removed_comments' && (
188 {(i.data as ModRemoveComment).removed
196 (i.data as ModRemoveComment).post_id
197 }/comment/${(i.data as ModRemoveComment).comment_id}`}
199 {(i.data as ModRemoveComment).comment_content}
207 (i.data as ModRemoveComment).comment_user_name
210 {(i.data as ModRemoveComment).comment_user_name}
214 {(i.data as ModRemoveComment).reason &&
215 ` reason: ${(i.data as ModRemoveComment).reason}`}
219 {i.type_ == 'removed_communities' && (
221 {(i.data as ModRemoveCommunity).removed
228 to={`/c/${(i.data as ModRemoveCommunity).community_name}`}
230 {(i.data as ModRemoveCommunity).community_name}
234 {(i.data as ModRemoveCommunity).reason &&
235 ` reason: ${(i.data as ModRemoveCommunity).reason}`}
238 {(i.data as ModRemoveCommunity).expires &&
240 .utc((i.data as ModRemoveCommunity).expires)
245 {i.type_ == 'banned_from_community' && (
248 {(i.data as ModBanFromCommunity).banned
255 (i.data as ModBanFromCommunity).other_user_name
258 {(i.data as ModBanFromCommunity).other_user_name}
261 <span> from the community </span>
265 (i.data as ModBanFromCommunity).community_name
268 {(i.data as ModBanFromCommunity).community_name}
272 {(i.data as ModBanFromCommunity).reason &&
273 ` reason: ${(i.data as ModBanFromCommunity).reason}`}
276 {(i.data as ModBanFromCommunity).expires &&
278 .utc((i.data as ModBanFromCommunity).expires)
283 {i.type_ == 'added_to_community' && (
286 {(i.data as ModAddCommunity).removed
292 to={`/u/${(i.data as ModAddCommunity).other_user_name}`}
294 {(i.data as ModAddCommunity).other_user_name}
297 <span> as a mod to the community </span>
300 to={`/c/${(i.data as ModAddCommunity).community_name}`}
302 {(i.data as ModAddCommunity).community_name}
307 {i.type_ == 'banned' && (
310 {(i.data as ModBan).banned ? 'Banned ' : 'Unbanned '}{' '}
313 <Link to={`/u/${(i.data as ModBan).other_user_name}`}>
314 {(i.data as ModBan).other_user_name}
318 {(i.data as ModBan).reason &&
319 ` reason: ${(i.data as ModBan).reason}`}
322 {(i.data as ModBan).expires &&
324 .utc((i.data as ModBan).expires)
329 {i.type_ == 'added' && (
332 {(i.data as ModAdd).removed ? 'Removed ' : 'Appointed '}{' '}
335 <Link to={`/u/${(i.data as ModAdd).other_user_name}`}>
336 {(i.data as ModAdd).other_user_name}
339 <span> as an admin </span>
351 <div class="container">
352 {this.state.loading ? (
354 <svg class="icon icon-spinner spin">
355 <use xlinkHref="#icon-spinner"></use>
361 {this.state.communityName && (
363 className="text-white"
364 to={`/c/${this.state.communityName}`}
366 /c/{this.state.communityName}{' '}
371 <div class="table-responsive">
372 <table id="modlog_table" class="table table-sm table-hover">
373 <thead class="pointer">
393 {this.state.page > 1 && (
395 class="btn btn-sm btn-secondary mr-1"
396 onClick={linkEvent(this, this.prevPage)}
402 class="btn btn-sm btn-secondary"
403 onClick={linkEvent(this, this.nextPage)}
411 nextPage(i: Modlog) {
417 prevPage(i: Modlog) {
424 let modlogForm: GetModlogForm = {
425 community_id: this.state.communityId,
426 page: this.state.page,
429 WebSocketService.Instance.getModlog(modlogForm);
432 parseMessage(msg: any) {
434 let op: UserOperation = msgOp(msg);
436 alert(i18n.t(msg.error));
438 } else if (op == UserOperation.GetModlog) {
439 let res: GetModlogResponse = msg;
440 this.state.loading = false;
441 window.scrollTo(0, 0);
442 this.setCombined(res);