WIP: store and get relation between rooms and channels
This commit is contained in:
114
server/lib/room/channel.ts
Normal file
114
server/lib/room/channel.ts
Normal file
@ -0,0 +1,114 @@
|
||||
import { RegisterServerOptions } from '@peertube/peertube-types'
|
||||
import { getProsodyDomain } from '../prosody/config/domain'
|
||||
import * as path from 'path'
|
||||
import * as fs from 'fs'
|
||||
|
||||
/**
|
||||
* Stores that given room is related to given channel.
|
||||
* Can throw an exception.
|
||||
* @param options server options
|
||||
* @param channelId channel ID
|
||||
* @param roomJIDLocalPart room JID (only the local part)
|
||||
*/
|
||||
async function setChannel2Room (
|
||||
options: RegisterServerOptions,
|
||||
channelId: number,
|
||||
roomJIDLocalPart: string
|
||||
): Promise<void> {
|
||||
const logger = options.peertubeHelpers.logger
|
||||
logger.info(`Calling setChannel2Room for channel ${channelId} and room ${roomJIDLocalPart}...`)
|
||||
|
||||
_checkParameters(channelId, roomJIDLocalPart)
|
||||
|
||||
const prosodyDomain = await getProsodyDomain(options)
|
||||
|
||||
{
|
||||
const [channel2roomDir, channel2room] = await _getFilePath(
|
||||
options, channelId, roomJIDLocalPart, prosodyDomain, 'channel2room'
|
||||
)
|
||||
await fs.promises.mkdir(channel2roomDir, {
|
||||
recursive: true
|
||||
})
|
||||
await fs.promises.writeFile(
|
||||
channel2room,
|
||||
''
|
||||
)
|
||||
}
|
||||
|
||||
{
|
||||
const [room2channelDir, room2channel, room2channelFile] = await _getFilePath(
|
||||
options, channelId, roomJIDLocalPart, prosodyDomain, 'room2channel'
|
||||
)
|
||||
await fs.promises.mkdir(room2channelDir, {
|
||||
recursive: true
|
||||
})
|
||||
|
||||
// The video's channel could have changed. We must delete any deprecated file.
|
||||
const previousFiles = await fs.promises.readdir(room2channelDir)
|
||||
for (const filename of previousFiles) {
|
||||
if (filename !== room2channelFile) {
|
||||
const p = path.resolve(room2channelDir, filename)
|
||||
logger.info('Cleaning a deprecated room2channelFile: ' + p)
|
||||
await fs.promises.unlink(p)
|
||||
}
|
||||
}
|
||||
|
||||
await fs.promises.writeFile(
|
||||
room2channel,
|
||||
''
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
function _checkParameters (channelId: number | string, roomJIDLocalPart: string): void {
|
||||
channelId = channelId.toString()
|
||||
if (!/^\d+$/.test(channelId)) {
|
||||
throw new Error(`Invalid Channel ID: ${channelId}`)
|
||||
}
|
||||
|
||||
if (!/^[\w-.]+$/.test(roomJIDLocalPart)) { // channel.X or video uuid
|
||||
throw new Error(`Invalid ROOM JID: ${channelId}`)
|
||||
}
|
||||
}
|
||||
|
||||
async function _getFilePath (
|
||||
options: RegisterServerOptions,
|
||||
channelId: number | string,
|
||||
roomJIDLocalPart: string,
|
||||
prosodyDomain: string,
|
||||
way: 'channel2room' | 'room2channel'
|
||||
): Promise<[string, string, string]> {
|
||||
channelId = channelId.toString()
|
||||
|
||||
const roomJID = roomJIDLocalPart + '@' + prosodyDomain
|
||||
|
||||
if (way === 'channel2room') {
|
||||
const dir = path.resolve(
|
||||
options.peertubeHelpers.plugin.getDataDirectoryPath(),
|
||||
'channel2room',
|
||||
channelId
|
||||
)
|
||||
return [
|
||||
dir,
|
||||
path.resolve(dir, roomJID),
|
||||
roomJID
|
||||
]
|
||||
} else if (way === 'room2channel') {
|
||||
const dir = path.resolve(
|
||||
options.peertubeHelpers.plugin.getDataDirectoryPath(),
|
||||
'room2channel',
|
||||
roomJID
|
||||
)
|
||||
return [
|
||||
dir,
|
||||
path.resolve(dir, channelId),
|
||||
channelId
|
||||
]
|
||||
} else {
|
||||
throw new Error('Invalid way parameter')
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
setChannel2Room
|
||||
}
|
@ -6,6 +6,7 @@ import { getCheckAPIKeyMiddleware } from '../../middlewares/apikey'
|
||||
import { Affiliations, getVideoAffiliations, getChannelAffiliations } from '../../prosody/config/affiliations'
|
||||
import { fillVideoCustomFields } from '../../custom-fields'
|
||||
import { getChannelInfosById } from '../../database/channel'
|
||||
import { setChannel2Room } from '../../room/channel'
|
||||
|
||||
// See here for description: https://modules.prosody.im/mod_muc_http_defaults.html
|
||||
interface RoomDefaults {
|
||||
@ -78,6 +79,9 @@ async function initRoomApiRouter (options: RegisterServerOptions, router: Router
|
||||
},
|
||||
affiliations: affiliations
|
||||
}
|
||||
|
||||
await setChannel2Room(options, channelId, jid)
|
||||
|
||||
res.json(roomDefaults)
|
||||
} else {
|
||||
// FIXME: @peertube/peertype-types@4.2.2: wrongly considere video as MVideoThumbnail.
|
||||
@ -127,6 +131,9 @@ async function initRoomApiRouter (options: RegisterServerOptions, router: Router
|
||||
},
|
||||
affiliations: affiliations
|
||||
}
|
||||
|
||||
await setChannel2Room(options, video.channelId, jid)
|
||||
|
||||
res.json(roomDefaults)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user