handler now creates and disables/enables users
This commit is contained in:
		@@ -1,11 +1,13 @@
 | 
				
			|||||||
import { GuildMember } from "discord.js";
 | 
					import { GuildMember } from "discord.js";
 | 
				
			||||||
import { CreateUserByNameOperationRequest, SystemApi, UpdateUserPolicyOperationRequest, UserApi } from "./apis";
 | 
					import { CreateUserByNameOperationRequest, SystemApi, UpdateUserPasswordOperationRequest, UpdateUserPolicyOperationRequest, UserApi } from "./apis";
 | 
				
			||||||
import { UserDto } from "./models/UserDto";
 | 
					import { UserDto } from "./models/UserDto";
 | 
				
			||||||
import { Configuration, ConfigurationParameters } from "./runtime";
 | 
					import { Configuration, ConfigurationParameters } from "./runtime";
 | 
				
			||||||
import { CreateUserByNameRequest, UpdateUserPolicyRequest } from "./models";
 | 
					import { CreateUserByNameRequest, UpdateUserEasyPasswordRequest, UpdateUserPasswordRequest, UpdateUserPolicyRequest } from "./models";
 | 
				
			||||||
import { Config } from "../configuration";
 | 
					import { Config } from "../configuration";
 | 
				
			||||||
import { logger } from "../logger";
 | 
					import { logger } from "../logger";
 | 
				
			||||||
