import { getProsodyDomain } from './domain' ; import { getUserNameByChannelId } from '../../database/channel' ; interface Affiliations { [ jid ] : 'outcast' | 'none' | 'member' | 'admin' | 'owner' ; } async function _getCommonAffiliations ( options , prosodyDomain ) { const [ results ] = await options . peertubeHelpers . database . query ( 'SELECT "username" FROM "user"' + ' WHERE "user"."role" IN (0, 1)' ) ; if ( ! Array . isArray ( results ) ) { throw new Error ( 'getVideoAffiliations: query result is not an array.' ) ; } const r = { } ; for ( let i = 0 ; i < results . length ; i ++ ) { const result = results [ i ] ; if ( typeof result !== 'object' ) { throw new Error ( 'getVideoAffiliations: query result is not an object' ) ; } if ( ! ( 'username' in result ) ) { throw new Error ( 'getVideoAffiliations: no username field in result' ) ; } const jid = ( result . username as string ) + '@' + prosodyDomain ; r [ jid ] = 'owner' ; } return r ; } async function _addAffiliationByChannelId ( options , prosodyDomain , r , channelId ) { try { const username = await getUserNameByChannelId ( options , channelId ) ; if ( username === null ) { options . peertubeHelpers . logger . error ( `Failed to get the username for channelId '${ channelId }'.` ) ; } else { const userJid = username + '@' + prosodyDomain ; if ( ! ( userJid in r ) ) { r [ userJid ] = 'admin' ; } } } catch ( error ) { options . peertubeHelpers . logger . error ( 'Failed to get channel owner informations:' , error ) ; } } async function getVideoAffiliations ( options , video ) { const prosodyDomain = await getProsodyDomain ( options ) ; const r = await _getCommonAffiliations ( options , prosodyDomain ) ; if ( ! video . remote ) { await _addAffiliationByChannelId ( options , prosodyDomain , r , video . channelId ) ; } return r ; } async function getChannelAffiliations ( options , channelId ) { const prosodyDomain = await getProsodyDomain ( options ) ; const r = await _getCommonAffiliations ( options , prosodyDomain ) ; await _addAffiliationByChannelId ( options , prosodyDomain , r , channelId ) ; return r ; } export { Affiliations , getVideoAffiliations , getChannelAffiliations } ;