Wave-based onboarding makes the auto-invite + polling path obsolete. - Removes SlackService.inviteUserToSlack — admins now send invites through Slack's UI and flip the flag in our admin endpoint. - Removes the slack_invite_failed admin alert + its detector. The alert no longer has a meaningful trigger (we don't attempt invites). - Archives server/utils/checkSlackJoins.js (and its test) under _archive/ in case the polling pattern is needed again post-pilot. - Deletes the Nitro plugin that scheduled checkSlackJoins on boot + hourly. Nothing in nitro.config / nuxt.config / package.json registered it elsewhere. - Drops the slack_invite_failed branch from adminAlerts.test; the enum slug stays in adminAlertDismissal so historical dismissal rows continue to validate. notifyNewMember (vetting-channel notification) and findUserByEmail (used by the auto-flag helper) are retained.
262 lines
7.2 KiB
TypeScript
262 lines
7.2 KiB
TypeScript
import { WebClient } from "@slack/web-api";
|
|
|
|
export class SlackService {
|
|
private client: WebClient;
|
|
private vettingChannelId: string;
|
|
|
|
constructor(botToken: string, vettingChannelId: string) {
|
|
this.client = new WebClient(botToken);
|
|
this.vettingChannelId = vettingChannelId;
|
|
}
|
|
|
|
/**
|
|
* Find user in workspace by email
|
|
*/
|
|
async findUserByEmail(email: string): Promise<string | null> {
|
|
try {
|
|
const response = await this.client.users.lookupByEmail({ email });
|
|
return response.user?.id || null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Find user ID by username (display name or real name)
|
|
*/
|
|
async findUserIdByUsername(username: string): Promise<string | null> {
|
|
try {
|
|
const cleanUsername = username.replace("@", "").toLowerCase();
|
|
|
|
// List all users and search for matching username
|
|
const response = await this.client.users.list();
|
|
|
|
if (!response.members) {
|
|
return null;
|
|
}
|
|
|
|
// Search for user by name or display_name
|
|
const user = response.members.find((member: any) => {
|
|
const name = member.name?.toLowerCase() || "";
|
|
const realName = member.real_name?.toLowerCase() || "";
|
|
const displayName = member.profile?.display_name?.toLowerCase() || "";
|
|
|
|
return (
|
|
name === cleanUsername ||
|
|
displayName === cleanUsername ||
|
|
realName.includes(cleanUsername)
|
|
);
|
|
});
|
|
|
|
return user?.id || null;
|
|
} catch (error) {
|
|
console.error("Error looking up Slack user by username:", error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Open/get a DM channel with a user and return the channel ID
|
|
* This creates or opens a DM conversation and returns the channel ID (starts with D)
|
|
*/
|
|
async openDMChannel(userId: string): Promise<string | null> {
|
|
try {
|
|
const response = await this.client.conversations.open({
|
|
users: userId,
|
|
});
|
|
|
|
if (response.ok && response.channel?.id) {
|
|
console.log(
|
|
`Opened DM channel for user ${userId}: ${response.channel.id}`,
|
|
);
|
|
return response.channel.id;
|
|
}
|
|
|
|
return null;
|
|
} catch (error: any) {
|
|
console.error(
|
|
"Error opening DM channel:",
|
|
error.data?.error || error.message,
|
|
);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send a notification to the vetting channel about a new member
|
|
*/
|
|
async notifyNewMember(
|
|
memberName: string,
|
|
memberEmail: string,
|
|
circle: string,
|
|
contributionAmount: number,
|
|
invitationStatus: string = "manual_invitation_required",
|
|
): Promise<void> {
|
|
try {
|
|
let statusMessage = "";
|
|
let actionMessage = "";
|
|
|
|
switch (invitationStatus) {
|
|
case "existing_user_added_to_channel":
|
|
statusMessage =
|
|
"✅ Existing user automatically added to this channel.";
|
|
actionMessage = "Ready for vetting!";
|
|
break;
|
|
case "user_already_in_channel":
|
|
statusMessage = "✅ User is already in this channel.";
|
|
actionMessage = "Ready for vetting!";
|
|
break;
|
|
case "new_user_invited_to_workspace":
|
|
statusMessage =
|
|
"🎉 User successfully invited to workspace as single-channel guest.";
|
|
actionMessage = "Ready for vetting!";
|
|
break;
|
|
case "manual_invitation_required":
|
|
statusMessage = "📧 User needs to be manually invited to join Slack.";
|
|
actionMessage = `Please vet this new member before inviting them to other channels.`;
|
|
break;
|
|
default:
|
|
statusMessage = "⚠️ Invitation status unknown.";
|
|
actionMessage = "Manual review required.";
|
|
}
|
|
|
|
await this.client.chat.postMessage({
|
|
channel: this.vettingChannelId,
|
|
text: `New Ghost Guild member: ${memberName}`,
|
|
blocks: [
|
|
{
|
|
type: "header",
|
|
text: {
|
|
type: "plain_text",
|
|
text: "New Ghost Guild Member Registration",
|
|
},
|
|
},
|
|
{
|
|
type: "section",
|
|
fields: [
|
|
{
|
|
type: "mrkdwn",
|
|
text: `*Name:*\n${memberName}`,
|
|
},
|
|
{
|
|
type: "mrkdwn",
|
|
text: `*Email:*\n${memberEmail}`,
|
|
},
|
|
{
|
|
type: "mrkdwn",
|
|
text: `*Circle:*\n${circle}`,
|
|
},
|
|
{
|
|
type: "mrkdwn",
|
|
text: `*Contribution:*\n$${contributionAmount}/month`,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
type: "section",
|
|
text: {
|
|
type: "mrkdwn",
|
|
text: `*Status:* ${statusMessage}\n*Action:* ${actionMessage}`,
|
|
},
|
|
},
|
|
{
|
|
type: "divider",
|
|
},
|
|
],
|
|
});
|
|
} catch (error) {
|
|
console.error("Failed to send Slack notification:", error);
|
|
// Don't throw - this is non-critical
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a new Slack channel. Returns the new channel id and normalized name.
|
|
*/
|
|
async createChannel(
|
|
name: string,
|
|
isPrivate: boolean = false,
|
|
): Promise<{ id: string; name: string }> {
|
|
const normalized = name
|
|
.trim()
|
|
.replace(/^#/, '')
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9_-]+/g, '-')
|
|
.replace(/^-+|-+$/g, '')
|
|
.slice(0, 80)
|
|
|
|
const response = await this.client.conversations.create({
|
|
name: normalized,
|
|
is_private: isPrivate,
|
|
})
|
|
|
|
if (!response.ok || !response.channel?.id || !response.channel?.name) {
|
|
throw new Error(`Slack create failed: ${response.error || 'unknown'}`)
|
|
}
|
|
|
|
return { id: response.channel.id, name: response.channel.name }
|
|
}
|
|
|
|
/**
|
|
* Verify the Slack channel exists and bot has access
|
|
*/
|
|
async verifyChannelAccess(): Promise<boolean> {
|
|
try {
|
|
const response = await this.client.conversations.info({
|
|
channel: this.vettingChannelId,
|
|
});
|
|
return response.ok && !!response.channel;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get configured Slack service instance
|
|
*/
|
|
export function getSlackService(): SlackService | null {
|
|
const config = useRuntimeConfig();
|
|
|
|
if (!config.slackBotToken || !config.slackVettingChannelId) {
|
|
console.warn(
|
|
"Slack integration not configured - missing bot token or channel ID",
|
|
);
|
|
return null;
|
|
}
|
|
|
|
return new SlackService(config.slackBotToken, config.slackVettingChannelId);
|
|
}
|
|
|
|
/**
|
|
* Get a SlackService for operations that don't need the vetting channel.
|
|
*/
|
|
export function getSlackServiceNoVetting(): SlackService | null {
|
|
const config = useRuntimeConfig();
|
|
|
|
if (!config.slackBotToken) {
|
|
console.warn("Slack integration not configured - missing bot token");
|
|
return null;
|
|
}
|
|
|
|
return new SlackService(config.slackBotToken, "");
|
|
}
|
|
|
|
/**
|
|
* Get a SlackService backed by the AdminGhost app token for admin-only
|
|
* operations like channel creation. Falls back to the main bot token if
|
|
* AdminGhost isn't configured.
|
|
*/
|
|
export function getSlackAdminService(): SlackService | null {
|
|
const config = useRuntimeConfig();
|
|
|
|
const token = config.slackAdminBotToken || config.slackBotToken;
|
|
if (!token) {
|
|
console.warn(
|
|
"Slack admin integration not configured - missing admin bot token",
|
|
);
|
|
return null;
|
|
}
|
|
|
|
return new SlackService(token, "");
|
|
}
|