@knocklabs/client 0.8.3 → 0.8.4
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/cjs/clients/feed/feed.js +376 -92
- package/dist/cjs/clients/feed/feed.js.map +1 -1
- package/dist/cjs/clients/feed/store.js +23 -16
- package/dist/cjs/clients/feed/store.js.map +1 -1
- package/dist/esm/clients/feed/feed.js +247 -43
- package/dist/esm/clients/feed/feed.js.map +1 -1
- package/dist/esm/clients/feed/store.js +20 -15
- package/dist/esm/clients/feed/store.js.map +1 -1
- package/dist/types/clients/feed/feed.d.ts +5 -0
- package/dist/types/clients/feed/feed.d.ts.map +1 -1
- package/dist/types/clients/feed/store.d.ts.map +1 -1
- package/dist/types/clients/feed/types.d.ts +2 -1
- package/dist/types/clients/feed/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -43,10 +43,10 @@ class Feed {
|
|
|
43
43
|
});
|
|
44
44
|
this.defaultOptions = _objectSpread(_objectSpread({}, feedClientDefaults), options);
|
|
45
45
|
this.channel = this.apiClient.socket.channel("feeds:".concat(this.userFeedId), this.defaultOptions);
|
|
46
|
-
this.channel.on("new-message", resp => this.onNewMessageReceived(resp)); // Attempt to bind to listen to other events from this feed in different tabs
|
|
47
|
-
//
|
|
46
|
+
this.channel.on("new-message", resp => this.onNewMessageReceived(resp)); // Attempt to bind to listen to other events from this feed in different tabs
|
|
47
|
+
// Note: here we ensure `self` is available (it's not in server rendered envs)
|
|
48
48
|
|
|
49
|
-
this.broadcastChannel = "BroadcastChannel" in self ? new BroadcastChannel("knock:feed:".concat(this.userFeedId)) : null;
|
|
49
|
+
this.broadcastChannel = self && "BroadcastChannel" in self ? new BroadcastChannel("knock:feed:".concat(this.userFeedId)) : null;
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
52
|
* Cleans up a feed instance by destroying the store and disconnecting
|
|
@@ -92,6 +92,9 @@ class Feed {
|
|
|
92
92
|
case "items:unseen":
|
|
93
93
|
case "items:read":
|
|
94
94
|
case "items:unread":
|
|
95
|
+
case "items:all_read":
|
|
96
|
+
case "items:all_seen":
|
|
97
|
+
case "items:all_archived":
|
|
95
98
|
// When items are updated in any other tab, simply refetch to get the latest state
|
|
96
99
|
// to make sure that the state gets updated accordingly. In the future here we could
|
|
97
100
|
// maybe do this optimistically without the fetch.
|
|
@@ -133,41 +136,159 @@ class Feed {
|
|
|
133
136
|
})();
|
|
134
137
|
}
|
|
135
138
|
|
|
136
|
-
|
|
139
|
+
markAllAsSeen() {
|
|
137
140
|
var _this2 = this;
|
|
138
141
|
|
|
139
142
|
return _asyncToGenerator(function* () {
|
|
140
|
-
|
|
143
|
+
// To mark all of the messages as seen we:
|
|
144
|
+
// 1. Optimistically update *everything* we have in the store
|
|
145
|
+
// 2. We decrement the `unseen_count` to zero optimistically
|
|
146
|
+
// 3. We issue the API call to the endpoint
|
|
147
|
+
//
|
|
148
|
+
// Note: there is the potential for a race condition here because the bulk
|
|
149
|
+
// update is an async method, so if a new message comes in during this window before
|
|
150
|
+
// the update has been processed we'll effectively reset the `unseen_count` to be what it was.
|
|
151
|
+
//
|
|
152
|
+
// Note: here we optimistically handle the case whereby the feed is scoped to show only `unseen`
|
|
153
|
+
// items by removing everything from view.
|
|
154
|
+
var {
|
|
155
|
+
getState,
|
|
156
|
+
setState
|
|
157
|
+
} = _this2.store;
|
|
158
|
+
var {
|
|
159
|
+
metadata,
|
|
160
|
+
items
|
|
161
|
+
} = getState();
|
|
162
|
+
var isViewingOnlyUnseen = _this2.defaultOptions.status === "unseen"; // If we're looking at the unseen view, then we want to remove all of the items optimistically
|
|
163
|
+
// from the store given that nothing should be visible. We do this by resetting the store state
|
|
164
|
+
// and setting the current metadata counts to 0
|
|
165
|
+
|
|
166
|
+
if (isViewingOnlyUnseen) {
|
|
167
|
+
setState(store => store.resetStore(_objectSpread(_objectSpread({}, metadata), {}, {
|
|
168
|
+
total_count: 0,
|
|
169
|
+
unseen_count: 0
|
|
170
|
+
})));
|
|
171
|
+
} else {
|
|
172
|
+
// Otherwise we want to update the metadata and mark all of the items in the store as seen
|
|
173
|
+
setState(store => store.setMetadata(_objectSpread(_objectSpread({}, metadata), {}, {
|
|
174
|
+
unseen_count: 0
|
|
175
|
+
})));
|
|
176
|
+
var attrs = {
|
|
177
|
+
seen_at: new Date().toISOString()
|
|
178
|
+
};
|
|
179
|
+
var itemIds = items.map(item => item.id);
|
|
180
|
+
setState(store => store.setItemAttrs(itemIds, attrs));
|
|
181
|
+
} // Issue the API request to the bulk status change API
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
var result = yield _this2.makeBulkStatusUpdate("seen");
|
|
185
|
+
|
|
186
|
+
_this2.broadcaster.emit("items:all_seen", {
|
|
187
|
+
items
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
_this2.broadcastOverChannel("items:all_seen", {
|
|
191
|
+
items
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
return result;
|
|
195
|
+
})();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
markAsUnseen(itemOrItems) {
|
|
199
|
+
var _this3 = this;
|
|
200
|
+
|
|
201
|
+
return _asyncToGenerator(function* () {
|
|
202
|
+
_this3.optimisticallyPerformStatusUpdate(itemOrItems, "unseen", {
|
|
141
203
|
seen_at: null
|
|
142
204
|
}, "unseen_count");
|
|
143
205
|
|
|
144
|
-
return
|
|
206
|
+
return _this3.makeStatusUpdate(itemOrItems, "unseen");
|
|
145
207
|
})();
|
|
146
208
|
}
|
|
147
209
|
|
|
148
210
|
markAsRead(itemOrItems) {
|
|
149
|
-
var
|
|
211
|
+
var _this4 = this;
|
|
150
212
|
|
|
151
213
|
return _asyncToGenerator(function* () {
|
|
152
214
|
var now = new Date().toISOString();
|
|
153
215
|
|
|
154
|
-
|
|
216
|
+
_this4.optimisticallyPerformStatusUpdate(itemOrItems, "read", {
|
|
155
217
|
read_at: now
|
|
156
218
|
}, "unread_count");
|
|
157
219
|
|
|
158
|
-
return
|
|
220
|
+
return _this4.makeStatusUpdate(itemOrItems, "read");
|
|
221
|
+
})();
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
markAllAsRead() {
|
|
225
|
+
var _this5 = this;
|
|
226
|
+
|
|
227
|
+
return _asyncToGenerator(function* () {
|
|
228
|
+
// To mark all of the messages as read we:
|
|
229
|
+
// 1. Optimistically update *everything* we have in the store
|
|
230
|
+
// 2. We decrement the `unread_count` to zero optimistically
|
|
231
|
+
// 3. We issue the API call to the endpoint
|
|
232
|
+
//
|
|
233
|
+
// Note: there is the potential for a race condition here because the bulk
|
|
234
|
+
// update is an async method, so if a new message comes in during this window before
|
|
235
|
+
// the update has been processed we'll effectively reset the `unread_count` to be what it was.
|
|
236
|
+
//
|
|
237
|
+
// Note: here we optimistically handle the case whereby the feed is scoped to show only `unread`
|
|
238
|
+
// items by removing everything from view.
|
|
239
|
+
var {
|
|
240
|
+
getState,
|
|
241
|
+
setState
|
|
242
|
+
} = _this5.store;
|
|
243
|
+
var {
|
|
244
|
+
metadata,
|
|
245
|
+
items
|
|
246
|
+
} = getState();
|
|
247
|
+
var isViewingOnlyUnread = _this5.defaultOptions.status === "unread"; // If we're looking at the unread view, then we want to remove all of the items optimistically
|
|
248
|
+
// from the store given that nothing should be visible. We do this by resetting the store state
|
|
249
|
+
// and setting the current metadata counts to 0
|
|
250
|
+
|
|
251
|
+
if (isViewingOnlyUnread) {
|
|
252
|
+
setState(store => store.resetStore(_objectSpread(_objectSpread({}, metadata), {}, {
|
|
253
|
+
total_count: 0,
|
|
254
|
+
unread_count: 0
|
|
255
|
+
})));
|
|
256
|
+
} else {
|
|
257
|
+
// Otherwise we want to update the metadata and mark all of the items in the store as seen
|
|
258
|
+
setState(store => store.setMetadata(_objectSpread(_objectSpread({}, metadata), {}, {
|
|
259
|
+
unread_count: 0
|
|
260
|
+
})));
|
|
261
|
+
var attrs = {
|
|
262
|
+
read_at: new Date().toISOString()
|
|
263
|
+
};
|
|
264
|
+
var itemIds = items.map(item => item.id);
|
|
265
|
+
setState(store => store.setItemAttrs(itemIds, attrs));
|
|
266
|
+
} // Issue the API request to the bulk status change API
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
var result = yield _this5.makeBulkStatusUpdate("read");
|
|
270
|
+
|
|
271
|
+
_this5.broadcaster.emit("items:all_read", {
|
|
272
|
+
items
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
_this5.broadcastOverChannel("items:all_read", {
|
|
276
|
+
items
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
return result;
|
|
159
280
|
})();
|
|
160
281
|
}
|
|
161
282
|
|
|
162
283
|
markAsUnread(itemOrItems) {
|
|
163
|
-
var
|
|
284
|
+
var _this6 = this;
|
|
164
285
|
|
|
165
286
|
return _asyncToGenerator(function* () {
|
|
166
|
-
|
|
287
|
+
_this6.optimisticallyPerformStatusUpdate(itemOrItems, "unread", {
|
|
167
288
|
read_at: null
|
|
168
289
|
}, "unread_count");
|
|
169
290
|
|
|
170
|
-
return
|
|
291
|
+
return _this6.makeStatusUpdate(itemOrItems, "unread");
|
|
171
292
|
})();
|
|
172
293
|
}
|
|
173
294
|
/*
|
|
@@ -179,15 +300,15 @@ class Feed {
|
|
|
179
300
|
|
|
180
301
|
|
|
181
302
|
markAsArchived(itemOrItems) {
|
|
182
|
-
var
|
|
303
|
+
var _this7 = this;
|
|
183
304
|
|
|
184
305
|
return _asyncToGenerator(function* () {
|
|
185
306
|
var {
|
|
186
307
|
getState,
|
|
187
308
|
setState
|
|
188
|
-
} =
|
|
309
|
+
} = _this7.store;
|
|
189
310
|
var state = getState();
|
|
190
|
-
var shouldOptimisticallyRemoveItems =
|
|
311
|
+
var shouldOptimisticallyRemoveItems = _this7.defaultOptions.archived === "exclude";
|
|
191
312
|
var normalizedItems = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];
|
|
192
313
|
var itemIds = normalizedItems.map(item => item.id);
|
|
193
314
|
/*
|
|
@@ -235,19 +356,65 @@ class Feed {
|
|
|
235
356
|
});
|
|
236
357
|
}
|
|
237
358
|
|
|
238
|
-
return
|
|
359
|
+
return _this7.makeStatusUpdate(itemOrItems, "archived");
|
|
360
|
+
})();
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
markAllAsArchived() {
|
|
364
|
+
var _this8 = this;
|
|
365
|
+
|
|
366
|
+
return _asyncToGenerator(function* () {
|
|
367
|
+
// Note: there is the potential for a race condition here because the bulk
|
|
368
|
+
// update is an async method, so if a new message comes in during this window before
|
|
369
|
+
// the update has been processed we'll effectively reset the `unseen_count` to be what it was.
|
|
370
|
+
var {
|
|
371
|
+
setState,
|
|
372
|
+
getState
|
|
373
|
+
} = _this8.store;
|
|
374
|
+
var {
|
|
375
|
+
items
|
|
376
|
+
} = getState(); // Here if we're looking at a feed that excludes all of the archived items by default then we
|
|
377
|
+
// will want to optimistically remove all of the items from the feed as they are now all excluded
|
|
378
|
+
|
|
379
|
+
var shouldOptimisticallyRemoveItems = _this8.defaultOptions.archived === "exclude";
|
|
380
|
+
|
|
381
|
+
if (shouldOptimisticallyRemoveItems) {
|
|
382
|
+
// Reset the store to clear out all of items and reset the badge count
|
|
383
|
+
setState(store => store.resetStore());
|
|
384
|
+
} else {
|
|
385
|
+
// Mark all the entries being updated as archived either way so the state is correct
|
|
386
|
+
setState(store => {
|
|
387
|
+
var itemIds = items.map(i => i.id);
|
|
388
|
+
store.setItemAttrs(itemIds, {
|
|
389
|
+
archived_at: new Date().toISOString()
|
|
390
|
+
});
|
|
391
|
+
});
|
|
392
|
+
} // Issue the API request to the bulk status change API
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
var result = yield _this8.makeBulkStatusUpdate("archive");
|
|
396
|
+
|
|
397
|
+
_this8.broadcaster.emit("items:all_archived", {
|
|
398
|
+
items
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
_this8.broadcastOverChannel("items:all_archived", {
|
|
402
|
+
items
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
return result;
|
|
239
406
|
})();
|
|
240
407
|
}
|
|
241
408
|
|
|
242
409
|
markAsUnarchived(itemOrItems) {
|
|
243
|
-
var
|
|
410
|
+
var _this9 = this;
|
|
244
411
|
|
|
245
412
|
return _asyncToGenerator(function* () {
|
|
246
|
-
|
|
413
|
+
_this9.optimisticallyPerformStatusUpdate(itemOrItems, "unarchived", {
|
|
247
414
|
archived_at: null
|
|
248
415
|
});
|
|
249
416
|
|
|
250
|
-
return
|
|
417
|
+
return _this9.makeStatusUpdate(itemOrItems, "unarchived");
|
|
251
418
|
})();
|
|
252
419
|
}
|
|
253
420
|
/* Fetches the feed content, appending it to the store */
|
|
@@ -255,14 +422,14 @@ class Feed {
|
|
|
255
422
|
|
|
256
423
|
fetch() {
|
|
257
424
|
var _arguments = arguments,
|
|
258
|
-
|
|
425
|
+
_this10 = this;
|
|
259
426
|
|
|
260
427
|
return _asyncToGenerator(function* () {
|
|
261
428
|
var options = _arguments.length > 0 && _arguments[0] !== undefined ? _arguments[0] : {};
|
|
262
429
|
var {
|
|
263
430
|
setState,
|
|
264
431
|
getState
|
|
265
|
-
} =
|
|
432
|
+
} = _this10.store;
|
|
266
433
|
var {
|
|
267
434
|
networkStatus
|
|
268
435
|
} = getState(); // If there's an existing request in flight, then do nothing
|
|
@@ -278,16 +445,16 @@ class Feed {
|
|
|
278
445
|
return store.setNetworkStatus((_options$__loadingTyp = options.__loadingType) !== null && _options$__loadingTyp !== void 0 ? _options$__loadingTyp : NetworkStatus.loading);
|
|
279
446
|
}); // Always include the default params, if they have been set
|
|
280
447
|
|
|
281
|
-
var queryParams = _objectSpread(_objectSpread(_objectSpread({},
|
|
448
|
+
var queryParams = _objectSpread(_objectSpread(_objectSpread({}, _this10.defaultOptions), options), {}, {
|
|
282
449
|
// Unset options that should not be sent to the API
|
|
283
450
|
__loadingType: undefined,
|
|
284
451
|
__fetchSource: undefined,
|
|
285
452
|
__experimentalCrossBrowserUpdates: undefined
|
|
286
453
|
});
|
|
287
454
|
|
|
288
|
-
var result = yield
|
|
455
|
+
var result = yield _this10.apiClient.makeRequest({
|
|
289
456
|
method: "GET",
|
|
290
|
-
url: "/v1/users/".concat(
|
|
457
|
+
url: "/v1/users/".concat(_this10.knock.userId, "/feeds/").concat(_this10.feedId),
|
|
291
458
|
params: queryParams
|
|
292
459
|
});
|
|
293
460
|
|
|
@@ -322,7 +489,7 @@ class Feed {
|
|
|
322
489
|
} // Legacy `messages.new` event, should be removed in a future version
|
|
323
490
|
|
|
324
491
|
|
|
325
|
-
|
|
492
|
+
_this10.broadcast("messages.new", response); // Broadcast the appropriate event type depending on the fetch source
|
|
326
493
|
|
|
327
494
|
|
|
328
495
|
var feedEventType = options.__fetchSource === "socket" ? "items.received.realtime" : "items.received.page";
|
|
@@ -332,7 +499,7 @@ class Feed {
|
|
|
332
499
|
event: feedEventType
|
|
333
500
|
};
|
|
334
501
|
|
|
335
|
-
|
|
502
|
+
_this10.broadcast(eventPayload.event, eventPayload);
|
|
336
503
|
|
|
337
504
|
return {
|
|
338
505
|
data: response,
|
|
@@ -342,13 +509,13 @@ class Feed {
|
|
|
342
509
|
}
|
|
343
510
|
|
|
344
511
|
fetchNextPage() {
|
|
345
|
-
var
|
|
512
|
+
var _this11 = this;
|
|
346
513
|
|
|
347
514
|
return _asyncToGenerator(function* () {
|
|
348
515
|
// Attempts to fetch the next page of results (if we have any)
|
|
349
516
|
var {
|
|
350
517
|
getState
|
|
351
|
-
} =
|
|
518
|
+
} = _this11.store;
|
|
352
519
|
var {
|
|
353
520
|
pageInfo
|
|
354
521
|
} = getState();
|
|
@@ -358,7 +525,7 @@ class Feed {
|
|
|
358
525
|
return;
|
|
359
526
|
}
|
|
360
527
|
|
|
361
|
-
|
|
528
|
+
_this11.fetch({
|
|
362
529
|
after: pageInfo.after,
|
|
363
530
|
__loadingType: NetworkStatus.fetchMore
|
|
364
531
|
});
|
|
@@ -371,7 +538,7 @@ class Feed {
|
|
|
371
538
|
|
|
372
539
|
|
|
373
540
|
onNewMessageReceived(_ref) {
|
|
374
|
-
var
|
|
541
|
+
var _this12 = this;
|
|
375
542
|
|
|
376
543
|
return _asyncToGenerator(function* () {
|
|
377
544
|
var {
|
|
@@ -381,7 +548,7 @@ class Feed {
|
|
|
381
548
|
var {
|
|
382
549
|
getState,
|
|
383
550
|
setState
|
|
384
|
-
} =
|
|
551
|
+
} = _this12.store;
|
|
385
552
|
var {
|
|
386
553
|
items
|
|
387
554
|
} = getState();
|
|
@@ -389,7 +556,7 @@ class Feed {
|
|
|
389
556
|
|
|
390
557
|
setState(state => state.setMetadata(metadata)); // Fetch the items before the current head (if it exists)
|
|
391
558
|
|
|
392
|
-
|
|
559
|
+
_this12.fetch({
|
|
393
560
|
before: currentHead === null || currentHead === void 0 ? void 0 : currentHead.__cursor,
|
|
394
561
|
__fetchSource: "socket"
|
|
395
562
|
});
|
|
@@ -424,13 +591,13 @@ class Feed {
|
|
|
424
591
|
}
|
|
425
592
|
|
|
426
593
|
makeStatusUpdate(itemOrItems, type) {
|
|
427
|
-
var
|
|
594
|
+
var _this13 = this;
|
|
428
595
|
|
|
429
596
|
return _asyncToGenerator(function* () {
|
|
430
597
|
// Always treat items as a batch to use the corresponding batch endpoint
|
|
431
598
|
var items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];
|
|
432
599
|
var itemIds = items.map(item => item.id);
|
|
433
|
-
var result = yield
|
|
600
|
+
var result = yield _this13.apiClient.makeRequest({
|
|
434
601
|
method: "POST",
|
|
435
602
|
url: "/v1/messages/batch/".concat(type),
|
|
436
603
|
data: {
|
|
@@ -439,23 +606,60 @@ class Feed {
|
|
|
439
606
|
}); // Emit the event that these items had their statuses changed
|
|
440
607
|
// Note: we do this after the update to ensure that the server event actually completed
|
|
441
608
|
|
|
442
|
-
|
|
609
|
+
_this13.broadcaster.emit("items:".concat(type), {
|
|
443
610
|
items
|
|
444
611
|
});
|
|
445
612
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
payload: {
|
|
450
|
-
items
|
|
451
|
-
}
|
|
452
|
-
});
|
|
453
|
-
}
|
|
613
|
+
_this13.broadcastOverChannel("items:".concat(type), {
|
|
614
|
+
items
|
|
615
|
+
});
|
|
454
616
|
|
|
455
617
|
return result;
|
|
456
618
|
})();
|
|
457
619
|
}
|
|
458
620
|
|
|
621
|
+
makeBulkStatusUpdate(type) {
|
|
622
|
+
var _this14 = this;
|
|
623
|
+
|
|
624
|
+
return _asyncToGenerator(function* () {
|
|
625
|
+
// The base scope for the call should take into account all of the options currently
|
|
626
|
+
// set on the feed, as well as being scoped for the current user. We do this so that
|
|
627
|
+
// we ONLY make changes to the messages that are currently in view on this feed, and not
|
|
628
|
+
// all messages that exist.
|
|
629
|
+
var options = {
|
|
630
|
+
user_ids: [_this14.knock.userId],
|
|
631
|
+
engagement_status: _this14.defaultOptions.status !== "all" ? _this14.defaultOptions.status : undefined,
|
|
632
|
+
archived: _this14.defaultOptions.archived,
|
|
633
|
+
has_tenant: _this14.defaultOptions.has_tenant,
|
|
634
|
+
tenants: _this14.defaultOptions.tenant ? [_this14.defaultOptions.tenant] : undefined
|
|
635
|
+
};
|
|
636
|
+
return yield _this14.apiClient.makeRequest({
|
|
637
|
+
method: "POST",
|
|
638
|
+
url: "/v1/channels/".concat(_this14.feedId, "/messages/bulk/").concat(type),
|
|
639
|
+
data: options
|
|
640
|
+
});
|
|
641
|
+
})();
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
broadcastOverChannel(type, payload) {
|
|
645
|
+
// The broadcastChannel may not be available in non-browser environments
|
|
646
|
+
if (!this.broadcastChannel) {
|
|
647
|
+
return;
|
|
648
|
+
} // Here we stringify our payload and try and send as JSON such that we
|
|
649
|
+
// don't get any `An object could not be cloned` errors when trying to broadcast
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
try {
|
|
653
|
+
var stringifiedPayload = JSON.parse(JSON.stringify(payload));
|
|
654
|
+
this.broadcastChannel.postMessage({
|
|
655
|
+
type,
|
|
656
|
+
payload: stringifiedPayload
|
|
657
|
+
});
|
|
658
|
+
} catch (e) {
|
|
659
|
+
console.warn("Could not broadcast ".concat(type, ", got error: ").concat(e));
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
459
663
|
}
|
|
460
664
|
|
|
461
665
|
export default Feed;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/clients/feed/feed.ts"],"names":["EventEmitter2","EventEmitter","createStore","isRequestInFlight","NetworkStatus","feedClientDefaults","archived","Feed","constructor","knock","feedId","options","apiClient","client","userFeedId","buildUserFeedId","store","broadcaster","wildcard","delimiter","defaultOptions","channel","socket","on","resp","onNewMessageReceived","broadcastChannel","self","BroadcastChannel","teardown","leave","removeAllListeners","off","destroy","close","listenForUpdates","isConnected","connect","includes","state","join","__experimentalCrossBrowserUpdates","onmessage","e","data","type","fetch","eventName","callback","getState","markAsSeen","itemOrItems","now","Date","toISOString","optimisticallyPerformStatusUpdate","seen_at","makeStatusUpdate","markAsUnseen","markAsRead","read_at","markAsUnread","markAsArchived","setState","shouldOptimisticallyRemoveItems","normalizedItems","Array","isArray","itemIds","map","item","id","unseenCount","filter","i","length","unreadCount","updatedMetadata","metadata","total_count","unseen_count","unread_count","entriesToSet","items","setResult","entries","meta","page_info","pageInfo","setItemAttrs","archived_at","markAsUnarchived","networkStatus","setNetworkStatus","__loadingType","loading","queryParams","undefined","__fetchSource","result","makeRequest","method","url","userId","params","statusCode","body","error","status","response","before","opts","shouldSetPage","shouldAppend","after","broadcast","feedEventType","eventPayload","event","fetchNextPage","fetchMore","emit","currentHead","setMetadata","__cursor","attrs","badgeCountAttr","direction","startsWith","Math","max","message_ids","postMessage","payload"],"mappings":";;;;;;;AAEA,SAASA,aAAa,IAAIC,YAA1B,QAA8C,eAA9C;AAEA,OAAOC,WAAP,MAAwB,SAAxB;AAmBA,SAASC,iBAAT,EAA4BC,aAA5B,QAAiD,qBAAjD;AAUA;AACA,IAAMC,kBAAuD,GAAG;AAC9DC,EAAAA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";\nimport { EventEmitter2 as EventEmitter } from \"eventemitter2\";\nimport ApiClient from \"../../api\";\nimport createStore from \"./store\";\nimport {\n BindableFeedEvent,\n FeedMessagesReceivedPayload,\n FeedEventCallback,\n FeedEvent,\n FeedItemOrItems,\n FeedStoreState,\n FeedEventPayload,\n FeedRealTimeCallback,\n} from \"./types\";\nimport {\n FeedItem,\n FeedClientOptions,\n FetchFeedOptions,\n FeedResponse,\n FeedMetadata,\n} from \"./interfaces\";\nimport Knock from \"../../knock\";\nimport { isRequestInFlight, NetworkStatus } from \"../../networkStatus\";\n\nexport type Status =\n | \"seen\"\n | \"read\"\n | \"archived\"\n | \"unseen\"\n | \"unread\"\n | \"unarchived\";\n\n// Default options to apply\nconst feedClientDefaults: Pick<FeedClientOptions, \"archived\"> = {\n archived: \"exclude\",\n};\n\nclass Feed {\n private apiClient: ApiClient;\n private userFeedId: string;\n private channel: Channel;\n private broadcaster: EventEmitter;\n private defaultOptions: FeedClientOptions;\n private broadcastChannel: BroadcastChannel | null;\n\n // The raw store instance, used for binding in React and other environments\n public store: StoreApi<FeedStoreState>;\n\n constructor(\n readonly knock: Knock,\n readonly feedId: string,\n options: FeedClientOptions,\n ) {\n this.apiClient = knock.client();\n this.feedId = feedId;\n this.userFeedId = this.buildUserFeedId();\n this.store = createStore();\n this.broadcaster = new EventEmitter({ wildcard: true, delimiter: \".\" });\n this.defaultOptions = { ...feedClientDefaults, ...options };\n\n this.channel = this.apiClient.socket.channel(\n `feeds:${this.userFeedId}`,\n this.defaultOptions,\n );\n\n this.channel.on(\"new-message\", (resp) => this.onNewMessageReceived(resp));\n\n // Attempt to bind to listen to other events from this feed in different tabs for when\n // `items:updated` event is\n this.broadcastChannel =\n \"BroadcastChannel\" in self\n ? new BroadcastChannel(`knock:feed:${this.userFeedId}`)\n : null;\n }\n\n /**\n * Cleans up a feed instance by destroying the store and disconnecting\n * an open socket connection.\n */\n teardown() {\n this.channel.leave();\n this.broadcaster.removeAllListeners();\n this.channel.off(\"new-message\");\n this.store.destroy();\n\n if (this.broadcastChannel) {\n this.broadcastChannel.close();\n }\n }\n\n /*\n Initializes a real-time connection to Knock, connecting the websocket for the\n current ApiClient instance if the socket is not already connected.\n */\n listenForUpdates() {\n // Connect the socket only if we don't already have a connection\n if (!this.apiClient.socket.isConnected()) {\n this.apiClient.socket.connect();\n }\n\n // Only join the channel if we're not already in a joining state\n if ([\"closed\", \"errored\"].includes(this.channel.state)) {\n this.channel.join();\n }\n\n // Opt into receiving updates from _other tabs for the same user / feed_ via the broadcast\n // channel (iff it's enabled and exists)\n if (\n this.broadcastChannel &&\n this.defaultOptions.__experimentalCrossBrowserUpdates === true\n ) {\n this.broadcastChannel.onmessage = (e) => {\n switch (e.data.type) {\n case \"items:archived\":\n case \"items:unarchived\":\n case \"items:seen\":\n case \"items:unseen\":\n case \"items:read\":\n case \"items:unread\":\n // When items are updated in any other tab, simply refetch to get the latest state\n // to make sure that the state gets updated accordingly. In the future here we could\n // maybe do this optimistically without the fetch.\n return this.fetch();\n break;\n default:\n return null;\n }\n };\n }\n }\n\n /* Binds a handler to be invoked when event occurs */\n on(\n eventName: BindableFeedEvent,\n callback: FeedEventCallback | FeedRealTimeCallback,\n ) {\n this.broadcaster.on(eventName, callback);\n }\n\n off(\n eventName: BindableFeedEvent,\n callback: FeedEventCallback | FeedRealTimeCallback,\n ) {\n this.broadcaster.off(eventName, callback);\n }\n\n getState() {\n return this.store.getState();\n }\n\n async markAsSeen(itemOrItems: FeedItemOrItems) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"seen\",\n { seen_at: now },\n \"unseen_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"seen\");\n }\n\n async markAsUnseen(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"unseen\",\n { seen_at: null },\n \"unseen_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"unseen\");\n }\n\n async markAsRead(itemOrItems: FeedItemOrItems) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"read\",\n { read_at: now },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"read\");\n }\n\n async markAsUnread(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"unread\",\n { read_at: null },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"unread\");\n }\n\n /*\n Marking one or more items as archived should:\n\n - Decrement the badge count for any unread / unseen items\n - Remove the item from the feed list when the `archived` flag is \"exclude\" (default)\n\n TODO: how do we handle rollbacks?\n */\n async markAsArchived(itemOrItems: FeedItemOrItems) {\n const { getState, setState } = this.store;\n const state = getState();\n\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"exclude\";\n\n const normalizedItems = Array.isArray(itemOrItems)\n ? itemOrItems\n : [itemOrItems];\n\n const itemIds: string[] = normalizedItems.map((item) => item.id);\n\n /*\n In the code here we want to optimistically update counts and items\n that are persisted such that we can display updates immediately on the feed\n without needing to make a network request.\n\n Note: right now this does *not* take into account offline handling or any extensive retry\n logic, so rollbacks aren't considered. That probably needs to be a future consideration for\n this library.\n\n Scenarios to consider:\n\n ## Feed scope to archived *only*\n\n - Counts should not be decremented\n - Items should not be removed\n\n ## Feed scoped to exclude archived items (the default)\n\n - Counts should be decremented\n - Items should be removed\n\n ## Feed scoped to include archived items as well\n\n - Counts should not be decremented\n - Items should not be removed\n */\n\n if (shouldOptimisticallyRemoveItems) {\n // If any of the items are unseen or unread, then capture as we'll want to decrement\n // the counts for these in the metadata we have\n const unseenCount = normalizedItems.filter((i) => !i.seen_at).length;\n const unreadCount = normalizedItems.filter((i) => !i.read_at).length;\n\n // Build the new metadata\n const updatedMetadata = {\n ...state.metadata,\n total_count: state.metadata.total_count - normalizedItems.length,\n unseen_count: state.metadata.unseen_count - unseenCount,\n unread_count: state.metadata.unread_count - unreadCount,\n };\n\n // Remove the archiving entries\n const entriesToSet = state.items.filter(\n (item) => !itemIds.includes(item.id),\n );\n\n setState((state) =>\n state.setResult({\n entries: entriesToSet,\n meta: updatedMetadata,\n page_info: state.pageInfo,\n }),\n );\n } else {\n // Mark all the entries being updated as archived either way so the state is correct\n state.setItemAttrs(itemIds, { archived_at: new Date().toISOString() });\n }\n\n return this.makeStatusUpdate(itemOrItems, \"archived\");\n }\n\n async markAsUnarchived(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(itemOrItems, \"unarchived\", {\n archived_at: null,\n });\n\n return this.makeStatusUpdate(itemOrItems, \"unarchived\");\n }\n\n /* Fetches the feed content, appending it to the store */\n async fetch(options: FetchFeedOptions = {}) {\n const { setState, getState } = this.store;\n const { networkStatus } = getState();\n\n // If there's an existing request in flight, then do nothing\n if (isRequestInFlight(networkStatus)) {\n return;\n }\n\n // Set the loading type based on the request type it is\n setState((store) =>\n store.setNetworkStatus(options.__loadingType ?? NetworkStatus.loading),\n );\n\n // Always include the default params, if they have been set\n const queryParams = {\n ...this.defaultOptions,\n ...options,\n // Unset options that should not be sent to the API\n __loadingType: undefined,\n __fetchSource: undefined,\n __experimentalCrossBrowserUpdates: undefined,\n };\n\n const result = await this.apiClient.makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.knock.userId}/feeds/${this.feedId}`,\n params: queryParams,\n });\n\n if (result.statusCode === \"error\" || !result.body) {\n setState((store) => store.setNetworkStatus(NetworkStatus.error));\n\n return {\n status: result.statusCode,\n data: result.error || result.body,\n };\n }\n\n const response = {\n entries: result.body.entries,\n meta: result.body.meta,\n page_info: result.body.page_info,\n };\n\n if (options.before) {\n const opts = { shouldSetPage: false, shouldAppend: true };\n setState((state) => state.setResult(response, opts));\n } else if (options.after) {\n const opts = { shouldSetPage: true, shouldAppend: true };\n setState((state) => state.setResult(response, opts));\n } else {\n setState((state) => state.setResult(response));\n }\n\n // Legacy `messages.new` event, should be removed in a future version\n this.broadcast(\"messages.new\", response);\n\n // Broadcast the appropriate event type depending on the fetch source\n const feedEventType: FeedEvent =\n options.__fetchSource === \"socket\"\n ? \"items.received.realtime\"\n : \"items.received.page\";\n\n const eventPayload = {\n items: response.entries as FeedItem[],\n metadata: response.meta as FeedMetadata,\n event: feedEventType,\n };\n\n this.broadcast(eventPayload.event, eventPayload);\n\n return { data: response, status: result.statusCode };\n }\n\n async fetchNextPage() {\n // Attempts to fetch the next page of results (if we have any)\n const { getState } = this.store;\n const { pageInfo } = getState();\n\n if (!pageInfo.after) {\n // Nothing more to fetch\n return;\n }\n\n this.fetch({\n after: pageInfo.after,\n __loadingType: NetworkStatus.fetchMore,\n });\n }\n\n private broadcast(\n eventName: FeedEvent,\n data: FeedResponse | FeedEventPayload,\n ) {\n this.broadcaster.emit(eventName, data);\n }\n\n // Invoked when a new real-time message comes in from the socket\n private async onNewMessageReceived({\n metadata,\n }: FeedMessagesReceivedPayload) {\n // Handle the new message coming in\n const { getState, setState } = this.store;\n const { items } = getState();\n const currentHead: FeedItem | undefined = items[0];\n // Optimistically set the badge counts\n setState((state) => state.setMetadata(metadata));\n // Fetch the items before the current head (if it exists)\n this.fetch({ before: currentHead?.__cursor, __fetchSource: \"socket\" });\n }\n\n private buildUserFeedId() {\n return `${this.feedId}:${this.knock.userId}`;\n }\n\n private optimisticallyPerformStatusUpdate(\n itemOrItems: FeedItemOrItems,\n type: Status,\n attrs: object,\n badgeCountAttr?: \"unread_count\" | \"unseen_count\",\n ) {\n const { getState, setState } = this.store;\n const itemIds = Array.isArray(itemOrItems)\n ? itemOrItems.map((item) => item.id)\n : [itemOrItems.id];\n\n if (badgeCountAttr) {\n const { metadata } = getState();\n\n // Tnis is a hack to determine the direction of whether we're\n // adding or removing from the badge count\n const direction = type.startsWith(\"un\")\n ? itemIds.length\n : -itemIds.length;\n\n setState((store) =>\n store.setMetadata({\n ...metadata,\n [badgeCountAttr]: Math.max(0, metadata[badgeCountAttr] + direction),\n }),\n );\n }\n\n // Update the items with the given attributes\n setState((store) => store.setItemAttrs(itemIds, attrs));\n }\n\n private async makeStatusUpdate(itemOrItems: FeedItemOrItems, type: Status) {\n // Always treat items as a batch to use the corresponding batch endpoint\n const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];\n const itemIds = items.map((item) => item.id);\n\n const result = await this.apiClient.makeRequest({\n method: \"POST\",\n url: `/v1/messages/batch/${type}`,\n data: { message_ids: itemIds },\n });\n\n // Emit the event that these items had their statuses changed\n // Note: we do this after the update to ensure that the server event actually completed\n this.broadcaster.emit(`items:${type}`, { items });\n\n if (this.broadcastChannel) {\n this.broadcastChannel.postMessage({\n type: `items:${type}`,\n payload: { items },\n });\n }\n\n return result;\n }\n}\n\nexport default Feed;\n"],"file":"feed.js"}
|
|
1
|
+
{"version":3,"sources":["../../../../src/clients/feed/feed.ts"],"names":["EventEmitter2","EventEmitter","createStore","isRequestInFlight","NetworkStatus","feedClientDefaults","archived","Feed","constructor","knock","feedId","options","apiClient","client","userFeedId","buildUserFeedId","store","broadcaster","wildcard","delimiter","defaultOptions","channel","socket","on","resp","onNewMessageReceived","broadcastChannel","self","BroadcastChannel","teardown","leave","removeAllListeners","off","destroy","close","listenForUpdates","isConnected","connect","includes","state","join","__experimentalCrossBrowserUpdates","onmessage","e","data","type","fetch","eventName","callback","getState","markAsSeen","itemOrItems","now","Date","toISOString","optimisticallyPerformStatusUpdate","seen_at","makeStatusUpdate","markAllAsSeen","setState","metadata","items","isViewingOnlyUnseen","status","resetStore","total_count","unseen_count","setMetadata","attrs","itemIds","map","item","id","setItemAttrs","result","makeBulkStatusUpdate","emit","broadcastOverChannel","markAsUnseen","markAsRead","read_at","markAllAsRead","isViewingOnlyUnread","unread_count","markAsUnread","markAsArchived","shouldOptimisticallyRemoveItems","normalizedItems","Array","isArray","unseenCount","filter","i","length","unreadCount","updatedMetadata","entriesToSet","setResult","entries","meta","page_info","pageInfo","archived_at","markAllAsArchived","markAsUnarchived","networkStatus","setNetworkStatus","__loadingType","loading","queryParams","undefined","__fetchSource","makeRequest","method","url","userId","params","statusCode","body","error","response","before","opts","shouldSetPage","shouldAppend","after","broadcast","feedEventType","eventPayload","event","fetchNextPage","fetchMore","currentHead","__cursor","badgeCountAttr","direction","startsWith","Math","max","message_ids","user_ids","engagement_status","has_tenant","tenants","tenant","payload","stringifiedPayload","JSON","parse","stringify","postMessage","console","warn"],"mappings":";;;;;;;AAEA,SAASA,aAAa,IAAIC,YAA1B,QAA8C,eAA9C;AAEA,OAAOC,WAAP,MAAwB,SAAxB;AAmBA,SAASC,iBAAT,EAA4BC,aAA5B,QAAiD,qBAAjD;AAUA;AACA,IAAMC,kBAAuD,GAAG;AAC9DC,EAAAA,QAAQ,EAAE;AADoD,CAAhE;;AAIA,MAAMC,IAAN,CAAW;AAQT;AAGAC,EAAAA,WAAW,CACAC,KADA,EAEAC,MAFA,EAGTC,OAHS,EAIT;AAAA,SAHSF,KAGT,GAHSA,KAGT;AAAA,SAFSC,MAET,GAFSA,MAET;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AACA,SAAKE,SAAL,GAAiBH,KAAK,CAACI,MAAN,EAAjB;AACA,SAAKH,MAAL,GAAcA,MAAd;AACA,SAAKI,UAAL,GAAkB,KAAKC,eAAL,EAAlB;AACA,SAAKC,KAAL,GAAad,WAAW,EAAxB;AACA,SAAKe,WAAL,GAAmB,IAAIhB,YAAJ,CAAiB;AAAEiB,MAAAA,QAAQ,EAAE,IAAZ;AAAkBC,MAAAA,SAAS,EAAE;AAA7B,KAAjB,CAAnB;AACA,SAAKC,cAAL,mCAA2Bf,kBAA3B,GAAkDM,OAAlD;AAEA,SAAKU,OAAL,GAAe,KAAKT,SAAL,CAAeU,MAAf,CAAsBD,OAAtB,iBACJ,KAAKP,UADD,GAEb,KAAKM,cAFQ,CAAf;AAKA,SAAKC,OAAL,CAAaE,EAAb,CAAgB,aAAhB,EAAgCC,IAAD,IAAU,KAAKC,oBAAL,CAA0BD,IAA1B,CAAzC,EAbA,CAeA;AACA;;AACA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,CAAkC;AAChCzG,QAAAA,IADgC;AAEhCoG,QAAAA,OAAO,EAAEC;AAFuB,OAAlC;AAID,KAPD,CAOE,OAAOvG,CAAP,EAAU;AACV4G,MAAAA,OAAO,CAACC,IAAR,+BAAoC3G,IAApC,0BAAwDF,CAAxD;AACD;AACF;;AA7kBQ;;AAglBX,eAAepC,IAAf","sourcesContent":["import { Channel } from \"phoenix\";\nimport { StoreApi } from \"zustand\";\nimport { EventEmitter2 as EventEmitter } from \"eventemitter2\";\nimport ApiClient from \"../../api\";\nimport createStore from \"./store\";\nimport {\n BindableFeedEvent,\n FeedMessagesReceivedPayload,\n FeedEventCallback,\n FeedEvent,\n FeedItemOrItems,\n FeedStoreState,\n FeedEventPayload,\n FeedRealTimeCallback,\n} from \"./types\";\nimport {\n FeedItem,\n FeedClientOptions,\n FetchFeedOptions,\n FeedResponse,\n FeedMetadata,\n} from \"./interfaces\";\nimport Knock from \"../../knock\";\nimport { isRequestInFlight, NetworkStatus } from \"../../networkStatus\";\n\nexport type Status =\n | \"seen\"\n | \"read\"\n | \"archived\"\n | \"unseen\"\n | \"unread\"\n | \"unarchived\";\n\n// Default options to apply\nconst feedClientDefaults: Pick<FeedClientOptions, \"archived\"> = {\n archived: \"exclude\",\n};\n\nclass Feed {\n private apiClient: ApiClient;\n private userFeedId: string;\n private channel: Channel;\n private broadcaster: EventEmitter;\n private defaultOptions: FeedClientOptions;\n private broadcastChannel: BroadcastChannel | null;\n\n // The raw store instance, used for binding in React and other environments\n public store: StoreApi<FeedStoreState>;\n\n constructor(\n readonly knock: Knock,\n readonly feedId: string,\n options: FeedClientOptions,\n ) {\n this.apiClient = knock.client();\n this.feedId = feedId;\n this.userFeedId = this.buildUserFeedId();\n this.store = createStore();\n this.broadcaster = new EventEmitter({ wildcard: true, delimiter: \".\" });\n this.defaultOptions = { ...feedClientDefaults, ...options };\n\n this.channel = this.apiClient.socket.channel(\n `feeds:${this.userFeedId}`,\n this.defaultOptions,\n );\n\n this.channel.on(\"new-message\", (resp) => this.onNewMessageReceived(resp));\n\n // Attempt to bind to listen to other events from this feed in different tabs\n // Note: here we ensure `self` is available (it's not in server rendered envs)\n this.broadcastChannel =\n self && \"BroadcastChannel\" in self\n ? new BroadcastChannel(`knock:feed:${this.userFeedId}`)\n : null;\n }\n\n /**\n * Cleans up a feed instance by destroying the store and disconnecting\n * an open socket connection.\n */\n teardown() {\n this.channel.leave();\n this.broadcaster.removeAllListeners();\n this.channel.off(\"new-message\");\n this.store.destroy();\n\n if (this.broadcastChannel) {\n this.broadcastChannel.close();\n }\n }\n\n /*\n Initializes a real-time connection to Knock, connecting the websocket for the\n current ApiClient instance if the socket is not already connected.\n */\n listenForUpdates() {\n // Connect the socket only if we don't already have a connection\n if (!this.apiClient.socket.isConnected()) {\n this.apiClient.socket.connect();\n }\n\n // Only join the channel if we're not already in a joining state\n if ([\"closed\", \"errored\"].includes(this.channel.state)) {\n this.channel.join();\n }\n\n // Opt into receiving updates from _other tabs for the same user / feed_ via the broadcast\n // channel (iff it's enabled and exists)\n if (\n this.broadcastChannel &&\n this.defaultOptions.__experimentalCrossBrowserUpdates === true\n ) {\n this.broadcastChannel.onmessage = (e) => {\n switch (e.data.type) {\n case \"items:archived\":\n case \"items:unarchived\":\n case \"items:seen\":\n case \"items:unseen\":\n case \"items:read\":\n case \"items:unread\":\n case \"items:all_read\":\n case \"items:all_seen\":\n case \"items:all_archived\":\n // When items are updated in any other tab, simply refetch to get the latest state\n // to make sure that the state gets updated accordingly. In the future here we could\n // maybe do this optimistically without the fetch.\n return this.fetch();\n break;\n default:\n return null;\n }\n };\n }\n }\n\n /* Binds a handler to be invoked when event occurs */\n on(\n eventName: BindableFeedEvent,\n callback: FeedEventCallback | FeedRealTimeCallback,\n ) {\n this.broadcaster.on(eventName, callback);\n }\n\n off(\n eventName: BindableFeedEvent,\n callback: FeedEventCallback | FeedRealTimeCallback,\n ) {\n this.broadcaster.off(eventName, callback);\n }\n\n getState() {\n return this.store.getState();\n }\n\n async markAsSeen(itemOrItems: FeedItemOrItems) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"seen\",\n { seen_at: now },\n \"unseen_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"seen\");\n }\n\n async markAllAsSeen() {\n // To mark all of the messages as seen we:\n // 1. Optimistically update *everything* we have in the store\n // 2. We decrement the `unseen_count` to zero optimistically\n // 3. We issue the API call to the endpoint\n //\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unseen_count` to be what it was.\n //\n // Note: here we optimistically handle the case whereby the feed is scoped to show only `unseen`\n // items by removing everything from view.\n const { getState, setState } = this.store;\n const { metadata, items } = getState();\n\n const isViewingOnlyUnseen = this.defaultOptions.status === \"unseen\";\n\n // If we're looking at the unseen view, then we want to remove all of the items optimistically\n // from the store given that nothing should be visible. We do this by resetting the store state\n // and setting the current metadata counts to 0\n if (isViewingOnlyUnseen) {\n setState((store) =>\n store.resetStore({\n ...metadata,\n total_count: 0,\n unseen_count: 0,\n }),\n );\n } else {\n // Otherwise we want to update the metadata and mark all of the items in the store as seen\n setState((store) => store.setMetadata({ ...metadata, unseen_count: 0 }));\n\n const attrs = { seen_at: new Date().toISOString() };\n const itemIds = items.map((item) => item.id);\n\n setState((store) => store.setItemAttrs(itemIds, attrs));\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"seen\");\n\n this.broadcaster.emit(`items:all_seen`, { items });\n this.broadcastOverChannel(`items:all_seen`, { items });\n\n return result;\n }\n\n async markAsUnseen(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"unseen\",\n { seen_at: null },\n \"unseen_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"unseen\");\n }\n\n async markAsRead(itemOrItems: FeedItemOrItems) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"read\",\n { read_at: now },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"read\");\n }\n\n async markAllAsRead() {\n // To mark all of the messages as read we:\n // 1. Optimistically update *everything* we have in the store\n // 2. We decrement the `unread_count` to zero optimistically\n // 3. We issue the API call to the endpoint\n //\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unread_count` to be what it was.\n //\n // Note: here we optimistically handle the case whereby the feed is scoped to show only `unread`\n // items by removing everything from view.\n const { getState, setState } = this.store;\n const { metadata, items } = getState();\n\n const isViewingOnlyUnread = this.defaultOptions.status === \"unread\";\n\n // If we're looking at the unread view, then we want to remove all of the items optimistically\n // from the store given that nothing should be visible. We do this by resetting the store state\n // and setting the current metadata counts to 0\n if (isViewingOnlyUnread) {\n setState((store) =>\n store.resetStore({\n ...metadata,\n total_count: 0,\n unread_count: 0,\n }),\n );\n } else {\n // Otherwise we want to update the metadata and mark all of the items in the store as seen\n setState((store) => store.setMetadata({ ...metadata, unread_count: 0 }));\n\n const attrs = { read_at: new Date().toISOString() };\n const itemIds = items.map((item) => item.id);\n\n setState((store) => store.setItemAttrs(itemIds, attrs));\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"read\");\n\n this.broadcaster.emit(`items:all_read`, { items });\n this.broadcastOverChannel(`items:all_read`, { items });\n\n return result;\n }\n\n async markAsUnread(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"unread\",\n { read_at: null },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"unread\");\n }\n\n /*\n Marking one or more items as archived should:\n\n - Decrement the badge count for any unread / unseen items\n - Remove the item from the feed list when the `archived` flag is \"exclude\" (default)\n\n TODO: how do we handle rollbacks?\n */\n async markAsArchived(itemOrItems: FeedItemOrItems) {\n const { getState, setState } = this.store;\n const state = getState();\n\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"exclude\";\n\n const normalizedItems = Array.isArray(itemOrItems)\n ? itemOrItems\n : [itemOrItems];\n\n const itemIds: string[] = normalizedItems.map((item) => item.id);\n\n /*\n In the code here we want to optimistically update counts and items\n that are persisted such that we can display updates immediately on the feed\n without needing to make a network request.\n\n Note: right now this does *not* take into account offline handling or any extensive retry\n logic, so rollbacks aren't considered. That probably needs to be a future consideration for\n this library.\n\n Scenarios to consider:\n\n ## Feed scope to archived *only*\n\n - Counts should not be decremented\n - Items should not be removed\n\n ## Feed scoped to exclude archived items (the default)\n\n - Counts should be decremented\n - Items should be removed\n\n ## Feed scoped to include archived items as well\n\n - Counts should not be decremented\n - Items should not be removed\n */\n\n if (shouldOptimisticallyRemoveItems) {\n // If any of the items are unseen or unread, then capture as we'll want to decrement\n // the counts for these in the metadata we have\n const unseenCount = normalizedItems.filter((i) => !i.seen_at).length;\n const unreadCount = normalizedItems.filter((i) => !i.read_at).length;\n\n // Build the new metadata\n const updatedMetadata = {\n ...state.metadata,\n total_count: state.metadata.total_count - normalizedItems.length,\n unseen_count: state.metadata.unseen_count - unseenCount,\n unread_count: state.metadata.unread_count - unreadCount,\n };\n\n // Remove the archiving entries\n const entriesToSet = state.items.filter(\n (item) => !itemIds.includes(item.id),\n );\n\n setState((state) =>\n state.setResult({\n entries: entriesToSet,\n meta: updatedMetadata,\n page_info: state.pageInfo,\n }),\n );\n } else {\n // Mark all the entries being updated as archived either way so the state is correct\n state.setItemAttrs(itemIds, { archived_at: new Date().toISOString() });\n }\n\n return this.makeStatusUpdate(itemOrItems, \"archived\");\n }\n\n async markAllAsArchived() {\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unseen_count` to be what it was.\n const { setState, getState } = this.store;\n const { items } = getState();\n\n // Here if we're looking at a feed that excludes all of the archived items by default then we\n // will want to optimistically remove all of the items from the feed as they are now all excluded\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"exclude\";\n\n if (shouldOptimisticallyRemoveItems) {\n // Reset the store to clear out all of items and reset the badge count\n setState((store) => store.resetStore());\n } else {\n // Mark all the entries being updated as archived either way so the state is correct\n setState((store) => {\n const itemIds = items.map((i) => i.id);\n store.setItemAttrs(itemIds, { archived_at: new Date().toISOString() });\n });\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"archive\");\n\n this.broadcaster.emit(`items:all_archived`, { items });\n this.broadcastOverChannel(`items:all_archived`, { items });\n\n return result;\n }\n\n async markAsUnarchived(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(itemOrItems, \"unarchived\", {\n archived_at: null,\n });\n\n return this.makeStatusUpdate(itemOrItems, \"unarchived\");\n }\n\n /* Fetches the feed content, appending it to the store */\n async fetch(options: FetchFeedOptions = {}) {\n const { setState, getState } = this.store;\n const { networkStatus } = getState();\n\n // If there's an existing request in flight, then do nothing\n if (isRequestInFlight(networkStatus)) {\n return;\n }\n\n // Set the loading type based on the request type it is\n setState((store) =>\n store.setNetworkStatus(options.__loadingType ?? NetworkStatus.loading),\n );\n\n // Always include the default params, if they have been set\n const queryParams = {\n ...this.defaultOptions,\n ...options,\n // Unset options that should not be sent to the API\n __loadingType: undefined,\n __fetchSource: undefined,\n __experimentalCrossBrowserUpdates: undefined,\n };\n\n const result = await this.apiClient.makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.knock.userId}/feeds/${this.feedId}`,\n params: queryParams,\n });\n\n if (result.statusCode === \"error\" || !result.body) {\n setState((store) => store.setNetworkStatus(NetworkStatus.error));\n\n return {\n status: result.statusCode,\n data: result.error || result.body,\n };\n }\n\n const response = {\n entries: result.body.entries,\n meta: result.body.meta,\n page_info: result.body.page_info,\n };\n\n if (options.before) {\n const opts = { shouldSetPage: false, shouldAppend: true };\n setState((state) => state.setResult(response, opts));\n } else if (options.after) {\n const opts = { shouldSetPage: true, shouldAppend: true };\n setState((state) => state.setResult(response, opts));\n } else {\n setState((state) => state.setResult(response));\n }\n\n // Legacy `messages.new` event, should be removed in a future version\n this.broadcast(\"messages.new\", response);\n\n // Broadcast the appropriate event type depending on the fetch source\n const feedEventType: FeedEvent =\n options.__fetchSource === \"socket\"\n ? \"items.received.realtime\"\n : \"items.received.page\";\n\n const eventPayload = {\n items: response.entries as FeedItem[],\n metadata: response.meta as FeedMetadata,\n event: feedEventType,\n };\n\n this.broadcast(eventPayload.event, eventPayload);\n\n return { data: response, status: result.statusCode };\n }\n\n async fetchNextPage() {\n // Attempts to fetch the next page of results (if we have any)\n const { getState } = this.store;\n const { pageInfo } = getState();\n\n if (!pageInfo.after) {\n // Nothing more to fetch\n return;\n }\n\n this.fetch({\n after: pageInfo.after,\n __loadingType: NetworkStatus.fetchMore,\n });\n }\n\n private broadcast(\n eventName: FeedEvent,\n data: FeedResponse | FeedEventPayload,\n ) {\n this.broadcaster.emit(eventName, data);\n }\n\n // Invoked when a new real-time message comes in from the socket\n private async onNewMessageReceived({\n metadata,\n }: FeedMessagesReceivedPayload) {\n // Handle the new message coming in\n const { getState, setState } = this.store;\n const { items } = getState();\n const currentHead: FeedItem | undefined = items[0];\n // Optimistically set the badge counts\n setState((state) => state.setMetadata(metadata));\n // Fetch the items before the current head (if it exists)\n this.fetch({ before: currentHead?.__cursor, __fetchSource: \"socket\" });\n }\n\n private buildUserFeedId() {\n return `${this.feedId}:${this.knock.userId}`;\n }\n\n private optimisticallyPerformStatusUpdate(\n itemOrItems: FeedItemOrItems,\n type: Status,\n attrs: object,\n badgeCountAttr?: \"unread_count\" | \"unseen_count\",\n ) {\n const { getState, setState } = this.store;\n const itemIds = Array.isArray(itemOrItems)\n ? itemOrItems.map((item) => item.id)\n : [itemOrItems.id];\n\n if (badgeCountAttr) {\n const { metadata } = getState();\n\n // Tnis is a hack to determine the direction of whether we're\n // adding or removing from the badge count\n const direction = type.startsWith(\"un\")\n ? itemIds.length\n : -itemIds.length;\n\n setState((store) =>\n store.setMetadata({\n ...metadata,\n [badgeCountAttr]: Math.max(0, metadata[badgeCountAttr] + direction),\n }),\n );\n }\n\n // Update the items with the given attributes\n setState((store) => store.setItemAttrs(itemIds, attrs));\n }\n\n private async makeStatusUpdate(itemOrItems: FeedItemOrItems, type: Status) {\n // Always treat items as a batch to use the corresponding batch endpoint\n const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];\n const itemIds = items.map((item) => item.id);\n\n const result = await this.apiClient.makeRequest({\n method: \"POST\",\n url: `/v1/messages/batch/${type}`,\n data: { message_ids: itemIds },\n });\n\n // Emit the event that these items had their statuses changed\n // Note: we do this after the update to ensure that the server event actually completed\n this.broadcaster.emit(`items:${type}`, { items });\n this.broadcastOverChannel(`items:${type}`, { items });\n\n return result;\n }\n\n private async makeBulkStatusUpdate(type: \"seen\" | \"read\" | \"archive\") {\n // The base scope for the call should take into account all of the options currently\n // set on the feed, as well as being scoped for the current user. We do this so that\n // we ONLY make changes to the messages that are currently in view on this feed, and not\n // all messages that exist.\n const options = {\n user_ids: [this.knock.userId],\n engagement_status:\n this.defaultOptions.status !== \"all\"\n ? this.defaultOptions.status\n : undefined,\n archived: this.defaultOptions.archived,\n has_tenant: this.defaultOptions.has_tenant,\n tenants: this.defaultOptions.tenant\n ? [this.defaultOptions.tenant]\n : undefined,\n };\n\n return await this.apiClient.makeRequest({\n method: \"POST\",\n url: `/v1/channels/${this.feedId}/messages/bulk/${type}`,\n data: options,\n });\n }\n\n private broadcastOverChannel(type: string, payload: any) {\n // The broadcastChannel may not be available in non-browser environments\n if (!this.broadcastChannel) {\n return;\n }\n\n // Here we stringify our payload and try and send as JSON such that we\n // don't get any `An object could not be cloned` errors when trying to broadcast\n try {\n const stringifiedPayload = JSON.parse(JSON.stringify(payload));\n\n this.broadcastChannel.postMessage({\n type,\n payload: stringifiedPayload,\n });\n } catch (e) {\n console.warn(`Could not broadcast ${type}, got error: ${e}`);\n }\n }\n}\n\nexport default Feed;\n"],"file":"feed.js"}
|