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

Contact.ts « src - github.com/jsxc/jsxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 12a415d863eb303ac7452227dc0a2fe652f650a6 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import { IContact, ContactType, ContactSubscription } from './Contact.interface';
import Storage from './Storage';
import JID from './JID';
import Account from './Account';
import PersistentMap from './util/PersistentMap';
import IIdentifiable from './Identifiable.interface';
import Log from './util/Log';
import { Presence } from './connection/AbstractConnection';
import { EncryptionState } from './plugin/AbstractPlugin';
import Transcript from './Transcript';
import ChatWindowController from './ChatWindowController';
import Avatar from './Avatar';
import Message from './Message';
import ChatWindow from './ui/ChatWindow';
import ContactProvider from './ContactProvider';
import DiscoInfo from './DiscoInfo';
import { IJID } from './JID.interface';

export default class Contact implements IIdentifiable, IContact {
   protected storage: Storage;

   protected readonly account: Account;

   protected data: PersistentMap;

   protected id: string;

   protected jid: JID;

   protected chatWindow;

   protected chatWindowController;

   protected transcript: Transcript;

   public static getProviderId(account: Account, id: string) {
      let data = new PersistentMap(account.getStorage(), 'contact', id);

      return data.get('provider');
   }

   constructor(account: Account, jid: IJID, name?: string);
   constructor(account: Account, id: string);
   constructor() {
      this.account = arguments[0];
      this.storage = this.account.getStorage();

      if (typeof arguments[1] === 'string') {
         this.initExistingContact(arguments[1]);
      } else {
         this.initNewContact(arguments[1], arguments[2]);
      }

      this.id = this.data.get('id');
   }

   private initExistingContact(id: string) {
      this.data = new PersistentMap(this.storage, 'contact', id);

      if (!this.data.get('id')) {
         throw new Error(`Could not find existing contact with id "${id}".`);
      }

      this.jid = new JID(this.data.get('jid'));
   }

   private initNewContact(jid: JID, name?: string) {
      let id = jid.bare;
      this.jid = jid;

      let defaultData = {
         id,
         jid: this.jid.full,
         name: name || this.jid.bare,
         presence: Presence.offline,
         status: '',
         subscription: ContactSubscription.NONE,
         resources: {},
         groups: [],
         type: ContactType.CHAT,
         provider: 'fallback',
         rnd: Math.random(), // force storage event
      };

      this.data = new PersistentMap(this.storage, 'contact', id);

      this.data.set(defaultData);
   }

   public async delete() {
      this.data.delete();
   }

   public getChatWindow(): ChatWindow {
      if (!this.chatWindow) {
         this.chatWindow = new ChatWindow(this);
      }

      return this.chatWindow;
   }

   public getChatWindowController(): ChatWindowController {
      if (!this.chatWindowController) {
         this.chatWindowController = new ChatWindowController(this, this.data);
      }

      return this.chatWindowController;
   }

   public getAccount(): Account {
      return this.account;
   }

   public addSystemMessage(messageString: string): Message {
      let message = new Message({
         peer: this.getJid(),
         direction: Message.DIRECTION.SYS,
         plaintextMessage: messageString,
         unread: false,
      });

      this.getTranscript().pushMessage(message);

      return message;
   }

   public setResource = (resource: string) => {
      this.jid = new JID(this.jid.bare + '/' + resource);

      this.data.set('jid', this.jid.full);
   };

   public clearResources() {
      this.data.set('resources', {});
   }

   public setPresence(resource: string, presence: Presence) {
      Log.debug('set presence for ' + this.jid.bare + ' / ' + resource, presence);

      if (resource) {
         let resources = this.data.get('resources') || {};

         if (presence === Presence.offline) {
            delete resources[resource];
         } else {
            resources[resource] = presence;
         }

         this.data.set('resources', resources);
      }

      presence = this.getHighestPresence();

      this.data.set('presence', presence);
   }

   public getCapableResources(features: string[]): Promise<string[]>;
   public getCapableResources(features: string): Promise<string[]>;
   public getCapableResources(features): Promise<string[]> {
      return this.account.getDiscoInfoRepository().getCapableResources(this, features);
   }

   public hasFeatureByResource(resource: string, features: string[]): Promise<{}>;
   public hasFeatureByResource(resource: string, feature: string): Promise<{}>;
   public hasFeatureByResource(resource, feature) {
      if (!resource) {
         throw new Error('I can not lookup a feature without resource');
      }

      let jid = new JID(this.jid.bare + '/' + resource);

      return this.account.getDiscoInfoRepository().hasFeature(jid, feature);
   }

