Minor fix + code refactoring.

This commit is contained in:
John Livingston 2021-08-04 00:57:14 +02:00
parent 5300992806
commit ddaf57b5d5
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
2 changed files with 32 additions and 25 deletions

View File

@ -18,9 +18,34 @@ async function getChannelNameById (options: RegisterServerOptions, channelId: nu
options.peertubeHelpers.logger.debug(`getChannelNameById: channel ${channelId} not found.`) options.peertubeHelpers.logger.debug(`getChannelNameById: channel ${channelId} not found.`)
return null return null
} }
return (results[0].preferredUsername ?? null) as string 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
} }
export { export {
getChannelNameById getChannelNameById,
getUserNameByChannelId
} }

View File

@ -1,4 +1,5 @@
import { getProsodyDomain } from './domain' import { getProsodyDomain } from './domain'
import { getUserNameByChannelId } from '../../database/channel'
interface Affiliations { [jid: string]: 'outcast' | 'none' | 'member' | 'admin' | 'owner' } interface Affiliations { [jid: string]: 'outcast' | 'none' | 'member' | 'admin' | 'owner' }
@ -45,30 +46,11 @@ async function getVideoAffiliations (options: RegisterServerOptions, video: MVid
} }
async function _getVideoOwnerUsername (options: RegisterServerOptions, video: MVideoThumbnail): Promise<string> { async function _getVideoOwnerUsername (options: RegisterServerOptions, video: MVideoThumbnail): Promise<string> {
// checking type of video.channelId const username = await getUserNameByChannelId(options, video.channelId)
if (!video.channelId) { if (username === null) {
throw new Error('Missing channelId on video') throw new Error('Username not found')
} }
if (!Number.isInteger(video.channelId)) { return username
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 { export {