From 27d8ab7fc6bc9010bf425d7bf52e652439d9f808 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Wed, 5 May 2021 18:00:03 +0200 Subject: [PATCH] Prosody: the video owner is admin for the room. --- server/@types/peertube.d.ts | 1 + server/lib/prosody/config/affiliations.ts | 39 +++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/server/@types/peertube.d.ts b/server/@types/peertube.d.ts index f64a4cf3..39112356 100644 --- a/server/@types/peertube.d.ts +++ b/server/@types/peertube.d.ts @@ -75,6 +75,7 @@ interface MVideoThumbnail { // FIXME: this interface is not complete. commentsEnabled: boolean downloadEnabled: boolean state: VideoState + channelId: number } // Keep the order diff --git a/server/lib/prosody/config/affiliations.ts b/server/lib/prosody/config/affiliations.ts index a7451ec2..d05218b2 100644 --- a/server/lib/prosody/config/affiliations.ts +++ b/server/lib/prosody/config/affiliations.ts @@ -1,6 +1,6 @@ interface Affiliations { [jid: string]: 'outcast' | 'none' | 'member' | 'admin' | 'owner' } -async function getVideoAffiliations (options: RegisterServerOptions, _video: MVideoThumbnail): Promise { +async function getVideoAffiliations (options: RegisterServerOptions, video: MVideoThumbnail): Promise { const peertubeHelpers = options.peertubeHelpers // Get all admins and moderators const [results] = await peertubeHelpers.database.query( @@ -23,11 +23,46 @@ async function getVideoAffiliations (options: RegisterServerOptions, _video: MVi r[jid] = 'owner' } - // TODO: add a 'admin' affiliation for video owner + // Adding an 'admin' affiliation for video owner + // NB: if it fails, we want previous results to be returned... + try { + const userName = await _getVideoOwnerUsername(options, video) + const userJid = userName + '@localhost' + r[userJid] = 'admin' + } catch (error) { + peertubeHelpers.logger.error('Failed to get video owner informations:', error) + } return r } +async function _getVideoOwnerUsername (options: RegisterServerOptions, video: MVideoThumbnail): Promise { + // checking type of video.channelId + if (!video.channelId) { + throw new Error('Missing channelId on video') + } + if (!Number.isInteger(video.channelId)) { + throw new Error('Invalid channelId: not an integer') + } + const [results] = await options.peertubeHelpers.database.query( + 'SELECT "user"."username"' + + ' FROM "videoChannel"' + + ' JOIN "account" ON "account"."id" = "videoChannel"."accountId"' + + ' JOIN "user" ON "account"."userId" = "user"."id" ' + + ' WHERE "videoChannel"."id" = ' + video.channelId.toString() + ) + if (!Array.isArray(results)) { + throw new Error('_getVideoOwnerUsername: query result is not an array.') + } + if (!results[0]) { + throw new Error('_getVideoOwnerUsername: no user found') + } + if (!results[0].username) { + throw new Error('_getVideoOwnerUsername: no username in result') + } + return results[0].username as string +} + export { Affiliations, getVideoAffiliations