@jskit-ai/users-core 0.2.3 → 0.2.5
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/locales/en.json +1 -7
- package/package.json +42 -19
- package/patterns/user-administration-server/PATTERN.md +16 -1
- package/patterns/user-administration-server/example/packages/users/package.json +3 -2
- package/patterns/user-administration-server/example/packages/users/src/server/types.js +5 -0
- package/patterns/user-administration-server/example/packages/users-workspace/package.json +4 -3
- package/patterns/user-administration-server/example/packages/users-workspace/src/server/UsersWorkspaceFeature.js +2 -9
- package/patterns/user-administration-server/example/packages/users-workspace/src/server/types.js +5 -0
- package/src/server/UsersFeature.js +26 -78
- package/src/server/UsersIdentityProvider.js +11 -22
- package/src/server/accountNotifications/accountNotificationsActions.js +28 -41
- package/src/server/accountNotifications/{bootAccountNotificationsRoutes.js → accountNotificationsRoutes.js} +1 -4
- package/src/server/accountNotifications/accountNotificationsService.js +9 -10
- package/src/server/accountPreferences/accountPreferencesActions.js +28 -41
- package/src/server/accountPreferences/{bootAccountPreferencesRoutes.js → accountPreferencesRoutes.js} +1 -4
- package/src/server/accountPreferences/accountPreferencesService.js +10 -10
- package/src/server/accountProfile/accountProfileActions.js +87 -127
- package/src/server/accountProfile/{bootAccountProfileRoutes.js → accountProfileRoutes.js} +12 -18
- package/src/server/accountProfile/accountProfileService.js +19 -35
- package/src/server/accountProfile/avatarService.js +15 -39
- package/src/server/accountProfile/avatarStorageService.js +2 -1
- package/src/server/accountSecurity/accountSecurityActions.js +100 -148
- package/src/server/accountSecurity/{bootAccountSecurityRoutes.js → accountSecurityRoutes.js} +9 -8
- package/src/server/accountSecurity/accountSecurityService.js +15 -44
- package/src/server/common/formatters/accountAvatarFormatter.js +4 -4
- package/src/server/common/formatters/accountSecurityStatusFormatter.js +2 -3
- package/src/server/common/formatters/accountSettingsResponseFormatter.js +11 -2
- package/src/server/common/repositories/userProfilesRepository.js +31 -55
- package/src/server/common/repositories/userSettingsRepository.js +16 -21
- package/src/server/common/services/accountContextService.js +9 -7
- package/src/server/common/services/authProfileSyncService.js +15 -42
- package/src/server/common/support/accountSettingsJsonApiTransport.js +10 -3
- package/src/server/common/support/realtimeServiceEvents.js +3 -2
- package/src/server/common/support/resolveActionUser.js +9 -2
- package/src/server/types.js +29 -0
- package/src/server/usersBootstrapContributor.js +60 -84
- package/src/server/usersExtensions.js +1 -0
- package/src/server/usersIdentity.js +28 -0
- package/src/server/usersRuntime.js +61 -0
- package/src/shared/operationMessages.js +6 -15
- package/src/shared/resources/userProfileResource.js +3 -31
- package/src/shared/resources/userProfileValues.js +32 -0
- package/src/shared/resources/userSettingsResource.js +2 -4
- package/test/accountProfileService.test.js +1 -0
- package/test/accountSecurityService.test.js +2 -4
- package/test/exportsContract.test.js +2 -0
- package/test/featureRuntime.test.js +11 -8
- package/test/i18nCodes.test.js +3 -10
- package/test/usersBootstrapContributor.test.js +0 -1
- package/test/usersPackageScaffoldContract.test.js +3 -5
- package/test/usersRouteRequestInputValidator.test.js +4 -4
- package/src/server/common/repositories/repositoryUtils.js +0 -44
- package/src/server/common/support/deepFreeze.js +0 -1
|
@@ -8,16 +8,16 @@ import {
|
|
|
8
8
|
accountSettingsResponseFormatter
|
|
9
9
|
} from "../common/formatters/accountSettingsResponseFormatter.js";
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
async function updatePreferences(request, user, payload
|
|
11
|
+
/**
|
|
12
|
+
* A signed-in user's display and regional preferences.
|
|
13
|
+
* @param {object} dependencies
|
|
14
|
+
* @param {import("../types.js").UserSettingsRepository} dependencies.userSettingsRepository
|
|
15
|
+
* @param {import("../types.js").UserProfilesRepository} dependencies.userProfilesRepository
|
|
16
|
+
* @param {import("@jskit-ai/auth-core/server/types").AuthService} dependencies.authService The installed provider's `auth.service`; it reports the security status.
|
|
17
|
+
*/
|
|
18
|
+
function createService({ userSettingsRepository, userProfilesRepository, authService }) {
|
|
19
|
+
// Saves the validated preference fields and answers with the account's settings.
|
|
20
|
+
async function updatePreferences(request, user, payload) {
|
|
21
21
|
const profile = await resolveUserProfile(userProfilesRepository, user);
|
|
22
22
|
if (!profile) {
|
|
23
23
|
throw new AppError(404, "User profile was not found.", { code: "users.profile.notFound" });
|
|
@@ -13,139 +13,99 @@ const settingsProfileUpdateInputValidator = deepFreeze({
|
|
|
13
13
|
mode: userProfileResource.operations.patch.body.mode
|
|
14
14
|
});
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
idempotency: "none",
|
|
36
|
-
async run(accountProfileService, input, context) {
|
|
37
|
-
return accountProfileService.getForUser(resolveRequest(context), resolveActionUser(context, input), {
|
|
38
|
-
context
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
id: "settings.profile.update",
|
|
44
|
-
version: 1,
|
|
45
|
-
kind: "command",
|
|
46
|
-
channels: ["api", "automation", "internal"],
|
|
47
|
-
surfaces: ["*"],
|
|
48
|
-
permission: {
|
|
49
|
-
require: "authenticated"
|
|
50
|
-
},
|
|
51
|
-
input: settingsProfileUpdateInputValidator,
|
|
52
|
-
output: null,
|
|
53
|
-
extensions: {
|
|
54
|
-
assistant: {
|
|
55
|
-
description: "Update the signed-in user’s display name.",
|
|
56
|
-
output: userSettingsResource.operations.view.output,
|
|
57
|
-
transformResult: (result) => result.response.value
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
idempotency: "optional",
|
|
61
|
-
events: ACCOUNT_SETTINGS_AND_BOOTSTRAP_EVENTS,
|
|
62
|
-
async run(accountProfileService, input, context) {
|
|
63
|
-
return accountProfileService.updateProfile(
|
|
64
|
-
resolveRequest(context),
|
|
65
|
-
resolveActionUser(context, input),
|
|
66
|
-
input,
|
|
67
|
-
{
|
|
68
|
-
context
|
|
16
|
+
/** @param {{ accountProfileService: import("../types.js").AccountProfileService }} dependencies */
|
|
17
|
+
function createAccountProfileActions({ accountProfileService }) {
|
|
18
|
+
return Object.freeze([
|
|
19
|
+
Object.freeze({
|
|
20
|
+
id: "settings.read",
|
|
21
|
+
version: 1,
|
|
22
|
+
kind: "query",
|
|
23
|
+
channels: ["api", "automation", "internal"],
|
|
24
|
+
surfaces: ["*"],
|
|
25
|
+
permission: {
|
|
26
|
+
require: "authenticated"
|
|
27
|
+
},
|
|
28
|
+
input: emptyInputValidator,
|
|
29
|
+
output: null,
|
|
30
|
+
extensions: {
|
|
31
|
+
assistant: {
|
|
32
|
+
description: "Read the signed-in user’s profile, preferences, notifications and security status.",
|
|
33
|
+
output: userSettingsResource.operations.view.output,
|
|
34
|
+
transformResult: (result) => result.value
|
|
69
35
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
stream: input.stream,
|
|
94
|
-
mimeType: input.mimeType,
|
|
95
|
-
fileName: input.fileName,
|
|
96
|
-
uploadDimension: input.uploadDimension
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
return accountProfileService.uploadAvatar(
|
|
36
|
+
},
|
|
37
|
+
execute: (input, context) => accountProfileService.getForUser(resolveRequest(context), resolveActionUser(context, input))
|
|
38
|
+
}),
|
|
39
|
+
Object.freeze({
|
|
40
|
+
id: "settings.profile.update",
|
|
41
|
+
version: 1,
|
|
42
|
+
kind: "command",
|
|
43
|
+
channels: ["api", "automation", "internal"],
|
|
44
|
+
surfaces: ["*"],
|
|
45
|
+
permission: {
|
|
46
|
+
require: "authenticated"
|
|
47
|
+
},
|
|
48
|
+
input: settingsProfileUpdateInputValidator,
|
|
49
|
+
output: null,
|
|
50
|
+
extensions: {
|
|
51
|
+
assistant: {
|
|
52
|
+
description: "Update the signed-in user’s display name.",
|
|
53
|
+
output: userSettingsResource.operations.view.output,
|
|
54
|
+
transformResult: (result) => result.response.value
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
events: ACCOUNT_SETTINGS_AND_BOOTSTRAP_EVENTS,
|
|
58
|
+
execute: (input, context) => accountProfileService.updateProfile(
|
|
100
59
|
resolveRequest(context),
|
|
101
60
|
resolveActionUser(context, input),
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
61
|
+
input
|
|
62
|
+
)
|
|
63
|
+
}),
|
|
64
|
+
Object.freeze({
|
|
65
|
+
id: "settings.profile.avatar.upload",
|
|
66
|
+
version: 1,
|
|
67
|
+
kind: "command",
|
|
68
|
+
channels: ["api", "automation", "internal"],
|
|
69
|
+
surfaces: ["*"],
|
|
70
|
+
permission: {
|
|
71
|
+
require: "authenticated"
|
|
72
|
+
},
|
|
73
|
+
input: userProfileResource.operations.avatarUpload.body,
|
|
74
|
+
output: null,
|
|
75
|
+
extensions: {
|
|
76
|
+
assistant: {
|
|
77
|
+
exclude: "Use the authenticated account profile file picker; upload streams must not enter assistant arguments."
|
|
105
78
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
{
|
|
110
|
-
id: "settings.profile.avatar.delete",
|
|
111
|
-
version: 1,
|
|
112
|
-
kind: "command",
|
|
113
|
-
channels: ["api", "automation", "internal"],
|
|
114
|
-
surfaces: ["*"],
|
|
115
|
-
permission: {
|
|
116
|
-
require: "authenticated"
|
|
117
|
-
},
|
|
118
|
-
input: userProfileResource.operations.avatarDelete.body,
|
|
119
|
-
output: null,
|
|
120
|
-
extensions: {
|
|
121
|
-
assistant: {
|
|
122
|
-
description: "Remove the signed-in user’s uploaded avatar.",
|
|
123
|
-
output: userSettingsResource.operations.view.output,
|
|
124
|
-
transformResult: (result) => result.value
|
|
125
|
-
}
|
|
126
|
-
},
|
|
127
|
-
idempotency: "none",
|
|
128
|
-
events: ACCOUNT_SETTINGS_AND_BOOTSTRAP_EVENTS,
|
|
129
|
-
async run(accountProfileService, input, context) {
|
|
130
|
-
return accountProfileService.deleteAvatar(
|
|
79
|
+
},
|
|
80
|
+
events: ACCOUNT_SETTINGS_AND_BOOTSTRAP_EVENTS,
|
|
81
|
+
execute: (input, context) => accountProfileService.uploadAvatar(
|
|
131
82
|
resolveRequest(context),
|
|
132
83
|
resolveActionUser(context, input),
|
|
133
|
-
|
|
134
|
-
|
|
84
|
+
input
|
|
85
|
+
)
|
|
86
|
+
}),
|
|
87
|
+
Object.freeze({
|
|
88
|
+
id: "settings.profile.avatar.delete",
|
|
89
|
+
version: 1,
|
|
90
|
+
kind: "command",
|
|
91
|
+
channels: ["api", "automation", "internal"],
|
|
92
|
+
surfaces: ["*"],
|
|
93
|
+
permission: {
|
|
94
|
+
require: "authenticated"
|
|
95
|
+
},
|
|
96
|
+
input: { schema: userProfileResource.operations.avatarDelete.body.schema },
|
|
97
|
+
output: null,
|
|
98
|
+
extensions: {
|
|
99
|
+
assistant: {
|
|
100
|
+
description: "Remove the signed-in user’s uploaded avatar.",
|
|
101
|
+
output: userSettingsResource.operations.view.output,
|
|
102
|
+
transformResult: (result) => result.value
|
|
135
103
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
function buildAccountProfileActions({ accountProfileService } = {}) {
|
|
142
|
-
if (!accountProfileService) throw new TypeError("buildAccountProfileActions requires accountProfileService.");
|
|
143
|
-
return accountProfileActionSpecifications.map(({ run, ...definition }) => Object.freeze({
|
|
144
|
-
...definition,
|
|
145
|
-
execute(input, context) {
|
|
146
|
-
return run(accountProfileService, input, context);
|
|
147
|
-
}
|
|
148
|
-
}));
|
|
104
|
+
},
|
|
105
|
+
events: ACCOUNT_SETTINGS_AND_BOOTSTRAP_EVENTS,
|
|
106
|
+
execute: (input, context) => accountProfileService.deleteAvatar(resolveRequest(context), resolveActionUser(context, input))
|
|
107
|
+
})
|
|
108
|
+
]);
|
|
149
109
|
}
|
|
150
110
|
|
|
151
|
-
export {
|
|
111
|
+
export { createAccountProfileActions };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { withStandardErrorResponses } from "@jskit-ai/http-runtime/shared/validators/errorResponses";
|
|
2
2
|
import { createJsonApiResourceRouteContract } from "@jskit-ai/http-runtime/shared/validators/jsonApiRouteTransport";
|
|
3
3
|
import { DEFAULT_IMAGE_UPLOAD_MAX_BYTES } from "@jskit-ai/uploads-runtime/shared";
|
|
4
|
+
import { resolveActor } from "@jskit-ai/kernel/server/actions/actionContributorHelpers";
|
|
4
5
|
import { userSettingsResource } from "../../shared/resources/userSettingsResource.js";
|
|
5
6
|
import { userProfileResource } from "../../shared/resources/userProfileResource.js";
|
|
6
7
|
import { resolveAccountSettingsResourceId } from "../common/support/accountSettingsJsonApiTransport.js";
|
|
@@ -12,17 +13,14 @@ const USER_SETTINGS_RESOURCE_TRANSPORT = Object.freeze({
|
|
|
12
13
|
getRecordId: resolveAccountSettingsResourceId
|
|
13
14
|
});
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
throw new TypeError("registerAccountProfileRoutes requires runtime.uploads.");
|
|
24
|
-
}
|
|
25
|
-
|
|
16
|
+
/**
|
|
17
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeRouter} router
|
|
18
|
+
* @param {object} dependencies
|
|
19
|
+
* @param {import("../types.js").AccountProfileService} dependencies.accountProfileService
|
|
20
|
+
* @param {import("@jskit-ai/auth-core/server/types").AuthService} dependencies.authService
|
|
21
|
+
* @param {import("@jskit-ai/uploads-runtime/server/types").RuntimeUploads} dependencies.uploads
|
|
22
|
+
*/
|
|
23
|
+
function registerAccountProfileRoutes(router, { accountProfileService, authService, uploads }) {
|
|
26
24
|
router.register(
|
|
27
25
|
"GET",
|
|
28
26
|
"/api/settings",
|
|
@@ -66,11 +64,11 @@ function registerAccountProfileRoutes(router, { accountProfileService, authServi
|
|
|
66
64
|
input: request.input.body
|
|
67
65
|
});
|
|
68
66
|
|
|
69
|
-
if (result
|
|
67
|
+
if (result.session) {
|
|
70
68
|
authService.writeSessionCookies(reply, result.session);
|
|
71
69
|
}
|
|
72
70
|
|
|
73
|
-
reply.code(200).send(result
|
|
71
|
+
reply.code(200).send(result.response);
|
|
74
72
|
}
|
|
75
73
|
);
|
|
76
74
|
|
|
@@ -85,11 +83,7 @@ function registerAccountProfileRoutes(router, { accountProfileService, authServi
|
|
|
85
83
|
}
|
|
86
84
|
},
|
|
87
85
|
async function (request, reply) {
|
|
88
|
-
const avatar = await accountProfileService.readAvatar(
|
|
89
|
-
context: {
|
|
90
|
-
actor: request.user
|
|
91
|
-
}
|
|
92
|
-
});
|
|
86
|
+
const avatar = await accountProfileService.readAvatar(resolveActor(null, request));
|
|
93
87
|
|
|
94
88
|
reply
|
|
95
89
|
.header("Cache-Control", "private, max-age=31536000, immutable")
|
|
@@ -8,17 +8,16 @@ import {
|
|
|
8
8
|
accountSettingsResponseFormatter
|
|
9
9
|
} from "../common/formatters/accountSettingsResponseFormatter.js";
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
async function getForUser(request, user, options = {}) {
|
|
11
|
+
/**
|
|
12
|
+
* A signed-in user's profile: display name and avatar.
|
|
13
|
+
* @param {object} dependencies
|
|
14
|
+
* @param {import("../types.js").UserSettingsRepository} dependencies.userSettingsRepository
|
|
15
|
+
* @param {import("../types.js").UserProfilesRepository} dependencies.userProfilesRepository
|
|
16
|
+
* @param {import("@jskit-ai/auth-core/server/types").AuthService} dependencies.authService The installed provider's `auth.service`; it may keep the display name itself.
|
|
17
|
+
* @param {import("../types.js").AvatarService} dependencies.avatarService
|
|
18
|
+
*/
|
|
19
|
+
function createService({ userSettingsRepository, userProfilesRepository, authService, avatarService }) {
|
|
20
|
+
async function getForUser(request, user) {
|
|
22
21
|
const profile = await resolveUserProfile(userProfilesRepository, user);
|
|
23
22
|
if (!profile) {
|
|
24
23
|
throw new AppError(404, "User profile was not found.", { code: "users.profile.notFound" });
|
|
@@ -35,23 +34,16 @@ function createService({
|
|
|
35
34
|
}));
|
|
36
35
|
}
|
|
37
36
|
|
|
38
|
-
async function updateProfile(request, user, payload
|
|
37
|
+
async function updateProfile(request, user, payload) {
|
|
39
38
|
const profile = await resolveUserProfile(userProfilesRepository, user);
|
|
40
39
|
if (!profile) {
|
|
41
40
|
throw new AppError(404, "User profile was not found.", { code: "users.profile.notFound" });
|
|
42
41
|
}
|
|
43
42
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
session = result?.session || null;
|
|
49
|
-
updatedProfile = result?.profile || null;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (!updatedProfile) {
|
|
53
|
-
updatedProfile = await userProfilesRepository.updateDisplayNameById(profile.id, payload.displayName);
|
|
54
|
-
}
|
|
43
|
+
const result = await authService.updateDisplayName(request, { displayName: payload.displayName });
|
|
44
|
+
const session = result?.session || null;
|
|
45
|
+
const updatedProfile =
|
|
46
|
+
result?.profile || (await userProfilesRepository.updateDisplayNameById(profile.id, payload.displayName));
|
|
55
47
|
|
|
56
48
|
const settings = await userSettingsRepository.ensureForUserId(updatedProfile.id);
|
|
57
49
|
const securityStatus = await resolveSecurityStatus(authService, request);
|
|
@@ -67,15 +59,11 @@ function createService({
|
|
|
67
59
|
};
|
|
68
60
|
}
|
|
69
61
|
|
|
70
|
-
async function uploadAvatar(request, user, avatarUpload
|
|
71
|
-
void options;
|
|
72
|
-
|
|
62
|
+
async function uploadAvatar(request, user, avatarUpload) {
|
|
73
63
|
const result = await avatarService.uploadForUser(user, avatarUpload);
|
|
74
64
|
const profile = result?.profile || null;
|
|
75
65
|
if (!profile) {
|
|
76
|
-
throw new
|
|
77
|
-
code: "users.avatar.uploadResultMissing"
|
|
78
|
-
});
|
|
66
|
+
throw new Error("Avatar upload completed without a profile result.");
|
|
79
67
|
}
|
|
80
68
|
|
|
81
69
|
const settings = await userSettingsRepository.ensureForUserId(profile.id);
|
|
@@ -89,9 +77,7 @@ function createService({
|
|
|
89
77
|
}));
|
|
90
78
|
}
|
|
91
79
|
|
|
92
|
-
async function deleteAvatar(request, user
|
|
93
|
-
void options;
|
|
94
|
-
|
|
80
|
+
async function deleteAvatar(request, user) {
|
|
95
81
|
const profile = await avatarService.clearForUser(user);
|
|
96
82
|
const settings = await userSettingsRepository.ensureForUserId(profile.id);
|
|
97
83
|
const securityStatus = await resolveSecurityStatus(authService, request);
|
|
@@ -104,9 +90,7 @@ function createService({
|
|
|
104
90
|
}));
|
|
105
91
|
}
|
|
106
92
|
|
|
107
|
-
async function readAvatar(
|
|
108
|
-
void options;
|
|
109
|
-
|
|
93
|
+
async function readAvatar(user) {
|
|
110
94
|
const avatar = await avatarService.readForUser(user);
|
|
111
95
|
if (!avatar) {
|
|
112
96
|
throw new AppError(404, "Avatar not found.", { code: "users.avatar.notFound" });
|
|
@@ -1,36 +1,15 @@
|
|
|
1
1
|
import { AppError } from "@jskit-ai/kernel/server/runtime/errors";
|
|
2
2
|
import { DEFAULT_IMAGE_UPLOAD_POLICY } from "@jskit-ai/uploads-runtime/shared";
|
|
3
|
-
import {
|
|
4
|
-
normalizeUploadPolicy,
|
|
5
|
-
readUploadBuffer,
|
|
6
|
-
validateUploadMimeType
|
|
7
|
-
} from "@jskit-ai/uploads-runtime/server/policy/uploadPolicy";
|
|
3
|
+
import { readUploadBuffer, validateUploadMimeType } from "@jskit-ai/uploads-runtime/server/policy/uploadPolicy";
|
|
8
4
|
import { resolveUserProfile } from "../common/services/accountContextService.js";
|
|
9
5
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return readUploadBuffer(stream, {
|
|
18
|
-
maxBytes,
|
|
19
|
-
fieldName: "avatar",
|
|
20
|
-
label: "Avatar"
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function createService({ userProfilesRepository, avatarStorageService, avatarPolicy } = {}) {
|
|
25
|
-
if (!userProfilesRepository) {
|
|
26
|
-
throw new TypeError("avatarService requires userProfilesRepository.");
|
|
27
|
-
}
|
|
28
|
-
if (!avatarStorageService) {
|
|
29
|
-
throw new TypeError("avatarService requires avatarStorageService.");
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const resolvedAvatarPolicy = resolveAvatarPolicy(avatarPolicy);
|
|
33
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Avatars are images within uploads-runtime's default image upload policy.
|
|
8
|
+
* @param {object} dependencies
|
|
9
|
+
* @param {import("../types.js").UserProfilesRepository} dependencies.userProfilesRepository
|
|
10
|
+
* @param {import("../types.js").AvatarStorageService} dependencies.avatarStorageService
|
|
11
|
+
*/
|
|
12
|
+
function createService({ userProfilesRepository, avatarStorageService }) {
|
|
34
13
|
async function resolveProfile(user) {
|
|
35
14
|
const profile = await resolveUserProfile(userProfilesRepository, user);
|
|
36
15
|
if (!profile) {
|
|
@@ -39,15 +18,17 @@ function createService({ userProfilesRepository, avatarStorageService, avatarPol
|
|
|
39
18
|
return profile;
|
|
40
19
|
}
|
|
41
20
|
|
|
42
|
-
async function uploadForUser(user, avatarUpload
|
|
21
|
+
async function uploadForUser(user, avatarUpload) {
|
|
43
22
|
const profile = await resolveProfile(user);
|
|
44
|
-
validateUploadMimeType(avatarUpload
|
|
23
|
+
validateUploadMimeType(avatarUpload.mimeType, DEFAULT_IMAGE_UPLOAD_POLICY, {
|
|
45
24
|
fieldName: "avatar",
|
|
46
25
|
label: "Avatar"
|
|
47
26
|
});
|
|
48
27
|
|
|
49
|
-
const buffer = await
|
|
50
|
-
maxBytes:
|
|
28
|
+
const buffer = await readUploadBuffer(avatarUpload.stream, {
|
|
29
|
+
maxBytes: DEFAULT_IMAGE_UPLOAD_POLICY.maxUploadBytes,
|
|
30
|
+
fieldName: "avatar",
|
|
31
|
+
label: "Avatar"
|
|
51
32
|
});
|
|
52
33
|
|
|
53
34
|
const avatarVersionMs = Date.now();
|
|
@@ -92,9 +73,4 @@ function createService({ userProfilesRepository, avatarStorageService, avatarPol
|
|
|
92
73
|
});
|
|
93
74
|
}
|
|
94
75
|
|
|
95
|
-
|
|
96
|
-
resolveAvatarPolicy,
|
|
97
|
-
readAvatarBuffer
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
export { createService, __testables };
|
|
76
|
+
export { createService };
|
|
@@ -15,7 +15,8 @@ function buildAvatarStorageKey(userId) {
|
|
|
15
15
|
return `${AVATAR_STORAGE_PREFIX}/${normalizedUserId}/avatar`;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
/** @param {{ storage: import("@jskit-ai/storage-runtime/server/types").RuntimeStorage }} dependencies */
|
|
19
|
+
function createService({ storage }) {
|
|
19
20
|
const uploadStorageService = createUploadStorageService({
|
|
20
21
|
storage,
|
|
21
22
|
mimeTypeDetector: detectCommonMimeTypeFromBuffer
|