Compare commits
No commits in common. "8e3f62c670be9eaff9e5d9a120c4ed7a556d8768" and "b01700df6c31ae67d8d465bfa13f24c461c06e88" have entirely different histories.
8e3f62c670
...
b01700df6c
@ -4,8 +4,6 @@ import { Config } from "../server/configuration";
|
|||||||
import { UserApi } from "./api/userApi";
|
import { UserApi } from "./api/userApi";
|
||||||
import { logger } from "../server/logger";
|
import { logger } from "../server/logger";
|
||||||
import { SystemApi } from "./api/systemApi";
|
import { SystemApi } from "./api/systemApi";
|
||||||
import { CreateUserByNameRequest, UpdateUserPolicyRequest, UpdateUserRequest } from "./api";
|
|
||||||
import { Maybe } from "../server/interfaces";
|
|
||||||
|
|
||||||
export class JellyfinHandler {
|
export class JellyfinHandler {
|
||||||
|
|
||||||
@ -42,46 +40,20 @@ export class JellyfinHandler {
|
|||||||
public async addPermissionsToUserAccount(jfUserAccount: UserDto, guildId: string, requestId: string): Promise<UserDto> {
|
public async addPermissionsToUserAccount(jfUserAccount: UserDto, guildId: string, requestId: string): Promise<UserDto> {
|
||||||
throw new Error("Method not implemented.");
|
throw new Error("Method not implemented.");
|
||||||
}
|
}
|
||||||
|
public async createUserAccountForDiscordUser(discordUser: GuildMember, guildId: string, requestId: string): Promise<UserDto> {
|
||||||
private generatePasswordForUser(user: GuildMember): string {
|
|
||||||
return user.nickname + user.user.discriminator
|
|
||||||
}
|
|
||||||
|
|
||||||
public async createUserAccountForDiscordUser(discordUser: GuildMember, guildId?: string, requestId?: string): Promise<UserDto> {
|
|
||||||
const newUserName = this.generateJFUserName(discordUser)
|
const newUserName = this.generateJFUserName(discordUser)
|
||||||
logger.info(`New Username for ${discordUser.displayName}: ${newUserName}`, { guildId, requestId })
|
logger.info(`New Username for ${discordUser.displayName}: ${newUserName}`, { guildId, requestId })
|
||||||
const req: CreateUserByNameRequest = {
|
throw new Error("Method not implemented.");
|
||||||
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 isUserAlreadyPresent(discordUser: GuildMember, guildId: string, requestId?: string): Promise<boolean> {
|
||||||
public async disableJfUser(user: UserDto, guildId?: string, requestId?: string): Promise<void> {
|
const jfUsernameFromDiscordUsername = this.generateJFUserName(discordUser)
|
||||||
if (user.id) {
|
const jfUsers = await this.getCurrentUsers(guildId, requestId)
|
||||||
logger.info(`Trying to disable user: ${user.name}|${user.id}`)
|
const currentJfUserNames = jfUsers.map(x => x.name)
|
||||||
const r: UpdateUserPolicyRequest = {
|
logger.debug(`Current JF Usernames: ${JSON.stringify(currentJfUserNames)}`)
|
||||||
...user.policy,
|
const presence = currentJfUserNames.find(x => x === jfUsernameFromDiscordUsername)
|
||||||
isDisabled: true
|
logger.debug(`Presence for DiscordUser ${discordUser.id}:${presence}`)
|
||||||
}
|
return presence !== undefined
|
||||||
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<boolean> {
|
|
||||||
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<UserDto[]> {
|
public async getCurrentUsers(guildId: string, requestId?: string): Promise<UserDto[]> {
|
||||||
try {
|
try {
|
||||||
const result = await this.userApi.getUsers(undefined, undefined, this.authHeader)
|
const result = await this.userApi.getUsers(undefined, undefined, this.authHeader)
|
||||||
@ -92,47 +64,15 @@ export class JellyfinHandler {
|
|||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getUser(discordUser: GuildMember, requestId?: string): Promise<Maybe<UserDto>> {
|
public async removeUser(newMember: GuildMember) {
|
||||||
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`)
|
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<void> {
|
|
||||||
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) {
|
public async upsertUser(newMember: GuildMember, level: string, requestId?: string) {
|
||||||
logger.error(`Trying to upsert user ${newMember.displayName}, with permissionLevel ${level}`)
|
logger.error(`Trying to upsert user ${newMember.displayName}, with permissionLevel ${level} but method is not implemented`)
|
||||||
const jfuser = await this.getUser(newMember, requestId)
|
const presence = await this.isUserAlreadyPresent(newMember, newMember.guild.id, requestId)
|
||||||
if (jfuser) {
|
if (presence) {
|
||||||
logger.info(`User with name ${newMember.displayName} is already present`)
|
logger.info(`User with name ${newMember.displayName} is already present`)
|
||||||
await this.enableUser(jfuser, requestId)
|
|
||||||
} else {
|
|
||||||
this.createUserAccountForDiscordUser(newMember, newMember.guild.id, requestId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { Collection } from "@discordjs/collection"
|
import { Collection } from "@discordjs/collection"
|
||||||
import { Role } from "discord.js"
|
import { Role } from "discord.js"
|
||||||
|
|
||||||
export type Maybe<T> = T | undefined | null
|
export type Maybe<T> = T | undefined
|
||||||
export interface Player {
|
export interface Player {
|
||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user