@subiz/widget-v4 4.1.163 → 4.1.166

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.
Files changed (50) hide show
  1. package/compiled_src/chatbox/ChatButton.js +4 -0
  2. package/compiled_src/chatbox/Event.js +2 -0
  3. package/compiled_src/chatbox/Header.js +1 -0
  4. package/compiled_src/chatbox/Widget.js +1 -1
  5. package/compiled_src/chatbox/main.js +6 -0
  6. package/compiled_src/store/inmem_localstorage.js +1 -0
  7. package/compiled_src/store/lsdb.js +5 -1
  8. package/compiled_src/store/store.js +39 -38
  9. package/dist/198a3614cdc.app.js +1 -0
  10. package/dist/{198a2dde018.chat.html → 198a3614cdc.chat.html} +1 -1
  11. package/dist/198a3614cdc.chat.js +1 -0
  12. package/dist/{198a2dde018.form.html → 198a3614cdc.form.html} +1 -1
  13. package/dist/198a3614cdc.form.js +1 -0
  14. package/dist/{198a2dde018.index.html → 198a3614cdc.index.html} +1 -1
  15. package/dist/{198a2dde018.library.html → 198a3614cdc.library.html} +1 -1
  16. package/dist/198a3614cdc.library.js +1 -0
  17. package/dist/{198a2dde018.preview.html → 198a3614cdc.preview.html} +1 -1
  18. package/dist/198a3614cdc.preview.js +1 -0
  19. package/dist/198a3614cdc.webview.js +1 -0
  20. package/dist/HASH +1 -1
  21. package/dist/screenshoot.js +52 -21
  22. package/inject.html +11 -2
  23. package/package.json +1 -1
  24. package/src/chatbox/ChatButton.js +5 -1
  25. package/src/chatbox/Event.js +2 -1
  26. package/src/chatbox/Header.js +1 -0
  27. package/src/chatbox/Widget.js +1 -1
  28. package/src/chatbox/main.js +6 -0
  29. package/src/lib.js +0 -1
  30. package/src/store/inmem_localstorage.js +1 -0
  31. package/src/store/kv.js +0 -1
  32. package/src/store/lsdb.js +6 -1
  33. package/src/store/store.js +43 -40
  34. package/tracking.js +2 -1
  35. package/dist/198a2dde018.app.js +0 -1
  36. package/dist/198a2dde018.chat.js +0 -1
  37. package/dist/198a2dde018.form.js +0 -1
  38. package/dist/198a2dde018.library.js +0 -1
  39. package/dist/198a2dde018.preview.js +0 -1
  40. package/dist/198a2dde018.webview.js +0 -1
  41. /package/dist/{198a2dde018.127.js → 198a3614cdc.127.js} +0 -0
  42. /package/dist/{198a2dde018.132.js → 198a3614cdc.132.js} +0 -0
  43. /package/dist/{198a2dde018.185.js → 198a3614cdc.185.js} +0 -0
  44. /package/dist/{198a2dde018.27.js → 198a3614cdc.27.js} +0 -0
  45. /package/dist/{198a2dde018.278.js → 198a3614cdc.278.js} +0 -0
  46. /package/dist/{198a2dde018.339.js → 198a3614cdc.339.js} +0 -0
  47. /package/dist/{198a2dde018.459.js → 198a3614cdc.459.js} +0 -0
  48. /package/dist/{198a2dde018.490.js → 198a3614cdc.490.js} +0 -0
  49. /package/dist/{198a2dde018.74.js → 198a3614cdc.74.js} +0 -0
  50. /package/dist/{198a2dde018.997.js → 198a3614cdc.997.js} +0 -0
package/dist/HASH CHANGED
@@ -1 +1 @@
1
- 198a2dde018
1
+ 198a3614cdc
@@ -420,6 +420,7 @@ module.exports = ajax
420
420
  var localStorageMemory = {};
421
421
  var cache = {};
422
422
  localStorageMemory.length = 0;
