Skip to content

Commit

Permalink
Sync user data on profile change
Browse files Browse the repository at this point in the history
  • Loading branch information
joonashak committed Aug 7, 2023
1 parent 9c0252e commit 021c2a1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
48 changes: 48 additions & 0 deletions app-nest/manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
display_information:
name: Hybridilusmu
description: Hybridityöskentelyn tukisovellus
features:
app_home:
home_tab_enabled: true
messages_tab_enabled: true
messages_tab_read_only_enabled: false
bot_user:
display_name: hybridilusmu
always_online: true
oauth_config:
scopes:
bot:
- channels:history
- channels:join
- chat:write
- groups:history
- groups:read
- im:history
- im:read
- mpim:history
- channels:read
- reactions:read
- usergroups:read
- commands
- users:read
settings:
event_subscriptions:
bot_events:
- member_joined_channel
- member_left_channel
- message.channels
- message.groups
- message.im
- message.mpim
- reaction_added
- app_home_opened
- subteam_created
- subteam_members_changed
- subteam_updated
- channel_left
- user_profile_changed
interactivity:
is_enabled: true
org_deploy_enabled: false
socket_mode_enabled: true
token_rotation_enabled: false
13 changes: 13 additions & 0 deletions app-nest/src/sync/sync.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Controller } from "@nestjs/common";
import BoltEvent from "../bolt/decorators/bolt-event.decorator";
import { UserSyncService } from "./user-sync.service";

@Controller()
export class SyncController {
constructor(private userSyncService: UserSyncService) {}

@BoltEvent("user_profile_changed")
userProfileChanged() {
return ({ event }) => this.userSyncService.syncUsers(event.user);
}
}
2 changes: 2 additions & 0 deletions app-nest/src/sync/sync.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Module } from "@nestjs/common";
import { UserModule } from "../entities/user/user.module";
import { UserService } from "../entities/user/user.service";
import { SyncController } from "./sync.controller";
import { SyncService } from "./sync.service";
import { UserSyncService } from "./user-sync.service";

@Module({
imports: [UserModule],
providers: [SyncService, UserSyncService, UserService],
controllers: [SyncController],
exports: [SyncService, UserSyncService],
})
export class SyncModule {}
27 changes: 22 additions & 5 deletions app-nest/src/sync/user-sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { UsersListResponse } from "@slack/web-api";
import { BoltUserService } from "../bolt/bolt-user.service";
import { UserService } from "../entities/user/user.service";

type SlackMember = UsersListResponse["members"][0];

@Injectable()
export class UserSyncService {
private logger = new Logger(UserSyncService.name);
Expand All @@ -12,11 +14,26 @@ export class UserSyncService {
private userService: UserService,
) {}

async syncUsers() {
this.logger.log("Starting user data synchronization.");
const data = await this.boltUserService.getUsers();
/**
* Synchronize users from Slack to local database.
*
* By default, all users are synchronized. Optionally, a single user may be
* updated by passing their data as `updateOverride` argument.
*/
async syncUsers(updateOverride?: SlackMember) {
if (updateOverride) {
this.logger.log(
`Starting user data synchronization for user ${updateOverride.id}.`,
);
} else {
this.logger.log("Starting user data synchronization for all users.");
}

const slackUsers = updateOverride
? [updateOverride]
: (await this.boltUserService.getUsers()).members;

const users = data.members.filter(this.appUserFilter).map((user) => ({
const users = slackUsers.filter(this.appUserFilter).map((user) => ({
slackId: user.id,
displayName: user.profile.display_name || "",
realName: user.profile.real_name || "",
Expand All @@ -29,7 +46,7 @@ export class UserSyncService {
/**
* Filter out bots, restricted and deleted users, leaving only real app users.
*/
private appUserFilter(user: UsersListResponse["members"][0]) {
private appUserFilter(user: SlackMember) {
return (
user.id !== "USLACKBOT" &&
!user.is_bot &&
Expand Down

0 comments on commit 021c2a1

Please sign in to comment.