From e9d1e550845a507eba0a4456712f76e83c40f54c Mon Sep 17 00:00:00 2001 From: John Livingston Date: Thu, 5 Aug 2021 18:25:27 +0200 Subject: [PATCH] Room list: handle channel rooms. --- client/admin-plugin-client-plugin.ts | 25 +++++++++-- server/lib/database/channel.ts | 5 ++- server/lib/routers/webchat.ts | 65 +++++++++++++++++++++++----- shared/lib/types.ts | 26 +++++++---- 4 files changed, 95 insertions(+), 26 deletions(-) diff --git a/client/admin-plugin-client-plugin.ts b/client/admin-plugin-client-plugin.ts index d591f940..d589928d 100644 --- a/client/admin-plugin-client-plugin.ts +++ b/client/admin-plugin-client-plugin.ts @@ -85,6 +85,7 @@ function register ({ registerHook, registerSettingsScript, peertubeHelpers }: Re RoomDescription: 'Room description', NotFound: 'Not found', Video: 'Video', + Channel: 'Channel', LastActivity: 'Last activity' } @@ -94,7 +95,7 @@ function register ({ registerHook, registerSettingsScript, peertubeHelpers }: Re const titleDescriptionEl = document.createElement('th') titleDescriptionEl.textContent = labels.RoomDescription const titleVideoEl = document.createElement('th') - titleVideoEl.textContent = labels.Video + titleVideoEl.textContent = `${labels.Video as string} / ${labels.Channel as string}` const titleLastActivityEl = document.createElement('th') titleLastActivityEl.textContent = labels.LastActivity titleLineEl.append(titleNameEl) @@ -103,8 +104,7 @@ function register ({ registerHook, registerSettingsScript, peertubeHelpers }: Re titleLineEl.append(titleLastActivityEl) table.append(titleLineEl) rooms.forEach(room => { - const uuid = room.localpart - const href = getBaseRoute() + '/webchat/room/' + encodeURIComponent(uuid) + const localpart = room.localpart const lineEl = document.createElement('tr') const nameEl = document.createElement('td') const aEl = document.createElement('a') @@ -125,7 +125,24 @@ function register ({ registerHook, registerSettingsScript, peertubeHelpers }: Re lineEl.append(lastActivityEl) table.append(lineEl) - if (/^[a-zA-A0-9-]+$/.test(uuid)) { + const channelMatches = localpart.match(/^channel\.(\d+)$/) + if (channelMatches) { + // Here we have a channel chat room + // The backend should have added informations here + // (because the Peertube API can't work with channelId...) + const href = getBaseRoute() + '/webchat/room/' + encodeURIComponent(localpart) + if (room.channel?.name) { + aEl.href = href // here we know that the channel still exists, so we can open the webchat. + const aVideoEl = document.createElement('a') + aVideoEl.textContent = room.channel?.displayName ?? '-' + aVideoEl.target = '_blank' + aVideoEl.href = '/video-channels/' + room.channel.name + videoEl.append(aVideoEl) + } + } else if (/^[a-zA-A0-9-]+$/.test(localpart)) { + // localpart must be a video uuid. + const uuid = localpart + const href = getBaseRoute() + '/webchat/room/' + encodeURIComponent(uuid) const p = fetch('/api/v1/videos/' + uuid, { method: 'GET', headers: peertubeHelpers.getAuthHeader() diff --git a/server/lib/database/channel.ts b/server/lib/database/channel.ts index 542d8a5e..573ffc21 100644 --- a/server/lib/database/channel.ts +++ b/server/lib/database/channel.ts @@ -60,7 +60,8 @@ async function getChannelInfosById (options: RegisterServerOptions, channelId: n } const [results] = await options.peertubeHelpers.database.query( 'SELECT' + - ' "actor"."preferredUsername" as "channelName", ' + + ' "actor"."preferredUsername" as "channelName",' + + ' "videoChannel"."id" as "channelId",' + ' "videoChannel"."name" as "channelDisplayName"' + ' FROM "videoChannel"' + ' RIGHT JOIN "actor" ON "actor"."id" = "videoChannel"."actorId"' + @@ -74,7 +75,7 @@ async function getChannelInfosById (options: RegisterServerOptions, channelId: n return null } return { - id: channelId, + id: results[0].channelId, name: results[0].channelName ?? '', displayName: results[0].channelDisplayName ?? '' } diff --git a/server/lib/routers/webchat.ts b/server/lib/routers/webchat.ts index f3544105..0a8b8e3e 100644 --- a/server/lib/routers/webchat.ts +++ b/server/lib/routers/webchat.ts @@ -1,11 +1,11 @@ import type { Router, RequestHandler, Request, Response, NextFunction } from 'express' import type { ProxyOptions } from 'express-http-proxy' -import type { ChatType, ProsodyListRoomsResult } from '../../../shared/lib/types' +import type { ChatType, ProsodyListRoomsResult, ProsodyListRoomsResultRoom } from '../../../shared/lib/types' import { getBaseRouterRoute, getBaseStaticRoute, isUserAdmin } from '../helpers' import { asyncMiddleware } from '../middlewares/async' import { getProsodyDomain } from '../prosody/config/domain' import { getAPIKey } from '../apikey' -import { getChannelNameById } from '../database/channel' +import { getChannelInfosById, getChannelNameById } from '../database/channel' import * as path from 'path' const bodyParser = require('body-parser') const got = require('got') @@ -31,10 +31,11 @@ async function initWebchatRouter (options: RegisterServerOptions): Promise => { res.removeHeader('X-Frame-Options') // this route can be opened in an iframe + const roomKey = req.params.roomKey const settings = await settingsManager.getSettings([ 'chat-type', 'chat-room', 'chat-server', 'chat-bosh-uri', 'chat-ws-uri', @@ -51,7 +52,9 @@ async function initWebchatRouter (options: RegisterServerOptions): Promise + rooms: ProsodyListRoomsResultRoom[] } type ProsodyListRoomsResult = ProsodyListRoomsResultError | ProsodyListRoomsResultSuccess export { ChatType, - ProsodyListRoomsResult + ProsodyListRoomsResult, + ProsodyListRoomsResultRoom }