From a80ccb6bfde42201c256a6f199a66e71fe6d12a1 Mon Sep 17 00:00:00 2001 From: mightypanders Date: Sun, 16 Apr 2023 02:04:08 +0200 Subject: [PATCH] add: JF handler to take care of JF interactions --- jellyfin/handler.ts | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 jellyfin/handler.ts diff --git a/jellyfin/handler.ts b/jellyfin/handler.ts new file mode 100644 index 0000000..0a9e32f --- /dev/null +++ b/jellyfin/handler.ts @@ -0,0 +1,64 @@ +import { User } from "discord.js"; +import { UserDto } from "./model/userDto"; +import { Config } from "../server/configuration"; +import { UserApi } from "./api/userApi"; +import { logger } from "../server/logger"; +import { SystemApi } from "./api/systemApi"; + +export class JellyfinHandler { + + private userApi: UserApi + private systemApi: SystemApi + private token: string + private authHeader: { headers: { 'X-MediaBrowser-Token': string } } + private config: Config + private serverName: string = ""; + + public async ServerName(): Promise { + if (this.serverName === "") { + const info = await this.systemApi.getSystemInfo(this.authHeader) + this.serverName = info.body.serverName ?? this.config.bot.jellyfin_url + } + return this.serverName + } + constructor(_config: Config, _userApi?: UserApi, _systemApi?: SystemApi) { + this.config = _config + this.userApi = _userApi ?? new UserApi(this.config.bot.jellyfin_url) + this.systemApi = _systemApi ?? new SystemApi(this.config.bot.jellyfin_url) + this.token = this.config.bot.jellfin_token + this.authHeader = { + headers: { + "X-MediaBrowser-Token": this.token + } + } + } + + private generateJFUserName(discordUser: User): string { + return discordUser.username + discordUser.discriminator + } + + public async addPermissionsToUserAccount(jfUserAccount: UserDto, guildId: string, requestId: string): Promise { + throw new Error("Method not implemented."); + } + public async createUserAccountForDiscordUser(discordUser: User, guildId: string, requestId: string): Promise { + const newUserName = this.generateJFUserName(discordUser) + logger.info(`New Username for ${discordUser.username}: ${newUserName}`, { guildId, requestId }) + throw new Error("Method not implemented."); + } + public async isUserAlreadyPresent(discordUser: User, guildId: string, requestId: string): Promise { + const jfUsernameFromDiscordUsername = this.generateJFUserName(discordUser) + const jfUsers = await this.getCurrentUsers(guildId, requestId) + const currentJfUserNames = jfUsers.map(x => x.name) + const presence = currentJfUserNames.find(x => x === jfUsernameFromDiscordUsername) + return presence !== undefined + } + public async getCurrentUsers(guildId: string, requestId: string): Promise { + try { + const result = await this.userApi.getUsers(undefined, undefined, this.authHeader) + return result.body + } catch (error) { + logger.error(`Could not fetch current users from jellyfin`, { guildId, requestId }) + } + return [] + } +}