Chat Federation: refactoring ActivityPub data:

The data format used by plugin v6.3.0 was not well suited.
Here comes a new data format, with S2S informations.
The plugin can automatically upgrade old format.
It also continues to provide the old format, so than remote instance
that did not update the plugin will still work.
This commit is contained in:
John Livingston
2023-05-24 15:09:56 +02:00
parent 4f9534dc11
commit 6ed69d2c2f
10 changed files with 547 additions and 212 deletions

View File

@ -1,4 +1,4 @@
import type { LiveChatJSONLDAttribute } from './types'
import type { LiveChatJSONLDAttributeV1 } from './types'
interface AnonymousConnectionInfos {
roomJID: string
@ -7,42 +7,59 @@ interface AnonymousConnectionInfos {
userJID: string
}
function anonymousConnectionInfos (livechatInfos: LiveChatJSONLDAttribute | false): AnonymousConnectionInfos | null {
function anonymousConnectionInfos (livechatInfos: LiveChatJSONLDAttributeV1 | false): AnonymousConnectionInfos | null {
if (!livechatInfos) { return null }
if (!livechatInfos.links) { 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: ''
userJID: livechatInfos.xmppserver.anonymous.virtualhost
}
for (const link of livechatInfos.links) {
// Note: userJID is on both links. But should have the same value.
if (link.type === 'xmpp-bosh-anonymous') {
r.boshUri = link.url
r.userJID = link.jid
} else if (link.type === 'xmpp-websocket-anonymous') {
r.wsUri = link.url
r.userJID = link.jid
}
if (livechatInfos.xmppserver.anonymous.bosh) {
r.boshUri = livechatInfos.xmppserver.anonymous.bosh
}
if (r.userJID === '') {
if (livechatInfos.xmppserver.anonymous.websocket) {
r.wsUri = livechatInfos.xmppserver.anonymous.websocket
}
if (!r.boshUri && !r.wsUri) {
return null
}
return r
}
function remoteAuthenticatedConnectionEnabled (livechatInfos: LiveChatJSONLDAttribute | false): boolean {
function remoteAuthenticatedConnectionEnabled (livechatInfos: LiveChatJSONLDAttributeV1): boolean {
if (!livechatInfos) { return false }
if (!livechatInfos.links) { return false }
if (livechatInfos.type !== 'xmpp') { return false }
for (const link of livechatInfos.links) {
if (link.type === 'xmpp-peertube-livechat-ws-s2s') { return true }
if (link.type === 'xmpp-s2s') { return true }
}
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 }
if (canDirectS2S && livechatInfos.xmppserver.directs2s) { return true }
return false
}
export {
anonymousConnectionInfos,
remoteAuthenticatedConnectionEnabled
remoteAuthenticatedConnectionEnabled,
compatibleRemoteAuthenticatedConnectionEnabled
}