import { Maybe } from "../interfaces";
 | 
					import { Maybe } from "../interfaces";
 | 
				
			||||||
 | 
					import { v4 as uuid } from "uuid";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class JellyfinHandler {
 | 
					export class JellyfinHandler {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -52,8 +54,8 @@ export class JellyfinHandler {
 | 
				
			|||||||
    throw new Error("Method not implemented.");
 | 
					    throw new Error("Method not implemented.");
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private generatePasswordForUser(user: GuildMember): string {
 | 
					  private generatePasswordForUser(): string {
 | 
				
			||||||
    return user.displayName + user.user.discriminator
 | 
					    return (Math.random() * 10000 + 10000).toFixed(0)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public async createUserAccountForDiscordUser(discordUser: GuildMember, guildId?: string, requestId?: string): Promise<UserDto> {
 | 
					  public async createUserAccountForDiscordUser(discordUser: GuildMember, guildId?: string, requestId?: string): Promise<UserDto> {
 | 
				
			||||||
@@ -62,33 +64,18 @@ export class JellyfinHandler {
 | 
				
			|||||||
    const req: CreateUserByNameOperationRequest = {
 | 
					    const req: CreateUserByNameOperationRequest = {
 | 
				
			||||||
      createUserByNameRequest: {
 | 
					      createUserByNameRequest: {
 | 
				
			||||||
        name: newUserName,
 | 
					        name: newUserName,
 | 
				
			||||||
        password: this.generatePasswordForUser(discordUser),
 | 
					        password: this.generatePasswordForUser(),
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    logger.debug(JSON.stringify(req))
 | 
					    logger.debug(JSON.stringify(req))
 | 
				
			||||||
    const createResult = await this.userApi.createUserByName(req)
 | 
					    const createResult = await this.userApi.createUserByName(req)
 | 
				
			||||||
    if (createResult)
 | 
					    if (createResult){
 | 
				
			||||||
 | 
					      (await discordUser.createDM()).send(`Ich hab dir mal nen Account angelegt :)\nDein Username ist ${createResult.name}, dein Password ist "${req.createUserByNameRequest.password}"!` )
 | 
				
			||||||
      return createResult
 | 
					      return createResult
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    else throw new Error('Could not create User in Jellyfin')
 | 
					    else throw new Error('Could not create User in Jellyfin')
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  public async disableJfUser(user: UserDto, guildId?: string, requestId?: string): Promise<void> {
 | 
					 | 
				
			||||||
    if (user.id) {
 | 
					 | 
				
			||||||
      logger.info(`Trying to disable user: ${user.name}|${user.id}`)
 | 
					 | 
				
			||||||
      const r: UpdateUserPolicyOperationRequest = {
 | 
					 | 
				
			||||||
        userId: user.id,
 | 
					 | 
				
			||||||
        updateUserPolicyRequest: {
 | 
					 | 
				
			||||||
          isDisabled: true
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      await this.userApi.updateUserPolicy(r)
 | 
					 | 
				
			||||||
      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> {
 | 
					  public async isUserAlreadyPresent(discordUser: GuildMember, requestId?: string): Promise<boolean> {
 | 
				
			||||||
    const jfuser = await this.getUser(discordUser, requestId)
 | 
					    const jfuser = await this.getUser(discordUser, requestId)
 | 
				
			||||||
    logger.debug(`Presence for DiscordUser ${discordUser.id}:${jfuser !== undefined}`)
 | 
					    logger.debug(`Presence for DiscordUser ${discordUser.id}:${jfuser !== undefined}`)
 | 
				
			||||||
@@ -117,16 +104,60 @@ export class JellyfinHandler {
 | 
				
			|||||||
    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)
 | 
					    const jfuser = await this.getUser(newMember, requestId)
 | 
				
			||||||
    if (jfuser) {
 | 
					    if (jfuser) {
 | 
				
			||||||
      await this.disableJfUser(jfuser, newMember.guild.id, requestId)
 | 
					      await this.disableUser(jfuser, newMember.guild.id, requestId)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public async enableUser(user: UserDto, guildId?: string, requestId?: string): Promise<void> {
 | 
					  public async resetUserPasswort(member: GuildMember, requestId?: string) {
 | 
				
			||||||
 | 
					    const jfUser = await this.getUser(member, requestId)
 | 
				
			||||||
 | 
					    if (jfUser && jfUser.id) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      const r: UpdateUserPasswordRequest = {
 | 
				
			||||||
 | 
					        newPw: this.generatePasswordForUser()
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      const shit: UpdateUserPasswordOperationRequest = {
 | 
				
			||||||
 | 
					        updateUserPasswordRequest: r,
 | 
				
			||||||
 | 
					        userId: jfUser.id
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      this.userApi.updateUserPassword(shit)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      (await member.createDM()).send("Ich konnte leider keinen User von dir auf Jellyfin finden. Bitte melde dich bei Markus oder Samantha!")
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public async disableUser(user: UserDto, guildId?: string, requestId?: string): Promise<void> {
 | 
				
			||||||
    if (user.id) {
 | 
					    if (user.id) {
 | 
				
			||||||
      logger.info(`Trying to enable user: ${user.name}|${user.id}`)
 | 
					      const jfUser = await this.getUser(<GuildMember>{displayName: user.name, guild: {id: guildId}}, requestId)
 | 
				
			||||||
 | 
					      logger.info(`Trying to disable user: ${user.name}|${user.id}|${JSON.stringify(jfUser, null, 2)}`)
 | 
				
			||||||
      const r: UpdateUserPolicyOperationRequest = {
 | 
					      const r: UpdateUserPolicyOperationRequest = {
 | 
				
			||||||
        userId: user.id ?? "",
 | 
					        userId: user.id ?? "",
 | 
				
			||||||
        updateUserPolicyRequest: {
 | 
					        updateUserPolicyRequest: {
 | 
				
			||||||
 | 
					          ...jfUser?.policy,
 | 
				
			||||||
 | 
					          isDisabled: true,
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      await this.userApi.updateUserPolicy(r)
 | 
				
			||||||
 | 
					      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 enableUser(user: UserDto, guildId: string, requestId?: string): Promise<void> {
 | 
				
			||||||
 | 
					    if (user.id) {
 | 
				
			||||||
 | 
					      const jfUser = await this.getUser(<GuildMember>{displayName: user.name, guild: {id: guildId}}, requestId)
 | 
				
			||||||
 | 
					      logger.info(`Trying to enable user: ${user.name}|${user.id}|${JSON.stringify(jfUser, null, 2)}`)
 | 
				
			||||||
 | 
					      const r: UpdateUserPolicyOperationRequest = {
 | 
				
			||||||
 | 
					        userId: user.id ?? "",
 | 
				
			||||||
 | 
					        updateUserPolicyRequest: {
 | 
				
			||||||
 | 
					          ...jfUser?.policy,
 | 
				
			||||||
 | 
					          isDisabled: false,
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      await this.userApi.updateUserPolicy(r)
 | 
					      await this.userApi.updateUserPolicy(r)
 | 
				
			||||||
@@ -142,13 +173,10 @@ export class JellyfinHandler {
 | 
				
			|||||||
    const jfuser = await this.getUser(newMember, requestId)
 | 
					    const jfuser = await this.getUser(newMember, requestId)
 | 
				
			||||||
    if (jfuser) {
 | 
					    if (jfuser) {
 | 
				
			||||||
      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)
 | 
					      await this.enableUser(jfuser, newMember.guild.id, requestId)
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      this.createUserAccountForDiscordUser(newMember, newMember.guild.id, requestId)
 | 
					      this.createUserAccountForDiscordUser(newMember, newMember.guild.id, requestId)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user