peertube-plugin-livechat/server/lib/federation/connection-infos.ts
John Livingston 472caf9f5b
Fix: direct s2s connection not always possible
Trying to connect to a remote instance using direct s2s won't work if local instance has not the feature enabled, and if the remote instance does not know the local one.
So using Websocket S2S in such case (that embed a discovery mecanism).
2023-06-01 13:06:59 +02:00

68 lines
2.1 KiB
TypeScript

import type { LiveChatJSONLDAttributeV1 } from './types'
interface AnonymousConnectionInfos {
roomJID: string
boshUri?: string
wsUri?: string
userJID: string
}
function anonymousConnectionInfos (livechatInfos: LiveChatJSONLDAttributeV1 | false): AnonymousConnectionInfos | null {
if (!livechatInfos) { return null }
if (livechatInfos.type !== 'xmpp') { return null }
if (!livechatInfos.xmppserver) { return null }
if (!livechatInfos.xmppserver.anonymous) { return null }
const r: AnonymousConnectionInfos = {
roomJID: livechatInfos.jid,
userJID: livechatInfos.xmppserver.anonymous.virtualhost
}
if (livechatInfos.xmppserver.anonymous.bosh) {
r.boshUri = livechatInfos.xmppserver.anonymous.bosh
}
if (livechatInfos.xmppserver.anonymous.websocket) {
r.wsUri = livechatInfos.xmppserver.anonymous.websocket
}
if (!r.boshUri && !r.wsUri) {
return null
}
return r
}
function remoteAuthenticatedConnectionEnabled (livechatInfos: LiveChatJSONLDAttributeV1): boolean {
if (!livechatInfos) { return false }
if (livechatInfos.type !== 'xmpp') { return false }
if (!('xmppserver' in livechatInfos)) { return false }
if (!livechatInfos.xmppserver) { return false }
if (livechatInfos.xmppserver.websockets2s) { return true }
if (livechatInfos.xmppserver.directs2s) { return true }
return false
}
function compatibleRemoteAuthenticatedConnectionEnabled (
livechatInfos: LiveChatJSONLDAttributeV1,
canWebsocketS2S: boolean,
canDirectS2S: boolean
): boolean {
if (!livechatInfos) { return false }
if (livechatInfos.type !== 'xmpp') { return false }
if (!('xmppserver' in livechatInfos)) { return false }
if (!livechatInfos.xmppserver) { return false }
if (canWebsocketS2S && livechatInfos.xmppserver.websockets2s) { return true }
// Note: see comments neer option s2s_peertubelivechat_no_outgoing_directs2s_to_peertube
// to understand why we need both to be true.
if (canDirectS2S && livechatInfos.xmppserver.directs2s) { return true }
return false
}
export {
anonymousConnectionInfos,
remoteAuthenticatedConnectionEnabled,
compatibleRemoteAuthenticatedConnectionEnabled
}