@peers-app/peers-sdk 0.7.26 → 0.7.28

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.
@@ -158,13 +158,13 @@ class UserContext {
158
158
  // sync my user to personal db
159
159
  if (!(0, lodash_1.isEqual)(me, meSigned)) {
160
160
  (0, keys_1.verifyObjectSignature)(meSigned);
161
- await (0, data_1.Users)(userContext.userDataContext).save(meSigned);
161
+ await (0, data_1.Users)(userContext.userDataContext).save(meSigned, { weakInsert: true });
162
162
  }
163
163
  // sync my user to all my groups
164
164
  for (const [, dataContext] of userContext.groupDataContexts) {
165
165
  let groupMe = await (0, data_1.Users)(dataContext).get(me.userId);
166
166
  if (!(0, lodash_1.isEqual)(groupMe, meSigned)) {
167
- await (0, data_1.Users)(dataContext).save(meSigned);
167
+ await (0, data_1.Users)(dataContext).save(meSigned, { weakInsert: true });
168
168
  }
169
169
  }
170
170
  // sync group objects to my personal db
@@ -1,6 +1,7 @@
1
1
  export interface ISaveOptions {
2
2
  restoreIfDeleted?: boolean;
3
3
  saveAsSnapshot?: boolean;
4
+ weakInsert?: boolean;
4
5
  }
5
6
  export interface IDataSource<T extends {
6
7
  [key: string]: any;
@@ -13,19 +13,24 @@ function getTrustLevelFn(me, serverUrl) {
13
13
  // if (deviceInfo.deviceId === thisDeviceId()) {
14
14
  // return TrustLevel.Untrusted;
15
15
  // }
16
- const device = await (0, data_1.Devices)(userDataContext).get(deviceInfo.deviceId) || {
16
+ const existingDevice = await (0, data_1.Devices)(userDataContext).get(deviceInfo.deviceId);
17
+ const device = existingDevice || {
17
18
  deviceId: deviceInfo.deviceId,
18
19
  userId: deviceInfo.userId,
19
20
  firstSeen: new Date(),
20
21
  lastSeen: new Date(),
21
- trustLevel: socket_type_1.TrustLevel.Trusted,
22
+ trustLevel: socket_type_1.TrustLevel.NewDevice,
22
23
  serverUrl,
23
24
  };
24
25
  device.lastSeen = new Date();
25
- device.trustLevel = socket_type_1.TrustLevel.Trusted;
26
+ const seenMoreThan2DaysAgo = Date.now() - device.firstSeen.getTime() > 1000 * 60 * 60 * 24 * 2;
27
+ device.trustLevel = seenMoreThan2DaysAgo ? socket_type_1.TrustLevel.Trusted : socket_type_1.TrustLevel.NewDevice;
26
28
  console.log(`Updating my own device: ${deviceInfo.deviceId}`);
27
- await (0, data_1.Devices)(userDataContext).save(device, { restoreIfDeleted: true });
28
- return socket_type_1.TrustLevel.Trusted;
29
+ await (0, data_1.Devices)(userDataContext).save(device, {
30
+ restoreIfDeleted: true,
31
+ weakInsert: true,
32
+ });
33
+ return device.trustLevel;
29
34
  }
30
35
  let [user, device, userTrustLevel] = await Promise.all([
31
36
  (0, data_1.Users)(userDataContext).get(deviceInfo.userId),
@@ -113,10 +118,16 @@ function getTrustLevelFn(me, serverUrl) {
113
118
  // }
114
119
  if (newUser) {
115
120
  // TODO: I'm not sure we immediately want to save this user to the my personal db...
116
- await (0, data_1.Users)(userDataContext).save(user, { restoreIfDeleted: true });
121
+ await (0, data_1.Users)(userDataContext).save(user, {
122
+ restoreIfDeleted: true,
123
+ weakInsert: true,
124
+ });
117
125
  }
118
126
  // device.trustLevel = remoteTrustLevel || trustLevel;
119
- await (0, data_1.Devices)(userDataContext).save(device, { restoreIfDeleted: true });
127
+ await (0, data_1.Devices)(userDataContext).save(device, {
128
+ restoreIfDeleted: true,
129
+ weakInsert: true,
130
+ });
120
131
  return device.trustLevel;
121
132
  };
122
133
  }
package/dist/utils.js CHANGED
@@ -59,10 +59,9 @@ function newid() {
59
59
  // Allocating one more character to the time and one less to the random number to avoid hitting max time in any conceivable future
60
60
  // now our max time is new Date(Number.parseInt('f55n5nmuua',36)) == +050705-08-09T23:40:06.178Z. 50k years from now.
61
61
  // We still have 15 chars for our random number. 36 ** 15 ~= 2e23
62
- // That is still a very large number and the chance of a collision should be so small as to be effectively unique in any single user's database
63
- // I wish I knew the math to prove that out.
62
+ // That is still a very large number and the chance of a collision should be so small as to be effectively globally unique
63
+ // and certainly unique in any group's database.
64
64
  const time = Date.now().toString(36).padStart(10, '0'); // e.g: "00kq6xh45f", length == 10
65
- //use nacl.randomBytes to be cryptographically secure
66
65
  return time + cryptoRandomString(15);
67
66
  }
68
67
  exports.MIN_ID = '0'.repeat(25);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peers-app/peers-sdk",
3
- "version": "0.7.26",
3
+ "version": "0.7.28",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/peers-app/peers-sdk.git"