1
0
mirror of https://github.com/spacebarchat/client.git synced 2024-11-25 11:42:30 +01:00

fix kick,ban,roles context menu items being shown without permission

This commit is contained in:
Puyodead1 2023-12-24 13:00:40 -05:00
parent d6e89d5d88
commit 85bd86a6e6
No known key found for this signature in database
GPG Key ID: A4FA4FEC0DD353FC
2 changed files with 33 additions and 12 deletions

View File

@ -1,6 +1,7 @@
// loosely based on https://github.com/revoltchat/frontend/blob/master/components/app/menus/UserContextMenu.tsx
import { modalController } from "../../controllers/modals";
import { useAppStore } from "../../stores/AppStore";
import GuildMember from "../../stores/objects/GuildMember";
import User from "../../stores/objects/User";
import { ContextMenu, ContextMenuButton, ContextMenuDivider } from "./ContextMenu";
@ -11,6 +12,10 @@ interface MenuProps {
}
function UserContextMenu({ user, member }: MenuProps) {
const app = useAppStore();
const guild = member ? app.guilds.get(member.guild.id) : undefined;
const guildMe = guild ? guild.members.get(app.account!.id) : undefined;
/**
* Copy user id to clipboard
*/
@ -50,19 +55,29 @@ function UserContextMenu({ user, member }: MenuProps) {
<ContextMenuButton disabled>Add Friend</ContextMenuButton>
<ContextMenuButton disabled>Block</ContextMenuButton>
<ContextMenuDivider />
{member && (
{member && guildMe && (
<>
<ContextMenuButton destructive onClick={kick}>
Kick {member?.nick ?? user.username}
</ContextMenuButton>
<ContextMenuButton destructive onClick={ban}>
Ban {member?.nick ?? user.username}
</ContextMenuButton>
<ContextMenuDivider />
<ContextMenuButton disabled icon="mdiChevronRight">
Roles
</ContextMenuButton>
<ContextMenuDivider />
{guildMe.hasPermission("KICK_MEMBERS") && (
<ContextMenuButton destructive onClick={kick}>
Kick {member?.nick ?? user.username}
</ContextMenuButton>
)}
{guildMe.hasPermission("BAN_MEMBERS") && (
<>
<ContextMenuButton destructive onClick={ban}>
Ban {member?.nick ?? user.username}
</ContextMenuButton>
<ContextMenuDivider />
</>
)}
{guildMe.hasPermission("MANAGE_ROLES") && (
<>
<ContextMenuButton disabled icon="mdiChevronRight">
Roles
</ContextMenuButton>
<ContextMenuDivider />
</>
)}
</>
)}

View File

@ -4,6 +4,7 @@ import {
GuildMemberFlags,
} from "@spacebarchat/spacebar-api-types/v9";
import { action, computed, observable } from "mobx";
import { PermissionResolvable, Permissions } from "../../utils/Permissions";
import AppStore from "../AppStore";
import Guild from "./Guild";
import Role from "./Role";
@ -68,4 +69,9 @@ export default class GuildMember {
async ban(reason?: string, deleteMessageSeconds?: number) {
return this.guild.banMember(this.user!.id, reason, deleteMessageSeconds);
}
hasPermission(permission: PermissionResolvable) {
const permissions = Permissions.getPermission(this.app.account!.id, this.guild);
return permissions.has(permission);
}
}