423
+ localStorageMemory.cache = cache;
423
424
  localStorageMemory.getItem = function (key) {
424
425
  if (key in cache) return cache[key];
425
426
  return null;
@@ -2216,6 +2217,8 @@ __webpack_require__.r(__webpack_exports__);
2216
2217
  /* harmony export */ isBlockEmpty: () => (/* binding */ isBlockEmpty),
2217
2218
  /* harmony export */ isConvoSeen: () => (/* binding */ isConvoSeen),
2218
2219
  /* harmony export */ isEmail: () => (/* binding */ isEmail),
2220
+ /* harmony export */ isEmptyBlock: () => (/* binding */ isEmptyBlock),
2221
+ /* harmony export */ isEmptyMessage: () => (/* binding */ isEmptyMessage),
2219
2222
  /* harmony export */ isNumber: () => (/* binding */ isNumber),
2220
2223
  /* harmony export */ isPhone: () => (/* binding */ isPhone),
2221
2224
  /* harmony export */ isURL: () => (/* binding */ isURL),
@@ -3475,6 +3478,15 @@ function absLink(url) {
3475
3478
  if (!url.startsWith('http://') && !url.startsWith('https://') && !url.startsWith('//')) return 'http://' + url;
3476
3479
  return url;
3477
3480
  }
3481
+ function isEmptyBlock(block) {
3482
+ if (!block || !block.type) return true;
3483
+ let hasContent = size(filter(map(block.content, block => isEmptyBlock(block)), isEmpty => !isEmpty)) > 0;
3484
+ return !trim(block.text) && !hasContent;
3485
+ }
3486
+ function isEmptyMessage(msg) {
3487
+ if (!msg) return true;
3488
+ if (!msg.text && isEmptyBlock(msg.block) && !size(msg.attachments)) return true;
3489
+ }
3478
3490
 
3479
3491
  /***/ })
3480
3492
 
@@ -4082,10 +4094,14 @@ const LIMIT = 80;
4082
4094
  }
4083
4095
  }
4084
4096
  clear(table) {
4097
+ var keys = [];
4085
4098
  for (var i = 0; i < this.localStorage.length; i++) {
4086
4099
  let key = this.localStorage.key(i);
4087
4100
  if (!key.startsWith(this.schema + '#' + table + '#')) continue;
4088
- this.localStorage.removeItem(key);
4101
+ keys.push(key);
4102
+ }
4103
+ for (var i = 0; i < keys.length; i++) {
4104
+ this.localStorage.removeItem(keys[i]);
4089
4105
  }
4090
4106
  this.allkeys = {};
4091
4107
  }
@@ -4812,6 +4828,7 @@ var cookie = __webpack_require__(5672);
4812
4828
  cookie.defaults.path = '/';
4813
4829
  cookie.expiresMultiplier = 60 * 60 * 24;
4814
4830
  cookie.defaults.expires = 3650;
4831
+ let store_memlocalStorage = __webpack_require__(450);
4815
4832
  const MAX_CONVO = 20;
4816
4833
  const page_loaded = util/* default */.A.Date.now();
