-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
314 lines (261 loc) · 8.27 KB
/
app.js
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
import {
setStorage,
getStorage,
fetchData,
setActiveTarget,
getActiveTarget,
setActiveUser,
getActiveUser,
displayDefaultActiveTarget,
init,
} from './js/helper.js';
import {
Conversation,
ChatDisplay
} from './js/chat/index.js'
let USERS = [];
let LASTCHATS = {};
let UNREADS = [];
const API = 'web-socket-chat-production.up.railway.app';
const PROTOCOL = 'https';
const RENDER_EVENT = 'RENDER_EVENT';
const chatDisplay = new ChatDisplay('#chatDisplay');
const conversation = new Conversation({ connectUrl: `${API}/ws-connect`, chatUrl: `${API}/ws-chat`, protocol: 'wss' });
const client = conversation.Client;
// start conversation
conversation.start(({ connectSocket, chatSocket }) => {
chatDisplay.showConnecting();
// socket on open
connectSocket.addEventListener('open', function(e) {
if (client.isOffline) { // for reconnect
client
.setId(getStorage('id'))
.from(getActiveUser())
.to(getActiveTarget())
.connect();
}
console.log(`connect-socket [open]: web socket connection established.`);
});
// socket on message
connectSocket.addEventListener('message', async function(e) {
const response = JSON.parse(e.data);
const { status, type, message, data } = response
if (type == 'connect') { // on user connect
const { user, id } = data;
setStorage('id', id); // save connection id
client.setId(id);
client.isOffline = false;
chatDisplay.showOnline();
getChats(); // set current active chat
await getLastChatsFor(client.user); // set last chat thumb for active user
document.dispatchEvent(new Event(RENDER_EVENT));
console.log(`connect-socket [message]: ${message} for user ${user}.`);
}
if (type == 'message') { // on message sent or received
const { chat } = data;
if (message == 'sent') {
insertLastChat(chat.target, chat); // push last chat to related target
console.log(`connect-socket [message]: Message sent to ${chat.target}`);
}
if (message == 'received') {
if ( // target currently active, insert to chat container
client.user === chat.target &&
getActiveTarget() === chat.user &&
getActiveTarget() !== client.user
){
chatDisplay.insertMessage('left', {
user: chat.user,
message: chat.message,
time: chat.time,
});
}
if ( // target not active, notify the user, push to unread
client.user === chat.target &&
getActiveTarget() !== chat.user &&
getActiveTarget !== client.user
){
chatDisplay.notify({
user: chat.user,
message: chat.message,
time: chat.time,
});
insertUnread(chat.id);
}
insertLastChat(chat.user, chat); // push last chat to related user
console.log(`connect-socket [message]: Message received from ${chat.user}`);
}
document.dispatchEvent(new Event(RENDER_EVENT));
}
});
// socket on close
connectSocket.addEventListener('close', function(e) {
client.isOffline = true;
chatDisplay.showOffline();
console.log(`connect-socket [close]: connection died, because ${e.reason}`);
chatSocket.close(1000, e.reason);
});
// socket on close
chatSocket.addEventListener('close', function(e) {
console.log(`chat-socket [close]: connection died, because ${e.reason}`);
});
});
function insertUnread(chat) {
UNREADS.push(chat);
}
function removeUnread(target) {
if (Object.keys(LASTCHATS).length <= 0) return;
let targetChats = LASTCHATS.to[target] || [];
let removedChatids = targetChats.map(({ id }) => id);
UNREADS = UNREADS.filter((unreadId) => !removedChatids.includes(unreadId));
}
function insertLastChat(target, chat) {
if (LASTCHATS.to[target].find(({ id }) => id === chat.id)) return;
LASTCHATS.to[target].push(chat);
}
function getChats() {
chatDisplay
.changeHeaderLeft(`To: ${client.target || ''}`)
.changeHeaderRight(`I'm <strong>${client.user || ''}</strong>`)
.setUserAndTarget(client.user, client.target);
if (client.isOffline) return;
chatDisplay
.showLoading()
.getMessages(`${PROTOCOL}://${API}/chats`, {
user: client.user,
target: client.target
})
.then((messages) => {
chatDisplay
.hideLoading()
.display(messages)
});
}
async function getUsers() {
try {
$('#refreshContact i').addClass('rotate');
const response = await fetchData(`${PROTOCOL}://${API}/users`);
const { data } = response;
USERS = data.users;
} catch (error) {
console.log(error.message)
}
$('#refreshContact i').removeClass('rotate');
}
async function getLastChatsFor(user) {
try {
const { data } = await fetchData(`${PROTOCOL}://${API}/last-chats?forUser=${user}`);
const toUser = {};
data.lastChats.forEach(lastChat => { toUser[lastChat.name] = lastChat.chats });
LASTCHATS = { from: user, to: toUser };
} catch (error) {
console.log(error.message);
}
}
$(document).ready(async function (e) {
// event listeners
document.addEventListener(RENDER_EVENT, function () {
init(USERS, LASTCHATS, UNREADS);
setActiveUser(getStorage('user'));
setActiveTarget(getStorage('target'));
displayDefaultActiveTarget(getStorage('target'));
$('.contact-item').on('click', function (e) {
const target = $(this).find('input').val();
$('.contact-item').removeClass('active');
$(this).addClass('active');
setActiveTarget(target);
setStorage('target', target);
client.to(target);
getChats();
removeUnread(client.target);
document.dispatchEvent(new Event(RENDER_EVENT));
});
});
$('#openSidebar').on('click', function (e) {
$('.sidebar').addClass('sidebar--open');
});
$('#closeSidebar').on('click', function (e) {
$('.sidebar').removeClass('sidebar--open');
});
$('#refreshContact').on('click', async function(e) {
e.preventDefault();
await getUsers();
document.dispatchEvent(new Event(RENDER_EVENT));
})
$('#from').on('change', function (e) {
setStorage('user', e.target.value);
if (client.isOffline) {
$('#reconnect').click();
return;
}
client
.from(e.target.value)
.connect();
});
$('#reconnect').on('click', function(e) {
client
.from(getActiveUser())
.to(getActiveTarget())
.reconnect();
});
$('#addUserForm').on('submit', async function (e) {
e.preventDefault();
const data = $(this).serializeArray()[0];
const user = data.value;
if (!user) return;
try {
const response = await fetchData(`${PROTOCOL}://${API}/users`, {
method: 'POST',
body: JSON.stringify({ user })
});
if (response.status != 'success') throw new Error(response.message);
// add users if not error
USERS = [...USERS, response.data.user];
Swal.fire({
position: 'top-end',
icon: 'success',
title: response.message,
showConfirmButton: false,
timer: 1500
});
} catch (error) {
Swal.fire({
position: 'top-end',
icon: 'error',
title: error.message,
showConfirmButton: false,
timer: 1500
});
}
$(this).find('input').val(''); // reset form add user
await getLastChatsFor(client.user); //get last chat for new added user
document.dispatchEvent(new Event(RENDER_EVENT)); // rerender
});
$('#chatForm').on('submit', function (e) {
e.preventDefault();
const message = $(this).serializeArray()[0].value;
const time = +new Date();
if (!message || !client.user || !client.target) return;
// insert message
chatDisplay.insertMessage('right', { message, time });
// clear chat form
$(this).find('input').val('');
// sent to target
try {
client
.to(getActiveTarget())
.send(message, time);
} catch(error) {
console.log(error.message);
}
});
// initial data
await getUsers(); // get users
// render to display users on UI
document.dispatchEvent(new Event(RENDER_EVENT));
// set client then connect
client
.setId(getStorage('id'))
.from(getActiveUser())
.to(getActiveTarget())
.connect();
});