diff options
author | sualko <klaus@jsxc.org> | 2021-12-31 14:24:26 +0300 |
---|---|---|
committer | sualko <klaus@jsxc.org> | 2021-12-31 17:07:45 +0300 |
commit | 3f0346c293a01ccc124a16e9c000499bceb735e3 (patch) | |
tree | 6c41c49930856b39c302951405c5542d46ed3dff | |
parent | 49e1ad7c81d76cf3af54ae9ddb1c9e08b4bae5a0 (diff) |
refactor: improve code structure
-rw-r--r-- | src/plugin/PluginAPI.ts | 2 | ||||
-rw-r--r-- | src/plugins/LastMessageCorrectionPlugin.ts | 106 |
2 files changed, 56 insertions, 52 deletions
diff --git a/src/plugin/PluginAPI.ts b/src/plugin/PluginAPI.ts index f5faa6c9..0acf9f8a 100644 --- a/src/plugin/PluginAPI.ts +++ b/src/plugin/PluginAPI.ts @@ -120,7 +120,7 @@ export default class PluginAPI implements IPluginAPI { } public addPreSendMessageStanzaProcessor( - processor: (message: Message, xmlMsg: Strophe.Builder) => Promise<[Message, Strophe.Builder]>, + processor: (message: IMessage, xmlMsg: Strophe.Builder) => Promise<[IMessage, Strophe.Builder]>, position?: number ) { this.account.getPipe('preSendMessageStanza').addProcessor(processor, position); diff --git a/src/plugins/LastMessageCorrectionPlugin.ts b/src/plugins/LastMessageCorrectionPlugin.ts index 8aa1d013..6613a27e 100644 --- a/src/plugins/LastMessageCorrectionPlugin.ts +++ b/src/plugins/LastMessageCorrectionPlugin.ts @@ -49,74 +49,78 @@ export default class LastMessageCorrectionPlugin extends AbstractPlugin { constructor(pluginAPI: PluginAPI) { super(MIN_VERSION, MAX_VERSION, pluginAPI); - pluginAPI.addAfterReceiveMessageProcessor(this.checkMessageCorrection, 90); + pluginAPI.registerCommand(CORRECTION_CMD, this.commandHandler, 'cmd_correction'); - pluginAPI.registerCommand( - CORRECTION_CMD, - async (args, contact, messageString) => { - const originalMessage = contact.getTranscript().getFirstOutgoingMessage(); + pluginAPI.addPreSendMessageStanzaProcessor(this.addReplaceElementToStanza, 90); - this.correctionRequests[contact.getUid()] = originalMessage; + pluginAPI.addAfterReceiveMessageProcessor(this.checkMessageCorrection, 90); + } - if (!originalMessage) { - return false; - } + private commandHandler = async (args: string[], contact: IContact, messageString: string) => { + const originalMessage = contact.getTranscript().getFirstOutgoingMessage(); - const chatWindow = contact.getChatWindow(); - const message = new Message({ - peer: contact.getJid(), - direction: Message.DIRECTION.OUT, - type: contact.getType(), - plaintextMessage: messageString.replace(/^\/fix /, ''), - attachment: chatWindow.getAttachment(), - unread: false, - original: originalMessage.getUid(), - }); + this.correctionRequests[contact.getUid()] = originalMessage; - contact.getTranscript().pushMessage(message); + if (!originalMessage || !contact.isChat()) { + return false; + } - chatWindow.clearAttachment(); + const chatWindow = contact.getChatWindow(); + const message = new Message({ + peer: contact.getJid(), + direction: Message.DIRECTION.OUT, + type: contact.getType(), + plaintextMessage: messageString.replace(/^\/fix /, ''), + attachment: chatWindow.getAttachment(), + unread: false, + original: originalMessage.getUid(), + }); - let pipe = contact.getAccount().getPipe('preSendMessage'); + contact.getTranscript().pushMessage(message); - return pipe - .run(contact, message) - .then(([contact, message]) => { - originalMessage.getLastVersion().setReplacedBy(message); + chatWindow.clearAttachment(); - contact.getAccount().getConnection().sendMessage(message); + let pipe = contact.getAccount().getPipe('preSendMessage'); - return true; - }) - .catch(err => { - this.pluginAPI.Log.warn('Error during preSendMessage pipe', err); + return pipe + .run(contact, message) + .then(([contact, message]) => { + originalMessage.getLastVersion().setReplacedBy(message); - return false; - }); - }, - 'cmd_correction' - ); + contact.getAccount().getConnection().sendMessage(message); - pluginAPI.addPreSendMessageStanzaProcessor(async (message, xmlMsg) => { - const contact = this.pluginAPI.getContact(message.getPeer()); - const originalMessage = this.correctionRequests[contact.getUid()]; + return true; + }) + .catch(err => { + this.pluginAPI.Log.warn('Error during preSendMessage pipe', err); - if (!originalMessage || originalMessage.getLastVersion().getUid() !== message.getUid()) { - return [message, xmlMsg]; - } + return false; + }); + }; - xmlMsg - .c('replace', { - xmlns: LMC, - id: originalMessage.getAttrId(), - }) - .up(); + private addReplaceElementToStanza = async ( + message: IMessage, + xmlMsg: Strophe.Builder + ): Promise<[IMessage, Strophe.Builder]> => { + const contact = this.pluginAPI.getContact(message.getPeer()); + const originalMessage = this.correctionRequests[contact.getUid()]; + if (!originalMessage || originalMessage.getLastVersion().getUid() !== message.getUid()) { return [message, xmlMsg]; - }, 90); - } + } + + delete this.correctionRequests[contact.getUid()]; + + xmlMsg + .c('replace', { + xmlns: LMC, + id: originalMessage.getAttrId(), + }) + .up(); + + return [message, xmlMsg]; + }; - // review carbon copy private checkMessageCorrection = async ( contact: IContact, message: IMessage, |