From 5d323b2dfdc8b84e2139383b652e89d112246ce0 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Fri, 21 Apr 2023 16:56:48 +0200 Subject: [PATCH] Chat Federation, connection to remote chat: Front-end connect using BOSH or WS directly on the remote server. If use is logged-in, his nickname is use as default nickname. --- client/videowatch-client-plugin.ts | 20 +-- conversejs/builtin.ts | 35 +++-- conversejs/index.html | 1 + server/lib/federation/connection-infos.ts | 36 +++++ server/lib/routers/api.ts | 3 +- server/lib/routers/webchat.ts | 182 +++++++++++++++------- shared/lib/video.ts | 38 ++++- 7 files changed, 236 insertions(+), 79 deletions(-) create mode 100644 server/lib/federation/connection-infos.ts diff --git a/client/videowatch-client-plugin.ts b/client/videowatch-client-plugin.ts index 69aa6e98..73ccc3a4 100644 --- a/client/videowatch-client-plugin.ts +++ b/client/videowatch-client-plugin.ts @@ -1,6 +1,6 @@ import type { Video } from '@peertube/peertube-types' import type { RegisterClientOptions } from '@peertube/peertube-types/client' -import { videoHasWebchat } from 'shared/lib/video' +import { videoHasWebchat, videoHasRemoteWebchat } from 'shared/lib/video' import { logger } from './videowatch/logger' import { closeSVG, openBlankChatSVG, openChatSVG, shareChatUrlSVG } from './videowatch/buttons' import { displayButton, displayButtonOptions } from './videowatch/button' @@ -247,19 +247,21 @@ function register (registerOptions: RegisterClientOptions): void { logger.log('No chat for anonymous users') return } - if (!videoHasWebchat(s, video)) { + if (!videoHasWebchat(s, video) && !videoHasRemoteWebchat(s, video)) { logger.log('This video has no webchat') return } let showShareUrlButton: boolean = false - const chatShareUrl = settings['chat-share-url'] ?? '' - if (chatShareUrl === 'everyone') { - showShareUrlButton = true - } else if (chatShareUrl === 'owner') { - showShareUrlButton = guessIsMine(registerOptions, video) - } else if (chatShareUrl === 'owner+moderators') { - showShareUrlButton = guessIsMine(registerOptions, video) || guessIamIModerator(registerOptions) + if (video.isLocal) { // No need for shareButton on remote chats. + const chatShareUrl = settings['chat-share-url'] ?? '' + if (chatShareUrl === 'everyone') { + showShareUrlButton = true + } else if (chatShareUrl === 'owner') { + showShareUrlButton = guessIsMine(registerOptions, video) + } else if (chatShareUrl === 'owner+moderators') { + showShareUrlButton = guessIsMine(registerOptions, video) || guessIamIModerator(registerOptions) + } } insertChatDom(container as HTMLElement, video, !!settings['chat-open-blank'], showShareUrlButton).then(() => { diff --git a/conversejs/builtin.ts b/conversejs/builtin.ts index 88891979..d2e1ce6d 100644 --- a/conversejs/builtin.ts +++ b/conversejs/builtin.ts @@ -82,6 +82,7 @@ function randomNick (base: string): string { interface InitConverseParams { jid: string + remoteAnonymousXMPPServer: boolean assetsPath: string room: string boshServiceUrl: string @@ -95,6 +96,7 @@ interface InitConverseParams { } window.initConverse = async function initConverse ({ jid, + remoteAnonymousXMPPServer, assetsPath, room, boshServiceUrl, @@ -205,6 +207,7 @@ window.initConverse = async function initConverse ({ // TODO: params.clear_messages_on_reconnection = true when muc_mam will be available. let isAuthenticated: boolean = false + let isRemoteWithNicknameSet: boolean = false if (authenticationUrl === '') { throw new Error('Missing authenticationUrl') } @@ -218,19 +221,27 @@ window.initConverse = async function initConverse ({ const auth = await authenticatedMode(authenticationUrl) if (auth) { - params.authentication = 'login' - params.auto_login = true - params.jid = auth.jid - params.password = auth.password - if (auth.nickname) { - params.nickname = auth.nickname + if (remoteAnonymousXMPPServer) { + // Spécial case: anonymous connection to remote XMPP server. + if (auth.nickname) { + params.nickname = auth.nickname + isRemoteWithNicknameSet = true + } } else { - params.muc_nickname_from_jid = true + params.authentication = 'login' + params.auto_login = true + params.jid = auth.jid + params.password = auth.password + if (auth.nickname) { + params.nickname = auth.nickname + } else { + params.muc_nickname_from_jid = true + } + // We dont need the keepalive. And I suppose it is related to some bugs when opening a previous chat window. + params.keepalive = false + isAuthenticated = true + // FIXME: use params.oauth_providers? } - // We dont need the keepalive. And I suppose it is related to some bugs when opening a previous chat window. - params.keepalive = false - isAuthenticated = true - // FIXME: use params.oauth_providers? } if (!isAuthenticated) { @@ -267,7 +278,7 @@ window.initConverse = async function initConverse ({ } }) - if (autoViewerMode && !isAuthenticated) { + if (autoViewerMode && !isAuthenticated && !isRemoteWithNicknameSet) { converse.plugins.add('livechatViewerModePlugin', { dependencies: ['converse-muc', 'converse-muc-views'], initialize: function () { diff --git a/conversejs/index.html b/conversejs/index.html index bab4e7f1..bdb921a2 100644 --- a/conversejs/index.html +++ b/conversejs/index.html @@ -16,6 +16,7 @@