@flashphoner/sfusdk 1.0.1-35 → 1.0.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,7 +22,6 @@ let state = SFU_STATE.NEW;
22
22
  let connectionConfig;
23
23
  let user;
24
24
  let im;
25
- let pendingUserList = [];
26
25
 
27
26
  function setupConnection(connection) {
28
27
  connection.onMessage = function(name, msg){
@@ -33,15 +32,28 @@ function setupConnection(connection) {
33
32
  notify(SFU_EVENT.MESSAGE, msg[0].message);
34
33
  } else if (msg[0].type === SFU_INTERNAL.MESSAGE_STATE) {
35
34
  im.onMessageState(msg);
35
+ notify(SFU_EVENT.MESSAGE_STATE, msg[0].status);
36
36
  } else if (msg[0].type === SFU_INTERNAL.USER_LIST) {
37
- while (pendingUserList.length > 0) {
38
- const promise = pendingUserList.pop();
39
- promise.resolve(msg[0].list);
40
- }
37
+ promises.resolve(msg[0].internalMessageId, msg[0].list);
41
38
  notify(SFU_EVENT.USER_LIST, msg[0].list);
42
39
  } else if (msg[0].type === SFU_INTERNAL.USER_CALENDAR) {
43
40
  promises.resolve(msg[0].internalMessageId, msg[0].calendar);
44
41
  notify(SFU_EVENT.USER_CALENDAR, msg[0].calendar);
42
+ } else if (msg[0].type === SFU_INTERNAL.NEW_CHAT) {
43
+ if(!promises.resolve(msg[0].internalMessageId, msg[0].info)) {
44
+ notify(SFU_EVENT.NEW_CHAT, msg[0].info);
45
+ }
46
+ } else if (msg[0].type === SFU_INTERNAL.CHAT_DELETED) {
47
+ notify(SFU_EVENT.CHAT_DELETED, msg[0].info);
48
+ } else if (msg[0].type === SFU_INTERNAL.CHAT_UPDATED) {
49
+ promises.resolve(msg[0].internalMessageId, msg[0].info);
50
+ notify(SFU_EVENT.CHAT_UPDATED, msg[0].info);
51
+ } else if (msg[0].type === SFU_INTERNAL.USER_CHATS) {
52
+ promises.resolve(msg[0].internalMessageId, msg[0].chats);
53
+ notify(SFU_EVENT.USER_CHATS, msg[0].chats);
54
+ } else if (msg[0].type === SFU_INTERNAL.CHAT_LOADED) {
55
+ promises.resolve(msg[0].internalMessageId, msg[0].chat);
56
+ notify(SFU_EVENT.CHAT_LOADED, msg[0].chat);
45
57
  } else if (msg[0].type === constants.SFU_ROOM_EVENT.OPERATION_FAILED && promises.promised(msg[0].internalMessageId)) {
46
58
  promises.reject(msg[0].internalMessageId, msg[0]);
47
59
  } else if (msg[0].type === constants.SFU_EVENT.ACK && promises.promised(msg[0].internalMessageId)) {
@@ -128,7 +140,8 @@ const connect = function(options) {
128
140
  /**
129
141
  * Send message
130
142
  * @param {Object} msg Message
131
- * @param {String} msg.to Recipient's id
143
+ * @param {String} msg.to Recipient's id (deprecated, use chatId instead)
144
+ * @param {String} msg.chatId Indicates chat this message belongs to
132
145
  * @param {String} msg.body Message body
133
146
  * @returns {Promise} Promise will resolve upon message delivery and reject if delivery was unsuccessful
134
147
  * @throws {Error} error if api isn't connected
@@ -144,26 +157,18 @@ const sendMessage = function(msg) {
144
157
  /**
145
158
  * Fetch available user list from server
146
159
  * @returns {Promise<Array<FlashphonerSFUExtended.UserListEntry>>}
147
- * @throws {Error} error if api isn't connected
148
160
  * @memberOf FlashphonerSFUExtended
149
161
 
150
162
  */
151
163
  const getUserList = function() {
152
- if (state !== SFU_STATE.AUTHENTICATED) {
153
- throw new Error("Can't get user list while in " + state + " state");
154
- }
155
164
  return new Promise(function (resolve, reject){
156
165
  if (state !== SFU_STATE.AUTHENTICATED) {
157
166
  reject(new Error("Can't get user list while in " + state + " state"));
158
167
  return;
159
168
  }
160
- if (pendingUserList.length > 0) {
161
- pendingUserList.push({resolve: resolve, reject: reject});
162
- return;
163
- } else {
164
- pendingUserList.push({resolve: resolve, reject: reject});
165
- }
166
- connection.send(constants.SFU_INTERNAL_API.GET_USER_LIST);
169
+ const id = uuidv4();
170
+ promises.add(id, resolve, reject);
171
+ connection.send(constants.SFU_INTERNAL_API.GET_USER_LIST, {internalMessageId: id});
167
172
  });
168
173
  };
169
174
 
@@ -229,6 +234,126 @@ const removeCalendarEvent = function(event) {
229
234
  });
230
235
  };
231
236
 
237
+ /**
238
+ * Fetch available chats from server
239
+ * @returns {Promise<Array<FlashphonerSFUExtended.UserCalendar>>}
240
+ * @memberOf FlashphonerSFUExtended
241
+ */
242
+ const getUserChats = function() {
243
+ return new Promise(function (resolve, reject){
244
+ if (state !== SFU_STATE.AUTHENTICATED) {
245
+ reject(new Error("Can't get user chats while in " + state + " state"));
246
+ return;
247
+ }
248
+ const id = uuidv4();
249
+ promises.add(id, resolve, reject);
250
+ connection.send(constants.SFU_INTERNAL_API.GET_USER_CHATS, {internalMessageId: id});
251
+ });
252
+ };
253
+
254
+ /**
255
+ * Fetch chat data from server
256
+ * @returns {Promise<Array<FlashphonerSFUExtended.UserCalendar>>}
257
+ * @memberOf FlashphonerSFUExtended
258
+ */
259
+ const loadChat = function(chat) {
260
+ return new Promise(function (resolve, reject){
261
+ if (state !== SFU_STATE.AUTHENTICATED) {
262
+ reject(new Error("Can't load chats while in " + state + " state"));
263
+ return;
264
+ }
265
+ const id = uuidv4();
266
+ promises.add(id, resolve, reject);
267
+ connection.send(constants.SFU_INTERNAL_API.LOAD_CHAT, {id: chat.id, internalMessageId: id});
268
+ });
269
+ };
270
+
271
+ /**
272
+ * Create chat
273
+ * @returns {Promise<Array<FlashphonerSFUExtended.UserCalendar>>}
274
+ * @memberOf FlashphonerSFUExtended
275
+ */
276
+ const createChat = function(chat) {
277
+ return new Promise(function (resolve, reject){
278
+ if (state !== SFU_STATE.AUTHENTICATED) {
279
+ reject(new Error("Can't create chats while in " + state + " state"));
280
+ return;
281
+ }
282
+ const id = uuidv4();
283
+ promises.add(id, resolve, reject);
284
+ connection.send(constants.SFU_INTERNAL_API.CREATE_CHAT, {id: chat.id, name: chat.name, members: chat.members, internalMessageId: id});
285
+ });
286
+ };
287
+
288
+ /**
289
+ * Delete chat
290
+ * @returns {Promise<Array<FlashphonerSFUExtended.UserCalendar>>}
291
+ * @memberOf FlashphonerSFUExtended
292
+ */
293
+ const deleteChat = function(chat) {
294
+ return new Promise(function (resolve, reject){
295
+ if (state !== SFU_STATE.AUTHENTICATED) {
296
+ reject(new Error("Can't delete chats while in " + state + " state"));
297
+ return;
298
+ }
299
+ const id = uuidv4();
300
+ promises.add(id, resolve, reject);
301
+ connection.send(constants.SFU_INTERNAL_API.DELETE_CHAT, {id: chat.id, internalMessageId: id});
302
+ });
303
+ };
304
+
305
+ /**
306
+ * Rename chat
307
+ * @returns {Promise<Array<FlashphonerSFUExtended.UserCalendar>>}
308
+ * @memberOf FlashphonerSFUExtended
309
+ */
310
+ const renameChat = function(chat) {
311
+ return new Promise(function (resolve, reject){
312
+ if (state !== SFU_STATE.AUTHENTICATED) {
313
+ reject(new Error("Can't rename chats while in " + state + " state"));
314
+ return;
315
+ }
316
+ const id = uuidv4();
317
+ promises.add(id, resolve, reject);
318
+ connection.send(constants.SFU_INTERNAL_API.RENAME_CHAT, {id: chat.id, name: chat.name, internalMessageId: id});
319
+ });
320
+ };
321
+
322
+ /**
323
+ * Add member to chat
324
+ * @returns {Promise<Array<FlashphonerSFUExtended.UserCalendar>>}
325
+ * @memberOf FlashphonerSFUExtended
326
+ */
327
+ const addMemberToChat = function(chat) {
328
+ return new Promise(function (resolve, reject){
329
+ if (state !== SFU_STATE.AUTHENTICATED) {
330
+ reject(new Error("Can't add member to chat while in " + state + " state"));
331
+ return;
332
+ }
333
+ const id = uuidv4();
334
+ promises.add(id, resolve, reject);
335
+ connection.send(constants.SFU_INTERNAL_API.ADD_MEMBER_TO_CHAT, {id: chat.id, member: chat.member, internalMessageId: id});
336
+ });
337
+ };
338
+
339
+ /**
340
+ * Remove member from chat
341
+ * @returns {Promise<Array<FlashphonerSFUExtended.UserCalendar>>}
342
+ * @memberOf FlashphonerSFUExtended
343
+ */
344
+ const removeMemberFromChat = function(chat) {
345
+ return new Promise(function (resolve, reject){
346
+ if (state !== SFU_STATE.AUTHENTICATED) {
347
+ reject(new Error("Can't remove member to chat while in " + state + " state"));
348
+ return;
349
+ }
350
+ const id = uuidv4();
351
+ promises.add(id, resolve, reject);
352
+ connection.send(constants.SFU_INTERNAL_API.REMOVE_MEMBER_FROM_CHAT, {id: chat.id, member: chat.member, internalMessageId: id});
353
+ });
354
+ };
355
+
356
+
232
357
  /**
233
358
  * Create room
234
359
  *
@@ -326,8 +451,6 @@ const disconnect = function() {
326
451
  value.leaveRoom();
327
452
  }
328
453
  user = undefined;
329
- pendingUserList = [];
330
- pendingUserCalendar = [];
331
454
  connection.close();
332
455
  connection = ws.createConnection();
333
456
  state = SFU_STATE.DISCONNECTED;
@@ -341,6 +464,13 @@ sfu.getUserList = getUserList;
341
464
  sfu.getUserCalendar = getUserCalendar;
342
465
  sfu.addCalendarEvent = addCalendarEvent;
343
466
  sfu.removeCalendarEvent = removeCalendarEvent;
467
+ sfu.getUserChats = getUserChats;
468
+ sfu.loadChat = loadChat;
469
+ sfu.createChat = createChat;
470
+ sfu.deleteChat = deleteChat;
471
+ sfu.renameChat = renameChat;
472
+ sfu.addMemberToChat = addMemberToChat;
473
+ sfu.removeMemberFromChat = removeMemberFromChat;
344
474
  sfu.room = room;
345
475
 
346
476
 
@@ -0,0 +1,151 @@
1
+ const sfu = require("../../sdk/sfu-extended");
2
+ const constants = require("../../sdk/constants");
3
+
4
+ const SERVER = "ws://127.0.0.1:8080";
5
+ const USER = "bob@flashphoner.com";
6
+ const PASSWD = "123456";
7
+ const CHAT_ID = "test-chat-id";
8
+ const CHAT_NAME = "test-chat";
9
+ const CHAT_NAME2 = "test-chat2";
10
+ const USER2 = "alice@flashphoner.com";
11
+ const MESSAGE_BODY = "This is a test message";
12
+ const MEMBERS = [
13
+ "bob@flashphoner.com",
14
+ "alice@flashphoner.com",
15
+ "kiri@flashphoner.com"
16
+ ];
17
+
18
+ beforeAll( async () => {
19
+ await sfu.connect({
20
+ url: SERVER,
21
+ username: USER,
22
+ password: PASSWD
23
+ });
24
+ const chats = await sfu.getUserChats();
25
+ if (chats) {
26
+ Object.entries(chats).map(async ([key, value]) => {
27
+ await sfu.deleteChat({id: value.id});
28
+ });
29
+ }
30
+ });
31
+
32
+ afterAll(() => {
33
+ return sfu.disconnect();
34
+ });
35
+
36
+
37
+ describe("connection", () => {
38
+ test("should connect to server", () => {
39
+ expect(sfu.state()).toBe(constants.SFU_STATE.AUTHENTICATED);
40
+ });
41
+ });
42
+
43
+ describe("contacts", () => {
44
+ test("should load user list", () => {
45
+ return sfu.getUserList();
46
+ });
47
+ });
48
+
49
+ describe("calendar", () => {
50
+ test("should load calendar", () => {
51
+ return sfu.getUserCalendar().then((calendar) => {
52
+ expect(calendar).toHaveProperty("events");
53
+ })
54
+ })
55
+ });
56
+
57
+ describe("chat", () => {
58
+ test("should create new chat", () => {
59
+ return sfu.createChat({
60
+ id: CHAT_ID,
61
+ name: CHAT_NAME
62
+ });
63
+ });
64
+ test("should load chats", () => {
65
+ return sfu.getUserChats().then((chats) => {
66
+ expect(chats).toBeTruthy();
67
+ });
68
+ });
69
+ test("should add new member", () => {
70
+ return sfu.addMemberToChat({
71
+ id: CHAT_ID,
72
+ member: USER2
73
+ });
74
+ });
75
+ test("should send a message", () => {
76
+ return sfu.sendMessage({
77
+ chatId: CHAT_ID,
78
+ body: MESSAGE_BODY
79
+ })
80
+ });
81
+ test("should load chat", () => {
82
+ return sfu.loadChat({
83
+ id: CHAT_ID
84
+ }).then((chat) => {
85
+ expect(chat.id).toBe(CHAT_ID);
86
+ expect(chat.name).toBe(CHAT_NAME);
87
+ expect(chat.members).toContain(USER);
88
+ expect(Array.isArray(chat.messages)).toBe(true);
89
+ expect(chat.messages.length).toBeGreaterThan(0);
90
+ });
91
+ });
92
+ test("should remove member", () => {
93
+ return sfu.removeMemberFromChat({
94
+ id: CHAT_ID,
95
+ member: USER2
96
+ });
97
+ });
98
+ test("should rename chat", () => {
99
+ return sfu.renameChat({
100
+ id: CHAT_ID,
101
+ name: CHAT_NAME2
102
+ });
103
+ });
104
+ test.skip("should receive message", (done) => {
105
+ sfu.on(constants.SFU_EVENT.MESSAGE, (msg) => {
106
+ done();
107
+ })
108
+ sfu.sendMessage({
109
+ chatId: CHAT_ID,
110
+ body: MESSAGE_BODY
111
+ });
112
+ });
113
+ test("should receive ACK message", (done) => {
114
+ sfu.on(constants.SFU_EVENT.MESSAGE_STATE, (state) => {
115
+ done();
116
+ })
117
+ sfu.sendMessage({
118
+ chatId: CHAT_ID,
119
+ body: MESSAGE_BODY
120
+ });
121
+ });
122
+ test("should create new chat without id", () => {
123
+ return sfu.createChat({
124
+ name: CHAT_NAME
125
+ });
126
+ });
127
+ test("should create new chat with member list", () => {
128
+ return sfu.createChat({
129
+ name: CHAT_NAME,
130
+ members: MEMBERS
131
+ });
132
+ });
133
+ test("should create new chat without name based on members", async () => {
134
+ const chat = await sfu.createChat({
135
+ members: MEMBERS
136
+ });
137
+ expect(chat.name).toBeTruthy();
138
+ });
139
+ test("should delete chat", () => {
140
+ return sfu.deleteChat({
141
+ id: CHAT_ID
142
+ });
143
+ });
144
+ test("should change name after member was added", async () => {
145
+ const chat = await sfu.createChat({});
146
+ expect(chat.name).toBeTruthy();
147
+ const name1 = chat.name;
148
+ const sameChat = await sfu.addMemberToChat({id: chat.id, member: USER2});
149
+ expect(sameChat.name !== name1).toBeTruthy();
150
+ });
151
+ });