-
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 #22 from funidata/trunk
Implement make-shift view switching in home tab
- Loading branch information
Showing
11 changed files
with
228 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,22 +1,40 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { Actions, Button, ViewBlockBuilder } from "slack-block-builder"; | ||
import { Actions, Button, Divider, ViewBlockBuilder } from "slack-block-builder"; | ||
import { Appendable } from "slack-block-builder/dist/internal"; | ||
import Action from "../../bolt/enums/action.enum"; | ||
import { ConfigService } from "../../common/config/config.service"; | ||
import { DevUiBuilder } from "../dev/dev-ui.builder"; | ||
import { ViewCache } from "./view.cache"; | ||
|
||
@Injectable() | ||
export class HomeTabControls { | ||
constructor(private devToolsBuilder: DevUiBuilder) {} | ||
constructor( | ||
private devToolsBuilder: DevUiBuilder, | ||
private configService: ConfigService, | ||
private viewCache: ViewCache, | ||
) {} | ||
|
||
async build(userId: string): Promise<Appendable<ViewBlockBuilder>> { | ||
const { selectedView } = await this.viewCache.get(userId); | ||
|
||
const devTools = this.configService.getConfig().hideDevTools | ||
? [] | ||
: this.devToolsBuilder.buildBlocks(); | ||
|
||
build(): Appendable<ViewBlockBuilder> { | ||
const devTools = this.devToolsBuilder.buildBlocks(); | ||
return [ | ||
...devTools, | ||
Actions().elements([ | ||
Button({ text: "Ilmoittautuminen" }), | ||
Button({ text: "Läsnäolijat", actionId: Action.OPEN_PRESENCE_VIEW }), | ||
Button({ text: "Asetukset" }), | ||
Button({ text: "Ilmoittautuminen", actionId: Action.OPEN_REGISTRATION_VIEW }).primary( | ||
selectedView === "registration", | ||
), | ||
Button({ text: "Läsnäolijat", actionId: Action.OPEN_PRESENCE_VIEW }).primary( | ||
selectedView === "presence", | ||
), | ||
Button({ text: "Asetukset", actionId: Action.OPEN_SETTINGS_VIEW }).primary( | ||
selectedView === "settings", | ||
), | ||
]), | ||
Divider(), | ||
]; | ||
} | ||
} |
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,32 +1,86 @@ | ||
import { Controller } from "@nestjs/common"; | ||
import { Appendable, ViewBlockBuilder } from "slack-block-builder/dist/internal"; | ||
import BoltAction from "../../bolt/decorators/bolt-action.decorator"; | ||
import BoltEvent from "../../bolt/decorators/bolt-event.decorator"; | ||
import Action from "../../bolt/enums/action.enum"; | ||
import Event from "../../bolt/enums/event.enum"; | ||
import { AppHomeOpenedArgs } from "../../bolt/types/app-home-opened.type"; | ||
import { BoltActionArgs } from "../../bolt/types/bolt-action-args.type"; | ||
import { HomeTabService } from "./home-tab.service"; | ||
import { ViewCache } from "./view.cache"; | ||
import { PresenceView } from "./views/presence.view"; | ||
import { RegistrationView } from "./views/registration/registration.view"; | ||
import { SettingsView } from "./views/settings.view"; | ||
|
||
type ViewProps = { | ||
actionArgs: BoltActionArgs; | ||
name: "presence" | "registration" | "settings"; | ||
contentFactory: () => Promise<Appendable<ViewBlockBuilder>>; | ||
}; | ||
|
||
@Controller() | ||
export class HomeTabController { | ||
constructor( | ||
private homeTabBuilder: HomeTabService, | ||
private presenceView: PresenceView, | ||
private registrationView: RegistrationView, | ||
private settingsView: SettingsView, | ||
private viewCache: ViewCache, | ||
) {} | ||
|
||
@BoltEvent(Event.APP_HOME_OPENED) | ||
async getView(args: AppHomeOpenedArgs) { | ||
const content = await this.registrationView.build(args.event.user); | ||
const { selectedView } = await this.viewCache.get(args.context.userId); | ||
let content: Appendable<ViewBlockBuilder> = []; | ||
|
||
if (selectedView === "presence") { | ||
content = await this.presenceView.build(); | ||
} else if (selectedView === "settings") { | ||
content = await this.settingsView.build(); | ||
} else { | ||
content = await this.registrationView.build(args.event.user); | ||
} | ||
|
||
await this.homeTabBuilder.publish(args, content); | ||
} | ||
|
||
@BoltAction(Action.OPEN_PRESENCE_VIEW) | ||
async openPresenceView(args: BoltActionArgs) { | ||
await args.ack(); | ||
const content = await this.presenceView.build(); | ||
this.homeTabBuilder.update(args, content); | ||
async openPresenceView(actionArgs: BoltActionArgs) { | ||
await this.openView({ | ||
actionArgs, | ||
contentFactory: async () => this.presenceView.build(), | ||
name: "presence", | ||
}); | ||
} | ||
|
||
@BoltAction(Action.OPEN_REGISTRATION_VIEW) | ||
async openRegistrationView(actionArgs: BoltActionArgs) { | ||
await this.openView({ | ||
actionArgs, | ||
contentFactory: async () => this.registrationView.build(actionArgs.context.userId), | ||
name: "registration", | ||
}); | ||
} | ||
|
||
@BoltAction(Action.OPEN_SETTINGS_VIEW) | ||
async openSettingsView(actionArgs: BoltActionArgs) { | ||
await this.openView({ | ||
actionArgs, | ||
contentFactory: async () => this.settingsView.build(), | ||
name: "settings", | ||
}); | ||
} | ||
|
||
/** | ||
* Abstract helper to show home tab views with less boilerplate. | ||
* | ||
* According to Bolt docs, `ack()` should be called ASAP. To comply with this, | ||
* we take a content factory instead of prebuild content as an argument. | ||
*/ | ||
private async openView({ actionArgs, contentFactory, name }: ViewProps) { | ||
await actionArgs.ack(); | ||
await this.viewCache.set(actionArgs.context.userId, { selectedView: name }); | ||
const content = await contentFactory(); | ||
this.homeTabBuilder.update(actionArgs, content); | ||
} | ||
} |
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,53 @@ | ||
import { CACHE_MANAGER } from "@nestjs/cache-manager"; | ||
import { Inject, Injectable, Logger } from "@nestjs/common"; | ||
import { Cache } from "cache-manager"; | ||
|
||
type ViewOptions = { | ||
selectedView: "registration" | "presence" | "settings"; | ||
}; | ||
|
||
const defaultOptions: ViewOptions = { | ||
selectedView: "registration", | ||
}; | ||
|
||
/** | ||
* View caching service implements lightweight non-persistent storage for view | ||
* selections. This data includes things like selected view, selected drop-down | ||
* values, etc. Basically anything that is nice to store for a better UX but | ||
* isn't something that needs to be persisted in the database. | ||
* | ||
* Note that while partial updates to the cached view objects are fine, nested | ||
* structures will be overwritten by the current implementation. | ||
*/ | ||
@Injectable() | ||
export class ViewCache { | ||
private logger = new Logger(ViewCache.name); | ||
|
||
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {} | ||
|
||
async get(userId: string): Promise<ViewOptions> { | ||
const cached = await this.getCachedValue(userId); | ||
|
||
if (cached) { | ||
this.logger.debug(`View cache hit for user ${userId}`, cached); | ||
} | ||
|
||
return { ...defaultOptions, ...cached }; | ||
} | ||
|
||
async set(userId: string, value: Partial<ViewOptions>): Promise<void> { | ||
const cached = await this.getCachedValue(userId); | ||
// Note that this will break for nested objects. | ||
const merged = { ...cached, ...value }; | ||
await this.cacheManager.set(this.cacheKey(userId), merged); | ||
return; | ||
} | ||
|
||
private cacheKey(userId: string): string { | ||
return `view-cache-${userId}`; | ||
} | ||
|
||
private getCachedValue(userId: string): Promise<Partial<ViewOptions>> { | ||
return this.cacheManager.get<Partial<ViewOptions>>(this.cacheKey(userId)); | ||
} | ||
} |
Oops, something went wrong.