-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
61 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn, Repository } from "typeorm"; | ||
import { Column, Entity, PrimaryColumn, Repository } from "typeorm"; | ||
|
||
@Entity() | ||
export class User { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
@PrimaryColumn() | ||
slackId: string; | ||
|
||
@Column() | ||
slackId: string; | ||
displayName: string; | ||
|
||
@Column() | ||
realName: string; | ||
} | ||
|
||
export type UserRepository = Repository<User>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,26 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { InjectRepository } from "@nestjs/typeorm"; | ||
import { DataSource } from "typeorm"; | ||
import { User, UserRepository } from "./user.entity"; | ||
|
||
@Injectable() | ||
export class UserService { | ||
constructor(@InjectRepository(User) private userRepository: UserRepository) {} | ||
constructor( | ||
@InjectRepository(User) private userRepository: UserRepository, | ||
private dataSource: DataSource, | ||
) {} | ||
|
||
async findAll() { | ||
return this.userRepository.find(); | ||
} | ||
|
||
async upsert(users: User[]) { | ||
return this.dataSource | ||
.createQueryBuilder() | ||
.insert() | ||
.into(User) | ||
.values(users) | ||
.orUpdate(["displayName", "realName"], ["slackId"]) | ||
.execute(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { UserModule } from "../entities/user/user.module"; | ||
import { UserService } from "../entities/user/user.service"; | ||
import { SyncService } from "./sync.service"; | ||
import { UserSyncService } from "./user-sync.service"; | ||
|
||
@Module({ providers: [SyncService, UserSyncService] }) | ||
@Module({ | ||
imports: [UserModule], | ||
providers: [SyncService, UserSyncService, UserService], | ||
}) | ||
export class SyncModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,42 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { Injectable, Logger } from "@nestjs/common"; | ||
import { UsersListResponse } from "@slack/web-api"; | ||
import { BoltUserService } from "../bolt/bolt-user.service"; | ||
import { UserService } from "../entities/user/user.service"; | ||
|
||
@Injectable() | ||
export class UserSyncService { | ||
constructor(private boltUserService: BoltUserService) {} | ||
private logger = new Logger(UserSyncService.name); | ||
|
||
constructor( | ||
private boltUserService: BoltUserService, | ||
private userService: UserService, | ||
) {} | ||
|
||
async syncUsers() { | ||
const users = await this.boltUserService.getUsers(); | ||
// FIXME: This needs to be limited in dev env to protect against rate-limit errors. Add UI button to fire this manually. | ||
this.logger.log("Starting user data synchronization."); | ||
const data = await this.boltUserService.getUsers(); | ||
|
||
const users = data.members.filter(this.appUserFilter).map((user) => ({ | ||
slackId: user.id, | ||
displayName: user.profile.display_name || "", | ||
realName: user.profile.real_name || "", | ||
})); | ||
|
||
await this.userService.upsert(users); | ||
this.logger.log("User data synchronized."); | ||
} | ||
|
||
// console.log(users); | ||
for (const user of users.members) { | ||
console.log(user); | ||
console.log(user.profile); | ||
} | ||
/** | ||
* Filter out bots, restricted and deleted users, leaving only real app users. | ||
*/ | ||
private appUserFilter(user: UsersListResponse["members"][0]) { | ||
return ( | ||
user.id !== "USLACKBOT" && | ||
!user.is_bot && | ||
!user.is_restricted && | ||
!user.is_ultra_restricted && | ||
!user.deleted | ||
); | ||
} | ||
} |