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

MultiUserContact.ts « src - github.com/jsxc/jsxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0743a8d7f2057e3ffc55dd64f04d2cff5b45684b (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
import Contact from './Contact';
import JID from './JID';
import MultiUserChatWindow from './ui/MultiUserChatWindow';
import PersistentMap from './util/PersistentMap';
import { Presence } from './connection/AbstractConnection';
import Form from './connection/Form';
import Account from './Account';
import { ContactSubscription } from './Contact.interface';
import { IMUCService } from '@connection/Connection.interface';
import { IJID } from './JID.interface';

export const enum AFFILIATION {
   ADMIN = 'admin',
   MEMBER = 'member',
   OUTCAST = 'outcast',
   OWNER = 'owner',
   NONE = 'none',
}
export const enum ROLE {
   MODERATOR = 'moderator',
   PARTICIPANT = 'participant',
   VISITOR = 'visitor',
   NONE = 'none',
}
// const enum ROOMSTATE {
//    INIT,
//    ENTERED,
//    EXITED,
//    AWAIT_DESTRUCTION,
//    DESTROYED,
// };
export const enum ROOMCONFIG {
   INSTANT = 'instant',
}

export default class MultiUserContact extends Contact {
   public static INSTANT_ROOMCONFIG = ROOMCONFIG.INSTANT;

   public static TYPE = 'groupchat';

   private members: PersistentMap<{ affiliation: AFFILIATION; role: ROLE; jid: string }>;

   constructor(account: Account, jid: IJID, name?: string);
   constructor(account: Account, id: string);
   constructor() {
      super(arguments[0], arguments[1], arguments[2]);

      this.data.set('type', MultiUserContact.TYPE);
   }

   private getMembers(): PersistentMap {
      if (!this.members) {
         this.members = new PersistentMap(this.account.getStorage(), 'members', this.getId());
      }

      return this.members;
   }

   private getService(): IMUCService {
      return this.account.getConnection().getMUCService();
   }

   public async invite(jid: JID, reason?: string) {
      let discoInfo = await this.account.getDiscoInfoRepository().requestDiscoInfo(this.getJid());
      let isMembersOnly = discoInfo.hasFeature('muc_membersonly');

      if (isMembersOnly) {
         this.getService().sendMediatedMultiUserInvitation(jid, this.getJid(), reason);

         return;
      }

      let contact = this.account.getContact(jid);
      let resources = jid.resource ? [jid.resource] : contact.getResources();

      if (resources.length > 0) {
         for (let resource of resources) {
            if (contact.hasFeatureByResource(resource, 'jabber:x:conference')) {
               let password = this.data.get('password');
               this.getService().sendDirectMultiUserInvitation(jid, this.getJid(), reason, password);
            }
         }

         return;
      }

      throw new Error('No invitation method available');
   }

   public changeTopic(topic: string) {
      return this.getService().changeTopic(this.getJid(), topic);
   }

   public changeNickname(nickname: string) {
      return this.getService().changeNickname(this.getJid(), nickname);
   }

   public kick(nickname: string, reason?: string) {
      return this.getService().kickUser(this.getJid(), nickname, reason);
   }

   public ban(target: IJID, reason?: string) {
      return this.getService().banUser(this.getJid(), target, reason);
   }

   public changeAffiliation(target: IJID, affiliation: string) {
      return this.getService().changeAffiliation(this.getJid(), target, affiliation);
   }

   public changeRole(nickname: string, role: string) {
      return this.getService().changeRole(this.getJid(), nickname, role);
   }

   public join() {
      this.data.set('joinDate', new Date());
      this.data.set('memberListComplete', false);
      this.removeAllMembers();

      this.refreshFeatures();

      return this.getService().joinMultiUserRoom(new JID(this.jid.bare, this.getNickname()), this.data.get('password'));
   }

   public leave() {
      return this.getService().leaveMultiUserRoom(this.getJid());
   }

   public destroy() {
      return this.getService().destroyMultiUserRoom(this.getJid());
   }

   public createInstantRoom() {
      return this.getService().createInstantRoom(this.jid);
   }

   public createPreconfiguredRoom() {
      if (!this.hasRoomConfiguration()) {
         return Promise.reject('No saved room configuration');
      }

      let form = Form.fromJSON(this.getRoomConfiguration());

      return this.getService().submitRoomConfiguration(this.getJid(), form);
   }

   public async refreshFeatures(): Promise<string[]> {
      const stanza = await this.account.getConnection().getDiscoService().getDiscoInfo(new JID(this.jid.bare));

      const features = $(stanza)
         .find('feature')
         .map((_, element) => $(element).attr('var'))
         .get()
         .filter(feature => feature && feature.startsWith('muc_'))
         .map(feature => feature.replace(/^muc_/, ''));

      this.data.set('features', features);

      return features;
   }

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

   public isMembersOnly(): boolean {
      return this.getFeatures().includes('membersonly');
   }

   public isModerated(): boolean {
      return this.getFeatures().includes('moderated');
   }

   public isNonAnonymous(): boolean {
      return this.getFeatures().includes('nonanonymous');
   }

   public isOpen(): boolean {
      return this.getFeatures().includes('open');
   }

   public isPasswordProtected(): boolean {
      return this.getFeatures().includes('passwordprotected');
   }

   public isPersistent(): boolean {
      return this.getFeatures().includes('persistent');
   }

   public isPublic(): boolean {
      return this.getFeatures().includes('public');
   }

   public isSemiAnonymous(): boolean {
      return this.getFeatures().includes('semianonymous');
   }

   public isTemporary(): boolean {
      return this.getFeatures().includes('temporary');
   }

   public isUnmoderated(): boolean {
      return this.getFeatures().includes('unmoderated');
   }

   public isUnsecured(): boolean {
      return this.getFeatures().includes('unsecured');
   }

   public setNickname(nickname: string) {
      //@TODO update ui according to affiliation and role
      this.data.set('nickname', nickname);
      this.setResource(nickname); //@REVIEW do we need the nickname field?
   }

   public getNickname(): string {
      return this.data.get('nickname');
   }

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

      return this.chatWindow;
   }

   public getMember(nickname: string): { affiliation?: AFFILIATION; role?: ROLE; jid?: JID } {
      let data = this.getMembers().get(nickname) || {};

      data.jid = data.jid ? new JID(data.jid.full || data.jid) : undefined;

      return data;
   }

   public addMember(nickname: string, affiliation?: AFFILIATION, role?: ROLE, jid?: JID): boolean {
      let isNewMember = !this.getMembers().get(nickname);

      this.getMembers().set(nickname, {
         affiliation,
         role,
         jid: jid?.full,
      });

      return isNewMember;
   }

   public removeMember(nickname: string) {
      this.getMembers().remove(nickname);
   }

   public getMemberIds(): string[] {
      return this.getMembers().getAllKeys();
   }

   public getSubscription(): ContactSubscription {
      return ContactSubscription.BOTH;
   }

   public setSubject(subject: string) {
      this.data.set('subject', subject);
   }

   public getSubject(): string {
      return this.data.get('subject');
   }

   public getPassword(): string {
      return this.data.get('password');
   }

   public setPassword(password: string) {
      this.data.set('password', password);
   }

   public setAutoJoin(autoJoin: boolean) {
      this.data.set('autoJoin', autoJoin);
   }

   public isAutoJoin(): boolean {
      return !!this.data.get('autoJoin');
   }

   public setBookmark(bookmark: boolean) {
      this.data.set('bookmark', bookmark);
   }

   public isBookmarked(): boolean {
      return !!this.data.get('bookmark');
   }

   public setRoomConfiguration(roomConfig) {
      this.data.set('roomConfig', roomConfig);
   }

   public getRoomConfiguration() {
      let roomConfig = this.data.get('roomConfig');

      return roomConfig !== ROOMCONFIG.INSTANT ? roomConfig : undefined;
   }

   public hasRoomConfiguration() {
      let roomConfig = this.data.get('roomConfig');

      return typeof roomConfig === 'object' && roomConfig !== null;
   }

   public isInstantRoom(): boolean {
      return this.data.get('roomConfig') === ROOMCONFIG.INSTANT;
   }

   public isMemberListComplete(): boolean {
      return this.data.get('memberListComplete');
   }

   public setMemberListComplete() {
      this.data.set('memberListComplete', true);
   }

   public getJoinDate(): Date {
      let dateString = this.data.get('joinDate');

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

   public registerMemberHook(id: string, func: (newValue: any, oldValue: any, key: string) => void);
   public registerMemberHook(func: (newValue: any, oldValue: any, key: string) => void);
   public registerMemberHook() {
      this.getMembers().registerHook.apply(this.getMembers(), arguments);
   }

   public registerNewMemberHook(func: (value: any, nickname: string) => void) {
      this.getMembers().registerNewHook(func);
   }

   public registerRemoveMemberHook(func: (nickname: string) => void) {
      this.getMembers().registerRemoveHook(func);
   }

   public shutdown() {
      this.data.set('resources', {});
      this.data.set('presence', Presence.offline);

      this.setNickname(null);
      this.removeAllMembers();
   }

   private removeAllMembers() {
      this.getMembers().empty();
   }
}