From fa9998e92c01ee7b7bc81a41a809729f30e1a58c Mon Sep 17 00:00:00 2001 From: Sammy Date: Fri, 23 Jun 2023 21:23:54 +0200 Subject: [PATCH] Unallow transcoding per default for new users --- server/jellyfin/handler.ts | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/server/jellyfin/handler.ts b/server/jellyfin/handler.ts index e47ac6c..8dc1dc9 100644 --- a/server/jellyfin/handler.ts +++ b/server/jellyfin/handler.ts @@ -2,7 +2,7 @@ import { GuildMember } from "discord.js"; import { JellyfinConfig, Maybe, PermissionLevel } from "../interfaces"; import { logger } from "../logger"; import { CreateUserByNameOperationRequest, DeleteUserRequest, GetItemsRequest, ItemsApi, SystemApi, UpdateUserPasswordOperationRequest, UpdateUserPolicyOperationRequest, UserApi } from "./apis"; -import { BaseItemDto, UpdateUserPasswordRequest } from "./models"; +import { BaseItemDto, UpdateUserPasswordRequest, UpdateUserPolicyRequest } from "./models"; import { UserDto } from "./models/UserDto"; import { Configuration, ConfigurationParameters } from "./runtime"; @@ -52,24 +52,46 @@ export class JellyfinHandler { return (Math.random() * 10000 + 10000).toFixed(0) } - public async createUserAccountForDiscordUser(discordUser: GuildMember, level: PermissionLevel, guildId?: string, requestId?: string): Promise { + public async createUserAccountForDiscordUser(discordUser: GuildMember, level: PermissionLevel, requestId: string, guildId?: string): Promise { const newUserName = this.generateJFUserName(discordUser, level) logger.info(`New Username for ${discordUser.displayName}: ${newUserName}`, { guildId, requestId }) const req: CreateUserByNameOperationRequest = { createUserByNameRequest: { name: newUserName, - password: this.generatePasswordForUser(), + password: this.generatePasswordForUser() } } logger.debug(JSON.stringify(req), { requestId, guildId }) const createResult = await this.userApi.createUserByName(req) if (createResult) { + if(createResult.policy) { + this.setUserPermissions(createResult, requestId, guildId) + } (await discordUser.createDM()).send(`Ich hab dir mal nen Account angelegt :)\nDein Username ist ${createResult.name}, dein Password ist "${req.createUserByNameRequest.password}"!`) return createResult } else throw new Error('Could not create User in Jellyfin') } + public async setUserPermissions(user: UserDto, requestId: string, guildId?: string) { + if(!user.policy || !user.id) { + logger.error(`Cannot update user policy. User ${user.name} has no policy to modify`, {guildId, requestId}) + return + } + user.policy.enableVideoPlaybackTranscoding = false + + const operation: UpdateUserPolicyRequest = { + ...user.policy, + enableVideoPlaybackTranscoding: false + } + + const request: UpdateUserPolicyOperationRequest = { + userId: user.id, + updateUserPolicyRequest: operation + } + this.userApi.updateUserPolicy(request) + } + public async isUserAlreadyPresent(discordUser: GuildMember, requestId?: string): Promise { const jfuser = await this.getUser(discordUser, requestId) logger.debug(`Presence for DiscordUser ${discordUser.id}:${jfuser !== undefined}`, { guildId: discordUser.guild.id, requestId })