WIP: use guildmember in jf handler, start user creation

This commit is contained in:
mightypanders 2023-04-18 23:46:26 +02:00
parent 4ff8c83cdf
commit b01700df6c

View File

@ -1,4 +1,4 @@
import { User } from "discord.js"; import { GuildMember, User } from "discord.js";
import { UserDto } from "./model/userDto"; import { UserDto } from "./model/userDto";
import { Config } from "../server/configuration"; import { Config } from "../server/configuration";
import { UserApi } from "./api/userApi"; import { UserApi } from "./api/userApi";
@ -12,7 +12,7 @@ export class JellyfinHandler {
private token: string private token: string
private authHeader: { headers: { 'X-MediaBrowser-Token': string } } private authHeader: { headers: { 'X-MediaBrowser-Token': string } }
private config: Config private config: Config
private serverName: string = ""; private serverName = "";
public async ServerName(): Promise<string> { public async ServerName(): Promise<string> {
if (this.serverName === "") { if (this.serverName === "") {
@ -33,26 +33,28 @@ export class JellyfinHandler {
} }
} }
private generateJFUserName(discordUser: User): string { private generateJFUserName(discordUser: GuildMember): string {
return discordUser.username + discordUser.discriminator return discordUser.displayName
} }
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: User, guildId: string, requestId: string): Promise<UserDto> { 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.username}: ${newUserName}`, { guildId, requestId }) logger.info(`New Username for ${discordUser.displayName}: ${newUserName}`, { guildId, requestId })
throw new Error("Method not implemented."); throw new Error("Method not implemented.");
} }
public async isUserAlreadyPresent(discordUser: User, guildId: string, requestId: string): Promise<boolean> { public async isUserAlreadyPresent(discordUser: GuildMember, guildId: string, requestId?: string): Promise<boolean> {
const jfUsernameFromDiscordUsername = this.generateJFUserName(discordUser) const jfUsernameFromDiscordUsername = this.generateJFUserName(discordUser)
const jfUsers = await this.getCurrentUsers(guildId, requestId) const jfUsers = await this.getCurrentUsers(guildId, requestId)
const currentJfUserNames = jfUsers.map(x => x.name) const currentJfUserNames = jfUsers.map(x => x.name)
logger.debug(`Current JF Usernames: ${JSON.stringify(currentJfUserNames)}`)
const presence = currentJfUserNames.find(x => x === jfUsernameFromDiscordUsername) const presence = currentJfUserNames.find(x => x === jfUsernameFromDiscordUsername)
logger.debug(`Presence for DiscordUser ${discordUser.id}:${presence}`)
return presence !== undefined return presence !== 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)
return result.body return result.body
@ -61,4 +63,16 @@ export class JellyfinHandler {
} }
return [] return []
} }
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`)
}
}
} }