2022-01-11 00:29:33 +00:00
|
|
|
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
|
|
|
|
2021-08-03 22:22:19 +00:00
|
|
|
async function getChannelNameById (options: RegisterServerOptions, channelId: number): Promise<string | null> {
|
|
|
|
if (!channelId) {
|
|
|
|
throw new Error('Missing channelId')
|
|
|
|
}
|
|
|
|
if (!Number.isInteger(channelId)) {
|
|
|
|
throw new Error('Invalid channelId: not an integer')
|
|
|
|
}
|
|
|
|
const [results] = await options.peertubeHelpers.database.query(
|
|
|
|
'SELECT "actor"."preferredUsername"' +
|
|
|
|
' FROM "videoChannel"' +
|
|
|
|
' RIGHT JOIN "actor" ON "actor"."id" = "videoChannel"."actorId"' +
|
|
|
|
' WHERE "videoChannel"."id" = ' + channelId.toString()
|
|
|
|
)
|
|
|
|
if (!Array.isArray(results)) {
|
|
|
|
throw new Error('getChannelNameById: query result is not an array.')
|
|
|
|
}
|
|
|
|
if (!results[0]) {
|
|
|
|
options.peertubeHelpers.logger.debug(`getChannelNameById: channel ${channelId} not found.`)
|
|
|
|
return null
|
|
|
|
}
|
2021-08-03 22:57:14 +00:00
|
|
|
return results[0].preferredUsername ?? null
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getUserNameByChannelId (options: RegisterServerOptions, channelId: number): Promise<string | null> {
|
|
|
|
if (!channelId) {
|
|
|
|
throw new Error('Missing channelId')
|
|
|
|
}
|
|
|
|
if (!Number.isInteger(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" = ' + channelId.toString()
|
|
|
|
)
|
|
|
|
if (!Array.isArray(results)) {
|
|
|
|
throw new Error('getUserNameByChannelId: query result is not an array.')
|
|
|
|
}
|
|
|
|
if (!results[0]) {
|
|
|
|
options.peertubeHelpers.logger.debug(`getUserNameByChannelId: channel ${channelId} not found.`)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return results[0].username ?? null
|
2021-08-03 22:22:19 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 13:41:49 +00:00
|
|
|
interface ChannelInfos {
|
|
|
|
id: number
|
|
|
|
name: string
|
|
|
|
displayName: string
|
2023-08-08 16:26:40 +00:00
|
|
|
ownerAccountId: number
|
2021-08-05 13:41:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getChannelInfosById (options: RegisterServerOptions, channelId: number): Promise<ChannelInfos | null> {
|
|
|
|
if (!channelId) {
|
|
|
|
throw new Error('Missing channelId')
|
|
|
|
}
|
|
|
|
if (!Number.isInteger(channelId)) {
|
|
|
|
throw new Error('Invalid channelId: not an integer')
|
|
|
|
}
|
|
|
|
const [results] = await options.peertubeHelpers.database.query(
|
|
|
|
'SELECT' +
|
2021-08-05 16:25:27 +00:00
|
|
|
' "actor"."preferredUsername" as "channelName",' +
|
|
|
|
' "videoChannel"."id" as "channelId",' +
|
2023-08-08 16:26:40 +00:00
|
|
|
' "videoChannel"."name" as "channelDisplayName",' +
|
|
|
|
' "videoChannel"."accountId" as "ownerAccountId"' +
|
2021-08-05 13:41:49 +00:00
|
|
|
' FROM "videoChannel"' +
|
|
|
|
' RIGHT JOIN "actor" ON "actor"."id" = "videoChannel"."actorId"' +
|
|
|
|
' WHERE "videoChannel"."id" = ' + channelId.toString()
|
|
|
|
)
|
|
|
|
if (!Array.isArray(results)) {
|
|
|
|
throw new Error('getChannelInfosById: query result is not an array.')
|
|
|
|
}
|
|
|
|
if (!results[0]) {
|
|
|
|
options.peertubeHelpers.logger.debug(`getChannelInfosById: channel ${channelId} not found.`)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return {
|
2021-08-05 16:25:27 +00:00
|
|
|
id: results[0].channelId,
|
2021-08-05 13:41:49 +00:00
|
|
|
name: results[0].channelName ?? '',
|
2023-08-08 16:26:40 +00:00
|
|
|
displayName: results[0].channelDisplayName ?? '',
|
|
|
|
ownerAccountId: results[0].ownerAccountId
|
2021-08-05 13:41:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 22:22:19 +00:00
|
|
|
export {
|
2021-08-03 22:57:14 +00:00
|
|
|
getChannelNameById,
|
2021-08-05 13:41:49 +00:00
|
|
|
getUserNameByChannelId,
|
|
|
|
getChannelInfosById
|
2021-08-03 22:22:19 +00:00
|
|
|
}
|