Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LastMessageCorrectionPlugin.ts « plugins « src - github.com/jsxc/jsxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c3db4c4680b8c9a18d428a680f5d13890bd08a21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { AbstractPlugin, IMetaData } from '../plugin/AbstractPlugin';
import PluginAPI from '../plugin/PluginAPI';
import Translation from '@util/Translation';
import * as Namespace from '@connection/xmpp/namespace';
import { IContact } from '@src/Contact.interface';
import { IMessage } from '@src/Message.interface';
import Message from '@src/Message';

/**
 * XEP-0308: Last Message Correction
 *
 * @version: 1.2.0
 * @see: https://xmpp.org/extensions/xep-0308.html
 *
 */

const CORRECTION_CMD = '/fix';
const LMC = 'urn:xmpp:message-correct:0';

const MIN_VERSION = '4.3.0';
const MAX_VERSION = '99.0.0';

Namespace.register('LAST_MSG_CORRECTION', LMC);

export default class LastMessageCorrectionPlugin extends AbstractPlugin {
   public static getId(): string {
      return 'lmc';
   }

   public static getName(): string {
      return 'Last Message Correction';
   }

   public static getMetaData(): IMetaData {
      return {
         description: Translation.t('setting-lmc-enable'),
         xeps: [
            {
               id: 'XEP-0308',
               name: 'Last Message Correction',
               version: '1.2.0',
            },
         ],
      };
   }

   private correctionRequests: { [contactUid: string]: IMessage } = {};

   constructor(pluginAPI: PluginAPI) {
      super(MIN_VERSION, MAX_VERSION, pluginAPI);

      pluginAPI.registerCommand(CORRECTION_CMD, this.commandHandler, 'cmd_correction');

      pluginAPI.addPreSendMessageStanzaProcessor(this.addReplaceElementToStanza, 90);

      pluginAPI.addAfterReceiveMessageProcessor(this.checkMessageCorrection, 90);

      pluginAPI.registerChatMessageMenuItem({
         generate: this.generateCorrectMessageMenuItem,
      });
   }

   private commandHandler = async (args: string[], contact: IContact, messageString: string) => {
      const originalMessage = contact.getTranscript().getFirstOutgoingMessage();

      this.correctionRequests[contact.getUid()] = originalMessage;

      if (!originalMessage || !contact.isChat()) {
         return false;
      }

      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(),
      });

      contact.getTranscript().pushMessage(message);

      chatWindow.clearAttachment();

      let pipe = contact.getAccount().getPipe('preSendMessage');

      return pipe
         .run(contact, message)
         .then(([contact, message]) => {
            originalMessage.getLastVersion().setReplacedBy(message);

            contact.getAccount().getConnection().sendMessage(message);

            return true;
         })
         .catch(err => {
            this.pluginAPI.Log.warn('Error during preSendMessage pipe', err);

            return false;
         });
   };

   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];
      }

      delete this.correctionRequests[contact.getUid()];

      xmlMsg
         .c('replace', {
            xmlns: LMC,
            id: originalMessage.getAttrId(),
         })
         .up();

      return [message, xmlMsg];
   };

   private checkMessageCorrection = async (
      contact: IContact,
      message: IMessage,
      stanza: Element
   ): Promise<[IContact, IMessage, Element]> => {
      const replaceElement = $(stanza).find(`>replace[xmlns="${LMC}"]`);

      if (replaceElement.length === 0) {
         return [contact, message, stanza];
      }

      const attrIdToBeReplaced = replaceElement.attr('id');

      if (!attrIdToBeReplaced) {
         return [contact, message, stanza];
      }

      const firstIncomingMessage = contact.getTranscript().getFirstIncomingMessage();

      if (firstIncomingMessage && firstIncomingMessage.getAttrId() === attrIdToBeReplaced) {
         message.setOriginal(firstIncomingMessage);

         firstIncomingMessage.getLastVersion().setReplacedBy(message);
      }

      return [contact, message, stanza];
   };

   private generateCorrectMessageMenuItem = (contact: IContact, message: IMessage) => {
      if (message.isOutgoing()) {
         const lastOutgoingMessage = contact.getTranscript().getFirstOutgoingMessage().getLastVersion();

         if (lastOutgoingMessage.getUid() === message.getUid()) {
            const chatWindow = contact.getChatWindow();

            return {
               id: 'lmc-edit',
               label: '',
               icon: 'edit',
               handler: () => {
                  let plaintextMessage = message.getPlaintextMessage();

                  if (message.hasAttachment()) {
                     const attachment = message.getAttachment();

                     chatWindow.setAttachment(attachment);
                     plaintextMessage = plaintextMessage.replace(attachment.getData() + '\n', '');
                  }

                  chatWindow.setInput(CORRECTION_CMD + ' ' + plaintextMessage);
               },
            };
         }
      }

      return false;
   };
}