peertube-plugin-livechat/client/common/moderation/register.ts

70 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-08-08 15:15:05 +00:00
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
import { renderModerationHome } from './templates/home'
2023-08-08 16:26:40 +00:00
import { renderModerationChannel } from './templates/channel'
2023-08-09 10:20:19 +00:00
import { vivifyModerationChannel } from './logic/channel'
2023-08-08 15:15:05 +00:00
2023-08-08 16:26:40 +00:00
/**
* Registers stuff related to the moderation settings.
* @param clientOptions Peertube client options
*/
2023-08-08 15:15:05 +00:00
async function registerModeration (clientOptions: RegisterClientOptions): Promise<void> {
const { peertubeHelpers, registerClientRoute, registerHook } = clientOptions
registerClientRoute({
route: 'livechat/moderation',
onMount: async ({ rootEl }) => {
rootEl.innerHTML = await renderModerationHome(clientOptions)
}
})
2023-08-08 16:26:40 +00:00
registerClientRoute({
route: 'livechat/moderation/channel',
onMount: async ({ rootEl }) => {
const urlParams = new URLSearchParams(window.location.search)
const channelId = urlParams.get('channelId') ?? ''
2023-08-09 10:20:19 +00:00
const html = await renderModerationChannel(clientOptions, channelId)
if (!html) {
// renderModerationChannel has already used the notifier to display an error
rootEl.innerHTML = ''
return
}
rootEl.innerHTML = html
2023-08-09 14:16:02 +00:00
await vivifyModerationChannel(clientOptions, rootEl, channelId)
2023-08-08 16:26:40 +00:00
}
})
2023-08-08 15:15:05 +00:00
registerHook({
target: 'filter:left-menu.links.create.result',
handler: async (links: any) => {
// Adding the links to livechat/moderation for logged users.
if (!peertubeHelpers.isLoggedIn()) { return links }
if (!Array.isArray(links)) { return links }
let myLibraryLinks
// Searching the 'in-my-library' entry.
for (const link of links) {
if (typeof link !== 'object') { continue }
if (!('key' in link)) { continue }
if (link.key !== 'in-my-library') { continue }
myLibraryLinks = link
break
}
if (!myLibraryLinks) { return links }
if (!Array.isArray(myLibraryLinks.links)) { return links }
const label = await peertubeHelpers.translate(LOC_MENU_MODERATION_LABEL)
myLibraryLinks.links.push({
label,
shortLabel: label,
path: '/p/livechat/moderation',
icon: 'live'
})
return links
}
})
}
export {
registerModeration
}