qiscus-sdk-core 2.16.0 → 2.16.1
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.
- package/dist/qiscus-sdk-core.js +24475 -34766
- package/dist/qiscus-sdk-core.js.map +1 -1
- package/dist/qiscus-sdk-core.min.js +24 -8
- package/dist/qiscus-sdk-core.min.js.map +1 -1
- package/lib/index.js +1847 -2705
- package/lib/lib/Comment.js +89 -142
- package/lib/lib/Room.js +52 -94
- package/lib/lib/adapters/auth.js +42 -69
- package/lib/lib/adapters/custom-event.js +26 -35
- package/lib/lib/adapters/expired-token.js +86 -113
- package/lib/lib/adapters/hook.js +15 -19
- package/lib/lib/adapters/http.js +129 -253
- package/lib/lib/adapters/mqtt.js +422 -648
- package/lib/lib/adapters/room.js +121 -147
- package/lib/lib/adapters/sync.js +233 -421
- package/lib/lib/adapters/user.js +175 -210
- package/lib/lib/is.js +13 -47
- package/lib/lib/match.js +49 -92
- package/lib/lib/url-builder.js +7 -18
- package/lib/lib/util.js +6 -20
- package/lib/lib/utils.js +52 -97
- package/package.json +21 -55
package/lib/lib/Comment.js
CHANGED
|
@@ -1,26 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
});
|
|
7
|
-
exports["default"] = void 0;
|
|
8
|
-
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
9
|
-
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
10
|
-
var _dateFns = require("date-fns");
|
|
11
|
-
var _utils = require("./utils.js");
|
|
12
|
-
var Comment = /*#__PURE__*/function () {
|
|
13
|
-
function Comment(comment) {
|
|
14
|
-
(0, _classCallCheck2["default"])(this, Comment);
|
|
2
|
+
const dateFns = require("date-fns");
|
|
3
|
+
const utils = require("./utils.js");
|
|
4
|
+
class Comment {
|
|
5
|
+
constructor(comment) {
|
|
15
6
|
this.id = comment.id;
|
|
16
7
|
this.before_id = comment.comment_before_id;
|
|
17
|
-
this.message =
|
|
8
|
+
this.message = utils.escapeHTML(comment.message);
|
|
18
9
|
this.username_as = comment.username_as || comment.username;
|
|
19
10
|
this.username_real = comment.username_real || comment.email;
|
|
20
11
|
this.email = this.username_real;
|
|
21
12
|
this.user_extras = comment.user_extras;
|
|
22
|
-
this.date =
|
|
23
|
-
this.time =
|
|
13
|
+
this.date = dateFns.format(comment.timestamp, "YYYY-MM-DD");
|
|
14
|
+
this.time = dateFns.format(comment.timestamp, "HH:mm");
|
|
24
15
|
this.timestamp = comment.timestamp;
|
|
25
16
|
this.unique_id = comment.unique_temp_id || comment.unique_id;
|
|
26
17
|
this.unique_temp_id = comment.unique_temp_id || comment.unique_id;
|
|
@@ -30,8 +21,6 @@ var Comment = /*#__PURE__*/function () {
|
|
|
30
21
|
this.unix_timestamp = comment.unix_timestamp;
|
|
31
22
|
this.unix_nano_timestamp = comment.unix_nano_timestamp;
|
|
32
23
|
this.extras = comment.extras;
|
|
33
|
-
|
|
34
|
-
/* comment status */
|
|
35
24
|
this.is_deleted = comment.is_deleted;
|
|
36
25
|
this.isPending = false;
|
|
37
26
|
this.isFailed = false;
|
|
@@ -41,136 +30,94 @@ var Comment = /*#__PURE__*/function () {
|
|
|
41
30
|
this.attachment = null;
|
|
42
31
|
this.payload = comment.payload;
|
|
43
32
|
this.status = comment.status;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
comment.payload.text =
|
|
33
|
+
if (comment.type === "reply") {
|
|
34
|
+
comment.payload.replied_comment_message = utils.escapeHTML(
|
|
35
|
+
comment.payload.replied_comment_message
|
|
36
|
+
);
|
|
37
|
+
comment.payload.text = utils.escapeHTML(comment.payload.text);
|
|
49
38
|
}
|
|
50
|
-
|
|
51
|
-
// supported comment type text, account_linking, buttons
|
|
52
|
-
// let supported_comment_type = [
|
|
53
|
-
// 'text','account_linking','buttons','reply','system_event','card', 'custom', 'contact_person', 'location',
|
|
54
|
-
// 'carousel'
|
|
55
|
-
// ];
|
|
56
39
|
this.type = comment.type;
|
|
57
|
-
this.subtype = comment.type ===
|
|
58
|
-
|
|
59
|
-
// comment status
|
|
60
|
-
if (comment.status === 'sent') {
|
|
40
|
+
this.subtype = comment.type === "custom" ? comment.payload.type : null;
|
|
41
|
+
if (comment.status === "sent") {
|
|
61
42
|
this.markAsSent();
|
|
62
|
-
} else if (comment.status ===
|
|
43
|
+
} else if (comment.status === "delivered") {
|
|
63
44
|
this.markAsDelivered();
|
|
64
|
-
} else if (comment.status ===
|
|
45
|
+
} else if (comment.status === "read") {
|
|
65
46
|
this.markAsRead();
|
|
66
47
|
}
|
|
67
48
|
}
|
|
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
|
-
this.isPending = false;
|
|
133
|
-
this.isSent = true;
|
|
134
|
-
this.isDelivered = true;
|
|
135
|
-
this.isRead = true;
|
|
136
|
-
this.status = 'read';
|
|
137
|
-
}
|
|
138
|
-
}, {
|
|
139
|
-
key: "markAsFailed",
|
|
140
|
-
value: function markAsFailed() {
|
|
141
|
-
this.isFailed = true;
|
|
142
|
-
this.isPending = false;
|
|
143
|
-
this.isStatus = 'failed';
|
|
49
|
+
isAttachment(message) {
|
|
50
|
+
return message.substring(0, "[file]".length) === "[file]";
|
|
51
|
+
}
|
|
52
|
+
isImageAttachment(message) {
|
|
53
|
+
return this.isAttachment(message) && message.match(/\.(jpg|jpeg|gif|png)/i) != null;
|
|
54
|
+
}
|
|
55
|
+
attachUniqueId(uniqueId) {
|
|
56
|
+
this.unique_id = uniqueId;
|
|
57
|
+
}
|
|
58
|
+
getAttachmentURI(message) {
|
|
59
|
+
if (!this.isAttachment(message)) return;
|
|
60
|
+
const messageLength = message.length;
|
|
61
|
+
const beginIndex = "[file]".length;
|
|
62
|
+
const endIndex = messageLength - "[/file]".length;
|
|
63
|
+
return message.substring(beginIndex, endIndex).trim();
|
|
64
|
+
}
|
|
65
|
+
setAttachment(attachment) {
|
|
66
|
+
this.attachment = attachment;
|
|
67
|
+
}
|
|
68
|
+
markAsPending() {
|
|
69
|
+
this.isPending = true;
|
|
70
|
+
this.isDelivered = false;
|
|
71
|
+
this.status = "pending";
|
|
72
|
+
}
|
|
73
|
+
markAsSent() {
|
|
74
|
+
this.isSent = true;
|
|
75
|
+
this.isPending = false;
|
|
76
|
+
this.isFailed = false;
|
|
77
|
+
this.status = "sent";
|
|
78
|
+
}
|
|
79
|
+
markAsDelivered({ actor, activeActorId } = {}) {
|
|
80
|
+
if (actor === activeActorId) return;
|
|
81
|
+
if (this.isRead || this.status === "read") return;
|
|
82
|
+
this.isSent = true;
|
|
83
|
+
this.isRead = false;
|
|
84
|
+
this.isDelivered = true;
|
|
85
|
+
this.status = "delivered";
|
|
86
|
+
}
|
|
87
|
+
markAsRead({ actor, activeActorId } = {}) {
|
|
88
|
+
if (actor === activeActorId) return;
|
|
89
|
+
this.isPending = false;
|
|
90
|
+
this.isSent = true;
|
|
91
|
+
this.isDelivered = true;
|
|
92
|
+
this.isRead = true;
|
|
93
|
+
this.status = "read";
|
|
94
|
+
}
|
|
95
|
+
markAsFailed() {
|
|
96
|
+
this.isFailed = true;
|
|
97
|
+
this.isPending = false;
|
|
98
|
+
this.isStatus = "failed";
|
|
99
|
+
}
|
|
100
|
+
// usually called when there's new comment with the same id
|
|
101
|
+
// we just need to update its content
|
|
102
|
+
update(data) {
|
|
103
|
+
this.id = data.id;
|
|
104
|
+
this.before_id = data.comment_before_id;
|
|
105
|
+
this.message = utils.escapeHTML(data.message);
|
|
106
|
+
if (data.payload) this.payload = data.payload;
|
|
107
|
+
if (data.status) this.status = data.status;
|
|
108
|
+
if (data.type === "reply") {
|
|
109
|
+
this.payload.replied_comment_message = utils.escapeHTML(
|
|
110
|
+
data.payload.replied_comment_message
|
|
111
|
+
);
|
|
112
|
+
this.payload.text = utils.escapeHTML(data.payload.text);
|
|
144
113
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
this.id = data.id;
|
|
152
|
-
this.before_id = data.comment_before_id;
|
|
153
|
-
this.message = (0, _utils.escapeHTML)(data.message);
|
|
154
|
-
/* comment status */
|
|
155
|
-
if (data.payload) this.payload = data.payload;
|
|
156
|
-
if (data.status) this.status = data.status;
|
|
157
|
-
|
|
158
|
-
// manage comment type
|
|
159
|
-
if (data.type === 'reply') {
|
|
160
|
-
this.payload.replied_comment_message = (0, _utils.escapeHTML)(data.payload.replied_comment_message);
|
|
161
|
-
this.payload.text = (0, _utils.escapeHTML)(data.payload.text);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// comment status
|
|
165
|
-
if (data.status === 'sent') {
|
|
166
|
-
this.markAsSent();
|
|
167
|
-
} else if (data.status === 'delivered') {
|
|
168
|
-
this.markAsDelivered();
|
|
169
|
-
} else if (data.status === 'read') {
|
|
170
|
-
this.markAsRead();
|
|
171
|
-
}
|
|
114
|
+
if (data.status === "sent") {
|
|
115
|
+
this.markAsSent();
|
|
116
|
+
} else if (data.status === "delivered") {
|
|
117
|
+
this.markAsDelivered();
|
|
118
|
+
} else if (data.status === "read") {
|
|
119
|
+
this.markAsRead();
|
|
172
120
|
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
module.exports = exports.default;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
module.exports = Comment;
|
package/lib/lib/Room.js
CHANGED
|
@@ -1,23 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports["default"] = exports.Room = void 0;
|
|
8
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
9
|
-
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
10
|
-
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
11
|
-
var _Comment = _interopRequireDefault(require("./Comment"));
|
|
12
|
-
/**
|
|
13
|
-
* Holds chat rooms for qiscus chat sdk
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* let Room = new Room(roomData);
|
|
17
|
-
* @export
|
|
18
|
-
* @class Room
|
|
19
|
-
*/
|
|
20
|
-
var Room = exports.Room = /*#__PURE__*/function () {
|
|
2
|
+
const Comment = require("./Comment.js");
|
|
3
|
+
class Room {
|
|
21
4
|
/**
|
|
22
5
|
* Creates an instance of Room.
|
|
23
6
|
* @param {any} roomData
|
|
@@ -26,8 +9,7 @@ var Room = exports.Room = /*#__PURE__*/function () {
|
|
|
26
9
|
* @param {string} last_comment_message
|
|
27
10
|
* @memberof Room
|
|
28
11
|
*/
|
|
29
|
-
|
|
30
|
-
(0, _classCallCheck2["default"])(this, Room);
|
|
12
|
+
constructor(roomData) {
|
|
31
13
|
this.id = roomData.id;
|
|
32
14
|
this.last_comment_id = roomData.last_comment_id;
|
|
33
15
|
this.last_comment_message = roomData.last_comment_message;
|
|
@@ -52,78 +34,54 @@ var Room = exports.Room = /*#__PURE__*/function () {
|
|
|
52
34
|
this.participantNumber = roomData.room_total_participants;
|
|
53
35
|
if (roomData.comments) this.receiveComments(roomData.comments);
|
|
54
36
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (this.participants == null) {
|
|
64
|
-
return this.participantNumber;
|
|
65
|
-
} else {
|
|
66
|
-
return this.participants.length;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}, {
|
|
70
|
-
key: "setTitle",
|
|
71
|
-
value: function setTitle(title) {
|
|
72
|
-
this.custom_title = title;
|
|
73
|
-
}
|
|
74
|
-
}, {
|
|
75
|
-
key: "setSubTitle",
|
|
76
|
-
value: function setSubTitle(subtitle) {
|
|
77
|
-
this.custom_subtitle = subtitle;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Receive a single comment
|
|
82
|
-
*
|
|
83
|
-
* @param {Comment} comment
|
|
84
|
-
* @memberof Room
|
|
85
|
-
*/
|
|
86
|
-
}, {
|
|
87
|
-
key: "receiveComment",
|
|
88
|
-
value: function receiveComment(comment) {
|
|
89
|
-
// Ignore if not from the same room
|
|
90
|
-
if (comment.room_id !== this.id) return;
|
|
91
|
-
// let's check first whether this room already has this specific comment
|
|
92
|
-
var commentToFind = this.comments.find(function (cmt) {
|
|
93
|
-
return cmt.unique_id === comment.unique_id;
|
|
94
|
-
});
|
|
95
|
-
if (commentToFind) {
|
|
96
|
-
commentToFind.id = comment.id;
|
|
97
|
-
commentToFind.message = comment.message;
|
|
98
|
-
commentToFind.date = comment.date;
|
|
99
|
-
commentToFind.time = comment.time;
|
|
100
|
-
commentToFind.unix_timestamp = comment.unix_timestamp;
|
|
101
|
-
} else {
|
|
102
|
-
this.comments = [].concat((0, _toConsumableArray2["default"])(this.comments), [comment]);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}, {
|
|
106
|
-
key: "receiveComments",
|
|
107
|
-
value: function receiveComments(comments) {
|
|
108
|
-
var _this = this;
|
|
109
|
-
comments.forEach(function (comment) {
|
|
110
|
-
_this.receiveComment(new _Comment["default"](comment));
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
}, {
|
|
114
|
-
key: "getParticipant",
|
|
115
|
-
value: function getParticipant(participantEmail) {
|
|
116
|
-
return this.participants.find(function (p) {
|
|
117
|
-
return p.email === participantEmail;
|
|
118
|
-
}) || null;
|
|
37
|
+
isCurrentlySelected(selected) {
|
|
38
|
+
return this.id === selected.id;
|
|
39
|
+
}
|
|
40
|
+
getParticipantCount() {
|
|
41
|
+
if (this.participants == null) {
|
|
42
|
+
return this.participantNumber;
|
|
43
|
+
} else {
|
|
44
|
+
return this.participants.length;
|
|
119
45
|
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
46
|
+
}
|
|
47
|
+
setTitle(title) {
|
|
48
|
+
this.custom_title = title;
|
|
49
|
+
}
|
|
50
|
+
setSubTitle(subtitle) {
|
|
51
|
+
this.custom_subtitle = subtitle;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Receive a single comment
|
|
55
|
+
*
|
|
56
|
+
* @param {Comment} comment
|
|
57
|
+
* @memberof Room
|
|
58
|
+
*/
|
|
59
|
+
receiveComment(comment) {
|
|
60
|
+
if (comment.room_id !== this.id) return;
|
|
61
|
+
const commentToFind = this.comments.find(
|
|
62
|
+
(cmt) => cmt.unique_id === comment.unique_id
|
|
63
|
+
);
|
|
64
|
+
if (commentToFind) {
|
|
65
|
+
commentToFind.id = comment.id;
|
|
66
|
+
commentToFind.message = comment.message;
|
|
67
|
+
commentToFind.date = comment.date;
|
|
68
|
+
commentToFind.time = comment.time;
|
|
69
|
+
commentToFind.unix_timestamp = comment.unix_timestamp;
|
|
70
|
+
} else {
|
|
71
|
+
this.comments = [...this.comments, comment];
|
|
126
72
|
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
73
|
+
}
|
|
74
|
+
receiveComments(comments) {
|
|
75
|
+
comments.forEach((comment) => {
|
|
76
|
+
this.receiveComment(new Comment(comment));
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
getParticipant(participantEmail) {
|
|
80
|
+
return this.participants.find((p) => p.email === participantEmail) || null;
|
|
81
|
+
}
|
|
82
|
+
addParticipant(participant) {
|
|
83
|
+
let participantToFind = this.getParticipant(participant.email);
|
|
84
|
+
if (!participantToFind) this.participants.push(participant);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
module.exports = Room;
|
package/lib/lib/adapters/auth.js
CHANGED
|
@@ -1,81 +1,54 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports["default"] = void 0;
|
|
8
|
-
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
9
|
-
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
10
|
-
var AuthAdapter = exports["default"] = /*#__PURE__*/function () {
|
|
2
|
+
class AuthAdapter {
|
|
11
3
|
/**
|
|
12
4
|
* Params used in this class
|
|
13
5
|
* @method constructor
|
|
14
6
|
* @param {Object} HTTPAdapter [Qiscus HTTP adapter]
|
|
15
7
|
* @return {void} Returns nothing
|
|
16
8
|
*/
|
|
17
|
-
|
|
18
|
-
(0, _classCallCheck2["default"])(this, AuthAdapter);
|
|
9
|
+
constructor(HTTPAdapter) {
|
|
19
10
|
this.HTTPAdapter = HTTPAdapter;
|
|
20
11
|
this.refreshToken = null;
|
|
21
12
|
this.userId = null;
|
|
22
13
|
this.autoRefreshToken = false;
|
|
23
14
|
}
|
|
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
|
-
var res = r.body.results;
|
|
65
|
-
_this3.refreshToken = res.refresh_token;
|
|
66
|
-
_this3.HTTPAdapter.token = res.token;
|
|
67
|
-
return res;
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
}, {
|
|
71
|
-
key: "expiredAuthToken",
|
|
72
|
-
value: function expiredAuthToken() {
|
|
73
|
-
return this.HTTPAdapter.post("api/v2/sdk/expire_user_token", {
|
|
74
|
-
user_id: this.userId
|
|
75
|
-
}).then(function (r) {
|
|
76
|
-
return r.body.results;
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
}]);
|
|
80
|
-
}();
|
|
81
|
-
module.exports = exports.default;
|
|
15
|
+
getNonce() {
|
|
16
|
+
return this.HTTPAdapter.post("api/v2/sdk/auth/nonce").then((res) => res.body.results);
|
|
17
|
+
}
|
|
18
|
+
loginOrRegister(params) {
|
|
19
|
+
return this.HTTPAdapter.post("api/v2/sdk/login_or_register", params).then((resp) => {
|
|
20
|
+
if (resp.body.status !== 200) return Promise.reject(resp);
|
|
21
|
+
let result = resp.body.results;
|
|
22
|
+
this.userId = result.user.email;
|
|
23
|
+
this.refreshToken = result.user.refresh_token;
|
|
24
|
+
return result;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
verifyIdentityToken(token) {
|
|
28
|
+
return this.HTTPAdapter.post("api/v2/sdk/auth/verify_identity_token", {
|
|
29
|
+
identity_token: token
|
|
30
|
+
}).then((resp) => {
|
|
31
|
+
let result = resp.body.results;
|
|
32
|
+
this.userId = result.user.email;
|
|
33
|
+
this.refreshToken = result.user.refresh_token;
|
|
34
|
+
return result;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
refreshAuthToken() {
|
|
38
|
+
return this.HTTPAdapter.post("api/v2/sdk/refresh_user_token", {
|
|
39
|
+
user_id: this.userId,
|
|
40
|
+
refresh_token: this.refreshToken
|
|
41
|
+
}).then((r) => {
|
|
42
|
+
let res = r.body.results;
|
|
43
|
+
this.refreshToken = res.refresh_token;
|
|
44
|
+
this.HTTPAdapter.token = res.token;
|
|
45
|
+
return res;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
expiredAuthToken() {
|
|
49
|
+
return this.HTTPAdapter.post(`api/v2/sdk/expire_user_token`, {
|
|
50
|
+
user_id: this.userId
|
|
51
|
+
}).then((r) => r.body.results);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
module.exports = AuthAdapter;
|
|
@@ -1,55 +1,46 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports["default"] = CustomEventAdapter;
|
|
8
|
-
var _is = _interopRequireDefault(require("../is"));
|
|
9
|
-
var _mitt = _interopRequireDefault(require("mitt"));
|
|
2
|
+
const is = require("../is.js");
|
|
3
|
+
const mitt = require("mitt");
|
|
10
4
|
function CustomEventAdapter(mqttAdapter, userId) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
mqttAdapter.mqtt.on(
|
|
5
|
+
const events = mitt();
|
|
6
|
+
const subscribedTopics = {};
|
|
7
|
+
const reTopic = /^r\/[\w]+\/[\w]+\/e$/i;
|
|
8
|
+
mqttAdapter.mqtt.on("message", (topic, payload) => {
|
|
15
9
|
if (reTopic.test(topic)) events.emit(topic, payload);
|
|
16
10
|
});
|
|
17
|
-
|
|
18
|
-
return "r/".concat(roomId, "/").concat(roomId, "/e");
|
|
19
|
-
};
|
|
11
|
+
const getTopic = (roomId) => `r/${roomId}/${roomId}/e`;
|
|
20
12
|
return {
|
|
21
|
-
publishEvent
|
|
22
|
-
if (
|
|
23
|
-
if (
|
|
24
|
-
if (
|
|
25
|
-
if (
|
|
26
|
-
|
|
13
|
+
publishEvent(roomId, payload) {
|
|
14
|
+
if (is.undefined(roomId)) throw new Error("`roomId` required");
|
|
15
|
+
if (is.not.string(roomId)) throw new TypeError("`roomId` must have type of string");
|
|
16
|
+
if (is.undefined(payload)) throw new Error("`payload` required");
|
|
17
|
+
if (is.not.object(payload)) throw new TypeError("`payload` must have type of object");
|
|
18
|
+
const _payload = JSON.stringify({
|
|
27
19
|
sender: userId,
|
|
28
20
|
// ?
|
|
29
21
|
data: payload
|
|
30
22
|
});
|
|
31
23
|
mqttAdapter.publish(getTopic(roomId), _payload);
|
|
32
24
|
},
|
|
33
|
-
subscribeEvent
|
|
34
|
-
if (
|
|
35
|
-
if (
|
|
36
|
-
if (
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
// Only allow 1 subcription for now
|
|
25
|
+
subscribeEvent(roomId, callback) {
|
|
26
|
+
if (is.undefined(roomId)) throw new Error("`roomId` required");
|
|
27
|
+
if (is.not.string(roomId)) throw new TypeError("`roomId` must have type of string");
|
|
28
|
+
if (is.undefined(callback)) throw new Error("`callback` required");
|
|
29
|
+
if (is.not.function(callback)) throw new TypeError("`callback` must have type of function");
|
|
30
|
+
const topic = getTopic(roomId);
|
|
40
31
|
if (subscribedTopics[topic]) return;
|
|
41
32
|
mqttAdapter.subscribe(topic);
|
|
42
|
-
|
|
43
|
-
|
|
33
|
+
const cb = (payload) => {
|
|
34
|
+
const parsedPayload = JSON.parse(payload);
|
|
44
35
|
callback(parsedPayload);
|
|
45
36
|
};
|
|
46
37
|
events.on(topic, cb);
|
|
47
38
|
subscribedTopics[topic] = cb;
|
|
48
39
|
},
|
|
49
|
-
unsubscribeEvent
|
|
50
|
-
if (
|
|
51
|
-
if (
|
|
52
|
-
|
|
40
|
+
unsubscribeEvent(roomId) {
|
|
41
|
+
if (is.undefined(roomId)) throw new Error("`roomId` required");
|
|
42
|
+
if (is.not.string(roomId)) throw new TypeError("`roomId` must have type of string");
|
|
43
|
+
const topic = getTopic(roomId);
|
|
53
44
|
if (!subscribedTopics[topic]) return;
|
|
54
45
|
mqttAdapter.unsubscribe(topic);
|
|
55
46
|
events.off(topic, subscribedTopics[topic]);
|
|
@@ -58,4 +49,4 @@ function CustomEventAdapter(mqttAdapter, userId) {
|
|
|
58
49
|
}
|
|
59
50
|
};
|
|
60
51
|
}
|
|
61
|
-
module.exports =
|
|
52
|
+
module.exports = CustomEventAdapter;
|