Skip to content

Commit

Permalink
Bot permissions (#7107)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianjelfs authored Dec 20, 2024
1 parent a0b36eb commit 9297a63
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions frontend/app/src/components/bots/CommandSelector.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
<script lang="ts">
import Close from "svelte-material-icons/Close.svelte";
import HoverIcon from "../HoverIcon.svelte";
import {
emptySlashCommandPermissions,
hasEveryRequiredPermission,
type FlattenedCommand,
} from "openchat-shared";
import { hasEveryRequiredPermission, type FlattenedCommand } from "openchat-shared";
import Translatable from "../Translatable.svelte";
import { i18nKey } from "../../i18n/i18n";
import {
Expand Down Expand Up @@ -33,6 +29,7 @@
} from "openchat-client";
import {
currentCommunityBots,
currentChatBots,
isPermitted,
selectedChatStore,
selectedCommunity,
Expand All @@ -59,13 +56,15 @@
let { onCancel, onNoMatches, onCommandSent, mode, messageContext }: Props = $props();
let emptyPermissions = emptySlashCommandPermissions();
let installedBots = $derived(
messageContext.chatId.kind === "channel" ? $currentCommunityBots : $currentChatBots,
);
let commands = $derived.by(() =>
$commandsStore.filter((c) => {
return hasPermissionForCommand(
c,
$currentCommunityBots,
installedBots,
$selectedChatStore,
$selectedCommunity,
);
Expand Down Expand Up @@ -116,16 +115,20 @@
function hasPermissionForCommand(
command: FlattenedCommand,
currenCommunityBots: Map<string, SlashCommandPermissions>,
installedBots: Map<string, SlashCommandPermissions>,
chat: ChatSummary | undefined,
community: CommunitySummary | undefined,
): boolean {
const userPermission = userHasPermissionForCommand(command, chat, community);
if (command.kind === "external_bot") {
// for an external bot we also need to know that the bot has been granted all the permissions it requires
const granted = currenCommunityBots.get(command.botId) ?? emptyPermissions;
const granted = installedBots.get(command.botId);
const required = command.permissions;
return userPermission && hasEveryRequiredPermission(required, granted);
return (
userPermission &&
granted !== undefined &&
hasEveryRequiredPermission(required, granted)
);
} else {
return userPermission;
}
Expand Down

0 comments on commit 9297a63

Please sign in to comment.