2021-05-06 11:31:55 +00:00
|
|
|
import { getProsodyDomain } from './domain'
|
2021-08-03 22:57:14 +00:00
|
|
|
import { getUserNameByChannelId } from '../../database/channel'
|
2021-05-06 11:31:55 +00:00
|
|
|
|
2021-05-05 13:22:37 +00:00
|
|
|
interface Affiliations { [jid: string]: 'outcast' | 'none' | 'member' | 'admin' | 'owner' }
|
|
|
|
|
2021-08-05 13:41:49 +00:00
|
|
|
async function _getCommonAffiliations (options: RegisterServerOptions, prosodyDomain: string): Promise<Affiliations> {
|
2021-05-05 13:22:37 +00:00
|
|
|
// Get all admins and moderators
|
2021-08-05 13:41:49 +00:00
|
|
|
const [results] = await options.peertubeHelpers.database.query(
|
2021-05-05 13:22:37 +00:00
|
|
|
'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: Affiliations = {}
|
|
|
|
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')
|
|
|
|
}
|
2021-05-06 11:31:55 +00:00
|
|
|
const jid = (result.username as string) + '@' + prosodyDomain
|
2021-05-05 13:22:37 +00:00
|
|
|
r[jid] = 'owner'
|
|
|
|
}
|
|
|
|
|
2021-08-05 13:41:49 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _addAffiliationByChannelId (
|
|
|
|
options: RegisterServerOptions,
|
|
|
|
prosodyDomain: string,
|
|
|
|
r: Affiliations,
|
|
|
|
channelId: number
|
|
|
|
): Promise<void> {
|
2021-05-05 16:00:03 +00:00
|
|
|
// NB: if it fails, we want previous results to be returned...
|
|
|
|
try {
|
2021-08-05 13:41:49 +00:00
|
|
|
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
|
2021-05-06 19:05:46 +00:00
|
|
|
if (!(userJid in r)) { // don't override if already owner!
|
|
|
|
r[userJid] = 'admin'
|
|
|
|
}
|
2021-05-06 10:42:07 +00:00
|
|
|
}
|
2021-05-05 16:00:03 +00:00
|
|
|
} catch (error) {
|
2021-08-05 13:41:49 +00:00
|
|
|
options.peertubeHelpers.logger.error('Failed to get channel owner informations:', error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getVideoAffiliations (options: RegisterServerOptions, video: MVideoThumbnail): Promise<Affiliations> {
|
|
|
|
const prosodyDomain = await getProsodyDomain(options)
|
|
|
|
const r = await _getCommonAffiliations(options, prosodyDomain)
|
|
|
|
|
|
|
|
// Adding an 'admin' affiliation for video owner
|
|
|
|
if (!video.remote) {
|
|
|
|
// don't add the video owner if it is a remote video!
|
|
|
|
await _addAffiliationByChannelId(options, prosodyDomain, r, video.channelId)
|
2021-05-05 16:00:03 +00:00
|
|
|
}
|
2021-05-05 13:22:37 +00:00
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-08-05 13:41:49 +00:00
|
|
|
async function getChannelAffiliations (options: RegisterServerOptions, channelId: number): Promise<Affiliations> {
|
|
|
|
const prosodyDomain = await getProsodyDomain(options)
|
|
|
|
const r = await _getCommonAffiliations(options, prosodyDomain)
|
|
|
|
|
|
|
|
// Adding an 'admin' affiliation for channel owner
|
|
|
|
// NB: remote channel can't be found, there are not in the videoChannel table.
|
|
|
|
await _addAffiliationByChannelId(options, prosodyDomain, r, channelId)
|
|
|
|
|
|
|
|
return r
|
2021-05-05 16:00:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 13:22:37 +00:00
|
|
|
export {
|
|
|
|
Affiliations,
|
2021-08-05 13:41:49 +00:00
|
|
|
getVideoAffiliations,
|
|
|
|
getChannelAffiliations
|
2021-05-05 13:22:37 +00:00
|
|
|
}
|