2023-05-04 23:34:53 +02:00
|
|
|
import { GuildMember } from "discord.js";
|
|
|
|
import { CreateUserByNameOperationRequest, SystemApi, UpdateUserPolicyOperationRequest, UserApi } from "./apis";
|
|
|
|
import { UserDto } from "./models/UserDto";
|
|
|
|
import { Configuration, ConfigurationParameters } from "./runtime";
|
|
|
|
import { CreateUserByNameRequest, UpdateUserPolicyRequest } from "./models";
|
|
|
|
import { Config } from "../configuration";
|
|
|
|
import { logger } from "../logger";
|
|
|
|
import { Maybe } from "../interfaces";
|
2023-04-16 02:04:08 +02:00
|
|
|
|
|
|
|
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)
|
2023-05-04 23:34:53 +02:00
|
|
|
this.serverName = info.serverName ?? this.config.bot.jellyfin_url
|
2023-04-16 02:04:08 +02:00
|
|
|
}
|
|
|
|
return this.serverName
|
|
|
|
}
|
|
|
|
constructor(_config: Config, _userApi?: UserApi, _systemApi?: SystemApi) {
|
|
|
|
this.config = _config
|
|
|
|
this.token = this.config.bot.jellfin_token
|
|
|
|
this.authHeader = {
|
|
|
|
headers: {
|
|
|
|
"X-MediaBrowser-Token": this.token
|
|
|
|
}
|
|
|
|
}
|
2023-06-03 22:06:23 +02:00
|
|
|
const userApiConfigurationParams: ConfigurationParameters = {
|
|
|
|
basePath: this.config.bot.jellyfin_url,
|
|
|
|
headers: this.authHeader.headers
|
|
|
|
}
|
|
|
|
const systemApiConfigurationParams: ConfigurationParameters = {
|
|
|
|
basePath: this.config.bot.jellyfin_url,
|
|
|
|
headers: this.authHeader.headers
|
|
|
|
}
|
|
|
|
this.userApi = _userApi ?? new UserApi(new Configuration(userApiConfigurationParams))
|
|
|
|
this.systemApi = _systemApi ?? new SystemApi(new Configuration(systemApiConfigurationParams))
|
2023-05-04 23:34:53 +02:00
|
|
|
logger.info(`Initialized Jellyfin handler`, { requestId: 'Init' })
|
2023-04-16 02:04:08 +02:00
|
|
|
}
|
|
|
|
|
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-20 20:54:20 +02:00
|
|
|
|
|
|
|
private generatePasswordForUser(user: GuildMember): string {
|
2023-06-03 22:06:23 +02:00
|
|
|
return user.displayName + user.user.discriminator
|
2023-04-20 20:54:20 +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-05-04 23:34:53 +02:00
|
|
|
const req: CreateUserByNameOperationRequest = {
|
|
|
|
createUserByNameRequest: {
|
|
|
|
name: newUserName,
|
|
|
|
password: this.generatePasswordForUser(discordUser),
|
|
|
|
}
|
2023-04-20 20:54:20 +02:00
|
|
|
}
|
|
|
|
logger.debug(JSON.stringify(req))
|
2023-06-03 22:06:23 +02:00
|
|
|
const createResult = await this.userApi.createUserByName(req)
|
2023-05-04 23:34:53 +02:00
|
|
|
if (createResult)
|
|
|
|
return createResult
|
2023-04-20 20:54:20 +02:00
|
|
|
else throw new Error('Could not create User in Jellyfin')
|
2023-04-16 02:04:08 +02:00
|
|
|
}
|
2023-04-20 20:54:20 +02:00
|
|
|
|
|
|
|
public async disableJfUser(user: UserDto, guildId?: string, requestId?: string): Promise<void> {
|
|
|
|
if (user.id) {
|
|
|
|
logger.info(`Trying to disable user: ${user.name}|${user.id}`)
|
2023-05-04 23:34:53 +02:00
|
|
|
const r: UpdateUserPolicyOperationRequest = {
|
|
|
|
userId: user.id,
|
|
|
|
updateUserPolicyRequest: {
|
|
|
|
isDisabled: true
|
|
|
|
},
|
2023-04-20 20:54:20 +02:00
|
|
|
}
|
2023-06-03 22:06:23 +02:00
|
|
|
await this.userApi.updateUserPolicy(r)
|
2023-04-20 20:54:20 +02:00
|
|
|
logger.info(`Succeeded with disabling user: ${user.name}`)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.error(`Can not disable user ${JSON.stringify(user)}, has no id?!`, { requestId, guildId })
|
|
|
|
}
|
2023-04-16 02:04:08 +02:00
|
|
|
}
|
2023-04-20 20:54:20 +02:00
|
|
|
|
|
|
|
public async isUserAlreadyPresent(discordUser: GuildMember, requestId?: string): Promise<boolean> {
|
|
|
|
const jfuser = await this.getUser(discordUser, requestId)
|
|
|
|
logger.debug(`Presence for DiscordUser ${discordUser.id}:${jfuser !== undefined}`)
|
|
|
|
return jfuser !== 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 {
|
2023-05-04 23:34:53 +02:00
|
|
|
logger.info(`Fetching current users from Jellyfin`, { requestId, guildId })
|
|
|
|
const result = await this.userApi.getUsers(undefined, this.authHeader)
|
|
|
|
return result
|
2023-04-16 02:04:08 +02:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(`Could not fetch current users from jellyfin`, { guildId, requestId })
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|
2023-04-18 23:46:26 +02:00
|
|
|
|
2023-04-20 20:54:20 +02:00
|
|
|
public async getUser(discordUser: GuildMember, requestId?: string): Promise<Maybe<UserDto>> {
|
|
|
|
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) {
|
2023-04-18 23:46:26 +02:00
|
|
|
logger.error(`Trying to remove user ${newMember.displayName}, but method is not implemented`)
|
2023-04-20 20:54:20 +02:00
|
|
|
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<void> {
|
|
|
|
if (user.id) {
|
|
|
|
logger.info(`Trying to enable user: ${user.name}|${user.id}`)
|
2023-05-04 23:34:53 +02:00
|
|
|
const r: UpdateUserPolicyOperationRequest = {
|
|
|
|
userId: user.id ?? "",
|
|
|
|
updateUserPolicyRequest: {
|
|
|
|
}
|
2023-04-20 20:54:20 +02:00
|
|
|
}
|
2023-06-03 22:06:23 +02:00
|
|
|
await this.userApi.updateUserPolicy(r)
|
2023-04-20 20:54:20 +02:00
|
|
|
logger.info(`Succeeded with enabling user: ${user.name}`)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.error(`Can not enable user ${JSON.stringify(user)}, has no id?!`, { requestId, guildId })
|
|
|
|
}
|
2023-04-18 23:46:26 +02:00
|
|
|
}
|
2023-04-20 20:54:20 +02:00
|
|
|
|
2023-04-18 23:46:26 +02:00
|
|
|
public async upsertUser(newMember: GuildMember, level: string, requestId?: string) {
|
2023-04-20 20:54:20 +02:00
|
|
|
logger.error(`Trying to upsert user ${newMember.displayName}, with permissionLevel ${level}`)
|
|
|
|
const jfuser = await this.getUser(newMember, requestId)
|
|
|
|
if (jfuser) {
|
2023-04-18 23:46:26 +02:00
|
|
|
logger.info(`User with name ${newMember.displayName} is already present`)
|
2023-04-20 20:54:20 +02:00
|
|
|
await this.enableUser(jfuser, requestId)
|
|
|
|
} else {
|
|
|
|
this.createUserAccountForDiscordUser(newMember, newMember.guild.id, requestId)
|
2023-04-18 23:46:26 +02:00
|
|
|
}
|
|
|
|
|
2023-04-20 20:54:20 +02:00
|
|
|
|
|
|
|
|
2023-04-18 23:46:26 +02:00
|
|
|
}
|
2023-04-16 02:04:08 +02:00
|
|
|
}
|
2023-05-04 23:34:53 +02:00
|
|
|
|