import { xml } from '@xmpp/client' ; import { Element } from 'ltx' ; import { id } from '../../../../core/id-generator' ; import { Message } from '../../../../core/message' ; import { MessageWithBodyStanza } from '../../../../core/stanza' ; import { AbstractXmppPlugin } from './abstract-xmpp-plugin' ; /**
 * https://xmpp.org/extensions/xep-0359.html
 */ export class MessageUuidPlugin extends AbstractXmppPlugin { public static extractIdFromStanza ( messageStanza ) { const originIdElement = messageStanza . getChild ( 'origin-id' ) ; const stanzaIdElement = messageStanza . getChild ( 'stanza-id' ) ; return messageStanza . attrs . id || ( originIdElement && originIdElement . attrs . id ) || ( stanzaIdElement && stanzaIdElement . attrs . id ) ; } beforeSendMessage ( messageStanza , message ) { const generatedId = id ( ) ; messageStanza . children . push ( xml ( 'origin-id' , { xmlns : 'urn:xmpp:sid:0' , id : generatedId } ) ) ; if ( message ) { message . id = generatedId ; } } afterSendMessage ( message , messageStanza ) { message . id = MessageUuidPlugin . extractIdFromStanza ( messageStanza ) ; } afterReceiveMessage ( message , messageStanza ) { message . id = MessageUuidPlugin . extractIdFromStanza ( messageStanza ) ; } }