@chat21/chat21-web-widget 5.0.52 → 5.0.53-rc.2
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/.github/workflows/docker-community-push-latest.yml +1 -0
- package/.github/workflows/docker-image-tag-community-tag-push.yml +1 -0
- package/CHANGELOG.md +16 -0
- package/Dockerfile +2 -2
- package/angular.json +4 -21
- package/deploy_amazon_beta.sh +1 -1
- package/deploy_amazon_prod.sh +1 -1
- package/package.json +17 -18
- package/src/app/app.component.scss +38 -10
- package/src/app/app.component.ts +2 -0
- package/src/app/app.module.ts +5 -3
- package/src/app/component/conversation-detail/conversation/conversation.component.html +13 -1
- package/src/app/component/conversation-detail/conversation/conversation.component.scss +19 -0
- package/src/app/component/conversation-detail/conversation/conversation.component.ts +50 -9
- package/src/app/component/conversation-detail/conversation-content/conversation-content.component.spec.ts +0 -2
- package/src/app/component/conversation-detail/conversation-content/conversation-content.component.ts +0 -10
- package/src/app/component/conversation-detail/conversation-footer/conversation-footer.component.ts +22 -0
- package/src/app/component/form/form-builder/form-builder.component.ts +1 -1
- package/src/app/component/form/inputs/form-checkbox/form-checkbox.component.ts +1 -1
- package/src/app/component/form/inputs/form-label/form-label.component.ts +1 -1
- package/src/app/component/form/inputs/form-text/form-text.component.ts +2 -2
- package/src/app/component/form/inputs/form-textarea/form-textarea.component.ts +2 -2
- package/src/app/component/form/prechat-form/prechat-form.component.ts +1 -1
- package/src/app/component/home/home.component.html +7 -15
- package/src/app/component/message/bubble-message/bubble-message.component.html +1 -4
- package/src/app/component/message/bubble-message/bubble-message.component.ts +1 -37
- package/src/app/component/message/image/image.component.ts +1 -9
- package/src/app/component/message/info-message/info-message.component.html +3 -3
- package/src/app/component/message/info-message/info-message.component.ts +0 -10
- package/src/app/directives/tooltip.directive.spec.ts +8 -0
- package/src/app/directives/tooltip.directive.ts +46 -0
- package/src/app/providers/global-settings.service.ts +2 -2
- package/src/app/utils/rules.ts +13 -0
- package/src/base_script.html +57 -0
- package/src/chat21-core/providers/abstract/archivedconversations-handler.service.ts +5 -5
- package/src/chat21-core/providers/abstract/conversation-handler.service.ts +6 -6
- package/src/chat21-core/providers/abstract/conversations-handler.service.ts +6 -6
- package/src/chat21-core/providers/abstract/groups-handler.service.ts +5 -5
- package/src/chat21-core/providers/abstract/logger.service.ts +1 -0
- package/src/chat21-core/providers/abstract/messagingAuth.service.ts +2 -2
- package/src/chat21-core/providers/abstract/presence.service.ts +2 -2
- package/src/chat21-core/providers/abstract/typing.service.ts +2 -2
- package/src/chat21-core/providers/abstract/upload.service.ts +1 -1
- package/src/chat21-core/providers/firebase/firebase-archivedconversations-handler.ts +1 -1
- package/src/chat21-core/providers/firebase/firebase-groups-handler.ts +1 -1
- package/src/chat21-core/providers/logger/customLogger.ts +4 -0
- package/src/chat21-core/providers/mqtt/mqtt-archivedconversations-handler.ts +1 -1
- package/src/chat21-core/providers/mqtt/mqtt-conversations-handler.ts +0 -1
- package/src/chat21-core/providers/mqtt/mqtt-groups-handler.ts +1 -1
- package/src/chat21-core/utils/constants.ts +3 -2
- package/src/chat21-core/utils/utils.ts +2 -0
- package/src/launch.js +4 -61
- package/src/polyfills.ts +0 -10
- package/src/test.ts +3 -1
- package/tsconfig.json +5 -3
- package/dist/3rdpartylicenses.txt +0 -942
- package/dist/assets/i18n/en.json +0 -94
- package/dist/assets/i18n/es.json +0 -90
- package/dist/assets/i18n/fr.json +0 -90
- package/dist/assets/i18n/it.json +0 -90
- package/dist/assets/images/avatar_bot_tiledesk.svg +0 -1
- package/dist/assets/images/chat_human_avatar.svg +0 -12
- package/dist/assets/images/f21ico-done.svg +0 -1
- package/dist/assets/images/f21ico-done_all.svg +0 -1
- package/dist/assets/images/f21ico-schedule.svg +0 -1
- package/dist/assets/images/file-alt-solid.png +0 -0
- package/dist/assets/images/light_avatar_placeholder.svg +0 -1
- package/dist/assets/images/tommy_bot_tiledesk.svg +0 -48
- package/dist/assets/images/website_mockup.jpg +0 -0
- package/dist/assets/js/chat21client.js +0 -1066
- package/dist/assets/js/mqtt/4.1.0/mqtt.min.js +0 -1
- package/dist/assets/js/mqtt/4.2.6/mqtt.min.js +0 -1
- package/dist/assets/js/mqtt/4.2.8/mqtt.min.js +0 -1
- package/dist/assets/sounds/Carme.mp3 +0 -0
- package/dist/assets/sounds/justsaying.mp3 +0 -0
- package/dist/assets/sounds/pling.mp3 +0 -0
- package/dist/assets/styles/tiledesk_v1.scss +0 -22
- package/dist/assets/twp/chatbot-panel.html +0 -424
- package/dist/assets/twp/index-dev.html +0 -2430
- package/dist/assets/twp/index.html +0 -603
- package/dist/assets/twp/tiledesk_widget_files/bootstrap.min.css +0 -5
- package/dist/assets/twp/tiledesk_widget_files/bootstrap.min.js +0 -7
- package/dist/assets/twp/tiledesk_widget_files/facebook.png +0 -0
- package/dist/assets/twp/tiledesk_widget_files/font-awesome.min.css +0 -4
- package/dist/assets/twp/tiledesk_widget_files/icon +0 -23
- package/dist/assets/twp/tiledesk_widget_files/jquery.min.js +0 -2
- package/dist/assets/twp/tiledesk_widget_files/linkedin.png +0 -0
- package/dist/assets/twp/tiledesk_widget_files/logo-black.png +0 -0
- package/dist/assets/twp/tiledesk_widget_files/logo-mobile.png +0 -0
- package/dist/assets/twp/tiledesk_widget_files/logo-short.png +0 -0
- package/dist/assets/twp/tiledesk_widget_files/logo.png +0 -0
- package/dist/assets/twp/tiledesk_widget_files/logo@2x-black.png +0 -0
- package/dist/assets/twp/tiledesk_widget_files/telegram.png +0 -0
- package/dist/assets/twp/tiledesk_widget_files/twitter.png +0 -0
- package/dist/assets/twp/tiledesk_widget_files/website-poly.min.css +0 -5623
- package/dist/assets/twp/tiledesk_widget_files/whatsapp.png +0 -0
- package/dist/environments/real_data/widget-config-docker.json +0 -28
- package/dist/environments/real_data/widget-config-firebase.json +0 -30
- package/dist/environments/real_data/widget-config-mqtt.json +0 -27
- package/dist/environments/real_data/widget-config-native-mqtt.json +0 -28
- package/dist/environments/real_data/widget-config-native-prod.json +0 -27
- package/dist/f21ico-done.svg +0 -1
- package/dist/f21ico-done_all.svg +0 -1
- package/dist/f21ico-schedule.svg +0 -1
- package/dist/favicon.ico +0 -0
- package/dist/iframe-style.css +0 -189
- package/dist/index.html +0 -16
- package/dist/launch.js +0 -428
- package/dist/main.js +0 -1
- package/dist/polyfills-es5.js +0 -1
- package/dist/polyfills.js +0 -1
- package/dist/runtime.js +0 -1
- package/dist/scripts.js +0 -1
- package/dist/styles.css +0 -1
- package/dist/styles.js +0 -1
- package/dist/test-auth-remote.html +0 -95
- package/dist/test-auth.html +0 -49
- package/dist/test-buttons.html +0 -147
- package/dist/test-custom-auth.html +0 -50
- package/dist/test-new.html +0 -231
- package/dist/test.html +0 -142
- package/dist/tiledesk.js +0 -72
- package/dist/vendor.js +0 -1
- package/dist/widget-config-template.json +0 -29
- package/dist/widget-config.json +0 -29
- package/e2e/protractor.conf.js +0 -32
- package/src/environments/environment.pre.ts +0 -42
- package/src/environments/environment.prod.ts +0 -40
- package/src/environments/environment.ts +0 -41
- package/src/environments/real_data/widget-config-docker.json +0 -28
- package/src/environments/real_data/widget-config-firebase.json +0 -30
- package/src/environments/real_data/widget-config-mqtt.json +0 -27
- package/src/environments/real_data/widget-config-native-mqtt.json +0 -28
- package/src/environments/real_data/widget-config-native-prod.json +0 -27
- package/src/test-auth-remote.html +0 -95
- package/src/test-auth.html +0 -49
- package/src/test-buttons.html +0 -147
|
@@ -1,1066 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Chat21Client
|
|
3
|
-
|
|
4
|
-
v0.1.12.3
|
|
5
|
-
|
|
6
|
-
@Author Andrea Sponziello
|
|
7
|
-
(c) Tiledesk 2020
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
let mqtt = require('./mqtt/4.2.6/mqtt.min.js');
|
|
11
|
-
// let mqtt = require('mqtt')
|
|
12
|
-
// let axios = require('axios');
|
|
13
|
-
|
|
14
|
-
const _CLIENTADDED = "/clientadded"
|
|
15
|
-
const _CLIENTUPDATED = "/clientupdated"
|
|
16
|
-
const _CLIENTDELETED = "/clientdeleted"
|
|
17
|
-
const CALLBACK_TYPE_ON_MESSAGE_UPDATED_FOR_CONVERSATION = "onMessageUpdatedForConversation"
|
|
18
|
-
const CALLBACK_TYPE_ON_MESSAGE_ADDED_FOR_CONVERSATION = "onMessageAddedForConversation"
|
|
19
|
-
|
|
20
|
-
class Chat21Client {
|
|
21
|
-
constructor(options) {
|
|
22
|
-
// console.log('CHAT21-CLIENT.JS HELLO ', mqtt)
|
|
23
|
-
this.client = null;
|
|
24
|
-
this.reconnections = 0 // just to check how many reconnections
|
|
25
|
-
this.client_id = this.uuidv4();
|
|
26
|
-
this.log = options.log ? true : false;
|
|
27
|
-
if (options && options.MQTTendpoint) {
|
|
28
|
-
if (options.MQTTendpoint.startsWith('/')) {
|
|
29
|
-
if (this.log) {
|
|
30
|
-
console.log("MQTTendpoint relative url");
|
|
31
|
-
}
|
|
32
|
-
var loc = window.location, new_uri;
|
|
33
|
-
if (loc.protocol === "https:") {
|
|
34
|
-
// new_uri = "wss:";
|
|
35
|
-
new_uri = "mqtt:";
|
|
36
|
-
|
|
37
|
-
} else {
|
|
38
|
-
// new_uri = "ws:";
|
|
39
|
-
new_uri = "mqtt:";
|
|
40
|
-
}
|
|
41
|
-
new_uri += "//" + loc.host;
|
|
42
|
-
// new_uri += loc.pathname + "/to/ws";
|
|
43
|
-
new_uri += options.MQTTendpoint;
|
|
44
|
-
this.endpoint = new_uri
|
|
45
|
-
} else {
|
|
46
|
-
this.endpoint = options.MQTTendpoint
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
this.endpoint = "ws://34.253.207.0:15675/ws"
|
|
52
|
-
}
|
|
53
|
-
this.APIendpoint = options.APIendpoint;
|
|
54
|
-
this.appid = options.appId;
|
|
55
|
-
if (this.log) {
|
|
56
|
-
console.log("final endpoint:", this.endpoint);
|
|
57
|
-
}
|
|
58
|
-
this.user_id = null;
|
|
59
|
-
this.jwt = null;
|
|
60
|
-
this.last_handler = 0;
|
|
61
|
-
|
|
62
|
-
// this.onMessageCallbacks = new Map();
|
|
63
|
-
// this.onConnectCallbacks = new Map();
|
|
64
|
-
|
|
65
|
-
this.onConversationAddedCallbacks = new Map();
|
|
66
|
-
this.onConversationUpdatedCallbacks = new Map();
|
|
67
|
-
this.onConversationDeletedCallbacks = new Map();
|
|
68
|
-
this.onArchivedConversationAddedCallbacks = new Map();
|
|
69
|
-
this.onArchivedConversationDeletedCallbacks = new Map();
|
|
70
|
-
this.onMessageAddedCallbacks = new Map();
|
|
71
|
-
this.onMessageUpdatedCallbacks = new Map();
|
|
72
|
-
this.onGroupUpdatedCallbacks = new Map();
|
|
73
|
-
this.callbackHandlers = new Map();
|
|
74
|
-
this.on_message_handler = null;
|
|
75
|
-
this.topic_inbox = null;
|
|
76
|
-
this.connected = false
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
subscribeToMyConversations(subscribedCallback) { // MESSAGES ETC.
|
|
80
|
-
// WILDCARS:
|
|
81
|
-
// MQTT: https://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices/
|
|
82
|
-
// RABBITMQ: https://www.cloudamqp.com/blog/2015-09-03-part4-rabbitmq-for-beginners-exchanges-routing-keys-bindings.html#topic-exchange
|
|
83
|
-
this.topic_inbox = 'apps/tilechat/users/' + this.user_id + "/#"
|
|
84
|
-
if (this.log) {
|
|
85
|
-
console.log("subscribing to:", this.user_id, "topic", this.topic_inbox);
|
|
86
|
-
}
|
|
87
|
-
this.client.subscribe(this.topic_inbox, (err) => {
|
|
88
|
-
if (err) {
|
|
89
|
-
console.error("An error occurred while subscribing user", this.user_id, "on topic:", this.topic_inbox, "Error:", err);
|
|
90
|
-
}
|
|
91
|
-
if (this.log) {
|
|
92
|
-
console.log("subscribed to:", this.topic_inbox, " with err", err)
|
|
93
|
-
}
|
|
94
|
-
subscribedCallback();
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
sendMessage(text, type, recipient_id, recipient_fullname, sender_fullname, attributes, metadata, channel_type, callback) {
|
|
99
|
-
// console.log("sendMessage:",text, recipient_id)
|
|
100
|
-
let dest_topic = `apps/${this.appid}/outgoing/users/${this.user_id}/messages/${recipient_id}/outgoing`
|
|
101
|
-
// console.log("dest_topic:", dest_topic)
|
|
102
|
-
let outgoing_message = {
|
|
103
|
-
text: text,
|
|
104
|
-
type: type,
|
|
105
|
-
recipient_fullname: recipient_fullname,
|
|
106
|
-
sender_fullname: sender_fullname,
|
|
107
|
-
attributes: attributes,
|
|
108
|
-
metadata: metadata,
|
|
109
|
-
channel_type: channel_type
|
|
110
|
-
}
|
|
111
|
-
// console.log("outgoing_message:", outgoing_message)
|
|
112
|
-
const payload = JSON.stringify(outgoing_message)
|
|
113
|
-
this.client.publish(dest_topic, payload, null, (err) => {
|
|
114
|
-
callback(err, outgoing_message)
|
|
115
|
-
})
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
basicMessageBuilder(text, type, recipient_fullname, sender_fullname, attributes, metadata, channel_type) {
|
|
119
|
-
let outgoing_message = {
|
|
120
|
-
text: text,
|
|
121
|
-
type: type,
|
|
122
|
-
recipient_fullname: recipient_fullname,
|
|
123
|
-
sender_fullname: sender_fullname,
|
|
124
|
-
attributes: attributes,
|
|
125
|
-
metadata: metadata,
|
|
126
|
-
channel_type: channel_type
|
|
127
|
-
}
|
|
128
|
-
return outgoing_message;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
sendMessageRaw(outgoing_message, recipient_id, callback) {
|
|
132
|
-
// callback - function (err)
|
|
133
|
-
// console.log("recipient_id:", recipient_id)
|
|
134
|
-
let dest_topic = `apps/${this.appid}/outgoing/users/${this.user_id}/messages/${recipient_id}/outgoing`
|
|
135
|
-
if (this.log) {
|
|
136
|
-
console.log("dest_topic:", dest_topic)
|
|
137
|
-
}
|
|
138
|
-
// let outgoing_message = {
|
|
139
|
-
// text: text,
|
|
140
|
-
// type: type,
|
|
141
|
-
// recipient_fullname: recipient_fullname,
|
|
142
|
-
// sender_fullname: sender_fullname,
|
|
143
|
-
// attributes: attributes,
|
|
144
|
-
// metadata: metadata,
|
|
145
|
-
// channel_type: channel_type
|
|
146
|
-
// }
|
|
147
|
-
// console.log("outgoing_message:", outgoing_message)
|
|
148
|
-
const payload = JSON.stringify(outgoing_message)
|
|
149
|
-
this.client.publish(dest_topic, payload, null, (err) => {
|
|
150
|
-
callback(err, outgoing_message)
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
updateMessageStatus(messageId, conversWith, status, callback) {
|
|
155
|
-
// callback - function (err)
|
|
156
|
-
if (this.log) {
|
|
157
|
-
console.log("updating recipient_id:", messageId, "on conversWith", conversWith, "status", status)
|
|
158
|
-
}
|
|
159
|
-
// 'apps/tilechat/users/USER_ID/messages/CONVERS_WITH/MESSAGE_ID/update'
|
|
160
|
-
let dest_topic = `apps/${this.appid}/users/${this.user_id}/messages/${conversWith}/${messageId}/update`
|
|
161
|
-
if (this.log) {
|
|
162
|
-
console.log("update dest_topic:", dest_topic);
|
|
163
|
-
}
|
|
164
|
-
let message_patch = {
|
|
165
|
-
status: status
|
|
166
|
-
}
|
|
167
|
-
const payload = JSON.stringify(message_patch)
|
|
168
|
-
if (this.log) {
|
|
169
|
-
console.log("payload:", payload)
|
|
170
|
-
}
|
|
171
|
-
this.client.publish(dest_topic, payload, null, (err) => {
|
|
172
|
-
if (callback) {
|
|
173
|
-
callback(err, message_patch)
|
|
174
|
-
}
|
|
175
|
-
})
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
updateConversationIsNew(conversWith, is_new, callback) {
|
|
179
|
-
// callback - function (err)
|
|
180
|
-
if (this.log) {
|
|
181
|
-
console.log("updating conversation with:", conversWith, "is_new", is_new);
|
|
182
|
-
}
|
|
183
|
-
// 'apps/tilechat/users/USER_ID/conversations/CONVERS_WITH/update'
|
|
184
|
-
let dest_topic = `apps/${this.appid}/users/${this.user_id}/conversations/${conversWith}/update` //'apps/tilechat/users/' + this.user_id + '/conversations/' + conversWith + '/update'
|
|
185
|
-
if (this.log) {
|
|
186
|
-
console.log("update dest_topic:", dest_topic);
|
|
187
|
-
}
|
|
188
|
-
let patch = {
|
|
189
|
-
is_new: is_new
|
|
190
|
-
}
|
|
191
|
-
const payload = JSON.stringify(patch)
|
|
192
|
-
if (this.log) {
|
|
193
|
-
console.log("payload:", payload);
|
|
194
|
-
}
|
|
195
|
-
this.client.publish(dest_topic, payload, null, (err) => {
|
|
196
|
-
if (callback) {
|
|
197
|
-
callback(err)
|
|
198
|
-
}
|
|
199
|
-
})
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
groupCreate(name, group_id, members, callback) {
|
|
203
|
-
// example:
|
|
204
|
-
// {
|
|
205
|
-
// "group_id":"group-tiledeskteam",
|
|
206
|
-
// "group_name":"Tiledesk Team",
|
|
207
|
-
// "group_members":{
|
|
208
|
-
// "608bc83b3d0b3e494f4d0578":1,
|
|
209
|
-
// "608bc81f3d0b3e494f4d0575":1,
|
|
210
|
-
// "6067513cb64a9b1ba259839c":1
|
|
211
|
-
// }
|
|
212
|
-
// }
|
|
213
|
-
|
|
214
|
-
// callback - function (err)
|
|
215
|
-
if (this.log) {
|
|
216
|
-
console.log("creating group:", name, "id", group_id, "members", members)
|
|
217
|
-
}
|
|
218
|
-
// who creates the group is also group-owner
|
|
219
|
-
// ex.: http://localhost:8004/api/tilechat/04-ANDREASPONZIELLO/groups
|
|
220
|
-
// let data = {
|
|
221
|
-
// group_name: name,
|
|
222
|
-
// group_id: group_id,
|
|
223
|
-
// group_members: members
|
|
224
|
-
// }
|
|
225
|
-
// let headers = {
|
|
226
|
-
// "authorization": this.jwt,
|
|
227
|
-
// "Content-Type": "application/json;charset=UTF-8"
|
|
228
|
-
// }
|
|
229
|
-
|
|
230
|
-
const URL = `${this.APIendpoint}/${this.appid}/groups`
|
|
231
|
-
if (this.log) {
|
|
232
|
-
console.log("creating group...", URL)
|
|
233
|
-
}
|
|
234
|
-
let options = {
|
|
235
|
-
url: URL,
|
|
236
|
-
headers: {
|
|
237
|
-
"Authorization": this.jwt,
|
|
238
|
-
"Content-Type": "application/json;charset=UTF-8"
|
|
239
|
-
},
|
|
240
|
-
data: {
|
|
241
|
-
group_name: name,
|
|
242
|
-
group_id: group_id,
|
|
243
|
-
group_members: members
|
|
244
|
-
},
|
|
245
|
-
method: 'POST'
|
|
246
|
-
}
|
|
247
|
-
Chat21Client.myrequest(options, (err, response, json) => {
|
|
248
|
-
if (err) {
|
|
249
|
-
callback(err, null);
|
|
250
|
-
}
|
|
251
|
-
else if (json && callback) {
|
|
252
|
-
callback(null, json);
|
|
253
|
-
}
|
|
254
|
-
}, this.log);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
groupData(group_id, callback) {
|
|
258
|
-
const URL = `${this.APIendpoint}/${this.appid}/groups/${group_id}`
|
|
259
|
-
// console.log("creating group...", URL)
|
|
260
|
-
let options = {
|
|
261
|
-
url: URL,
|
|
262
|
-
headers: {
|
|
263
|
-
"Authorization": this.jwt,
|
|
264
|
-
"Content-Type": "application/json;charset=UTF-8"
|
|
265
|
-
},
|
|
266
|
-
method: 'GET'
|
|
267
|
-
}
|
|
268
|
-
Chat21Client.myrequest(options, (err, response, json) => {
|
|
269
|
-
if (err) {
|
|
270
|
-
callback(err, null);
|
|
271
|
-
}
|
|
272
|
-
else if (json && callback) {
|
|
273
|
-
callback(null, json);
|
|
274
|
-
}
|
|
275
|
-
}, this.log);
|
|
276
|
-
// const URL = `${this.APIendpoint}/${this.appid}/groups/${group_id}`
|
|
277
|
-
// console.log("getting group...", URL)
|
|
278
|
-
// var xmlhttp = new XMLHttpRequest();
|
|
279
|
-
// xmlhttp.open("GET", URL, true);
|
|
280
|
-
// xmlhttp.setRequestHeader("authorization", this.jwt);
|
|
281
|
-
// xmlhttp.onreadystatechange = function() {
|
|
282
|
-
// if (callback && xmlhttp.readyState == 4 && xmlhttp.status == 200 && xmlhttp.responseText) {
|
|
283
|
-
// try {
|
|
284
|
-
// const json = JSON.parse(xmlhttp.responseText)
|
|
285
|
-
// callback(null, json.result)
|
|
286
|
-
// }
|
|
287
|
-
// catch (err) {
|
|
288
|
-
// console.error("parsing json ERROR", err)
|
|
289
|
-
// callback(err, null)
|
|
290
|
-
// }
|
|
291
|
-
// }
|
|
292
|
-
// };
|
|
293
|
-
// xmlhttp.send(null);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
groupLeave(group_id, member_id, callback) {
|
|
297
|
-
if (this.log) {
|
|
298
|
-
console.log("leaving group:", group_id);
|
|
299
|
-
}
|
|
300
|
-
const URL = `${this.APIendpoint}/${this.appid}/groups/${group_id}/members/${member_id}`
|
|
301
|
-
if (this.log) {
|
|
302
|
-
console.log("leaving group:", URL)
|
|
303
|
-
}
|
|
304
|
-
let options = {
|
|
305
|
-
url: URL,
|
|
306
|
-
headers: {
|
|
307
|
-
"Authorization": this.jwt,
|
|
308
|
-
"Content-Type": "application/json;charset=UTF-8"
|
|
309
|
-
},
|
|
310
|
-
method: 'DELETE'
|
|
311
|
-
}
|
|
312
|
-
Chat21Client.myrequest(options, (err, response, json) => {
|
|
313
|
-
if (err) {
|
|
314
|
-
callback(err, null);
|
|
315
|
-
}
|
|
316
|
-
else if (callback) {
|
|
317
|
-
callback(null, json);
|
|
318
|
-
}
|
|
319
|
-
}, this.log);
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
groupJoin(group_id, member_id, callback) {
|
|
323
|
-
if (this.log) {
|
|
324
|
-
console.log("leaving group:", group_id);
|
|
325
|
-
}
|
|
326
|
-
const URL = `${this.APIendpoint}/${this.appid}/groups/${group_id}/members`
|
|
327
|
-
if (this.log) {
|
|
328
|
-
console.log("joining group:", URL)
|
|
329
|
-
}
|
|
330
|
-
let options = {
|
|
331
|
-
url: URL,
|
|
332
|
-
headers: {
|
|
333
|
-
"Authorization": this.jwt,
|
|
334
|
-
"Content-Type": "application/json;charset=UTF-8"
|
|
335
|
-
},
|
|
336
|
-
data: {
|
|
337
|
-
member_id: member_id
|
|
338
|
-
},
|
|
339
|
-
method: 'POST'
|
|
340
|
-
}
|
|
341
|
-
Chat21Client.myrequest(options, (err, response, json) => {
|
|
342
|
-
if (err) {
|
|
343
|
-
callback(err, null);
|
|
344
|
-
}
|
|
345
|
-
else if (callback) {
|
|
346
|
-
callback(null, json);
|
|
347
|
-
}
|
|
348
|
-
}, this.log);
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
groupSetMembers(group_id, members, callback) {
|
|
352
|
-
// example:
|
|
353
|
-
// {
|
|
354
|
-
// "members":{
|
|
355
|
-
// "608bc83b3d0b3e494f4d0578":1,
|
|
356
|
-
// "608bc81f3d0b3e494f4d0575":1,
|
|
357
|
-
// "6067513cb64a9b1ba259839c":1
|
|
358
|
-
// }
|
|
359
|
-
// }
|
|
360
|
-
if (this.log) {
|
|
361
|
-
console.log("setting group members of", group_id, "members", members)
|
|
362
|
-
}
|
|
363
|
-
const URL = `${this.APIendpoint}/${this.appid}/groups/${group_id}/members`
|
|
364
|
-
if (this.log) {
|
|
365
|
-
console.log("setting group members...", URL)
|
|
366
|
-
}
|
|
367
|
-
let options = {
|
|
368
|
-
url: URL,
|
|
369
|
-
headers: {
|
|
370
|
-
"Authorization": this.jwt,
|
|
371
|
-
"Content-Type": "application/json;charset=UTF-8"
|
|
372
|
-
},
|
|
373
|
-
data: {
|
|
374
|
-
members: members
|
|
375
|
-
},
|
|
376
|
-
method: 'PUT'
|
|
377
|
-
}
|
|
378
|
-
Chat21Client.myrequest(options, (err, response, json) => {
|
|
379
|
-
if (err) {
|
|
380
|
-
callback(err, null);
|
|
381
|
-
}
|
|
382
|
-
else if (json && callback) {
|
|
383
|
-
callback(null, json);
|
|
384
|
-
}
|
|
385
|
-
}, this.log);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
saveInstance(instance_id, data, callback) {
|
|
389
|
-
if (this.log) {
|
|
390
|
-
console.log("saving instance_id:", instance_id, "data", data);
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
// /:app_id/:user_id/instances/:instance_id
|
|
394
|
-
const URL = `${this.APIendpoint}/${this.appid}/${this.user_id}/instances/${instance_id}`
|
|
395
|
-
if (this.log) {
|
|
396
|
-
console.log("saving instance...");
|
|
397
|
-
}
|
|
398
|
-
let options = {
|
|
399
|
-
url: URL,
|
|
400
|
-
headers: {
|
|
401
|
-
"Authorization": this.jwt,
|
|
402
|
-
"Content-Type": "application/json;charset=UTF-8"
|
|
403
|
-
},
|
|
404
|
-
data: data,
|
|
405
|
-
method: 'POST'
|
|
406
|
-
}
|
|
407
|
-
Chat21Client.myrequest(options, (err, response, json) => {
|
|
408
|
-
if (err) {
|
|
409
|
-
callback(err, null);
|
|
410
|
-
}
|
|
411
|
-
else if (json && callback) {
|
|
412
|
-
callback(null, json);
|
|
413
|
-
}
|
|
414
|
-
}, this.log);
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
archiveConversation(conversWith, callback) {
|
|
418
|
-
// callback - function (err)
|
|
419
|
-
if (this.log) {
|
|
420
|
-
console.log("archiving conversation with:", conversWith)
|
|
421
|
-
}
|
|
422
|
-
// 'apps/tilechat/users/USER_ID/conversations/CONVERS_WITH/archive'
|
|
423
|
-
let dest_topic = 'apps/tilechat/users/' + this.user_id + '/conversations/' + conversWith + '/archive'
|
|
424
|
-
if (this.log) {
|
|
425
|
-
console.log("archive dest_topic:", dest_topic)
|
|
426
|
-
}
|
|
427
|
-
// let patch = {
|
|
428
|
-
// action: 'archive'
|
|
429
|
-
// }
|
|
430
|
-
const payload = JSON.stringify({})
|
|
431
|
-
// console.log("payload:", payload)
|
|
432
|
-
this.client.publish(dest_topic, payload, null, (err) => {
|
|
433
|
-
if (callback) {
|
|
434
|
-
callback(err)
|
|
435
|
-
}
|
|
436
|
-
})
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
// onMessage(callback) {
|
|
440
|
-
// this.last_handler++
|
|
441
|
-
// this.onMessageCallbacks.set(this.last_handler, callback)
|
|
442
|
-
// return this.last_handler;
|
|
443
|
-
// }
|
|
444
|
-
|
|
445
|
-
onConversationAdded(callback) {
|
|
446
|
-
this.last_handler++
|
|
447
|
-
this.onConversationAddedCallbacks.set(this.last_handler, callback)
|
|
448
|
-
return this.last_handler;
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
onConversationUpdated(callback) {
|
|
452
|
-
this.last_handler++
|
|
453
|
-
this.onConversationUpdatedCallbacks.set(this.last_handler, callback)
|
|
454
|
-
return this.last_handler;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
onConversationDeleted(callback) {
|
|
458
|
-
this.last_handler++
|
|
459
|
-
this.onConversationDeletedCallbacks.set(this.last_handler, callback)
|
|
460
|
-
return this.last_handler;
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
onArchivedConversationAdded(callback) {
|
|
464
|
-
this.last_handler++
|
|
465
|
-
this.onArchivedConversationAddedCallbacks.set(this.last_handler, callback)
|
|
466
|
-
return this.last_handler;
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
onArchivedConversationDeleted(callback) {
|
|
470
|
-
this.last_handler++
|
|
471
|
-
this.onArchivedConversationDeletedCallbacks.set(this.last_handler, callback)
|
|
472
|
-
return this.last_handler;
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
onMessageAdded(callback) {
|
|
476
|
-
this.last_handler++
|
|
477
|
-
this.onMessageAddedCallbacks.set(this.last_handler, callback)
|
|
478
|
-
return this.last_handler;
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
onMessageAddedInConversation(conversWith, callback) {
|
|
482
|
-
this.last_handler++
|
|
483
|
-
const callback_obj = {
|
|
484
|
-
"type": CALLBACK_TYPE_ON_MESSAGE_ADDED_FOR_CONVERSATION,
|
|
485
|
-
"conversWith": conversWith,
|
|
486
|
-
"callback": callback
|
|
487
|
-
}
|
|
488
|
-
this.callbackHandlers.set(this.last_handler, callback_obj)
|
|
489
|
-
// TODO (for performance): addToMessageAddedInConversationCallbacks(conversWith, this.last_handler)
|
|
490
|
-
|
|
491
|
-
// this.callbackHandlers = new Map();
|
|
492
|
-
// key: handler_id
|
|
493
|
-
// value: {
|
|
494
|
-
// "type": "messageAddedInConversation",
|
|
495
|
-
// "conversWith": "ID",
|
|
496
|
-
// "callback": callback
|
|
497
|
-
// }
|
|
498
|
-
return this.last_handler;
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
onMessageUpdatedInConversation(conversWith, callback) {
|
|
502
|
-
this.last_handler++
|
|
503
|
-
const callback_obj = {
|
|
504
|
-
"type": CALLBACK_TYPE_ON_MESSAGE_UPDATED_FOR_CONVERSATION,
|
|
505
|
-
"conversWith": conversWith,
|
|
506
|
-
"callback": callback
|
|
507
|
-
}
|
|
508
|
-
this.callbackHandlers.set(this.last_handler, callback_obj)
|
|
509
|
-
|
|
510
|
-
// this.last_handler++
|
|
511
|
-
// callback_obj = {
|
|
512
|
-
// "conversWith": conversWith,
|
|
513
|
-
// "callback": callback
|
|
514
|
-
// }
|
|
515
|
-
// this.onMessageUpdatedCallbacks.set(this.last_handler, callback_obj)
|
|
516
|
-
return this.last_handler;
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
onMessageUpdated(callback) {
|
|
520
|
-
this.last_handler += 1
|
|
521
|
-
this.onMessageUpdatedCallbacks.set(this.last_handler, callback)
|
|
522
|
-
return this.last_handler;
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
onGroupUpdated(callback) {
|
|
526
|
-
this.last_handler += 1
|
|
527
|
-
this.onGroupUpdatedCallbacks.set(this.last_handler, callback)
|
|
528
|
-
return this.last_handler;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
removeOnMessageAddedHandler(handler) {
|
|
532
|
-
this.onMessageAddedCallbacks.delete(handler);
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
removeOnGroupUpdatedHandler(handler) {
|
|
536
|
-
this.onGroupUpdatedCallbacks.delete(handler);
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
start(subscribedCallback) {
|
|
540
|
-
if (this.on_message_handler) {
|
|
541
|
-
if (this.log) {
|
|
542
|
-
console.log("this.on_message_handler already subscribed. Reconnected num", this.reconnections)
|
|
543
|
-
}
|
|
544
|
-
callbsubscribedCallbackack();
|
|
545
|
-
return
|
|
546
|
-
}
|
|
547
|
-
this.subscribeToMyConversations(() => {
|
|
548
|
-
// no more than one "on_message" handler, thanks.
|
|
549
|
-
this.on_message_handler = this.client.on('message', (topic, message) => {
|
|
550
|
-
if (this.log) {
|
|
551
|
-
console.log("topic:" + topic + "\nmessage payload:" + message)
|
|
552
|
-
}
|
|
553
|
-
const _topic = this.parseTopic(topic)
|
|
554
|
-
if (!_topic) {
|
|
555
|
-
if (this.log) {
|
|
556
|
-
console.log("Invalid message topic:", topic);
|
|
557
|
-
}
|
|
558
|
-
return;
|
|
559
|
-
}
|
|
560
|
-
const conversWith = _topic.conversWith
|
|
561
|
-
try {
|
|
562
|
-
const message_json = JSON.parse(message.toString())
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
// TEMPORARILY DISABLED, ADDED-CONVERSATIONS ARE OBSERVED BY NEW MESSAGES.
|
|
566
|
-
// MOVED TO: this.onMessageAddedCallbacks
|
|
567
|
-
// if (this.onConversationAddedCallbacks) {
|
|
568
|
-
// if (topic.includes("/conversations/") && topic.endsWith(_CLIENTADDED)) {
|
|
569
|
-
// // map.forEach((value, key, map) =>)
|
|
570
|
-
// this.onConversationAddedCallbacks.forEach((callback, handler, map) => {
|
|
571
|
-
// callback(message_json, _topic)
|
|
572
|
-
// });
|
|
573
|
-
// }
|
|
574
|
-
// }
|
|
575
|
-
|
|
576
|
-
if (this.onConversationUpdatedCallbacks) {
|
|
577
|
-
// example topic: apps.tilechat.users.ME.conversations.CONVERS-WITH.clientdeleted
|
|
578
|
-
if (topic.includes("/conversations/") && topic.endsWith(_CLIENTUPDATED)) {
|
|
579
|
-
if (this.log) {
|
|
580
|
-
console.log("conversation updated! /conversations/, topic:", topic)
|
|
581
|
-
}
|
|
582
|
-
// map.forEach((value, key, map) =>)
|
|
583
|
-
this.onConversationUpdatedCallbacks.forEach((callback, handler, map) => {
|
|
584
|
-
callback(JSON.parse(message.toString()), _topic)
|
|
585
|
-
});
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
if (this.onConversationDeletedCallbacks) {
|
|
590
|
-
if (topic.includes("/conversations/") && topic.endsWith(_CLIENTDELETED)) {
|
|
591
|
-
// map.forEach((value, key, map) =>)
|
|
592
|
-
if (this.log) {
|
|
593
|
-
console.log("conversation deleted! /conversations/, topic:", topic, message.toString() );
|
|
594
|
-
}
|
|
595
|
-
this.onConversationDeletedCallbacks.forEach((callback, handler, map) => {
|
|
596
|
-
callback(JSON.parse(message.toString()), _topic)
|
|
597
|
-
});
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
if (this.onArchivedConversationAddedCallbacks) {
|
|
602
|
-
if (topic.includes("/archived_conversations/") && topic.endsWith(_CLIENTADDED)) {
|
|
603
|
-
// map.forEach((value, key, map) =>)
|
|
604
|
-
this.onArchivedConversationAddedCallbacks.forEach((callback, handler, map) => {
|
|
605
|
-
callback(JSON.parse(message.toString()), _topic)
|
|
606
|
-
});
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
if (this.onArchivedConversationDeletedCallbacks) {
|
|
611
|
-
if (topic.includes("/archived_conversations/") && topic.endsWith(_CLIENTDELETED)) {
|
|
612
|
-
// map.forEach((value, key, map) =>)
|
|
613
|
-
this.onArchivedConversationDeletedCallbacks.forEach((callback, handler, map) => {
|
|
614
|
-
callback(JSON.parse(message.toString()), _topic)
|
|
615
|
-
});
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
// *********************************************************
|
|
620
|
-
// This snippet is important to get all messages and notify
|
|
621
|
-
// conversation > added (to create a conversation entry)
|
|
622
|
-
// *********************************************************
|
|
623
|
-
// if (this.onMessageAddedCallbacks) {
|
|
624
|
-
// console.log("ttttttttt")
|
|
625
|
-
if (topic.includes("/messages/") && topic.endsWith(_CLIENTADDED)) {
|
|
626
|
-
if (this.onMessageAddedCallbacks) {
|
|
627
|
-
this.onMessageAddedCallbacks.forEach((callback, handler, map) => {
|
|
628
|
-
callback(JSON.parse(message.toString()), _topic)
|
|
629
|
-
});
|
|
630
|
-
}
|
|
631
|
-
// Observing conversations added from messages
|
|
632
|
-
// console.log("Observing conversations added from messages", message_json);
|
|
633
|
-
// if (this.onConversationAddedCallbacks) {
|
|
634
|
-
let update_conversation = true;
|
|
635
|
-
|
|
636
|
-
if (message_json.attributes && message_json.attributes.updateconversation == false) {
|
|
637
|
-
update_conversation = false
|
|
638
|
-
}
|
|
639
|
-
if (update_conversation && this.onConversationAddedCallbacks) {
|
|
640
|
-
this.onConversationAddedCallbacks.forEach((callback, handler, map) => {
|
|
641
|
-
message_json.is_new = true;
|
|
642
|
-
const message_for_conv_string = JSON.stringify(message_json);
|
|
643
|
-
callback(JSON.parse(message_for_conv_string), _topic)
|
|
644
|
-
});
|
|
645
|
-
}
|
|
646
|
-
// }
|
|
647
|
-
}
|
|
648
|
-
// }
|
|
649
|
-
|
|
650
|
-
if (this.onMessageUpdatedCallbacks) {
|
|
651
|
-
if (topic.includes("/messages/") && topic.endsWith(_CLIENTUPDATED)) {
|
|
652
|
-
this.onMessageUpdatedCallbacks.forEach((callback, handler, map) => {
|
|
653
|
-
callback(JSON.parse(message.toString()), _topic)
|
|
654
|
-
});
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
if (this.onGroupUpdatedCallbacks) {
|
|
659
|
-
if (topic.includes("/groups/") && topic.endsWith(_CLIENTUPDATED)) {
|
|
660
|
-
this.onGroupUpdatedCallbacks.forEach((callback, handler, map) => {
|
|
661
|
-
callback(JSON.parse(message.toString()), _topic)
|
|
662
|
-
});
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
// // ******* NEW!!
|
|
667
|
-
this.callbackHandlers.forEach((value, key, map) => {
|
|
668
|
-
const callback_obj = value
|
|
669
|
-
// callback_obj = {
|
|
670
|
-
// "type": "onMessageUpdatedForConversation",
|
|
671
|
-
// "conversWith": conversWith,
|
|
672
|
-
// "callback": callback
|
|
673
|
-
// }
|
|
674
|
-
const type = callback_obj.type
|
|
675
|
-
if (topic.includes("/messages/") && topic.endsWith(_CLIENTADDED)) {
|
|
676
|
-
if (this.log) { console.log("/messages/_CLIENTADDED") }
|
|
677
|
-
if (type === CALLBACK_TYPE_ON_MESSAGE_ADDED_FOR_CONVERSATION) {
|
|
678
|
-
if (conversWith === callback_obj.conversWith) {
|
|
679
|
-
if (this.log) { console.log("/messages/_CLIENTADDED on: ", conversWith)}
|
|
680
|
-
callback_obj.callback(JSON.parse(message.toString()), _topic)
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
if (topic.includes("/messages/") && topic.endsWith(_CLIENTUPDATED)) {
|
|
685
|
-
if (this.log) {console.log("/messages/_CLIENTUPDATED")}
|
|
686
|
-
if (type === CALLBACK_TYPE_ON_MESSAGE_UPDATED_FOR_CONVERSATION) {
|
|
687
|
-
if (conversWith === callback_obj.conversWith) {
|
|
688
|
-
if (this.log) {console.log("/messages/_CLIENTUPDATED on: ", conversWith);}
|
|
689
|
-
callback_obj.callback(JSON.parse(message.toString()), _topic)
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
})
|
|
694
|
-
|
|
695
|
-
// if (topic.includes("/messages/") && topic.endsWith(_CLIENTUPDATED)) {
|
|
696
|
-
// this.onMessageUpdatedInConversationCallbacks.forEach((obj, handler, map) => {
|
|
697
|
-
// if (conversWith === obj.conversWith) {
|
|
698
|
-
// callback(message_json, _topic)
|
|
699
|
-
// }
|
|
700
|
-
// });
|
|
701
|
-
// }
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
}
|
|
705
|
-
catch (err) {
|
|
706
|
-
console.error("ERROR:", err)
|
|
707
|
-
}
|
|
708
|
-
})
|
|
709
|
-
subscribedCallback();
|
|
710
|
-
})
|
|
711
|
-
|
|
712
|
-
// console.log("HANDLER_:", this.on_message_handler)
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
parseTopic(topic) {
|
|
716
|
-
var topic_parts = topic.split("/");
|
|
717
|
-
// /apps/tilechat/users/(ME)/messages/RECIPIENT_ID/ACTION
|
|
718
|
-
if (topic_parts.length >= 7) {
|
|
719
|
-
const app_id = topic_parts[1];
|
|
720
|
-
const sender_id = topic_parts[3];
|
|
721
|
-
const recipient_id = topic_parts[5];
|
|
722
|
-
const convers_with = recipient_id;
|
|
723
|
-
const me = sender_id;
|
|
724
|
-
const parsed = {
|
|
725
|
-
"conversWith": convers_with
|
|
726
|
-
}
|
|
727
|
-
return parsed;
|
|
728
|
-
}
|
|
729
|
-
return null;
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
lastArchivedConversations(callback) {
|
|
733
|
-
// ex.: http://localhost:8004/tilechat/04-ANDREASPONZIELLO/archived_conversations
|
|
734
|
-
const URL = `${this.APIendpoint}/${this.appid}/${this.user_id}/archived_conversations`
|
|
735
|
-
if (this.log) {console.log("getting last archived conversations...", URL)}
|
|
736
|
-
var xmlhttp = new XMLHttpRequest();
|
|
737
|
-
xmlhttp.open("GET", URL, true);
|
|
738
|
-
xmlhttp.setRequestHeader("authorization", this.jwt);
|
|
739
|
-
xmlhttp.onreadystatechange = function() {
|
|
740
|
-
// console.log("onreadystatechange!")
|
|
741
|
-
if (callback && xmlhttp.readyState == 4 && xmlhttp.status == 200 && xmlhttp.responseText) {
|
|
742
|
-
try {
|
|
743
|
-
const json = JSON.parse(xmlhttp.responseText)
|
|
744
|
-
callback(null, json.result)
|
|
745
|
-
}
|
|
746
|
-
catch (err) {
|
|
747
|
-
console.error("parsing json ERROR", err)
|
|
748
|
-
callback(err, null)
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
};
|
|
752
|
-
xmlhttp.send(null);
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
lastConversations(archived, callback) {
|
|
756
|
-
// ex.: http://localhost:8004/tilechat/04-ANDREASPONZIELLO/conversations
|
|
757
|
-
const archived_url_part = archived ? '/archived' : '';
|
|
758
|
-
const URL = `${this.APIendpoint}/${this.appid}/${this.user_id}/conversations` + archived_url_part;
|
|
759
|
-
if (this.log) {console.log("getting last convs...", URL);}
|
|
760
|
-
var xmlhttp = new XMLHttpRequest();
|
|
761
|
-
xmlhttp.open("GET", URL, true);
|
|
762
|
-
xmlhttp.setRequestHeader("authorization", this.jwt);
|
|
763
|
-
xmlhttp.onreadystatechange = function() {
|
|
764
|
-
// console.log("onreadystatechange!")
|
|
765
|
-
if (callback && xmlhttp.readyState == 4 && xmlhttp.status == 200 && xmlhttp.responseText) {
|
|
766
|
-
try {
|
|
767
|
-
const json = JSON.parse(xmlhttp.responseText)
|
|
768
|
-
callback(null, json.result)
|
|
769
|
-
}
|
|
770
|
-
catch (err) {
|
|
771
|
-
console.error("parsing json ERROR", err)
|
|
772
|
-
callback(err, null)
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
};
|
|
776
|
-
xmlhttp.send(null);
|
|
777
|
-
}
|
|
778
|
-
|
|
779
|
-
conversationDetail(conversWith, callback) {
|
|
780
|
-
if (this.log) {
|
|
781
|
-
console.log("conversationDetail(). searching on user:", this.user_id, " - conversWith:", conversWith)
|
|
782
|
-
}
|
|
783
|
-
this.crossConversationDetail(conversWith, false, callback);
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
archivedConversationDetail(conversWith, callback) {
|
|
787
|
-
if (this.log) {
|
|
788
|
-
console.log("archivedConversationDetail(). searching on user:", this.user_id, " - conversWith:", conversWith)
|
|
789
|
-
}
|
|
790
|
-
this.crossConversationDetail(conversWith, true, callback);
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
crossConversationDetail(conversWith, archived, callback) {
|
|
794
|
-
if (this.log) {
|
|
795
|
-
console.log("searching on user:", this.user_id, " - conv of conversWith:", conversWith, " - archived:", archived)
|
|
796
|
-
}
|
|
797
|
-
let path = "conversations";
|
|
798
|
-
if (archived) {
|
|
799
|
-
path = "archived_conversations"
|
|
800
|
-
}
|
|
801
|
-
// ex.: http://localhost:8004/tilechat/04-ANDREASPONZIELLO/conversations/CONVERS_WITH
|
|
802
|
-
//const URL = `${this.APIendpoint}/${this.appid}/${this.user_id}/conversations/${conversWith}`
|
|
803
|
-
const URL = `${this.APIendpoint}/${this.appid}/${this.user_id}/${path}/${conversWith}`
|
|
804
|
-
if (this.log) {
|
|
805
|
-
console.log("getting conversation detail:", URL);
|
|
806
|
-
console.log("conversWith:", conversWith);
|
|
807
|
-
}
|
|
808
|
-
|
|
809
|
-
let options = {
|
|
810
|
-
url: URL,
|
|
811
|
-
headers: {
|
|
812
|
-
"Authorization": this.jwt
|
|
813
|
-
// "Content-Type": "application/json;charset=UTF-8"
|
|
814
|
-
},
|
|
815
|
-
method: 'GET'
|
|
816
|
-
}
|
|
817
|
-
Chat21Client.myrequest(options, (err, response, json) => {
|
|
818
|
-
if (this.log) {
|
|
819
|
-
console.log("JSON...", json);
|
|
820
|
-
}
|
|
821
|
-
if (json && json.result && Array.isArray(json.result) && json.result.length ==1) {
|
|
822
|
-
callback(null, json.result[0]);
|
|
823
|
-
}
|
|
824
|
-
else {
|
|
825
|
-
callback(null, null);
|
|
826
|
-
}
|
|
827
|
-
}, this.log);
|
|
828
|
-
|
|
829
|
-
// var xmlhttp = new XMLHttpRequest();
|
|
830
|
-
// xmlhttp.open("GET", URL, true);
|
|
831
|
-
// xmlhttp.setRequestHeader("authorization", this.jwt);
|
|
832
|
-
// xmlhttp.onreadystatechange = function() {
|
|
833
|
-
// if (callback && xmlhttp.readyState == 4 && xmlhttp.status == 200 && xmlhttp.responseText) {
|
|
834
|
-
// try {
|
|
835
|
-
// const json = JSON.parse(xmlhttp.responseText);
|
|
836
|
-
// if (json && json.result && Array.isArray(json.result) && json.result.length ==1) {
|
|
837
|
-
// callback(null, json.result[0]);
|
|
838
|
-
// }
|
|
839
|
-
// else {
|
|
840
|
-
// callback({"message": "Incorrect conversation result."}, null);
|
|
841
|
-
// }
|
|
842
|
-
// }
|
|
843
|
-
// catch (err) {
|
|
844
|
-
// console.error("parsing json ERROR", err);
|
|
845
|
-
// callback(err, null);
|
|
846
|
-
// }
|
|
847
|
-
// }
|
|
848
|
-
// };
|
|
849
|
-
// xmlhttp.send(null);
|
|
850
|
-
}
|
|
851
|
-
|
|
852
|
-
lastMessages(convers_with, callback) {
|
|
853
|
-
// console.log("START: ", this.user_id)
|
|
854
|
-
// ex.: http://localhost:8004/tilechat/04-ANDREASPONZIELLO/conversations
|
|
855
|
-
const URL = this.APIendpoint + "/" + this.appid + "/" + this.user_id + "/conversations/" + convers_with + "/messages"
|
|
856
|
-
// console.log("getting last messages", URL)
|
|
857
|
-
// console.log("END")
|
|
858
|
-
var xmlhttp = new XMLHttpRequest();
|
|
859
|
-
xmlhttp.open("GET", URL, true);
|
|
860
|
-
xmlhttp.setRequestHeader("authorization", this.jwt);
|
|
861
|
-
xmlhttp.onreadystatechange = function() {
|
|
862
|
-
// console.log("onreadystatechange messages!")
|
|
863
|
-
if (callback && xmlhttp.readyState == 4 && xmlhttp.status == 200 && xmlhttp.responseText) {
|
|
864
|
-
// console.log("xmlhttp...", xmlhttp.responseText)
|
|
865
|
-
try {
|
|
866
|
-
// console.log("parsing json messages")
|
|
867
|
-
const json = JSON.parse(xmlhttp.responseText)
|
|
868
|
-
callback(null, json.result)
|
|
869
|
-
}
|
|
870
|
-
catch (err) {
|
|
871
|
-
console.error("parsing json messages ERROR", err)
|
|
872
|
-
callback(err, null)
|
|
873
|
-
}
|
|
874
|
-
}
|
|
875
|
-
};
|
|
876
|
-
xmlhttp.send(null);
|
|
877
|
-
}
|
|
878
|
-
|
|
879
|
-
static myrequest(options, callback, log) {
|
|
880
|
-
// url: options.url,
|
|
881
|
-
// headers: options.headers,
|
|
882
|
-
// data: options.data,
|
|
883
|
-
// method: options.method
|
|
884
|
-
if (log) {
|
|
885
|
-
console.log("HTTP Request:", options);
|
|
886
|
-
}
|
|
887
|
-
if (isBrowser()) {
|
|
888
|
-
let xmlhttp = new XMLHttpRequest();
|
|
889
|
-
xmlhttp.open(options.method, options.url, true);
|
|
890
|
-
Object.keys(options.headers).forEach((key) => {
|
|
891
|
-
xmlhttp.setRequestHeader(key, options.headers[key]);
|
|
892
|
-
});
|
|
893
|
-
xmlhttp.onreadystatechange = function() {
|
|
894
|
-
if (callback && xmlhttp.readyState == 4 && xmlhttp.status == 200 && xmlhttp.responseText) {
|
|
895
|
-
try {
|
|
896
|
-
const json = JSON.parse(xmlhttp.responseText)
|
|
897
|
-
callback(null, null, json)
|
|
898
|
-
}
|
|
899
|
-
catch (err) {
|
|
900
|
-
console.error("parsing json ERROR", err)
|
|
901
|
-
callback(err, null)
|
|
902
|
-
}
|
|
903
|
-
}
|
|
904
|
-
};
|
|
905
|
-
if (options.method === 'POST') {
|
|
906
|
-
xmlhttp.send(JSON.stringify(options.data));
|
|
907
|
-
}
|
|
908
|
-
else {
|
|
909
|
-
xmlhttp.send(null);
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
else {
|
|
913
|
-
axios(
|
|
914
|
-
{
|
|
915
|
-
url: options.url,
|
|
916
|
-
method: options.method,
|
|
917
|
-
data: options.data,
|
|
918
|
-
headers: options.headers
|
|
919
|
-
})
|
|
920
|
-
.then(function (response) {
|
|
921
|
-
if (log) {console.log("response.status:", response.status);}
|
|
922
|
-
if (callback) {
|
|
923
|
-
callback(null, response.headers, response.data);
|
|
924
|
-
}
|
|
925
|
-
})
|
|
926
|
-
.catch(function (error) {
|
|
927
|
-
console.error("Axios call error:", error);
|
|
928
|
-
if (callback) {
|
|
929
|
-
callback(error, null, null);
|
|
930
|
-
}
|
|
931
|
-
});
|
|
932
|
-
|
|
933
|
-
// request(
|
|
934
|
-
// {
|
|
935
|
-
// url: options.url,
|
|
936
|
-
// headers: options.headers,
|
|
937
|
-
// json: options.json,
|
|
938
|
-
// method: options.method
|
|
939
|
-
// },
|
|
940
|
-
// function(err, res, resbody) {
|
|
941
|
-
// if (log) {
|
|
942
|
-
// console.log("** For url:", options.url);
|
|
943
|
-
// console.log("** Options:", options);
|
|
944
|
-
// console.log("** Err:", err);
|
|
945
|
-
// console.log("** Response headers:\n", res.headers);
|
|
946
|
-
// console.log("** Response body:\n", res.body);
|
|
947
|
-
// }
|
|
948
|
-
// if (callback) {
|
|
949
|
-
// callback(err, res, resbody);
|
|
950
|
-
// }
|
|
951
|
-
// }
|
|
952
|
-
// );
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
connect(user_id, jwt, callback) {
|
|
958
|
-
this.user_id = user_id;
|
|
959
|
-
// console.log("userid:", this.user_id)
|
|
960
|
-
this.jwt = jwt
|
|
961
|
-
if (this.log) {
|
|
962
|
-
console.log("connecting user_id:", user_id)
|
|
963
|
-
console.log("using jwt token:", jwt)
|
|
964
|
-
}
|
|
965
|
-
|
|
966
|
-
if (this.client) {
|
|
967
|
-
this.client.end()
|
|
968
|
-
}
|
|
969
|
-
const presence_topic = 'apps/tilechat/users/' + this.user_id + '/presence/' + this.client_id
|
|
970
|
-
let options = {
|
|
971
|
-
keepalive: 10,
|
|
972
|
-
// protocolId: 'MQTT',
|
|
973
|
-
// protocolVersion: 4,
|
|
974
|
-
// clean: true,
|
|
975
|
-
reconnectPeriod: 1000,
|
|
976
|
-
// connectTimeout: 30 * 1000,
|
|
977
|
-
will: {
|
|
978
|
-
topic: presence_topic,
|
|
979
|
-
payload: '{"disconnected":true}',
|
|
980
|
-
qos: 1,
|
|
981
|
-
retain: true
|
|
982
|
-
},
|
|
983
|
-
clientId: this.client_id,
|
|
984
|
-
username: 'JWT',
|
|
985
|
-
password: jwt,
|
|
986
|
-
rejectUnauthorized: false
|
|
987
|
-
}
|
|
988
|
-
if (this.log) {console.log("starting mqtt connection with LWT on:", presence_topic, this.endpoint)}
|
|
989
|
-
// client = mqtt.connect('mqtt://127.0.0.1:15675/ws',options)
|
|
990
|
-
this.client = mqtt.connect(this.endpoint,options)
|
|
991
|
-
|
|
992
|
-
this.client.on('connect', // TODO if token is wrong it must reply with an error!
|
|
993
|
-
() => {
|
|
994
|
-
if (this.log) {console.log("Chat client connected. User:" + user_id)}
|
|
995
|
-
if (!this.connected) {
|
|
996
|
-
if (this.log) {console.log("Chat client first connection for:" + user_id)}
|
|
997
|
-
this.connected = true
|
|
998
|
-
this.start( () => {
|
|
999
|
-
callback();
|
|
1000
|
-
});
|
|
1001
|
-
}
|
|
1002
|
-
}
|
|
1003
|
-
);
|
|
1004
|
-
this.client.on('reconnect',
|
|
1005
|
-
() => {
|
|
1006
|
-
if (this.log) {console.log("Chat client reconnect event");}
|
|
1007
|
-
}
|
|
1008
|
-
);
|
|
1009
|
-
this.client.on('close',
|
|
1010
|
-
() => {
|
|
1011
|
-
if (this.log) {console.log("Chat client close event");}
|
|
1012
|
-
}
|
|
1013
|
-
);
|
|
1014
|
-
this.client.on('offline',
|
|
1015
|
-
() => {
|
|
1016
|
-
if (this.log) {console.log("Chat client offline event");}
|
|
1017
|
-
}
|
|
1018
|
-
);
|
|
1019
|
-
this.client.on('error',
|
|
1020
|
-
(error) => {
|
|
1021
|
-
console.error("Chat client error event", error);
|
|
1022
|
-
}
|
|
1023
|
-
);
|
|
1024
|
-
}
|
|
1025
|
-
|
|
1026
|
-
close(callback) {
|
|
1027
|
-
if (this.topic_inbox) {
|
|
1028
|
-
this.client.unsubscribe(this.topic_inbox, (err) => {
|
|
1029
|
-
if (this.log) {console.log("unsubscribed from", this.topic_inbox);}
|
|
1030
|
-
this.client.end(() => {
|
|
1031
|
-
this.connected = false
|
|
1032
|
-
// reset all subscriptions
|
|
1033
|
-
this.onConversationAddedCallbacks = new Map();
|
|
1034
|
-
this.onConversationUpdatedCallbacks = new Map();
|
|
1035
|
-
this.onConversationDeletedCallbacks = new Map();
|
|
1036
|
-
this.onArchivedConversationAddedCallbacks = new Map();
|
|
1037
|
-
this.onArchivedConversationDeletedCallbacks = new Map();
|
|
1038
|
-
this.onMessageAddedCallbacks = new Map();
|
|
1039
|
-
this.onMessageUpdatedCallbacks = new Map();
|
|
1040
|
-
this.onGroupUpdatedCallbacks = new Map();
|
|
1041
|
-
this.callbackHandlers = new Map();
|
|
1042
|
-
this.on_message_handler = null
|
|
1043
|
-
this.topic_inbox = null;
|
|
1044
|
-
if (callback) {
|
|
1045
|
-
callback();
|
|
1046
|
-
}
|
|
1047
|
-
})
|
|
1048
|
-
});
|
|
1049
|
-
}
|
|
1050
|
-
}
|
|
1051
|
-
|
|
1052
|
-
uuidv4() {
|
|
1053
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
1054
|
-
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
1055
|
-
return v.toString(16);
|
|
1056
|
-
});
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1059
|
-
|
|
1060
|
-
function isBrowser() {
|
|
1061
|
-
return true;
|
|
1062
|
-
// return false;
|
|
1063
|
-
}
|
|
1064
|
-
|
|
1065
|
-
export { Chat21Client }; // Browser
|
|
1066
|
-
// module.exports = { Chat21Client };
|