import { Component } from 'inferno'; import { Link } from 'inferno-router'; import { Subscription } from "rxjs"; import { retryWhen, delay, take } from 'rxjs/operators'; import { UserOperation, GetModlogForm, GetModlogResponse, ModRemovePost, ModLockPost, ModRemoveComment, ModRemoveCommunity, ModBanFromCommunity, ModBan, ModAddCommunity, ModAdd } from '../interfaces'; import { WebSocketService } from '../services'; import { msgOp, addTypeInfo } from '../utils'; import { MomentTime } from './moment-time'; import * as moment from 'moment'; interface ModlogState { removed_posts: Array, locked_posts: Array, removed_comments: Array, removed_communities: Array, banned_from_community: Array, banned: Array, added_to_community: Array, added: Array, loading: boolean; } export class Modlog extends Component { private subscription: Subscription; private emptyState: ModlogState = { removed_posts: [], locked_posts: [], removed_comments: [], removed_communities: [], banned_from_community: [], banned: [], added_to_community: [], added: [], loading: true } constructor(props: any, context: any) { super(props, context); this.state = this.emptyState; this.subscription = WebSocketService.Instance.subject .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) .subscribe( (msg) => this.parseMessage(msg), (err) => console.error(err), () => console.log('complete') ); let modlogForm: GetModlogForm = { }; WebSocketService.Instance.getModlog(modlogForm); } componentWillUnmount() { this.subscription.unsubscribe(); } combined() { let combined: Array<{type_: string, data: ModRemovePost | ModLockPost | ModRemoveCommunity}> = []; let removed_posts = addTypeInfo(this.state.removed_posts, "removed_posts"); let locked_posts = addTypeInfo(this.state.locked_posts, "locked_posts"); let removed_comments = addTypeInfo(this.state.removed_comments, "removed_comments"); let removed_communities = addTypeInfo(this.state.removed_communities, "removed_communities"); let banned_from_community = addTypeInfo(this.state.banned_from_community, "banned_from_community"); let added_to_community = addTypeInfo(this.state.added_to_community, "added_to_community"); combined.push(...removed_posts); combined.push(...locked_posts); combined.push(...removed_comments); combined.push(...removed_communities); combined.push(...banned_from_community); combined.push(...added_to_community); // Sort them by time combined.sort((a, b) => b.data.when_.localeCompare(a.data.when_)); console.log(combined); return ( {combined.map(i => {i.data.mod_user_name} {i.type_ == 'removed_posts' && <> {(i.data as ModRemovePost).removed? 'Removed' : 'Restored'} Post {(i.data as ModRemovePost).post_name}
{(i.data as ModRemovePost).reason && ` reason: ${(i.data as ModRemovePost).reason}`}
} {i.type_ == 'locked_posts' && <> {(i.data as ModLockPost).locked? 'Locked' : 'Unlocked'} Post {(i.data as ModLockPost).post_name} } {i.type_ == 'removed_comments' && <> {(i.data as ModRemoveComment).removed? 'Removed' : 'Restored'} Comment {(i.data as ModRemoveComment).comment_content}
{(i.data as ModRemoveComment).reason && ` reason: ${(i.data as ModRemoveComment).reason}`}
} {i.type_ == 'removed_communities' && <> {(i.data as ModRemoveCommunity).removed ? 'Removed' : 'Restored'} Community {i.data.community_name}
{(i.data as ModRemoveCommunity).reason && ` reason: ${(i.data as ModRemoveCommunity).reason}`}
{(i.data as ModRemoveCommunity).expires && ` expires: ${moment.utc((i.data as ModRemoveCommunity).expires).fromNow()}`}
} {i.type_ == 'banned_from_community' && <> {(i.data as ModBanFromCommunity).banned ? 'Banned ' : 'Unbanned '} {(i.data as ModBanFromCommunity).other_user_name}
{(i.data as ModBanFromCommunity).reason && ` reason: ${(i.data as ModBanFromCommunity).reason}`}
{(i.data as ModBanFromCommunity).expires && ` expires: ${moment.utc((i.data as ModBanFromCommunity).expires).fromNow()}`}
} {i.type_ == 'added_to_community' && <> {(i.data as ModAddCommunity).removed ? 'Removed ' : 'Appointed '} {(i.data as ModAddCommunity).other_user_name} as a mod to the community {i.data.community_name} } ) } ); } render() { return (
{this.state.loading ?

:

Modlog

{this.combined()}
Time Mod Action
}
); } parseMessage(msg: any) { console.log(msg); let op: UserOperation = msgOp(msg); if (msg.error) { alert(msg.error); return; } else if (op == UserOperation.GetModlog) { let res: GetModlogResponse = msg; this.state.loading = false; this.state.removed_posts = res.removed_posts; this.state.locked_posts = res.locked_posts; this.state.removed_comments = res.removed_comments; this.state.removed_communities = res.removed_communities; this.state.banned_from_community = res.banned_from_community; this.state.added_to_community = res.added_to_community; this.setState(this.state); } } }