Custom channel emoticons WIP (#130)
This commit is contained in:
@ -13,6 +13,7 @@ import { initAuthApiRouter, initUserAuthApiRouter } from './api/auth'
|
||||
import { initFederationServerInfosApiRouter } from './api/federation-server-infos'
|
||||
import { initConfigurationApiRouter } from './api/configuration'
|
||||
import { initPromoteApiRouter } from './api/promote'
|
||||
import { initEmojisRouter } from './emojis'
|
||||
|
||||
/**
|
||||
* Initiate API routes
|
||||
@ -42,6 +43,7 @@ async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
|
||||
|
||||
await initConfigurationApiRouter(options, router)
|
||||
await initPromoteApiRouter(options, router)
|
||||
await initEmojisRouter(options, router)
|
||||
|
||||
if (isDebugMode(options)) {
|
||||
// Only add this route if the debug mode is enabled at time of the server launch.
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
||||
import type { Router, Request, Response, NextFunction } from 'express'
|
||||
import type { ChannelInfos, ChannelEmojis } from '../../../../shared/lib/types'
|
||||
import type { ChannelConfiguration, ChannelEmojisConfiguration, ChannelInfos } from '../../../../shared/lib/types'
|
||||
import { asyncMiddleware } from '../../middlewares/async'
|
||||
import { getCheckConfigurationChannelMiddleware } from '../../middlewares/configuration/channel'
|
||||
import { checkConfigurationEnabledMiddleware } from '../../middlewares/configuration/configuration'
|
||||
@ -15,6 +15,7 @@ import {
|
||||
} from '../../configuration/channel/storage'
|
||||
import { sanitizeChannelConfigurationOptions } from '../../configuration/channel/sanitize'
|
||||
import { getConverseJSParams } from '../../../lib/conversejs/params'
|
||||
import { Emojis } from '../../../lib/emojis'
|
||||
|
||||
async function initConfigurationApiRouter (options: RegisterServerOptions, router: Router): Promise<void> {
|
||||
const logger = options.peertubeHelpers.logger
|
||||
@ -57,7 +58,7 @@ async function initConfigurationApiRouter (options: RegisterServerOptions, route
|
||||
await getChannelConfigurationOptions(options, channelInfos.id) ??
|
||||
getDefaultChannelConfigurationOptions(options)
|
||||
|
||||
const result = {
|
||||
const result: ChannelConfiguration = {
|
||||
channel: channelInfos,
|
||||
configuration: channelOptions
|
||||
}
|
||||
@ -101,7 +102,7 @@ async function initConfigurationApiRouter (options: RegisterServerOptions, route
|
||||
}
|
||||
|
||||
logger.debug('Data seems ok, storing them.')
|
||||
const result = {
|
||||
const result: ChannelConfiguration = {
|
||||
channel: channelInfos,
|
||||
configuration: channelOptions
|
||||
}
|
||||
@ -113,25 +114,62 @@ async function initConfigurationApiRouter (options: RegisterServerOptions, route
|
||||
|
||||
router.get('/configuration/channel/emojis/:channelId', asyncMiddleware([
|
||||
checkConfigurationEnabledMiddleware(options),
|
||||
getCheckConfigurationChannelMiddleware(options, true),
|
||||
getCheckConfigurationChannelMiddleware(options),
|
||||
async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
|
||||
if (!res.locals.channelInfos) {
|
||||
logger.error('Missing channelInfos in res.locals, should not happen')
|
||||
try {
|
||||
if (!res.locals.channelInfos) {
|
||||
throw new Error('Missing channelInfos in res.locals, should not happen')
|
||||
}
|
||||
|
||||
const emojis = Emojis.singleton()
|
||||
const channelInfos = res.locals.channelInfos as ChannelInfos
|
||||
|
||||
const channelEmojis =
|
||||
(await emojis.channelCustomEmojisDefinition(channelInfos.id)) ??
|
||||
emojis.emptyChannelDefinition()
|
||||
|
||||
const result: ChannelEmojisConfiguration = {
|
||||
channel: channelInfos,
|
||||
emojis: channelEmojis
|
||||
}
|
||||
res.status(200)
|
||||
res.json(result)
|
||||
} catch (err) {
|
||||
logger.error(err)
|
||||
res.sendStatus(500)
|
||||
return
|
||||
}
|
||||
// const channelInfos = res.locals.channelInfos as ChannelInfos
|
||||
}
|
||||
]))
|
||||
|
||||
const channelEmojis: ChannelEmojis = {
|
||||
customEmojis: [{
|
||||
sn: ':test:',
|
||||
url: '/dist/images/custom_emojis/xmpp.png',
|
||||
isCategoryEmoji: true
|
||||
}]
|
||||
router.post('/configuration/channel/emojis/:channelId', asyncMiddleware([
|
||||
checkConfigurationEnabledMiddleware(options),
|
||||
getCheckConfigurationChannelMiddleware(options),
|
||||
async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
|
||||
try {
|
||||
if (!res.locals.channelInfos) {
|
||||
throw new Error('Missing channelInfos in res.locals, should not happen')
|
||||
}
|
||||
|
||||
const emojis = Emojis.singleton()
|
||||
const channelInfos = res.locals.channelInfos as ChannelInfos
|
||||
|
||||
const emojisDefinition = req.body
|
||||
let emojisDefinitionSanitized
|
||||
try {
|
||||
emojisDefinitionSanitized = await emojis.sanitizeChannelDefinition(channelInfos.id, emojisDefinition)
|
||||
} catch (err) {
|
||||
logger.warn(err)
|
||||
res.sendStatus(400)
|
||||
return
|
||||
}
|
||||
|
||||
await emojis.saveChannelDefinition(channelInfos.id, emojisDefinitionSanitized)
|
||||
|
||||
res.sendStatus(200)
|
||||
} catch (err) {
|
||||
logger.error(err)
|
||||
res.sendStatus(500)
|
||||
}
|
||||
|
||||
res.status(200)
|
||||
res.json(channelEmojis)
|
||||
}
|
||||
]))
|
||||
}
|
||||
|
90
server/lib/routers/emojis.ts
Normal file
90
server/lib/routers/emojis.ts
Normal file
@ -0,0 +1,90 @@
|
||||
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
||||
import type { Router, Request, Response, NextFunction } from 'express'
|
||||
import { asyncMiddleware } from '../middlewares/async'
|
||||
import { Emojis } from '../emojis'
|
||||
|
||||
export async function initEmojisRouter (
|
||||
options: RegisterServerOptions,
|
||||
router: Router
|
||||
): Promise<void> {
|
||||
const logger = options.peertubeHelpers.logger
|
||||
|
||||
router.get(
|
||||
'/emojis/channel/:channelId/definition',
|
||||
asyncMiddleware(async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
|
||||
try {
|
||||
const emojis = Emojis.singletonSafe()
|
||||
if (!emojis) {
|
||||
res.sendStatus(404)
|
||||
return
|
||||
}
|
||||
|
||||
const channelId = parseInt(req.params.channelId)
|
||||
if (!channelId || isNaN(channelId)) {
|
||||
res.sendStatus(400)
|
||||
return
|
||||
}
|
||||
|
||||
if (!await emojis.channelHasCustomEmojis(channelId)) {
|
||||
res.sendStatus(404)
|
||||
return
|
||||
}
|
||||
|
||||
res.sendFile(emojis.channelCustomEmojisDefinitionPath(channelId))
|
||||
} catch (err) {
|
||||
logger.error(err)
|
||||
res.sendStatus(500)
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
router.get(
|
||||
'/emojis/channel/:channelId/files/:fileName',
|
||||
asyncMiddleware(async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
|
||||
try {
|
||||
const emojis = Emojis.singletonSafe()
|
||||
if (!emojis) {
|
||||
res.sendStatus(404)
|
||||
return
|
||||
}
|
||||
|
||||
const channelId = parseInt(req.params.channelId)
|
||||
if (!channelId || isNaN(channelId)) {
|
||||
res.sendStatus(400)
|
||||
return
|
||||
}
|
||||
|
||||
const fileName = req.params.fileName
|
||||
if (!emojis.validImageFileName(fileName)) {
|
||||
res.sendStatus(400)
|
||||
return
|
||||
}
|
||||
|
||||
if (!await emojis.channelHasCustomEmojis(channelId)) {
|
||||
res.sendStatus(404)
|
||||
return
|
||||
}
|
||||
|
||||
res.sendFile(
|
||||
emojis.channelCustomEmojisFilePath(channelId, fileName),
|
||||
{
|
||||
immutable: true,
|
||||
maxAge: 1000 * 60 * 60 * 24 // 24h
|
||||
},
|
||||
(err) => {
|
||||
if (err) {
|
||||
res.sendStatus(404)
|
||||
}
|
||||
}
|
||||
)
|
||||
} catch (err) {
|
||||
logger.error(err)
|
||||
res.sendStatus(500)
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
Reference in New Issue
Block a user