Initialize prosody-list-rooms button.
This commit is contained in:
parent
cc9cbf692a
commit
56e74e0877
@ -53,4 +53,8 @@
|
|||||||
|
|
||||||
.peertube-plugin-livechat-warning {
|
.peertube-plugin-livechat-warning {
|
||||||
color: orange;
|
color: orange;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.peertube-plugin-livechat-error {
|
||||||
|
color: red;
|
||||||
|
}
|
||||||
|
1
client/@types/peertube.d.ts
vendored
1
client/@types/peertube.d.ts
vendored
@ -11,6 +11,7 @@ interface RegisterClientHelpers {
|
|||||||
// NB: getBaseRouterRoute will come with Peertube > 3.2.1 (3.3.0?)
|
// NB: getBaseRouterRoute will come with Peertube > 3.2.1 (3.3.0?)
|
||||||
getBaseRouterRoute?: () => string
|
getBaseRouterRoute?: () => string
|
||||||
isLoggedIn: () => boolean
|
isLoggedIn: () => boolean
|
||||||
|
getAuthHeader: () => { 'Authorization': string } | undefined
|
||||||
getSettings: () => Promise<{ [ name: string ]: string }>
|
getSettings: () => Promise<{ [ name: string ]: string }>
|
||||||
notifier: {
|
notifier: {
|
||||||
info: (text: string, title?: string, timeout?: number) => void
|
info: (text: string, title?: string, timeout?: number) => void
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import type { ChatType } from 'shared/lib/types'
|
import type { ChatType, ProsodyListRoomsResult } from 'shared/lib/types'
|
||||||
|
|
||||||
interface ActionPluginSettingsParams {
|
interface ActionPluginSettingsParams {
|
||||||
npmName: string
|
npmName: string
|
||||||
@ -31,6 +31,59 @@ function register ({ registerHook, registerSettingsScript, peertubeHelpers }: Re
|
|||||||
diagButton.setAttribute('href', getBaseRoute() + '/settings/diagnostic')
|
diagButton.setAttribute('href', getBaseRoute() + '/settings/diagnostic')
|
||||||
diagButton.setAttribute('target', '_blank')
|
diagButton.setAttribute('target', '_blank')
|
||||||
})
|
})
|
||||||
|
console.log('[peertube-plugin-livechat] Initializing prosody-list-rooms button')
|
||||||
|
const listRoomsButtons: NodeListOf<HTMLAnchorElement> =
|
||||||
|
document.querySelectorAll('.peertube-plugin-livechat-prosody-list-rooms')
|
||||||
|
listRoomsButtons.forEach(listRoomsButton => {
|
||||||
|
if (listRoomsButton.classList.contains('btn')) { return }
|
||||||
|
listRoomsButton.classList.add('btn')
|
||||||
|
listRoomsButton.classList.add('action-button')
|
||||||
|
listRoomsButton.classList.add('orange-button')
|
||||||
|
listRoomsButton.onclick = async (): Promise<void> => {
|
||||||
|
console.log('[peertube-plugin-livechat] Opening the room list...')
|
||||||
|
// TODO: use showModal (seems buggy with Peertube 3.2.1)
|
||||||
|
|
||||||
|
try {
|
||||||
|
document.querySelectorAll('.peertube-plugin-livechat-prosody-list-rooms-content')
|
||||||
|
.forEach(dom => dom.remove())
|
||||||
|
const container = document.createElement('div')
|
||||||
|
container.classList.add('peertube-plugin-livechat-prosody-list-rooms-content')
|
||||||
|
container.textContent = '...'
|
||||||
|
listRoomsButton.after(container)
|
||||||
|
|
||||||
|
const response = await fetch(getBaseRoute() + '/settings/prosody-list-rooms', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: peertubeHelpers.getAuthHeader()
|
||||||
|
})
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Response is not ok')
|
||||||
|
}
|
||||||
|
const json: ProsodyListRoomsResult = await response.json()
|
||||||
|
if (!json.ok) {
|
||||||
|
container.textContent = json.error
|
||||||
|
container.classList.add('peertube-plugin-livechat-error')
|
||||||
|
} else {
|
||||||
|
container.textContent = ''
|
||||||
|
const table = document.createElement('table')
|
||||||
|
container.append(table)
|
||||||
|
json.rooms.forEach(room => {
|
||||||
|
const lineEl = document.createElement('tr')
|
||||||
|
const nameEl = document.createElement('td')
|
||||||
|
const aEl = document.createElement('a')
|
||||||
|
aEl.textContent = room.name
|
||||||
|
aEl.href = room.href
|
||||||
|
aEl.target = '_blank'
|
||||||
|
nameEl.append(aEl)
|
||||||
|
lineEl.append(nameEl)
|
||||||
|
table.append(lineEl)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
peertubeHelpers.notifier.error('Room list failed')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
registerSettingsScript({
|
registerSettingsScript({
|
||||||
@ -41,6 +94,7 @@ function register ({ registerHook, registerSettingsScript, peertubeHelpers }: Re
|
|||||||
return options.formValues['chat-type'] !== ('disabled' as ChatType)
|
return options.formValues['chat-type'] !== ('disabled' as ChatType)
|
||||||
case 'prosody-port':
|
case 'prosody-port':
|
||||||
case 'chat-type-help-builtin-prosody':
|
case 'chat-type-help-builtin-prosody':
|
||||||
|
case 'prosody-list-rooms':
|
||||||
return options.formValues['chat-type'] !== ('builtin-prosody' as ChatType)
|
return options.formValues['chat-type'] !== ('builtin-prosody' as ChatType)
|
||||||
case 'chat-server':
|
case 'chat-server':
|
||||||
case 'chat-room':
|
case 'chat-room':
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import type { Router, Request, Response, NextFunction } from 'express'
|
import type { Router, Request, Response, NextFunction } from 'express'
|
||||||
|
import type { ChatType, ProsodyListRoomsResult } from '../../../shared/lib/types'
|
||||||
import { diag } from '../diagnostic'
|
import { diag } from '../diagnostic'
|
||||||
import { getBaseStaticRoute, isUserAdmin } from '../helpers'
|
import { getBaseStaticRoute, isUserAdmin } from '../helpers'
|
||||||
import { asyncMiddleware } from '../middlewares/async'
|
import { asyncMiddleware } from '../middlewares/async'
|
||||||
@ -39,6 +40,38 @@ async function initSettingsRouter (options: RegisterServerOptions): Promise<Rout
|
|||||||
}
|
}
|
||||||
))
|
))
|
||||||
|
|
||||||
|
router.get('/prosody-list-rooms', asyncMiddleware(
|
||||||
|
async (req: Request, res: Response, _next: NextFunction) => {
|
||||||
|
if (!res.locals.authenticated) {
|
||||||
|
res.sendStatus(403)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (!await isUserAdmin(options, res)) {
|
||||||
|
res.sendStatus(403)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const chatType: ChatType = await options.settingsManager.getSetting('chat-type') as ChatType
|
||||||
|
if (chatType !== 'builtin-prosody') {
|
||||||
|
const message = 'Please save the settings first.' // TODO: translate?
|
||||||
|
res.status(200)
|
||||||
|
const r: ProsodyListRoomsResult = {
|
||||||
|
ok: false,
|
||||||
|
error: message
|
||||||
|
}
|
||||||
|
res.json(r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200)
|
||||||
|
const r: ProsodyListRoomsResult = {
|
||||||
|
ok: true,
|
||||||
|
rooms: [] // TODO: get room list from Prosody
|
||||||
|
}
|
||||||
|
res.json(r)
|
||||||
|
}
|
||||||
|
))
|
||||||
|
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,6 +91,13 @@ Please read the
|
|||||||
private: true
|
private: true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
registerSetting({
|
||||||
|
name: 'prosody-list-rooms',
|
||||||
|
label: 'List existing rooms',
|
||||||
|
type: 'html',
|
||||||
|
descriptionHTML: '<a class="peertube-plugin-livechat-prosody-list-rooms">List rooms</a>',
|
||||||
|
private: true
|
||||||
|
})
|
||||||
registerSetting({
|
registerSetting({
|
||||||
name: 'prosody-port',
|
name: 'prosody-port',
|
||||||
label: 'Prosody port',
|
label: 'Prosody port',
|
||||||
|
@ -1,5 +1,21 @@
|
|||||||
type ChatType = 'disabled' | 'builtin-prosody' | 'builtin-converse' | 'external-uri'
|
type ChatType = 'disabled' | 'builtin-prosody' | 'builtin-converse' | 'external-uri'
|
||||||
|
|
||||||
export {
|
interface ProsodyListRoomsResultError {
|
||||||
ChatType
|
ok: false
|
||||||
|
error: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ProsodyListRoomsResultSuccess {
|
||||||
|
ok: true
|
||||||
|
rooms: Array<{
|
||||||
|
name: string
|
||||||
|
href: string
|
||||||
|
}>
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProsodyListRoomsResult = ProsodyListRoomsResultError | ProsodyListRoomsResultSuccess
|
||||||
|
|
||||||
|
export {
|
||||||
|
ChatType,
|
||||||
|
ProsodyListRoomsResult
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user