2023-04-18 23:46:26 +02:00
|
|
|
import { GuildMember, User } from "discord.js";
|
2023-04-16 02:04:08 +02:00
|
|
|
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
|
2023-04-18 23:46:26 +02:00
|
|
|
private serverName = "";
|
2023-04-16 02:04:08 +02:00
|
|
|
|
|
|
|
public async ServerName(): Promise<string> {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-18 23:46:26 +02:00
|
|
|
private generateJFUserName(discordUser: GuildMember): string {
|
|
|
|
return discordUser.displayName
|
2023-04-16 02:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async addPermissionsToUserAccount(jfUserAccount: UserDto, guildId: string, requestId: string): Promise<UserDto> {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
2023-04-18 23:46:26 +02:00
|
|
|
public async createUserAccountForDiscordUser(discordUser: GuildMember, guildId: string, requestId: string): Promise<UserDto> {
|
2023-04-16 02:04:08 +02:00
|
|
|
const newUserName = this.generateJFUserName(discordUser)
|
2023-04-18 23:46:26 +02:00
|
|
|
logger.info(`New Username for ${discordUser.displayName}: ${newUserName}`, { guildId, requestId })
|
2023-04-16 02:04:08 +02:00
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
2023-04-18 23:46:26 +02:00
|
|
|
public async isUserAlreadyPresent(discordUser: GuildMember, guildId: string, requestId?: string): Promise<boolean> {
|
2023-04-16 02:04:08 +02:00
|
|
|
const jfUsernameFromDiscordUsername = this.generateJFUserName(discordUser)
|
|
|
|
const jfUsers = await this.getCurrentUsers(guildId, requestId)
|
|
|
|
const currentJfUserNames = jfUsers.map(x => x.name)
|
2023-04-18 23:46:26 +02:00
|
|
|
logger.debug(`Current JF Usernames: ${JSON.stringify(currentJfUserNames)}`)
|
2023-04-16 02:04:08 +02:00
|
|
|
const presence = currentJfUserNames.find(x => x === jfUsernameFromDiscordUsername)
|
2023-04-18 23:46:26 +02:00
|
|
|
logger.debug(`Presence for DiscordUser ${discordUser.id}:${presence}`)
|
2023-04-16 02:04:08 +02:00
|
|
|
return presence !== undefined
|
|
|
|
}
|
2023-04-18 23:46:26 +02:00
|
|
|
public async getCurrentUsers(guildId: string, requestId?: string): Promise<UserDto[]> {
|
2023-04-16 02:04:08 +02:00
|
|
|
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 []
|
|
|
|
}
|
2023-04-18 23:46:26 +02:00
|
|
|
|
|
|
|
public async removeUser(newMember: GuildMember) {
|
|
|
|
logger.error(`Trying to remove user ${newMember.displayName}, but method is not implemented`)
|
|
|
|
}
|
|
|
|
public async upsertUser(newMember: GuildMember, level: string, requestId?: string) {
|
|
|
|
logger.error(`Trying to upsert user ${newMember.displayName}, with permissionLevel ${level} but method is not implemented`)
|
|
|
|
const presence = await this.isUserAlreadyPresent(newMember, newMember.guild.id, requestId)
|
|
|
|
if (presence) {
|
|
|
|
logger.info(`User with name ${newMember.displayName} is already present`)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-04-16 02:04:08 +02:00
|
|
|
}
|