]> Untitled Git - lemmy-ui.git/blob - src/shared/utils/roles/can-mod.ts
df639b7fabd3b0d426b7478bf1489df2c331e9d5
[lemmy-ui.git] / src / shared / utils / roles / can-mod.ts
1 import { CommunityModeratorView, PersonView } from "lemmy-js-client";
2 import { UserService } from "../../services";
3
4 export default function canMod(
5   creator_id: number,
6   mods?: CommunityModeratorView[],
7   admins?: PersonView[],
8   myUserInfo = UserService.Instance.myUserInfo,
9   onSelf = false
10 ): boolean {
11   // You can do moderator actions only on the mods added after you.
12   let adminsThenMods =
13     admins
14       ?.map(a => a.person.id)
15       .concat(mods?.map(m => m.moderator.id) ?? []) ?? [];
16
17   if (myUserInfo) {
18     const myIndex = adminsThenMods.findIndex(
19       id => id == myUserInfo.local_user_view.person.id
20     );
21     if (myIndex == -1) {
22       return false;
23     } else {
24       // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
25       adminsThenMods = adminsThenMods.slice(0, myIndex + (onSelf ? 0 : 1));
26       return !adminsThenMods.includes(creator_id);
27     }
28   } else {
29     return false;
30   }
31 }