this repo has no description
1/**
2 * Check if a user has permission to manage a Slack channel
3 * Returns true if the user is:
4 * - A global admin (in ADMINS env var)
5 * - The channel creator
6 * - A channel manager
7 */
8export async function canManageChannel(
9 userId: string,
10 channelId: string,
11): Promise<boolean> {
12 // Check if user is a global admin
13 const admins = process.env.ADMINS?.split(",").map((id) => id.trim()) || [];
14 if (admins.includes(userId)) {
15 return true;
16 }
17
18 try {
19 // Check if user is channel creator
20 const channelInfo = await fetch(
21 "https://slack.com/api/conversations.info",
22 {
23 method: "POST",
24 headers: {
25 "Content-Type": "application/json",
26 Authorization: `Bearer ${process.env.SLACK_BOT_TOKEN}`,
27 },
28 body: JSON.stringify({ channel: channelId }),
29 },
30 ).then((res) => res.json());
31
32 if (channelInfo.ok && channelInfo.channel?.creator === userId) {
33 return true;
34 }
35
36 // Check if user is a channel manager
37 if (
38 process.env.SLACK_USER_COOKIE &&
39 process.env.SLACK_USER_TOKEN &&
40 process.env.SLACK_API_URL
41 ) {
42 const formdata = new FormData();
43 formdata.append("token", process.env.SLACK_USER_TOKEN);
44 formdata.append("entity_id", channelId);
45
46 const response = await fetch(
47 `${process.env.SLACK_API_URL}/api/admin.roles.entity.listAssignments`,
48 {
49 method: "POST",
50 headers: {
51 Cookie: process.env.SLACK_USER_COOKIE,
52 },
53 body: formdata,
54 },
55 );
56
57 const json = await response.json();
58
59 if (json.ok) {
60 const managers = json.role_assignments?.[0]?.users || [];
61 if (managers.includes(userId)) {
62 return true;
63 }
64 }
65 }
66 } catch (error) {
67 console.error("Error checking channel permissions:", error);
68 }
69
70 return false;
71}