15 } from "@utils/helpers";
16 import { amAdmin, amMod } from "@utils/roles";
17 import type { QueryParams } from "@utils/types";
18 import { Choice, RouteDataResponse } from "@utils/types";
19 import { NoOptionI18nKeys } from "i18next";
20 import { Component, linkEvent } from "inferno";
21 import { T } from "inferno-i18next-dess";
22 import { Link } from "inferno-router";
23 import { RouteComponentProps } from "inferno-router/dist/Route";
25 AdminPurgeCommentView,
26 AdminPurgeCommunityView,
34 GetPersonDetailsResponse,
37 ModBanFromCommunityView,
42 ModRemoveCommunityView,
44 ModTransferCommunityView,
47 } from "lemmy-js-client";
48 import { fetchLimit } from "../config";
49 import { InitialFetchRequest } from "../interfaces";
50 import { FirstLoadService, I18NextService } from "../services";
51 import { HttpService, RequestState } from "../services/HttpService";
52 import { HtmlTags } from "./common/html-tags";
53 import { Icon, Spinner } from "./common/icon";
54 import { MomentTime } from "./common/moment-time";
55 import { Paginator } from "./common/paginator";
56 import { SearchableSelect } from "./common/searchable-select";
57 import { CommunityLink } from "./community/community-link";
58 import { PersonListing } from "./person/person-listing";
60 type FilterType = "mod" | "user";
66 | ModRemoveCommentView
67 | ModRemoveCommunityView
68 | ModBanFromCommunityView
71 | ModTransferCommunityView
73 | AdminPurgePersonView
74 | AdminPurgeCommunityView
76 | AdminPurgeCommentView;
78 type ModlogData = RouteDataResponse<{
79 res: GetModlogResponse;
80 communityRes: GetCommunityResponse;
81 modUserResponse: GetPersonDetailsResponse;
82 userResponse: GetPersonDetailsResponse;
85 interface ModlogType {
87 type_: ModlogActionType;
93 const getModlogQueryParams = () =>
94 getQueryParams<ModlogProps>({
95 actionType: getActionFromString,
96 modId: getIdFromString,
97 userId: getIdFromString,
98 page: getPageFromString,
101 interface ModlogState {
102 res: RequestState<GetModlogResponse>;
103 communityRes: RequestState<GetCommunityResponse>;
104 loadingModSearch: boolean;
105 loadingUserSearch: boolean;
106 modSearchOptions: Choice[];
107 userSearchOptions: Choice[];
110 interface ModlogProps {
112 userId?: number | null;
113 modId?: number | null;
114 actionType: ModlogActionType;
117 function getActionFromString(action?: string): ModlogActionType {
118 return action !== undefined ? (action as ModlogActionType) : "All";
121 const getModlogActionMapper =
123 actionType: ModlogActionType,
124 getAction: (view: View) => { id: number; when_: string }
126 (view: View & { moderator?: Person; admin?: Person }): ModlogType => {
127 const { id, when_ } = getAction(view);
134 moderator: view.moderator ?? view.admin,
138 function buildCombined({
146 admin_purged_comments,
147 admin_purged_communities,
148 admin_purged_persons,
151 banned_from_community,
152 transferred_to_community,
153 }: GetModlogResponse): ModlogType[] {
154 const combined = removed_posts
156 getModlogActionMapper(
158 ({ mod_remove_post }: ModRemovePostView) => mod_remove_post
163 getModlogActionMapper(
165 ({ mod_lock_post }: ModLockPostView) => mod_lock_post
171 getModlogActionMapper(
173 ({ mod_feature_post }: ModFeaturePostView) => mod_feature_post
178 removed_comments.map(
179 getModlogActionMapper(
181 ({ mod_remove_comment }: ModRemoveCommentView) => mod_remove_comment
186 removed_communities.map(
187 getModlogActionMapper(
188 "ModRemoveCommunity",
189 ({ mod_remove_community }: ModRemoveCommunityView) =>
195 banned_from_community.map(
196 getModlogActionMapper(
197 "ModBanFromCommunity",
198 ({ mod_ban_from_community }: ModBanFromCommunityView) =>
199 mod_ban_from_community
204 added_to_community.map(
205 getModlogActionMapper(
207 ({ mod_add_community }: ModAddCommunityView) => mod_add_community
212 transferred_to_community.map(
213 getModlogActionMapper(
214 "ModTransferCommunity",
215 ({ mod_transfer_community }: ModTransferCommunityView) =>
216 mod_transfer_community
222 getModlogActionMapper("ModAdd", ({ mod_add }: ModAddView) => mod_add)
227 getModlogActionMapper("ModBan", ({ mod_ban }: ModBanView) => mod_ban)
231 admin_purged_persons.map(
232 getModlogActionMapper(
234 ({ admin_purge_person }: AdminPurgePersonView) => admin_purge_person
239 admin_purged_communities.map(
240 getModlogActionMapper(
241 "AdminPurgeCommunity",
242 ({ admin_purge_community }: AdminPurgeCommunityView) =>
243 admin_purge_community
248 admin_purged_posts.map(
249 getModlogActionMapper(
251 ({ admin_purge_post }: AdminPurgePostView) => admin_purge_post
256 admin_purged_comments.map(
257 getModlogActionMapper(
259 ({ admin_purge_comment }: AdminPurgeCommentView) =>
266 combined.sort((a, b) => b.when_.localeCompare(a.when_));
271 function renderModlogType({ type_, view }: ModlogType) {
273 case "ModRemovePost": {
274 const mrpv = view as ModRemovePostView;
276 mod_remove_post: { reason, removed },
282 <span>{removed ? "Removed " : "Restored "}</span>
284 Post <Link to={`/post/${id}`}>{name}</Link>
288 <div>reason: {reason}</div>
295 case "ModLockPost": {
297 mod_lock_post: { locked },
299 } = view as ModLockPostView;
303 <span>{locked ? "Locked " : "Unlocked "}</span>
305 Post <Link to={`/post/${id}`}>{name}</Link>
311 case "ModFeaturePost": {
313 mod_feature_post: { featured, is_featured_community },
315 } = view as ModFeaturePostView;
319 <span>{featured ? "Featured " : "Unfeatured "}</span>
321 Post <Link to={`/post/${id}`}>{name}</Link>
323 <span>{is_featured_community ? " In Community" : " In Local"}</span>
327 case "ModRemoveComment": {
328 const mrc = view as ModRemoveCommentView;
330 mod_remove_comment: { reason, removed },
331 comment: { id, content },
337 <span>{removed ? "Removed " : "Restored "}</span>
339 Comment <Link to={`/comment/${id}`}>{content}</Link>
343 by <PersonListing person={commenter} />
347 <div>reason: {reason}</div>
354 case "ModRemoveCommunity": {
355 const mrco = view as ModRemoveCommunityView;
357 mod_remove_community: { reason, expires, removed },
363 <span>{removed ? "Removed " : "Restored "}</span>
365 Community <CommunityLink community={community} />
369 <div>reason: {reason}</div>
374 <div>expires: {formatPastDate(expires)}</div>
381 case "ModBanFromCommunity": {
382 const mbfc = view as ModBanFromCommunityView;
384 mod_ban_from_community: { reason, expires, banned },
391 <span>{banned ? "Banned " : "Unbanned "}</span>
393 <PersonListing person={banned_person} />
395 <span> from the community </span>
397 <CommunityLink community={community} />
401 <div>reason: {reason}</div>
406 <div>expires: {formatPastDate(expires)}</div>
413 case "ModAddCommunity": {
415 mod_add_community: { removed },
418 } = view as ModAddCommunityView;
422 <span>{removed ? "Removed " : "Appointed "}</span>
424 <PersonListing person={modded_person} />
426 <span> as a mod to the community </span>
428 <CommunityLink community={community} />
434 case "ModTransferCommunity": {
435 const { community, modded_person } = view as ModTransferCommunityView;
439 <span>Transferred</span>
441 <CommunityLink community={community} />
445 <PersonListing person={modded_person} />
453 mod_ban: { reason, expires, banned },
455 } = view as ModBanView;
459 <span>{banned ? "Banned " : "Unbanned "}</span>
461 <PersonListing person={banned_person} />
465 <div>reason: {reason}</div>
470 <div>expires: {formatPastDate(expires)}</div>
479 mod_add: { removed },
481 } = view as ModAddView;
485 <span>{removed ? "Removed " : "Appointed "}</span>
487 <PersonListing person={modded_person} />
489 <span> as an admin </span>
493 case "AdminPurgePerson": {
495 admin_purge_person: { reason },
496 } = view as AdminPurgePersonView;
500 <span>Purged a Person</span>
503 <div>reason: {reason}</div>
510 case "AdminPurgeCommunity": {
512 admin_purge_community: { reason },
513 } = view as AdminPurgeCommunityView;
517 <span>Purged a Community</span>
520 <div>reason: {reason}</div>
527 case "AdminPurgePost": {
529 admin_purge_post: { reason },
531 } = view as AdminPurgePostView;
535 <span>Purged a Post from from </span>
536 <CommunityLink community={community} />
539 <div>reason: {reason}</div>
546 case "AdminPurgeComment": {
548 admin_purge_comment: { reason },
550 } = view as AdminPurgeCommentView;
555 Purged a Comment from <Link to={`/post/${id}`}>{name}</Link>
559 <div>reason: {reason}</div>
579 filterType: FilterType;
580 onChange: (option: Choice) => void;
581 value?: number | null;
582 onSearch: (text: string) => void;
586 <div className="col-sm-6 mb-3">
587 <label className="mb-2" htmlFor={`filter-${filterType}`}>
588 {I18NextService.i18n.t(`filter_by_${filterType}` as NoOptionI18nKeys)}
591 id={`filter-${filterType}`}
595 label: I18NextService.i18n.t("all"),
606 async function createNewOptions({
612 oldOptions: Choice[];
615 const newOptions: Choice[] = [];
618 const selectedUser = oldOptions.find(
619 ({ value }) => value === id.toString()
623 newOptions.push(selectedUser);
627 if (text.length > 0) {
629 ...(await fetchUsers(text))
630 .slice(0, Number(fetchLimit))
631 .map<Choice>(personToChoice)
638 export class Modlog extends Component<
639 RouteComponentProps<{ communityId?: string }>,
642 private isoData = setIsoData<ModlogData>(this.context);
644 state: ModlogState = {
645 res: { state: "empty" },
646 communityRes: { state: "empty" },
647 loadingModSearch: false,
648 loadingUserSearch: false,
649 userSearchOptions: [],
650 modSearchOptions: [],
654 props: RouteComponentProps<{ communityId?: string }>,
657 super(props, context);
658 this.handlePageChange = this.handlePageChange.bind(this);
659 this.handleUserChange = this.handleUserChange.bind(this);
660 this.handleModChange = this.handleModChange.bind(this);
662 // Only fetch the data if coming from another route
663 if (FirstLoadService.isFirstLoad) {
664 const { res, communityRes, modUserResponse, userResponse } =
665 this.isoData.routeData;
673 if (modUserResponse.state === "success") {
676 modSearchOptions: [personToChoice(modUserResponse.data.person_view)],
680 if (userResponse.state === "success") {
683 userSearchOptions: [personToChoice(userResponse.data.person_view)],
689 async componentDidMount() {
690 await this.refetch();
694 const res = this.state.res;
695 const combined = res.state == "success" ? buildCombined(res.data) : [];
702 <MomentTime published={i.when_} />
705 {this.amAdminOrMod && i.moderator ? (
706 <PersonListing person={i.moderator} />
708 <div>{this.modOrAdminText(i.moderator)}</div>
711 <td>{renderModlogType(i)}</td>
718 get amAdminOrMod(): boolean {
720 this.state.communityRes.state == "success" &&
721 amMod(this.state.communityRes.data.moderators);
722 return amAdmin() || amMod_;
725 modOrAdminText(person?: Person): string {
727 this.isoData.site_res.admins.some(
728 ({ person: { id } }) => id === person.id
730 ? I18NextService.i18n.t("admin")
731 : I18NextService.i18n.t("mod");
734 get documentTitle(): string {
735 return `Modlog - ${this.isoData.site_res.site_view.site.name}`;
745 const { actionType, modId, userId } = getModlogQueryParams();
748 <div className="modlog container-lg">
750 title={this.documentTitle}
751 path={this.context.router.route.match.url}
754 <h1 className="h4 mb-4">{I18NextService.i18n.t("modlog")}</h1>
757 className="alert alert-warning text-sm-start text-xs-center"
761 icon="alert-triangle"
763 classes="me-sm-2 mx-auto d-sm-inline d-block"
765 <T i18nKey="modlog_content_warning" class="d-inline">
769 {this.state.communityRes.state === "success" && (
772 className="text-body"
773 to={`/c/${this.state.communityRes.data.community_view.community.name}`}
775 /c/{this.state.communityRes.data.community_view.community.name}{" "}
777 <span>{I18NextService.i18n.t("modlog")}</span>
780 <div className="row mb-2">
781 <div className="col-sm-6">
784 onChange={linkEvent(this, this.handleFilterActionChange)}
785 className="form-select"
788 <option disabled aria-hidden="true">
789 {I18NextService.i18n.t("filter_by_action")}
791 <option value={"All"}>{I18NextService.i18n.t("all")}</option>
792 <option value={"ModRemovePost"}>Removing Posts</option>
793 <option value={"ModLockPost"}>Locking Posts</option>
794 <option value={"ModFeaturePost"}>Featuring Posts</option>
795 <option value={"ModRemoveComment"}>Removing Comments</option>
796 <option value={"ModRemoveCommunity"}>Removing Communities</option>
797 <option value={"ModBanFromCommunity"}>
798 Banning From Communities
800 <option value={"ModAddCommunity"}>Adding Mod to Community</option>
801 <option value={"ModTransferCommunity"}>
802 Transferring Communities
804 <option value={"ModAdd"}>Adding Mod to Site</option>
805 <option value={"ModBan"}>Banning From Site</option>
809 <div className="row mb-2">
812 onChange={this.handleUserChange}
813 onSearch={this.handleSearchUsers}
815 options={userSearchOptions}
816 loading={loadingUserSearch}
818 {!this.isoData.site_res.site_view.local_site
819 .hide_modlog_mod_names && (
822 onChange={this.handleModChange}
823 onSearch={this.handleSearchMods}
825 options={modSearchOptions}
826 loading={loadingModSearch}
830 {this.renderModlogTable()}
835 renderModlogTable() {
836 switch (this.state.res.state) {
844 const page = getModlogQueryParams().page;
846 <div className="table-responsive">
847 <table id="modlog_table" className="table table-sm table-hover">
848 <thead className="pointer">
850 <th> {I18NextService.i18n.t("time")}</th>
851 <th>{I18NextService.i18n.t("mod")}</th>
852 <th>{I18NextService.i18n.t("action")}</th>
857 <Paginator page={page} onChange={this.handlePageChange} />
864 handleFilterActionChange(i: Modlog, event: any) {
866 actionType: event.target.value as ModlogActionType,
871 handlePageChange(page: number) {
872 this.updateUrl({ page });
875 handleUserChange(option: Choice) {
876 this.updateUrl({ userId: getIdFromString(option.value) ?? null, page: 1 });
879 handleModChange(option: Choice) {
880 this.updateUrl({ modId: getIdFromString(option.value) ?? null, page: 1 });
883 handleSearchUsers = debounce(async (text: string) => {
884 const { userId } = getModlogQueryParams();
885 const { userSearchOptions } = this.state;
886 this.setState({ loadingUserSearch: true });
888 const newOptions = await createNewOptions({
891 oldOptions: userSearchOptions,
895 userSearchOptions: newOptions,
896 loadingUserSearch: false,
900 handleSearchMods = debounce(async (text: string) => {
901 const { modId } = getModlogQueryParams();
902 const { modSearchOptions } = this.state;
903 this.setState({ loadingModSearch: true });
905 const newOptions = await createNewOptions({
908 oldOptions: modSearchOptions,
912 modSearchOptions: newOptions,
913 loadingModSearch: false,
917 async updateUrl({ actionType, modId, page, userId }: Partial<ModlogProps>) {
920 actionType: urlActionType,
923 } = getModlogQueryParams();
925 const queryParams: QueryParams<ModlogProps> = {
926 page: (page ?? urlPage).toString(),
927 actionType: actionType ?? urlActionType,
928 modId: getUpdatedSearchId(modId, urlModId),
929 userId: getUpdatedSearchId(userId, urlUserId),
932 const communityId = this.props.match.params.communityId;
934 this.props.history.push(
935 `/modlog${communityId ? `/${communityId}` : ""}${getQueryString(
940 await this.refetch();
944 const auth = myAuth();
945 const { actionType, page, modId, userId } = getModlogQueryParams();
946 const { communityId: urlCommunityId } = this.props.match.params;
947 const communityId = getIdFromString(urlCommunityId);
949 this.setState({ res: { state: "loading" } });
951 res: await HttpService.client.getModlog({
952 community_id: communityId,
956 other_person_id: userId ?? undefined,
957 mod_person_id: !this.isoData.site_res.site_view.local_site
958 .hide_modlog_mod_names
966 this.setState({ communityRes: { state: "loading" } });
968 communityRes: await HttpService.client.getCommunity({
976 static async fetchInitialData({
979 query: { modId: urlModId, page, userId: urlUserId, actionType },
982 }: InitialFetchRequest<QueryParams<ModlogProps>>): Promise<ModlogData> {
983 const pathSplit = path.split("/");
984 const communityId = getIdFromString(pathSplit[2]);
985 const modId = !site.site_view.local_site.hide_modlog_mod_names
986 ? getIdFromString(urlModId)
988 const userId = getIdFromString(urlUserId);
990 const modlogForm: GetModlog = {
991 page: getPageFromString(page),
993 community_id: communityId,
994 type_: getActionFromString(actionType),
995 mod_person_id: modId,
996 other_person_id: userId,
1000 let communityResponse: RequestState<GetCommunityResponse> = {
1005 const communityForm: GetCommunity = {
1010 communityResponse = await client.getCommunity(communityForm);
1013 let modUserResponse: RequestState<GetPersonDetailsResponse> = {
1018 const getPersonForm: GetPersonDetails = {
1023 modUserResponse = await client.getPersonDetails(getPersonForm);
1026 let userResponse: RequestState<GetPersonDetailsResponse> = {
1031 const getPersonForm: GetPersonDetails = {
1036 userResponse = await client.getPersonDetails(getPersonForm);
1040 res: await client.getModlog(modlogForm),
1041 communityRes: communityResponse,