   public getCapabilitiesByResource(resource: string): Promise<DiscoInfo | void> {
      let jid = new JID(this.jid.bare + '/' + resource);

      return this.account.getDiscoInfoRepository().getCapabilities(jid);
   }

   public registerCapableResourcesHook(features: string[], cb: (resources: string[]) => void);
   public registerCapableResourcesHook(features: string, cb: (resources: string[]) => void);
   public registerCapableResourcesHook(features, cb: (resources: string[]) => void) {
      if (typeof features === 'string') {
         features = [features];
      }

      this.getCapableResources(features).then(cb);

      this.registerHook('resources', () => {
         //@REVIEW trigger only on changes
         this.getCapableResources(features).then(cb);
      });
   }

   public getId(): string {
      return this.id;
   }

   public getUid(): string {
      return this.account.getUid() + '@' + this.getId();
   }

   public getJid(): JID {
      return this.jid;
   }

   public getResources(): string[] {
      return Object.keys(this.data.get('resources') || {});
   }

   public getPresence(resource?: string): Presence {
      if (resource) {
         let resources = this.data.get('resources') || {};

         return resources[resource];
      }

      return this.data.get('presence');
   }

   public getType(): ContactType {
      return this.data.get('type');
   }

   public isGroupChat() {
      return this.getType() === ContactType.GROUPCHAT;
   }

   public isChat() {
      return this.getType() === ContactType.CHAT;
   }

   public getNumberOfUnreadMessages(): number {
      return this.transcript.getNumberOfUnreadMessages();
   }

   public hasName(): boolean {
      return !!this.data.get('name');
   }

   public getName(): string {
      return this.data.get('name') || this.jid.bare;
   }

   public getProviderId(): string {
      return this.data.get('provider');
   }

   public getAvatar(): Promise<Avatar> {
      return this.account
         .getPipe('avatar')
         .run(this, undefined)
         .then(([, avatar]) => {
            if (!avatar) {
               throw new Error('No avatar available for ' + this.getId());
            }

            return avatar;
         });
   }

   public getSubscription(): ContactSubscription {
      return this.data.get('subscription');
   }

   public getVcard(): Promise<any> {
      return this.account.getConnection().getVcardService().loadVcard(this.getJid());
   }

   public setEncryptionState(state: EncryptionState, sourceId: string) {
      if (state !== EncryptionState.Plaintext && !sourceId) {
         throw new Error('No encryption source provided');
      }
      this.data.set('encryptionPlugin', state === EncryptionState.Plaintext ? null : sourceId);
      this.data.set('encryptionState', state);
   }

   public getEncryptionState(): EncryptionState {
      return this.data.get('encryptionState') || EncryptionState.Plaintext;
   }

   public getEncryptionPluginId(): string | null {
      return this.data.get('encryptionPlugin') || null;
   }

   public isEncrypted(): boolean {
      return this.getEncryptionState() !== EncryptionState.Plaintext && !!this.getEncryptionPluginId();
   }

   public getTranscript(): Transcript {
      if (!this.transcript) {
         this.transcript = new Transcript(this.storage, this);
      }

      return this.transcript;
   }

   public getStatus(): string {
      return this.data.get('status');
   }

   public setStatus(status: string) {
      return this.data.set('status', status);
   }

   public setName(name: string) {
      this.data.set('name', name);
   }

   public setProvider(provider: ContactProvider) {
      this.data.set('provider', provider.getUid());
   }

   public setSubscription(subscription: ContactSubscription) {
      this.data.set('subscription', subscription);
   }

   public setGroups(groups: string[]) {
      this.data.set('groups', groups);
   }

   public getGroups(): string[] {
      return this.data.get('groups') || [];
   }

   public getLastMessageDate(): Date {
      let lastMessage = this.data.get('lastMessage');

      return lastMessage ? new Date(lastMessage) : undefined;
   }

   public setLastMessageDate(lastMessage: Date) {
      this.data.set('lastMessage', lastMessage.toISOString());
   }

   public registerHook(property: string, func: (newValue: any, oldValue: any) => void) {
      this.data.registerHook(property, func);
   }

   private getHighestPresence(): Presence {
      let maxPresence = Presence.offline;
      let resources = this.data.get('resources');

      for (let resource in resources) {
         if (resources[resource] < maxPresence) {
            maxPresence = resources[resource];
         }
      }

      return maxPresence;
   }
}