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

Plugin.ts « mam « plugins « src - github.com/jsxc/jsxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ca54aa62fb8ba6b405761fea71895a5bae6707fc (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { AbstractPlugin, IMetaData } from '../../plugin/AbstractPlugin';
import ChatWindow from '../../ui/ChatWindow';
import Translation from '../../util/Translation';
import PersistentMap from '../../util/PersistentMap';
import JID from '../../JID';
import { IJID } from '../../JID.interface';
import * as Namespace from '../../connection/xmpp/namespace';
import Archive from './Archive';
import Contact from '@src/Contact';
import PluginAPI from '@src/plugin/PluginAPI';
import { IContact } from '@src/Contact.interface';

const MIN_VERSION = '4.0.0';
const MAX_VERSION = '99.0.0';

const MAM1 = 'urn:xmpp:mam:1';
const MAM2 = 'urn:xmpp:mam:2';

Namespace.register('MAM1', MAM1);
Namespace.register('MAM2', MAM2);

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

   public static getName(): string {
      return 'Message Archive Management';
   }

   public static getDescription(): string {
      return;
   }

   public static getMetaData(): IMetaData {
      return {
         description: Translation.t('setting-mam-enable'),
         xeps: [
            {
               id: 'XEP-0313',
               name: 'Message Archive Management',
               version: '0.6.3',
            },
         ],
      };
   }

   private archives: { [key: string]: Archive } = {};
   private queryContactRelation: PersistentMap;
   private supportCache: { [archiveJid: string]: string | boolean } = {};

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

      this.queryContactRelation = new PersistentMap(pluginAPI.getStorage(), 'mam', 'query');

      pluginAPI.registerChatWindowInitializedHook((chatWindow: ChatWindow, contact: Contact) => {
         this.addLoadButtonIfEnabled(chatWindow, contact);
      });

      pluginAPI.registerChatWindowClearedHook((chatWindow: ChatWindow, contact: Contact) => {
         let archiveJid = this.getArchiveJid(contact);

         if (this.supportCache[archiveJid.bare]) {
            this.getArchive(contact.getJid()).clear();
         }
      });

      this.pluginAPI.getConnection().registerHandler(this.onMamMessage, null, 'message', null);
   }

   public getStorage() {
      return this.pluginAPI.getStorage();
   }

   public getConnection() {
      return this.pluginAPI.getConnection();
   }

   public addQueryContactRelation(queryId: string, contact: IContact) {
      this.queryContactRelation.set(queryId, contact.getJid().bare);
   }

   public removeQueryContactRelation(queryId: string) {
      this.queryContactRelation.remove(queryId);
   }

   public async determineServerSupport(archivingJid: IJID) {
      if (typeof this.supportCache[archivingJid.bare] !== 'undefined') {
         return this.supportCache[archivingJid.bare];
      }

      let discoInfoRepository = this.pluginAPI.getDiscoInfoRepository();

      let version: string | boolean = false;
      try {
         let discoInfo = await discoInfoRepository.getCapabilities(archivingJid);

         if (discoInfo && discoInfo.hasFeature(MAM2)) {
            version = MAM2;
         } else if (discoInfo && discoInfo.hasFeature(MAM1)) {
            version = MAM1;
         }
      } catch (err) {
         this.pluginAPI.Log.warn('Could not determine MAM server support:', err);
      }

      if (version) {
         this.pluginAPI.Log.debug(archivingJid.bare + ' supports ' + version);
      } else {
         this.pluginAPI.Log.debug(archivingJid.bare + ' has no support for MAM');
      }

      this.supportCache[archivingJid.bare] = version;

      return version;
   }

   private getArchiveJid(contact: Contact) {
      let jid = contact.isGroupChat() ? contact.getJid() : this.getConnection().getJID();

      return new JID(jid.bare);
   }

   private addLoadButtonIfEnabled(chatWindow: ChatWindow, contact: Contact) {
      let archivingJid = this.getArchiveJid(contact);

      this.determineServerSupport(archivingJid).then(version => {
         if (version) {
            this.addLoadButton(chatWindow.getDom(), contact);
         }
      });
   }

   private addLoadButton(chatWindowElement: JQuery<HTMLElement>, contact: Contact) {
      let classNameShow = 'jsxc-show';
      let classNameMamEnable = 'jsxc-mam-enabled';
      let messageAreaElement = chatWindowElement.find('.jsxc-message-area');
      let fadeElement = chatWindowElement.find('.jsxc-window-fade');

      let archive = this.getArchive(contact.getJid());

      let element = $('<div>');
      element.addClass('jsxc-mam-load-more');
      element.appendTo(fadeElement);
      let spanElement = $('<span>').text(Translation.t('Load_older_messages'));
      spanElement.click(() => {
         archive.nextMessages();
      });
      element.append(spanElement);

      messageAreaElement.on('scroll', function () {
         let scrollTop : number     = messageAreaElement[0].scrollTop;
         if (scrollTop<0)
            scrollTop=scrollTop*(-1);

         if (((messageAreaElement[0].clientHeight + 42 > messageAreaElement[0].scrollHeight - scrollTop) && !archive.isExhausted())||messageAreaElement.text().trim().length===0) {
            element.addClass(classNameShow);
         } else {
            element.removeClass(classNameShow);
         }
      });

      messageAreaElement.trigger('scroll');

      messageAreaElement.on("DOMSubtreeModified",function(e){
         if ($(this).text().trim().length===0){
            $(this).parent().find('.jsxc-mam-load-more').addClass('jsxc-show');
         }
      });

      if (!archive.isExhausted()) {
         chatWindowElement.addClass(classNameMamEnable);
      }

      archive.registerExhaustedHook(isExhausted => {
         if (isExhausted) {
            chatWindowElement.removeClass(classNameMamEnable);
         } else {
            chatWindowElement.addClass(classNameMamEnable);
            messageAreaElement.scroll();
         }
      });
   }

   private onMamMessage = (stanza: string): boolean => {
      let stanzaElement = $(stanza);
      let resultElement = stanzaElement.find(`result[xmlns^="urn:xmpp:mam:"]`);
      let queryId = resultElement.attr('queryid');

      if (resultElement.length !== 1 || !queryId) {
         return true;
      }

      let forwardedElement = resultElement.find('forwarded[xmlns="urn:xmpp:forward:0"]');

      if (forwardedElement.length !== 1) {
         return true;
      }

      let bareJid = this.queryContactRelation.get(queryId);

      if (!bareJid) {
         return true;
      }

      let jid = new JID(bareJid);

      this.getArchive(jid).onForwardedMessage(forwardedElement);

      return true;
   };

   public getArchive(jid: IJID) {
      if (!this.archives[jid.bare]) {
         this.archives[jid.bare] = new Archive(this, this.pluginAPI.getContact(jid));
      }

      return this.archives[jid.bare];
   }
}