-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
321 lines (273 loc) · 9.44 KB
/
background.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
315
316
317
318
319
320
321
const browser = chromePromises()
let ws, chatMessages, notifications, users
main()
function main() {
reset()
if (navigator.onLine) {
connect()
}
}
chrome.idle.onStateChanged.addListener(state => {
console.log(`state=${state}`)
if (state === 'active' && navigator.onLine && (!ws || ws.readyState === WebSocket.CLOSED)) {
connect()
}
})
chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName === 'local' && Object.keys(changes).includes('organization')) {
log('organization_changed', changes.organization.newValue)
ws.close()
tryConnectWithOrganization(changes.organization.newValue)
}
})
function reset() {
localStorage.setItem('unreadCount', 0)
chrome.storage.local.get('notifications', items => notifications = items.notifications || {})
chatMessages = []
users = {}
chrome.storage.local.set({chatMessages, users})
chrome.storage.local.remove('info')
chrome.browserAction.setBadgeText({text: '!'})
chrome.browserAction.setTitle({title: 'Disconnected'})
}
function connect() {
chrome.storage.local.get('organization', ({organization}) => tryConnectWithOrganization(organization))
}
async function tryConnectWithOrganization(organization) {
try {
await connectWithOrganization(organization)
} catch (e) {
chrome.notifications.create('reconnect', {
type: 'basic',
iconUrl: 'icon.png',
title: 'Error',
message: e.message,
requireInteraction: true
})
}
}
async function connectWithOrganization(organization) {
const url = `https://${organization}.ryver.com/api/1/odata.svc/Ryver.Info()?$format=json`
const response = await fetch(url, {credentials: 'include'})
if (! response.ok) {
throw new Error(response.statusText)
}
const info = await response.json().then(json => json.d)
chrome.storage.local.set({info})
ws = createWebSocket(info)
checkUnreadTabs(organization)
}
async function checkUnreadTabs(organization) {
const unreadTabs = await getUnreadTabs(organization)
if (unreadTabs.length > 0) {
const descriptors = unreadTabs
.sort(orderByDesc(unreadTab => unreadTab.lastMessageDate))
.map(unreadTab => unreadTab.entity.__descriptor)
.join(' • ');
log('unreadTabs', descriptors, unreadTabs)
chrome.storage.local.set({unreadTabs})
chrome.notifications.clear('unreadTabs')
chrome.notifications.create('unreadTabs', {
type: 'basic',
iconUrl: 'icon.png',
title: unreadTabs.length + ' unread',
message: descriptors,
requireInteraction: true
})
}
}
async function getTabsState(organization) {
const url = `https://${organization}.ryver.com/api/1/odata.svc/Tabs.GetState()`
return await fetch(url, {credentials: 'include'}).then(res => res.json()).then(json => json.d)
}
async function getUnreadTabs(organization) {
const tabsState = await getTabsState(organization)
return Object.values(tabsState).filter(tab => tab.lastMessageId > tab.lastReadId)
}
function minimizeInfo({forums, me, teams, users}) {
const minimizeFields = ({avatarUrl, descriptor, id, jid}) => ({avatarUrl, descriptor, id, jid})
return {
me: {id: me.id},
forums: forums.map(minimizeFields),
teams: teams.map(minimizeFields),
users: users.map(minimizeFields)
}
}
function createWebSocket(info) {
const {findEntity, findEntities} = ryverLibrary(info)
const ws = new WebSocket('wss://prdchat.ryver.com/apt38/1/ratatoskr')
ws.onopen = onOpen
ws.onclose = onClose
ws.onmessage = onMessage
return ws
async function onOpen() {
console.log('ws open')
const cookie = await browser.cookies.get({ url: 'https://mrlabs.ryver.com', name: 'PHPSESSID' })
const sessionId = decodeURIComponent(cookie.value)
ws.send(JSON.stringify({
id: nextId(),
type: 'auth',
authorization: 'Session ' + sessionId,
agent: 'Ryver',
resource: 'Contatta-1496207329078'
}))
}
async function onClose() {
console.log('ws closed')
reset()
const { retryCount = 0 } = await browser.storage.local.get('retryCount')
const state = await browser.idle.queryState(60)
console.log(`retry state=${state} navigator.onLine=${navigator.onLine}`)
if (retryCount < 2 && navigator.onLine) { // TODO: need to check state?
chrome.storage.local.set({ retryCount: retryCount + 1 })
connect()
} else {
chrome.notifications.create('reconnect', {
type: 'basic',
iconUrl: 'icon.png',
title: 'WS Closed',
message: 'Reconnect?',
requireInteraction: true
})
}
}
function onMessage(event) {
const data = JSON.parse(event.data)
const {type} = data
if (type === 'ack') {
log('ack', data)
chrome.storage.local.remove('retryCount')
chrome.notifications.clear('reconnect')
chrome.browserAction.setBadgeText({text: ''})
chrome.browserAction.setTitle({title: 'Connected'})
ws.send(JSON.stringify({
presence: 'unavailable',
type: 'presence_change'
}))
}
else if (type === 'presence_change') {
const {client, from: fromId, presence, received} = data
const from = findEntity(fromId) || {descriptor: fromId}
const {descriptor} = from
log([dateToString(new Date(received)), type, descriptor, presence, client].join(' '))
users[fromId] = {descriptor, received, presence, from}
chrome.storage.local.set({users})
}
else if (type === 'user_typing') {
const [from, to] = findEntities(data.from, data.to)
log([type, from.descriptor, '➔', to.descriptor, ':', data.state].join(' '))
addChatMessage(data)
chrome.notifications.create(data.from, {
type: 'basic',
iconUrl: from.avatarUrl,
title: [from.descriptor, '➔', to.descriptor].join(' '),
message: [type, ':', data.state].join(' ')
})
}
else if (type === 'chat') {
const [from, to] = findEntities(data.from, data.to)
log([type, from.descriptor, '➔', to.descriptor, ':', data.text].join(' '), data)
addChatMessage(data)
const fromDescriptor = data.createSource ? data.createSource.displayName : from.descriptor
const fromAvatarUrl = data.createSource ? data.createSource.avatar : from.avatarUrl
notify({
title: [fromDescriptor, '➔', to.descriptor].join(' '),
message: data.text,
iconUrl: fromAvatarUrl
}, storeNotificationMetadata({from, to}))
}
else if (['chat_deleted', 'chat_updated'].includes(type)) {
log(data)
addChatMessage(data)
}
else if (type === 'mark_read') {
log(data)
}
else if (type === 'voice_change') {
log(data)
}
else {
if (!['event'].includes(type)) {
log(data)
notify({
title: 'unhandled event: ' + type,
message: event.data
})
}
}
function storeNotificationMetadata(metadata) {
return notificationId => {
notifications[notificationId] = metadata
chrome.storage.local.set({notifications})
}
}
}
}
function nextId() {
return 'BkD971TWZ'
}
function addChatMessage(data) {
const time = new Date().toISOString()
chatMessages.push({time, data})
chrome.storage.local.set({chatMessages})
const oldUnreadCount = parseInt(localStorage.getItem('unreadCount'))
const newUnreadCount = oldUnreadCount + 1
localStorage.setItem('unreadCount', newUnreadCount)
chrome.browserAction.setBadgeText({text: newUnreadCount.toString()})
}
function notify({type = 'basic', iconUrl, title, message}, callback) {
chrome.notifications.create({type, iconUrl: iconUrl || 'icon.png', title, message}, callback)
}
chrome.notifications.onClicked.addListener(handleNotificationClick)
chrome.notifications.onButtonClicked.addListener(handleNotificationClick)
// chrome.notifications.onClosed.addListener((notificationId, byUser) => {
// console.log({notificationId, byUser})
// })
function getEntityType(type) {
switch (type) {
case 'Entity.Forum':
return 'forums'
case 'Entity.Workroom':
return 'teams'
default:
return 'users'
}
}
async function handleNotificationClick(notificationId) {
if (notificationId === 'unreadTabs') {
const {organization, unreadTabs} = await browser.storage.local.get(['organization', 'unreadTabs'])
const {id, __metadata: {type}} = unreadTabs[0].entity
const entityType = getEntityType(type)
const url = `https://${organization}.ryver.com/index.html#${entityType}/${id}`
chrome.tabs.create({url})
chrome.notifications.clear(notificationId)
} else if (notificationId === 'reconnect') {
connect()
chrome.notifications.clear(notificationId)
} else {
const {info, notifications, organization} = await browser.storage.local.get(['info', 'notifications', 'organization'])
const notification = notifications[notificationId]
if (notification) {
const target = notification.to.entityType === 'teams' || notification.from.id === info.me.id ? notification.to : notification.from
const {entityType, id} = target
const url = `https://${organization}.ryver.com/index.html#${entityType}/${id}`
chrome.tabs.create({url})
} else {
const url = `https://${organization}.ryver.com`
chrome.tabs.create({url})
}
}
}
function log(...message) {
console.log(...message)
}
window.addEventListener('error', event => {
console.log('error', event)
chrome.notifications.create('error', {
type: 'basic',
iconUrl: 'icon.png',
title: 'Error',
message: event.message,
requireInteraction: true
})
})