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}
756 className="alert alert-warning text-sm-start text-xs-center"
760 icon="alert-triangle"
762 classes="me-sm-2 mx-auto d-sm-inline d-block"
764 <T i18nKey="modlog_content_warning" class="d-inline">
768 {this.state.communityRes.state === "success" && (
771 className="text-body"
772 to={`/c/${this.state.communityRes.data.community_view.community.name}`}
774 /c/{this.state.communityRes.data.community_view.community.name}{" "}
776 <span>{I18NextService.i18n.t("modlog")}</span>
779 <div className="row mb-2">
780 <div className="col-sm-6">
783 onChange={linkEvent(this, this.handleFilterActionChange)}
784 className="form-select"
787 <option disabled aria-hidden="true">
788 {I18NextService.i18n.t("filter_by_action")}
790 <option value={"All"}>{I18NextService.i18n.t("all")}</option>
791 <option value={"ModRemovePost"}>Removing Posts</option>
792 <option value={"ModLockPost"}>Locking Posts</option>
793 <option value={"ModFeaturePost"}>Featuring Posts</option>
794 <option value={"ModRemoveComment"}>Removing Comments</option>
795 <option value={"ModRemoveCommunity"}>
798 <option value={"ModBanFromCommunity"}>
799 Banning From Communities
801 <option value={"ModAddCommunity"}>
802 Adding Mod to Community
804 <option value={"ModTransferCommunity"}>
805 Transferring Communities
807 <option value={"ModAdd"}>Adding Mod to Site</option>
808 <option value={"ModBan"}>Banning From Site</option>
812 <div className="row mb-2">
815 onChange={this.handleUserChange}
816 onSearch={this.handleSearchUsers}
818 options={userSearchOptions}
819 loading={loadingUserSearch}
821 {!this.isoData.site_res.site_view.local_site
822 .hide_modlog_mod_names && (
825 onChange={this.handleModChange}
826 onSearch={this.handleSearchMods}
828 options={modSearchOptions}
829 loading={loadingModSearch}
833 {this.renderModlogTable()}
839 renderModlogTable() {
840 switch (this.state.res.state) {
848 const page = getModlogQueryParams().page;
850 <div className="table-responsive">
851 <table id="modlog_table" className="table table-sm table-hover">
852 <thead className="pointer">
854 <th> {I18NextService.i18n.t("time")}</th>
855 <th>{I18NextService.i18n.t("mod")}</th>
856 <th>{I18NextService.i18n.t("action")}</th>
861 <Paginator page={page} onChange={this.handlePageChange} />
868 handleFilterActionChange(i: Modlog, event: any) {
870 actionType: event.target.value as ModlogActionType,
875 handlePageChange(page: number) {
876 this.updateUrl({ page });
879 handleUserChange(option: Choice) {
880 this.updateUrl({ userId: getIdFromString(option.value) ?? null, page: 1 });
883 handleModChange(option: Choice) {
884 this.updateUrl({ modId: getIdFromString(option.value) ?? null, page: 1 });
887 handleSearchUsers = debounce(async (text: string) => {
888 const { userId } = getModlogQueryParams();
889 const { userSearchOptions } = this.state;
890 this.setState({ loadingUserSearch: true });
892 const newOptions = await createNewOptions({
895 oldOptions: userSearchOptions,
899 userSearchOptions: newOptions,
900 loadingUserSearch: false,
904 handleSearchMods = debounce(async (text: string) => {
905 const { modId } = getModlogQueryParams();
906 const { modSearchOptions } = this.state;
907 this.setState({ loadingModSearch: true });
909 const newOptions = await createNewOptions({
912 oldOptions: modSearchOptions,
916 modSearchOptions: newOptions,
917 loadingModSearch: false,
921 async updateUrl({ actionType, modId, page, userId }: Partial<ModlogProps>) {
924 actionType: urlActionType,
927 } = getModlogQueryParams();
929 const queryParams: QueryParams<ModlogProps> = {
930 page: (page ?? urlPage).toString(),
931 actionType: actionType ?? urlActionType,
932 modId: getUpdatedSearchId(modId, urlModId),
933 userId: getUpdatedSearchId(userId, urlUserId),
936 const communityId = this.props.match.params.communityId;
938 this.props.history.push(
939 `/modlog${communityId ? `/${communityId}` : ""}${getQueryString(
944 await this.refetch();
948 const auth = myAuth();
949 const { actionType, page, modId, userId } = getModlogQueryParams();
950 const { communityId: urlCommunityId } = this.props.match.params;
951 const communityId = getIdFromString(urlCommunityId);
953 this.setState({ res: { state: "loading" } });
955 res: await HttpService.client.getModlog({
956 community_id: communityId,
960 other_person_id: userId ?? undefined,
961 mod_person_id: !this.isoData.site_res.site_view.local_site
962 .hide_modlog_mod_names
970 this.setState({ communityRes: { state: "loading" } });
972 communityRes: await HttpService.client.getCommunity({
980 static async fetchInitialData({
983 query: { modId: urlModId, page, userId: urlUserId, actionType },
986 }: InitialFetchRequest<QueryParams<ModlogProps>>): Promise<ModlogData> {
987 const pathSplit = path.split("/");
988 const communityId = getIdFromString(pathSplit[2]);
989 const modId = !site.site_view.local_site.hide_modlog_mod_names
990 ? getIdFromString(urlModId)
992 const userId = getIdFromString(urlUserId);
994 const modlogForm: GetModlog = {
995 page: getPageFromString(page),
997 community_id: communityId,
998 type_: getActionFromString(actionType),
999 mod_person_id: modId,
1000 other_person_id: userId,
1004 let communityResponse: RequestState<GetCommunityResponse> = {
1009 const communityForm: GetCommunity = {
1014 communityResponse = await client.getCommunity(communityForm);
1017 let modUserResponse: RequestState<GetPersonDetailsResponse> = {
1022 const getPersonForm: GetPersonDetails = {
1027 modUserResponse = await client.getPersonDetails(getPersonForm);
1030 let userResponse: RequestState<GetPersonDetailsResponse> = {
1035 const getPersonForm: GetPersonDetails = {
1040 userResponse = await client.getPersonDetails(getPersonForm);
1044 res: await client.getModlog(modlogForm),
1045 communityRes: communityResponse,