import { GuildMember, 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"; import { CreateUserByNameRequest, UpdateUserPolicyRequest, UpdateUserRequest } from "./api"; import { Maybe } from "../server/interfaces"; export class JellyfinHandler { private userApi: UserApi private systemApi: SystemApi private token: string private authHeader: { headers: { 'X-MediaBrowser-Token': string } } private config: Config private serverName = ""; 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: GuildMember): string { return discordUser.displayName } public async addPermissionsToUserAccount(jfUserAccount: UserDto, guildId: string, requestId: string): Promise { throw new Error("Method not implemented."); } private generatePasswordForUser(user: GuildMember): string { return user.nickname + user.user.discriminator } public async createUserAccountForDiscordUser(discordUser: GuildMember, guildId?: string, requestId?: string): Promise { const newUserName = this.generateJFUserName(discordUser) logger.info(`New Username for ${discordUser.displayName}: ${newUserName}`, { guildId, requestId }) const req: CreateUserByNameRequest = { name: newUserName, password: this.generatePasswordForUser(discordUser) } logger.debug(JSON.stringify(req)) const createResult = await this.userApi.createUserByName(req, this.authHeader) if (createResult.body) return createResult.body else throw new Error('Could not create User in Jellyfin') } public async disableJfUser(user: UserDto, guildId?: string, requestId?: string): Promise { if (user.id) { logger.info(`Trying to disable user: ${user.name}|${user.id}`) const r: UpdateUserPolicyRequest = { ...user.policy, isDisabled: true } await this.userApi.updateUserPolicy(user.id, r, this.authHeader) logger.info(`Succeeded with disabling user: ${user.name}`) } else { logger.error(`Can not disable user ${JSON.stringify(user)}, has no id?!`, { requestId, guildId }) } } public async isUserAlreadyPresent(discordUser: GuildMember, requestId?: string): Promise { const jfuser = await this.getUser(discordUser, requestId) logger.debug(`Presence for DiscordUser ${discordUser.id}:${jfuser !== undefined}`) return jfuser !== 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 [] } public async getUser(discordUser: GuildMember, requestId?: string): Promise> { const jfUsernameFromDiscordUsername = this.generateJFUserName(discordUser) const jfUsers = await this.getCurrentUsers(discordUser.guild.id, requestId) const foundUser = jfUsers.find(x => x.name === jfUsernameFromDiscordUsername) return foundUser } public async removeUser(newMember: GuildMember, requestId?: string) { logger.error(`Trying to remove user ${newMember.displayName}, but method is not implemented`) const jfuser = await this.getUser(newMember, requestId) if (jfuser) { await this.disableJfUser(jfuser, newMember.guild.id, requestId) } } public async enableUser(user: UserDto, guildId?: string, requestId?: string): Promise { if (user.id) { logger.info(`Trying to enable user: ${user.name}|${user.id}`) const r: UpdateUserPolicyRequest = { ...user.policy, isDisabled: false } await this.userApi.updateUserPolicy(user.id, r, this.authHeader) logger.info(`Succeeded with enabling user: ${user.name}`) } else { logger.error(`Can not enable user ${JSON.stringify(user)}, has no id?!`, { requestId, guildId }) } } public async upsertUser(newMember: GuildMember, level: string, requestId?: string) { logger.error(`Trying to upsert user ${newMember.displayName}, with permissionLevel ${level}`) const jfuser = await this.getUser(newMember, requestId) if (jfuser) { logger.info(`User with name ${newMember.displayName} is already present`) await this.enableUser(jfuser, requestId) } else { this.createUserAccountForDiscordUser(newMember, newMember.guild.id, requestId) } } }