@abtnode/core 1.16.14 → 1.16.15-beta-74505f06

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/lib/api/team.js CHANGED
@@ -13,6 +13,7 @@ const {
13
13
  MAX_USER_PAGE_SIZE,
14
14
  STORE_DETAIL_PAGE_PATH_PREFIX,
15
15
  MAIN_CHAIN_ENDPOINT,
16
+ USER_AVATAR_URL_PREFIX,
16
17
  } = require('@abtnode/constant');
17
18
  const { isValid: isValidDid } = require('@arcblock/did');
18
19
  const { BlockletEvents } = require('@blocklet/constant');
@@ -25,9 +26,10 @@ const {
25
26
  } = require('@abtnode/auth/lib/passport');
26
27
  const { getPassportStatusEndpoint } = require('@abtnode/auth/lib/auth');
27
28
  const getBlockletInfo = require('@blocklet/meta/lib/info');
28
- const { getUserAvatarUrl, getAppAvatarUrl } = require('@abtnode/util/lib/user');
29
+ const { getUserAvatarUrl, getAppAvatarUrl, extractUserAvatar, getAvatarByUrl } = require('@abtnode/util/lib/user');
29
30
  const { getChainClient } = require('@abtnode/util/lib/get-chain-client');
30
31
  const { getWalletDid } = require('@blocklet/meta/lib/did-utils');
32
+ const isUrl = require('is-url');
31
33
 
32
34
  const { validateTrustedPassportIssuers } = require('../validators/trusted-passport');
33
35
  const { validateTrustedFactories } = require('../validators/trusted-factory');
