Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: store member directory profile urls on slack profile 👥 #563

Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions packages/core/src/modules/slack/services/slack-user.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { generatePath } from '@remix-run/react';

import { db } from '@oyster/db';

import { slack } from '@/modules/slack/instances';
import { ENV } from '@/shared/env';
import { RateLimiter } from '@/shared/utils/rate-limiter';

/**
* @see https://api.slack.com/methods/users.lookupByEmail
Expand Down Expand Up @@ -35,3 +40,82 @@ export async function updateSlackEmail(id: string, email: string) {
user: id,
});
}

/**
* Retrieve all members with both Slack and Member Directory Profiles
*/

export async function getMembersWithSlackAndDirectoryProfiles() {
try {
const members = await db
.selectFrom('students')
.select(['id', 'email', 'joinedSlackAt', 'joinedMemberDirectoryAt'])
.where('joinedSlackAt', 'is not', null)
.where('joinedMemberDirectoryAt', 'is not', null)
.execute();

return members;
} catch (error) {
console.error('Failed to retrieve members with profiles', error);

return [];
}
}

/**
* @see https://api.slack.com/apis/rate-limits#tier_t3
*/
const updateProfileRateLimiter = new RateLimiter('slack:profile:update', {
rateLimit: 50,
rateLimitWindow: 60,
});

export async function addMemberDirectoryURLToSlackProfile(
slackUserId: string,
memberId: string
) {
try {
await updateProfileRateLimiter.process();

const memberDirectoryURL = generatePath(ENV.MEMBER_DIRECTORY_URL, {
id: memberId,
});

const profile = {
fields: {
[ENV.SLACK_MEMBER_DIRECTORY_FIELD_ID]: {
value: memberDirectoryURL,
alt: 'Member Directory Profile',
},
},
};

await slack.users.profile.set({
profile: JSON.stringify(profile),
token: ENV.SLACK_ADMIN_TOKEN,
user: slackUserId,
});

console.log(`Successfully updated Slack Profile ${slackUserId}`);
} catch (error) {
console.error(`Failed To Update Slack Profile ${slackUserId}`, error);
}
}

export async function updateSlackProfilesWithMemberDirectoryURLs() {
const members = await getMembersWithSlackAndDirectoryProfiles();

console.log(`Found ${members.length} members to update`);

for (const member of members) {
const { id, email } = member;

const slackUser = await getSlackUserByEmail(email);

if (slackUser) {
await addMemberDirectoryURLToSlackProfile(slackUser.id, id);
} else {
console.log(`No Slack User Found for email: ${email}`);
}
}
}
3 changes: 3 additions & 0 deletions packages/core/src/shared/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const ENV = {
INTERNAL_SLACK_NOTIFICATIONS_CHANNEL_ID: process.env
.INTERNAL_SLACK_NOTIFICATIONS_CHANNEL_ID as string,
JWT_SECRET: process.env.JWT_SECRET as string,
MEMBER_DIRECTORY_URL: process.env.MEMBER_DIRECTORY_URL as string,
MEMBER_PROFILE_URL: process.env.MEMBER_PROFILE_URL as string,
REDIS_URL: process.env.REDIS_URL as string,
SENTRY_DSN: process.env.SENTRY_DSN as string,
Expand All @@ -28,6 +29,8 @@ export const ENV = {
SLACK_CLIENT_SECRET: process.env.SLACK_CLIENT_SECRET as string,
SLACK_INTRODUCTIONS_CHANNEL_ID: process.env
.SLACK_INTRODUCTIONS_CHANNEL_ID as string,
SLACK_MEMBER_DIRECTORY_FIELD_ID: process.env
.SLACK_MEMBER_DIRECTORY_FIELD_ID as string,
SLACK_SIGNING_SECRET: process.env.SLACK_SIGNING_SECRET as string,
STUDENT_PROFILE_URL: process.env.STUDENT_PROFILE_URL as string,
};
Expand Down
Loading