-
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.
Merge pull request #102 from funidata/nest
Sync users between slack and app database
- Loading branch information
Showing
16 changed files
with
320 additions
and
26 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 |
---|---|---|
@@ -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 |
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
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
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { BoltService } from "./bolt.service"; | ||
|
||
@Injectable() | ||
export class BoltUserService { | ||
constructor(private boltService: BoltService) {} | ||
|
||
async getUsers() { | ||
return this.boltService.getBolt().client.users.list(); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { SetMetadata } from "@nestjs/common"; | ||
|
||
export const BOLT_ACTION_KEY = "BoltAction"; | ||
|
||
const BoltAction = (actionName: string) => | ||
SetMetadata(BOLT_ACTION_KEY, actionName); | ||
|
||
export default BoltAction; |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Controller } from "@nestjs/common"; | ||
import BoltAction from "../bolt/decorators/bolt-action.decorator"; | ||
import { UserSyncService } from "../sync/user-sync.service"; | ||
|
||
@Controller() | ||
export class DevToolsController { | ||
constructor(private userSyncService: UserSyncService) {} | ||
|
||
@BoltAction("sync_users") | ||
syncUsers() { | ||
return async ({ ack }) => { | ||
await ack(); | ||
await this.userSyncService.syncUsers(); | ||
}; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { SyncModule } from "../sync/sync.module"; | ||
import { DevToolsController } from "./dev-tools.controller"; | ||
|
||
@Module({ imports: [SyncModule], controllers: [DevToolsController] }) | ||
export class DevToolsModule {} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const devTools = [ | ||
{ | ||
type: "header", | ||
text: { | ||
type: "plain_text", | ||
text: ":wrench: Developer Tools", | ||
}, | ||
}, | ||
{ | ||
type: "actions", | ||
elements: [ | ||
{ | ||
type: "button", | ||
text: { | ||
type: "plain_text", | ||
text: ":recycle: Sync Users", | ||
}, | ||
action_id: "sync_users", | ||
}, | ||
], | ||
}, | ||
{ | ||
type: "context", | ||
elements: [ | ||
{ | ||
type: "plain_text", | ||
text: "In development environment, users are not synchronized between local database and Slack on app start to avoid running into API rate limits due to hot-reloads. Sync users manually when necessary.", | ||
}, | ||
], | ||
}, | ||
{ | ||
type: "divider", | ||
}, | ||
]; | ||
|
||
export default devTools; |
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
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 |
---|---|---|
@@ -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); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +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 {} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Injectable, OnApplicationBootstrap } from "@nestjs/common"; | ||
import { inDevelopmentEnvironment } from "../config/utils"; | ||
import { UserSyncService } from "./user-sync.service"; | ||
|
||
@Injectable() | ||
export class SyncService implements OnApplicationBootstrap { | ||
constructor(private userSyncService: UserSyncService) {} | ||
|
||
async onApplicationBootstrap() { | ||
if (!inDevelopmentEnvironment) { | ||
await this.userSyncService.syncUsers(); | ||
} | ||
} | ||
} |
Oops, something went wrong.