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';
5 import { UserOperation, GetModlogForm, GetModlogResponse, ModRemovePost, ModLockPost, ModStickyPost, ModRemoveComment, ModRemoveCommunity, ModBanFromCommunity, ModBan, ModAddCommunity, ModAdd } from '../interfaces';
6 import { WebSocketService } from '../services';
7 import { msgOp, addTypeInfo, fetchLimit } from '../utils';
8 import { MomentTime } from './moment-time';
9 import * as moment from 'moment';
10 import { i18n } from '../i18next';
11 import { T } from 'inferno-i18next';
13 interface ModlogState {
14 combined: Array<{type_: string, data: ModRemovePost | ModLockPost | ModStickyPost | ModRemoveCommunity | ModAdd | ModBan}>,
16 communityName?: string,
21 export class Modlog extends Component<any, ModlogState> {
22 private subscription: Subscription;
23 private emptyState: ModlogState = {
29 constructor(props: any, context: any) {
30 super(props, context);
32 this.state = this.emptyState;
33 this.state.communityId = this.props.match.params.community_id ? Number(this.props.match.params.community_id) : undefined;
34 this.subscription = WebSocketService.Instance.subject
35 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
37 (msg) => this.parseMessage(msg),
38 (err) => console.error(err),
39 () => console.log('complete')
45 componentWillUnmount() {
46 this.subscription.unsubscribe();
50 document.title = `Modlog - ${WebSocketService.Instance.site.name}`;
53 setCombined(res: GetModlogResponse) {
54 let removed_posts = addTypeInfo(res.removed_posts, "removed_posts");
55 let locked_posts = addTypeInfo(res.locked_posts, "locked_posts");
56 let stickied_posts = addTypeInfo(res.stickied_posts, "stickied_posts");
57 let removed_comments = addTypeInfo(res.removed_comments, "removed_comments");
58 let removed_communities = addTypeInfo(res.removed_communities, "removed_communities");
59 let banned_from_community = addTypeInfo(res.banned_from_community, "banned_from_community");
60 let added_to_community = addTypeInfo(res.added_to_community, "added_to_community");
61 let added = addTypeInfo(res.added, "added");
62 let banned = addTypeInfo(res.banned, "banned");
63 this.state.combined = [];
65 this.state.combined.push(...removed_posts);
66 this.state.combined.push(...locked_posts);
67 this.state.combined.push(...stickied_posts);
68 this.state.combined.push(...removed_comments);
69 this.state.combined.push(...removed_communities);
70 this.state.combined.push(...banned_from_community);
71 this.state.combined.push(...added_to_community);
72 this.state.combined.push(...added);
73 this.state.combined.push(...banned);
75 if (this.state.communityId && this.state.combined.length > 0) {
76 this.state.communityName = (this.state.combined[0].data as ModRemovePost).community_name;
80 this.state.combined.sort((a, b) => b.data.when_.localeCompare(a.data.when_));
82 this.setState(this.state);
88 {this.state.combined.map(i =>
90 <td><MomentTime data={i.data} /></td>
91 <td><Link to={`/u/${i.data.mod_user_name}`}>{i.data.mod_user_name}</Link></td>
93 {i.type_ == 'removed_posts' &&
95 {(i.data as ModRemovePost).removed? 'Removed' : 'Restored'}
96 <span> Post <Link to={`/post/${(i.data as ModRemovePost).post_id}`}>{(i.data as ModRemovePost).post_name}</Link></span>
97 <div>{(i.data as ModRemovePost).reason && ` reason: ${(i.data as ModRemovePost).reason}`}</div>
100 {i.type_ == 'locked_posts' &&
102 {(i.data as ModLockPost).locked? 'Locked' : 'Unlocked'}
103 <span> Post <Link to={`/post/${(i.data as ModLockPost).post_id}`}>{(i.data as ModLockPost).post_name}</Link></span>
106 {i.type_ == 'stickied_posts' &&
108 {(i.data as ModStickyPost).stickied? 'Stickied' : 'Unstickied'}
109 <span> Post <Link to={`/post/${(i.data as ModStickyPost).post_id}`}>{(i.data as ModStickyPost).post_name}</Link></span>
112 {i.type_ == 'removed_comments' &&
114 {(i.data as ModRemoveComment).removed? 'Removed' : 'Restored'}
115 <span> Comment <Link to={`/post/${(i.data as ModRemoveComment).post_id}/comment/${(i.data as ModRemoveComment).comment_id}`}>{(i.data as ModRemoveComment).comment_content}</Link></span>
116 <span> by <Link to={`/u/${(i.data as ModRemoveComment).comment_user_name}`}>{(i.data as ModRemoveComment).comment_user_name}</Link></span>
117 <div>{(i.data as ModRemoveComment).reason && ` reason: ${(i.data as ModRemoveComment).reason}`}</div>
120 {i.type_ == 'removed_communities' &&
122 {(i.data as ModRemoveCommunity).removed ? 'Removed' : 'Restored'}
123 <span> Community <Link to={`/c/${(i.data as ModRemoveCommunity).community_name}`}>{(i.data as ModRemoveCommunity).community_name}</Link></span>
124 <div>{(i.data as ModRemoveCommunity).reason && ` reason: ${(i.data as ModRemoveCommunity).reason}`}</div>
125 <div>{(i.data as ModRemoveCommunity).expires && ` expires: ${moment.utc((i.data as ModRemoveCommunity).expires).fromNow()}`}</div>
128 {i.type_ == 'banned_from_community' &&
130 <span>{(i.data as ModBanFromCommunity).banned ? 'Banned ' : 'Unbanned '} </span>
131 <span><Link to={`/u/${(i.data as ModBanFromCommunity).other_user_name}`}>{(i.data as ModBanFromCommunity).other_user_name}</Link></span>
132 <span> from the community </span>
133 <span><Link to={`/c/${(i.data as ModBanFromCommunity).community_name}`}>{(i.data as ModBanFromCommunity).community_name}</Link></span>
134 <div>{(i.data as ModBanFromCommunity).reason && ` reason: ${(i.data as ModBanFromCommunity).reason}`}</div>
135 <div>{(i.data as ModBanFromCommunity).expires && ` expires: ${moment.utc((i.data as ModBanFromCommunity).expires).fromNow()}`}</div>
138 {i.type_ == 'added_to_community' &&
140 <span>{(i.data as ModAddCommunity).removed ? 'Removed ' : 'Appointed '} </span>
141 <span><Link to={`/u/${(i.data as ModAddCommunity).other_user_name}`}>{(i.data as ModAddCommunity).other_user_name}</Link></span>
142 <span> as a mod to the community </span>
143 <span><Link to={`/c/${(i.data as ModAddCommunity).community_name}`}>{(i.data as ModAddCommunity).community_name}</Link></span>
146 {i.type_ == 'banned' &&
148 <span>{(i.data as ModBan).banned ? 'Banned ' : 'Unbanned '} </span>
149 <span><Link to={`/u/${(i.data as ModBan).other_user_name}`}>{(i.data as ModBan).other_user_name}</Link></span>
150 <div>{(i.data as ModBan).reason && ` reason: ${(i.data as ModBan).reason}`}</div>
151 <div>{(i.data as ModBan).expires && ` expires: ${moment.utc((i.data as ModBan).expires).fromNow()}`}</div>
154 {i.type_ == 'added' &&
156 <span>{(i.data as ModAdd).removed ? 'Removed ' : 'Appointed '} </span>
157 <span><Link to={`/u/${(i.data as ModAdd).other_user_name}`}>{(i.data as ModAdd).other_user_name}</Link></span>
158 <span> as an admin </span>
173 <div class="container">
174 {this.state.loading ?
175 <h5 class=""><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h5> :
178 {this.state.communityName && <Link className="text-white" to={`/c/${this.state.communityName}`}>/c/{this.state.communityName} </Link>}
181 <div class="table-responsive">
182 <table id="modlog_table" class="table table-sm table-hover">
183 <thead class="pointer">
203 {this.state.page > 1 &&
204 <button class="btn btn-sm btn-secondary mr-1" onClick={linkEvent(this, this.prevPage)}><T i18nKey="prev">#</T></button>
206 <button class="btn btn-sm btn-secondary" onClick={linkEvent(this, this.nextPage)}><T i18nKey="next">#</T></button>
211 nextPage(i: Modlog) {
217 prevPage(i: Modlog) {
224 let modlogForm: GetModlogForm = {
225 community_id: this.state.communityId,
226 page: this.state.page,
229 WebSocketService.Instance.getModlog(modlogForm);
232 parseMessage(msg: any) {
234 let op: UserOperation = msgOp(msg);
236 alert(i18n.t(msg.error));
238 } else if (op == UserOperation.GetModlog) {
239 let res: GetModlogResponse = msg;
240 this.state.loading = false;
241 window.scrollTo(0,0);
242 this.setCombined(res);