@@ -36,6 +38,7 @@ const { validateCreatePermission, validateUpdatePermission } = require('../valid
36
38
 
37
39
  const { getBlocklet } = require('../util/blocklet');
38
40
  const StoreUtil = require('../util/store');
41
+ const { profileSchema } = require('../validators/user');
39
42
 
40
43
  const sanitizeUrl = (url) => {
41
44
  if (!url) {
@@ -466,6 +469,46 @@ class TeamAPI extends EventEmitter {
466
469
  return doc;
467
470
  }
468
471
 
472
+ async switchProfile({ teamDid, userDid, profile }) {
473
+ const state = await this.getUserState(teamDid);
474
+ // NOTICE: 这个 schema 没有对数据做任何 default 操作,所以没必要用 validate 之后的值
475
+ const { error } = profileSchema.validate({ ...profile, did: userDid });
476
+ if (error) {
477
+ throw new Error(error);
478
+ }
479
+ const oldUser = await state.getUser(userDid);
480
+ if (!oldUser) {
481
+ throw new Error('User is not exist', { userDid, teamDid });
482
+ }
483
+
484
+ let { avatar } = profile;
485
+ if (avatar.startsWith(USER_AVATAR_URL_PREFIX)) {
486
+ // pass
487
+ } else if (isUrl(avatar)) {
488
+ try {
489
+ avatar = await getAvatarByUrl(avatar);
490
+ avatar = await extractUserAvatar(avatar, { dataDirs: this.dataDirs });
491
+ } catch {
492
+ logger.error('Failed to convert external avatar', { teamDid, userDid, avatar });
493
+ throw new Error('Failed to convert external avatar');
494
+ }
495
+ } else {
496
+ // 仅当 avatar 是 base64 时,对 avatar 进行处理
497
+ const regex = /^data:image\/(\w+);base64,/;
498
+ const match = regex.exec(avatar);
499
+ if (match) {
500
+ avatar = await extractUserAvatar(avatar, { dataDirs: this.dataDirs });
501
+ } else {
502
+ logger.error('Profile avatar is invalid', { teamDid, userDid, profile });
503
+ throw new Error('Profile avatar is invalid');
504
+ }
505
+ }
506
+ const doc = await state.updateUser(userDid, { ...profile, avatar });
507
+ logger.info('User switch-profile successfully', { teamDid, userDid });
508
+ this.emit(EVENTS.USER_UPDATED, { teamDid, user: doc });
509
+ return doc;
510
+ }
511
+
469
512
  // Invite member
470
513
 
471
514
  /**
package/lib/index.js CHANGED
@@ -354,6 +354,7 @@ function ABTNode(options) {
354
354
  getUserByDid: teamAPI.getUserByDid.bind(teamAPI),
355
355
  isPassportValid: teamAPI.isPassportValid.bind(teamAPI),
356
356
  isConnectedAccount: teamAPI.isConnectedAccount.bind(teamAPI),
357
+ switchProfile: teamAPI.switchProfile.bind(teamAPI),
357
358
 
358
359
  // Access Control
359
360
  getRBAC: (did = options.nodeDid) => teamManager.getRBAC(did),
@@ -40,4 +40,12 @@ const loginSchema = Joi.object({
40
40
  .required(),
41
41
  }).unknown();
42
42
 
43
+ const profileSchema = Joi.object({
44
+ did: Joi.DID().trim().required(),
45
+ fullName: Joi.string().empty(''),
46
+ avatar: Joi.string().empty(''),
47
+ email: Joi.string().empty(''),
48
+ });
49
+
43
50
  exports.loginSchema = loginSchema;
51
+ exports.profileSchema = profileSchema;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.14",
6
+ "version": "1.16.15-beta-74505f06",
7
7
  "description": "",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,19 +19,19 @@
19
19
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
20
20
  "license": "Apache-2.0",
21
21
  "dependencies": {
22
- "@abtnode/analytics": "1.16.14",
23
- "@abtnode/auth": "1.16.14",
24
- "@abtnode/certificate-manager": "1.16.14",
25
- "@abtnode/constant": "1.16.14",
26
- "@abtnode/cron": "1.16.14",
27
- "@abtnode/logger": "1.16.14",
28
- "@abtnode/models": "1.16.14",
29
- "@abtnode/queue": "1.16.14",
30
- "@abtnode/rbac": "1.16.14",
31
- "@abtnode/router-provider": "1.16.14",
32
- "@abtnode/static-server": "1.16.14",
33
- "@abtnode/timemachine": "1.16.14",
34
- "@abtnode/util": "1.16.14",
22
+ "@abtnode/analytics": "1.16.15-beta-74505f06",
23
+ "@abtnode/auth": "1.16.15-beta-74505f06",
24
+ "@abtnode/certificate-manager": "1.16.15-beta-74505f06",
25
+ "@abtnode/constant": "1.16.15-beta-74505f06",
26
+ "@abtnode/cron": "1.16.15-beta-74505f06",
27
+ "@abtnode/logger": "1.16.15-beta-74505f06",
28
+ "@abtnode/models": "1.16.15-beta-74505f06",
29
+ "@abtnode/queue": "1.16.15-beta-74505f06",
30
+ "@abtnode/rbac": "1.16.15-beta-74505f06",
31
+ "@abtnode/router-provider": "1.16.15-beta-74505f06",
32
+ "@abtnode/static-server": "1.16.15-beta-74505f06",
33
+ "@abtnode/timemachine": "1.16.15-beta-74505f06",
34
+ "@abtnode/util": "1.16.15-beta-74505f06",
35
35
  "@arcblock/did": "1.18.89",
36
36
  "@arcblock/did-auth": "1.18.89",
37
37
  "@arcblock/did-ext": "^1.18.89",
@@ -42,10 +42,10 @@
42
42
  "@arcblock/pm2-events": "^0.0.5",
43
43
  "@arcblock/validator": "^1.18.89",
44
44
  "@arcblock/vc": "1.18.89",
45
- "@blocklet/constant": "1.16.14",
46
- "@blocklet/meta": "1.16.14",
47
- "@blocklet/resolver": "1.16.14",
48
- "@blocklet/sdk": "1.16.14",
45
+ "@blocklet/constant": "1.16.15-beta-74505f06",
46
+ "@blocklet/meta": "1.16.15-beta-74505f06",
47
+ "@blocklet/resolver": "1.16.15-beta-74505f06",
48
+ "@blocklet/sdk": "1.16.15-beta-74505f06",
49
49
  "@did-space/client": "^0.2.141",
50
50
  "@fidm/x509": "^1.2.1",
51
51
  "@ocap/mcrypto": "1.18.89",
@@ -98,5 +98,5 @@
98
98
  "jest": "^27.5.1",
99
99
  "unzipper": "^0.10.11"
100
100
  },
101
- "gitHead": "a2f588d8d5cbe9312260ffbd8fb4cb3bd4104853"
101
+ "gitHead": "344ae4a65afe846111eb1f901e09e0f59f333861"
102
102
  }