4817
4834
  class Store {
@@ -4822,6 +4839,7 @@ class Store {
4822
4839
  store_defineProperty(this, "_currentViewingProduct", {});
4823
4840
  store_defineProperty(this, "new_visitor", true);
4824
4841
  store_defineProperty(this, "matchUser", () => {
4842
+ if (!this.cache) return undefined;
4825
4843
  this._touchAcc();
4826
4844
  return this.cache.match('user');
4827
4845
  });
@@ -4830,6 +4848,7 @@ class Store {
4830
4848
  store_defineProperty(this, "matchPaymentMethods", () => this.cache.match('payment_methods'));
4831
4849
  store_defineProperty(this, "matchUnread", () => {
4832
4850
  let user = this.matchUser();
4851
+ if (!user || !user.id) return;
4833
4852
  let allconvos = this.matchConvo('*');
4834
4853
  let unreadcount = 0;
4835
4854
  util/* default */.A.map(allconvos, convo => {
@@ -4960,6 +4979,7 @@ class Store {
4960
4979
  this.seenQueue.push(id);
4961
4980
  });
4962
4981
  store_defineProperty(this, "onUpdate", (o, cb) => this.pubsub.on2(o, 'account', cb));
4982
+ store_defineProperty(this, "onDestroyed", (o, cb) => this.pubsub.on2(o, 'destroy', cb));
4963
4983
  store_defineProperty(this, "onConnection", (o, cb) => this.pubsub.on2(o, 'connection', cb));
4964
4984
  store_defineProperty(this, "onResetCSS", (o, cb) => this.pubsub.on2(o, 'resetCSS', cb));
4965
4985
  store_defineProperty(this, "onHighlight", (o, cb) => this.pubsub.on2(o, 'highlight', cb));
@@ -4996,7 +5016,6 @@ class Store {
4996
5016
  this.FileURL = FileURL;
4997
5017
  this._track_page_view = true;
4998
5018
  this.device_type = 'desktop';
4999
- this.lightConvos = {};
5000
5019
  if (this.fake) this.api = new api_fake(ApiURL, FileURL);else this.api = new api(ApiURL, FileURL);
5001
5020
  if (!inmem) {
5002
5021
  try {
@@ -5261,7 +5280,12 @@ class Store {
5261
5280
  }
5262
5281
  }
5263
5282
  }
5264
- if (this.cookie.get('__sbref', userRef) != userRef) cleanMyLocalStorage();
5283
+ if (this.cookie.get('__sbref', userRef) != userRef) {
5284
+ cleanMyLocalStorage();
5285
+ this.clearBrowserData();
5286
+ }
5287
+ this.lightConvos = {};
5288
+ this.cache = new kv(new lsdb('sbz2023', true), 'kv');
5265
5289
  this.api.setAuth({
5266
5290
  accid: accid
5267
5291
  });
@@ -5334,6 +5358,7 @@ class Store {
5334
5358
  util/* default */.A.clearInterval(this._seenInterval);
5335
5359
  this._seenInterval = util/* default */.A.setInterval(() => {
5336
5360
  if (this._die) return util/* default */.A.clearInterval(this._seenInterval);
5361
+ if (!this.cache) return;
5337
5362
  this.seenQueue = [...new Set(this.seenQueue)];
5338
5363
  let user = this.cache.match('user') || {};
5339
5364
  util/* default */.A.map(this.seenQueue, id => {
@@ -5367,7 +5392,7 @@ class Store {
5367
5392
  let user = this.matchUser();
5368
5393
  if (!old) return;
5369
5394
  let mem = util/* default */.A.find(old.members, mem => mem.type == 'user' && mem.id == user.id);
5370
- mem.seen_at = this.now();
5395
+ if (mem) mem.seen_at = this.now();
5371
5396
  this.kv.put('light_convos', this.lightConvos);
5372
5397
  this.g_haveSentSeenM[lastmsgsent.id] = true;
5373
5398
  return;
@@ -5940,7 +5965,7 @@ class Store {
5940
5965
  async loopTime() {
5941
5966
  if (this._die) return;
5942
5967
  let startms = this.now();
5943
- if (!util/* default */.A.get(this.cache.match('user'), 'id')) return util/* default */.A.setTimeout(() => this.loopTime(), 240000);
5968
+ if (!this.cache || !util/* default */.A.get(this.cache.match('user'), 'id')) return util/* default */.A.setTimeout(() => this.loopTime(), 240000);
5944
5969
  let {
5945
5970
  body: t,
5946
5971
  error
@@ -5977,15 +6002,17 @@ class Store {
5977
6002
  util/* default */.A.each(this.cookie.all(), (v, k) => {
5978
6003
  if ((k || '').startsWith('__sb')) this.cookie.remove(k);
5979
6004
  });
5980
- this.kv.destroy();
6005
+ if (this.kv) this.kv.destroy();
5981
6006
  this.kv = new kv(new lsdb('sbz2023', this.inmem), 'kv');
5982
- this.chain.destroy();
6007
+ if (this.chain) this.chain.destroy();
5983
6008
  this.chain = new Chain(new lsdb('cbz2023', true), 'ch');
6009
+ if (this.realtime) this.realtime.stop();
6010
+ if (this.cache) this.cache.destroy();
6011
+ this.pubsub.publish('destroy');
5984
6012
  }
5985
6013
  destroy() {
5986
6014
  this._die = true;
5987
- if (this.kv) this.kv.destroy();
5988
- if (this.chain) this.chain.destroy();
6015
+ this.clearBrowserData();
5989
6016
  util/* default */.A.clearInterval(this._seenInterval);
5990
6017
  if (this.realtime) this.realtime.stop();
5991
6018
  this.readyGate.close();
@@ -6121,6 +6148,7 @@ let cleanLocalStorage = () => {
6121
6148
  for (var key in localStorage) {
6122
6149
  if (key && (key.startsWith('cbz2020') || key.startsWith('sbz2020'))) {
6123
6150
  localStorage.removeItem(key);
6151
+ store_memlocalStorage.removeItem(key);
6124
6152
  }
6125
6153
  }
6126
6154
  });
@@ -6135,6 +6163,7 @@ let cleanMyLocalStorage = () => {
6135
6163
  for (var key in localStorage) {
6136
6164
  if (key && (key.startsWith('cbz2020') || key.startsWith('sbz2020') || key.startsWith('sbz2022') || key.startsWith('sbz2023'))) {
6137
6165
  localStorage.removeItem(key);
6166
+ store_memlocalStorage.removeItem(key);
6138
6167
  }
6139
6168
  }
6140
6169
  });
@@ -6326,6 +6355,9 @@ class ChatButton extends common/* Com */.Mw {
6326
6355
  let lastview = this.props.store.matchLastview();
6327
6356
  if (util/* default */.A.get(lastview, 'view')) {
6328
6357
  if (lastview.view !== 'list' || util/* default */.A.size(this.props.store.matchConvo('*')) > 0) {
6358
+ if (lastview.view == 'chat' && !util/* default */.A.get(lastview, 'opts.cid')) {
6359
+ return this.onPreviewClick();
6360
+ }
6329
6361
  return this.props.onClick && this.props.onClick(Object.assign({}, lastview.opts, {
6330
6362
  view: lastview.view
6331
6363
  }));
@@ -6356,6 +6388,7 @@ class ChatButton extends common/* Com */.Mw {
6356
6388
  }
6357
6389
  renderPreview() {
6358
6390
  let account = this.props.store.matchAccount();
6391
+ if (!account) return null;
6359
6392
  let accountSetting = this.props.plugin.chatbox;
6360
6393
  let byid = util/* default */.A.get(this.state.previewEvent, 'by.display_id', util/* default */.A.get(this.state.previewEvent, 'by.id'));
6361
6394
  let agent = this.props.store.matchAgent(byid);
@@ -6922,6 +6955,7 @@ Vui lòng hỏi chúng tôi bất cứ điều gì hoặc chia sẻ phản hồi
6922
6955
  if (util/* default */.A.size(allconvos) === 0) return null;
6923
6956
  var $unreadcount = null;
6924
6957
  let user = this.props.store.matchUser();
6958
+ if (!user || !user.id) return null;
6925
6959
  let unreadcount = 0;
6926
6960
  util/* default */.A.map(allconvos, convo => {
6927
6961
  if (convo.id == this.props.cid) return;
@@ -8548,7 +8582,7 @@ class TextMessage extends common/* Com */.Mw {
8548
8582
  }, tags);
8549
8583
  }
8550
8584
  renderBlock(block) {
8551
- if (isEmptyBlock(block)) return null;
8585
+ if (util/* default */.A.isEmptyBlock(block)) return null;
8552
8586
  let t = new util/* default */.A.Date(util/* default */.A.get(this.props.ev, 'created')).toLocaleString();
8553
8587
  let byType = util/* default */.A.get(this.props.ev, 'by.type');
8554
8588
  if (byType !== 'user') byType = 'agent';
@@ -8574,11 +8608,6 @@ class TextMessage extends common/* Com */.Mw {
8574
8608
  }));
8575
8609
  }
8576
8610
  }
8577
- function isEmptyBlock(block) {
8578
- if (!block || !block.type) return true;
8579
- let hasContent = util/* default */.A.size(util/* default */.A.filter(util/* default */.A.map(block.content, block => isEmptyBlock(block)), isEmpty => !isEmpty)) > 0;
8580
- return !util/* default */.A.trim(block.text) && !hasContent;
8581
- }
8582
8611
  ;// ./src/chatbox/InfoMessage.js
8583
8612
 
8584
8613
  function getRateLang(rating) {
@@ -8855,7 +8884,7 @@ class Event extends common/* Com */.Mw {
8855
8884
  let ev = this.props.event.ev;
8856
8885
  let attachments = util/* default */.A.get(ev, 'data.message.attachments');
8857
8886
  let hasForm = util/* default */.A.find(attachments, att => att.type === 'form');
8858
- if (isEmptyMessage(util/* default */.A.get(ev, 'data.message'))) return null;
8887
+ if (util/* default */.A.isEmptyMessage(util/* default */.A.get(ev, 'data.message'))) return null;
8859
8888
  this.tryLoadOrder(ev);
8860
8889
  let t = new util/* default */.A.Date(ev.created).toLocaleString();
8861
8890
  if (util/* default */.A.get(ev, 'by.type') !== 'user') {
@@ -9124,10 +9153,6 @@ class Event extends common/* Com */.Mw {
9124
9153
  return acc.name;
9125
9154
  }
9126
9155
  }
9127
- function isEmptyMessage(msg) {
9128
- if (!msg) return true;
9129
- if (!msg.text && !msg.block && !util/* default */.A.size(msg.attachments)) return true;
9130
- }
9131
9156
  ;// ./src/chatbox/Events.js
9132
9157
 
9133
9158
 
@@ -9946,7 +9971,7 @@ class Widget extends common/* Com */.Mw {
9946
9971
  creating: true
9947
9972
  });
9948
9973
  let botid = cid.split('_')[0].slice(7);
9949
- let oldconvo = this.props.store.matchConvo(this.props.cid);
9974
+ let oldconvo = this.props.store.matchConvo(this.props.cid) || {};
9950
9975
  const {
9951
9976
  convo,
9952
9977
  error
@@ -11078,6 +11103,12 @@ class Main extends common/* Com */.Mw {
11078
11103
  this.props.store.onResetCSS(this, () => this.refreshCSS());
11079
11104
  this.props.store.onConvoEvent(this, ev => this.onConvoEvent(ev));
11080
11105
  this.props.store.onUpdate(this, _ => this.checkExpand());
11106
+ this.props.store.onDestroyed(this, _ => {
11107
+ this.setState({
11108
+ widgetView: '',
11109
+ widgetConvoId: ''
11110
+ });
11111
+ });
11081
11112
  await this.refreshCSS();
11082
11113
  this.setState({
11083
11114
  cssready: true
package/inject.html CHANGED
@@ -4,8 +4,17 @@
4
4
  </head>
5
5
  <body style="background: #ffca7a; display: flex; margin: 0">
6
6
  <div
7
- style="margin: auto; overflow: hidden; position: relative ; width: 750px; height: 100vh; background: #ffe2b7; border-left: 3px dashed #ffa92a; border-right: 3px dashed #ffa92a"
7
+ style="
8
+ margin: auto;
9
+ overflow: hidden;
10
+ position: relative;
11
+ width: 750px;
12
+ height: 100vh;
13
+ background: #ffe2b7;
14
+ border-left: 3px dashed #ffa92a;
15
+ border-right: 3px dashed #ffa92a;
16
+ "
8
17
  class="container"
9
- ></div>
18
+ ></div>
10
19
  </body>
11
20
  </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subiz/widget-v4",
3
- "version": "4.1.163",
3
+ "version": "4.1.166",
4
4
  "main": "./compiled_src/lib.js",
5
5
  "scripts": {
6
6
  "dev": "npx webpack serve --config webpack.dev.js",
@@ -171,6 +171,10 @@ export default class ChatButton extends Com {
171
171
  // if lastview is list, make sure we have at least one convo or fallback
172
172
  // to preview click
173
173
  if (lastview.view !== 'list' || sb.size(this.props.store.matchConvo('*')) > 0) {
174
+ if (lastview.view == 'chat' && !sb.get(lastview, 'opts.cid')) {
175
+ // dont have last view => equal preview click
176
+ return this.onPreviewClick()
177
+ }
174
178
  return this.props.onClick && this.props.onClick(Object.assign({}, lastview.opts, {view: lastview.view})) // last view
175
179
  }
176
180
  }
@@ -181,7 +185,6 @@ export default class ChatButton extends Com {
181
185
 
182
186
  onPreviewClick(e) {
183
187
  let cid = sb.convoOfEv(this.state.previewEvent)
184
-
185
188
  if (!cid) {
186
189
  let convos = sb.map(this.props.store.matchConvo('*'))
187
190
  let convo = sb.find(convos, (convo) => !sb.isConvoSeen(convo, this.props.store.matchUser().id))
@@ -200,6 +203,7 @@ export default class ChatButton extends Com {
200
203
 
201
204
  renderPreview() {
202
205
  let account = this.props.store.matchAccount()
206
+ if (!account) return null
203
207
  let accountSetting = this.props.plugin.chatbox
204
208
  let byid = sb.get(this.state.previewEvent, 'by.display_id', sb.get(this.state.previewEvent, 'by.id'))
205
209
  // let agent = sb.cloneDeep(this.props.store.matchAgent(byid)) || {}
@@ -411,7 +411,8 @@ export default class Event extends Com {
411
411
  renderAIDevNote() {
412
412
  if (!this.props.isLast) return null
413
413
  if (!this.props.store.isAIDevMode()) return null
414
-
414
+ let cid = sb.get(this.props.event.ev, 'data.message.conversation_id', '')
415
+ if (cid.startsWith('cslight')) return null
415
416
  let ev = sb.get(this.props.event, 'ev') || {}
416
417
  let pongs = sb.get(ev, 'data.message.pongs') || []
417
418
  let hasLike = this.hasLike()
@@ -209,6 +209,7 @@ Vui lòng hỏi chúng tôi bất cứ điều gì hoặc chia sẻ phản hồi
209
209
 
210
210
  var $unreadcount = null
211
211
  let user = this.props.store.matchUser()
212
+ if (!user || !user.id) return null
212
213
 
213
214
  let unreadcount = 0
214
215
  sb.map(allconvos, (convo) => {
@@ -783,7 +783,7 @@ export default class Widget extends Com {
783
783
  let _creating = this._creating
784
784
  this.setState({creating: true})
785
785
  let botid = cid.split('_')[0].slice(7) // 6 = len(cslight)
786
- let oldconvo = this.props.store.matchConvo(this.props.cid)
786
+ let oldconvo = this.props.store.matchConvo(this.props.cid) || {}
787
787
  const {convo, error} = await this.props.store.startConvo({
788
788
  locale: this.props.t.locale(),
789
789
  message: text,
@@ -52,6 +52,12 @@ export default class Main extends Com {
52
52
  this.props.store.onResetCSS(this, () => this.refreshCSS())
53
53
  this.props.store.onConvoEvent(this, (ev) => this.onConvoEvent(ev))
54
54
  this.props.store.onUpdate(this, (_) => this.checkExpand())
55
+ this.props.store.onDestroyed(this, (_) => {
56
+ this.setState({
57
+ widgetView: '',
58
+ widgetConvoId: '',
59
+ })
60
+ })
55
61
  await this.refreshCSS()
56
62
  this.setState({cssready: true})
57
63
 
package/src/lib.js CHANGED
@@ -57,7 +57,6 @@ class Widget {
57
57
 
58
58
  async login(accid, user_id, tokenGenerator, $container) {
59
59
  let user = await this.store.login(accid, user_id, tokenGenerator)
60
-
61
60
  if (!$container) $container = document.body
62
61
  this.pluginMgr = new PluginMgr(this, $container)
63
62
 
@@ -3,6 +3,7 @@ var localStorageMemory = {}
3
3
  var cache = {}
4
4
 
5
5
  localStorageMemory.length = 0
6
+ localStorageMemory.cache = cache
6
7
 
7
8
  localStorageMemory.getItem = function (key) {
8
9
  if (key in cache) return cache[key]
package/src/store/kv.js CHANGED
@@ -31,7 +31,6 @@ export default class {
31
31
  inmemItemM[item.key] = item.value
32
32
  })
33
33
  this.cache = inmemItemM
34
-
35
34
  // If the cache is full. Randomly remove half of the cache. credit to thanos
36
35
  // this is so simple since we don't need to keep track of last modified time
37
36
  // for each key. Implementing a LRU cache won't add much value.
package/src/store/lsdb.js CHANGED
@@ -44,10 +44,15 @@ export default class {
44
44
  }
45
45
 
46
46
  clear(table) {
47
+ var keys = []
47
48
  for (var i = 0; i < this.localStorage.length; i++) {
48
49
  let key = this.localStorage.key(i)
49
50
  if (!key.startsWith(this.schema + '#' + table + '#')) continue
50
- this.localStorage.removeItem(key)
51
+ keys.push(key)
52
+ }
53
+
54
+ for (var i = 0; i < keys.length; i++) {
55
+ this.localStorage.removeItem(keys[i])
51
56
  }
52
57
  this.allkeys = {}
53
58
  }
@@ -11,7 +11,7 @@ var cookie = require('cookie_js')
11
11
  cookie.defaults.path = '/'
12
12
  cookie.expiresMultiplier = 60 * 60 * 24 // days
13
13
  cookie.defaults.expires = 3650 // 10 year
14
-
14
+ let memlocalStorage = require('./inmem_localstorage.js')
15
15
  const MAX_CONVO = 20
16
16
  const page_loaded = sb.Date.now()
17
17
 
@@ -35,7 +35,6 @@ export default class Store {
35
35
  this.FileURL = FileURL
36
36
  this._track_page_view = true
37
37
  this.device_type = 'desktop'
38
- this.lightConvos = {} // convo that have not been created, for faking bot conversation
39
38
  if (this.fake) this.api = new APIFake(ApiURL, FileURL)
40
39
  else this.api = new API(ApiURL, FileURL)
41
40
 
@@ -125,6 +124,7 @@ export default class Store {
125
124
  this.setUser({attributes: [{type: 'text', text: ga_client_id, key: 'ga_client_id'}]})
126
125
  }
127
126
  }, 2000)
127
+ this.loopCheckConnectionStatus()
128
128
  }
129
129
 
130
130
  async loopCheckConnectionStatus() {
@@ -331,6 +331,7 @@ export default class Store {
331
331
 
332
332
  // must ensure only call this function after init is done
333
333
  matchUser = () => {
334
+ if (!this.cache) return undefined
334
335
  this._touchAcc()
335
336
  return this.cache.match('user')
336
337
  }
@@ -342,6 +343,7 @@ export default class Store {
342
343
 
343
344
  matchUnread = () => {
344
345
  let user = this.matchUser()
346
+ if (!user || !user.id) return // not init
345
347
  let allconvos = this.matchConvo('*')
346
348
  let unreadcount = 0
347
349
  sb.map(allconvos, (convo) => {
@@ -497,10 +499,7 @@ export default class Store {
497
499
  now = () => sb.Date.now() + this.timeDiff // millisecond on server
498
500
 
499
501
  async init(accid, mask, userRef, options) {
500
- this.readyGate.close()
501
- accid = (accid || '').trim()
502
502
  this.g_sync = {}
503
-
504
503
  if (!options || !options.use_subdomain_cookie) {
505
504
  if (window && window.location && window.location.hostname) {
506
505
  cookie.defaults.domain = '.' + window.location.hostname
@@ -538,21 +537,38 @@ export default class Store {
538
537
  }
539
538
  }
540
539
  }
540
+ if (this.cookie.get('__sbref', userRef) != userRef) {
541
+ cleanMyLocalStorage()
542
+ this.clearBrowserData()
543
+ }
541
544
 
542
- if (this.cookie.get('__sbref', userRef) != userRef) cleanMyLocalStorage()
543
545
  this.api.setAuth({accid: accid})
544
546
  // set cookie forever
545
547
  if (userRef) this.cookie.set('__sbref', userRef)
546
- let user = await this.api.setAuth({accid: accid, mask: mask, ref: userRef})
548
+
549
+ return this._initResource(
550
+ accid,
551
+ {accid: accid, mask: mask, ref: userRef},
552
+ {account_id: accid, user_ref: userRef, user_mask: mask},
553
+ )
554
+ }
555
+
556
+ async _initResource(accid, auth, realtimep) {
557
+ this.g_sync = {}
558
+
559
+ let user = await this.api.setAuth(auth)
547
560
  if (user.error) {
548
561
  console.error(user)
549
562
  return user
550
563
  }
551
564
 
565
+ this.readyGate.close()
566
+ accid = (accid || '').trim()
567
+ this.lightConvos = this.kv.match('light_convos') || {}
568
+ this.cache = new KV(new LSDB('sbz2023', true), 'kv')
552
569
  if (this.realtime) this.realtime.stop()
553
570
  if (this.fake) this.realtime = new RealtimeFake()
554
- else
555
- this.realtime = new Realtime(this.RealtimeURL, {account_id: accid, user_ref: userRef, user_mask: mask}, xhrsend)
571
+ else this.realtime = new Realtime(this.RealtimeURL, realtimep, xhrsend)
556
572
 
557
573
  this.realtime.onEvent(this.onWs.bind(this))
558
574
  this.realtime.onInterrupted(async () => {
@@ -560,43 +576,24 @@ export default class Store {
560
576
  this.reconnect()
561
577
  })
562
578
 
563
- this.lightConvos = this.kv.match('light_convos') || {}
564
579
  this.seenLoop()
565
580
  await this.reconnect({user: user}) // already fetched
566
581
  this.readyGate.open()
567
582
  this.reconnectWs(accid, user.id)
568
- this.loopCheckConnectionStatus()
569
583
  return user
570
584
  }
571
585
 
572
586
  async login(accid, user_id, tokenGenerator) {
573
587
  if (this._die) return dieErr
574
- let user = await this.api.setAuth({accid: accid, profile_id: user_id, tokenGenerator: tokenGenerator})
575
- if (user.error) return user
576
-
588
+ cleanMyLocalStorage()
577
589
  this.clearBrowserData()
578
- this.readyGate.close()
579
- accid = (accid || '').trim()
580
590
 
581
- this.g_sync = {}
582
591
  this.new_visitor = false // for checking returning or new visitor
583
-
584
- if (this.realtime) this.realtime.stop()
585
- if (this.fake) this.realtime = new RealtimeFake()
586
- else this.realtime = new Realtime(this.RealtimeURL, {getAccessToken: () => this.api.token_generator()})
587
-
588
- this.realtime.onEvent(this.onWs.bind(this))
589
- this.realtime.onInterrupted(async () => {
590
- await this.reconnectWs(accid, user.id)
591
- this.reconnect()
592
- })
593
-
594
- this.seenLoop()
595
- await this.reconnect()
596
- this.readyGate.open()
597
- this.reconnectWs(accid, user.id)
598
-
599
- return user
592
+ return this._initResource(
593
+ accid,
594
+ {accid: accid, profile_id: user_id, tokenGenerator: tokenGenerator},
595
+ {getAccessToken: () => this.api.token_generator()},
596
+ )
600
597
  }
601
598
 
602
599
  logout(userRef) {
@@ -611,6 +608,7 @@ export default class Store {
611
608
  sb.clearInterval(this._seenInterval)
612
609
  this._seenInterval = sb.setInterval(() => {
613
610
  if (this._die) return sb.clearInterval(this._seenInterval)
611
+ if (!this.cache) return // not init
614
612
  // deduplicate
615
613
  this.seenQueue = [...new Set(this.seenQueue)]
616
614
  let user = this.cache.match('user') || {}
@@ -647,7 +645,7 @@ export default class Store {
647
645
  let user = this.matchUser()
648
646
  if (!old) return
649
647
  let mem = sb.find(old.members, (mem) => mem.type == 'user' && mem.id == user.id)
650
- mem.seen_at = this.now()
648
+ if (mem) mem.seen_at = this.now()
651
649
  this.kv.put('light_convos', this.lightConvos)
652
650
  this.g_haveSentSeenM[lastmsgsent.id] = true
653
651
  return
@@ -1196,6 +1194,7 @@ export default class Store {
1196
1194
  }
1197
1195
 
1198
1196
  onUpdate = (o, cb) => this.pubsub.on2(o, 'account', cb)
1197
+ onDestroyed = (o, cb) => this.pubsub.on2(o, 'destroy', cb)
1199
1198
  onConnection = (o, cb) => this.pubsub.on2(o, 'connection', cb)
1200
1199
  onResetCSS = (o, cb) => this.pubsub.on2(o, 'resetCSS', cb)
1201
1200
  onHighlight = (o, cb) => this.pubsub.on2(o, 'highlight', cb)
@@ -1310,7 +1309,7 @@ export default class Store {
1310
1309
  let startms = this.now()
1311
1310
 
1312
1311
  // store not init
1313
- if (!sb.get(this.cache.match('user'), 'id')) return sb.setTimeout(() => this.loopTime(), 240000)
1312
+ if (!this.cache || !sb.get(this.cache.match('user'), 'id')) return sb.setTimeout(() => this.loopTime(), 240000)
1314
1313
 
1315
1314
  let {body: t, error} = await this.api.time()
1316
1315
  if (error) return sb.setTimeout(() => this.loopTime(), 30000)
@@ -1352,16 +1351,18 @@ export default class Store {
1352
1351
  sb.each(this.cookie.all(), (v, k) => {
1353
1352
  if ((k || '').startsWith('__sb')) this.cookie.remove(k)
1354
1353
  })
1355
- this.kv.destroy()
1354
+ if (this.kv) this.kv.destroy()
1356
1355
  this.kv = new KV(new LSDB('sbz2023', this.inmem), 'kv')
1357
- this.chain.destroy()
1356
+ if (this.chain) this.chain.destroy()
1358
1357
  this.chain = new Chain(new LSDB('cbz2023', true), 'ch')
1358
+ if (this.realtime) this.realtime.stop()
1359
+ if (this.cache) this.cache.destroy()
1360
+ this.pubsub.publish('destroy')
1359
1361
  }
1360
1362
 
1361
1363
  destroy() {
1362
1364
  this._die = true
1363
- if (this.kv) this.kv.destroy()
1364
- if (this.chain) this.chain.destroy()
1365
+ this.clearBrowserData()
1365
1366
  sb.clearInterval(this._seenInterval)
1366
1367
  if (this.realtime) this.realtime.stop()
1367
1368
  this.readyGate.close()
@@ -1511,6 +1512,7 @@ let cleanLocalStorage = () => {
1511
1512
  for (var key in localStorage) {
1512
1513
  if (key && (key.startsWith('cbz2020') || key.startsWith('sbz2020'))) {
1513
1514
  localStorage.removeItem(key)
1515
+ memlocalStorage.removeItem(key)
1514
1516
  }
1515
1517
  }
1516
1518
  })
@@ -1532,6 +1534,7 @@ let cleanMyLocalStorage = () => {
1532
1534
  key.startsWith('sbz2023'))
1533
1535
  ) {
1534
1536
  localStorage.removeItem(key)
1537
+ memlocalStorage.removeItem(key)
1535
1538
  }
1536
1539
  }
1537
1540
  })
package/tracking.js CHANGED
@@ -22,6 +22,7 @@ setTimeout(() => {
22
22
  // Collect device and browser info
23
23
  var info = {
24
24
  status: status,
25
+ href: window.location.href,
25
26
  userAgent: navigator.userAgent,
26
27
  platform: navigator.platform,
27
28
  language: navigator.language,
@@ -41,4 +42,4 @@ setTimeout(() => {
41
42
  }
42
43
 
43
44
  xhr.send(JSON.stringify(info))
44
- }, 10000)
45
+ }, 15000)