Skip to content

Commit d712d2f

Browse files
authored
Make ResourcesHost sync (#3056)
1 parent 59e242d commit d712d2f

File tree

13 files changed

+86
-97
lines changed

13 files changed

+86
-97
lines changed

src/components/authorization/policy/executor/policy-dumper.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class PolicyDumper {
4646
const book = xlsx.utils.book_new();
4747

4848
const chalk = new Chalk({ level: 0 });
49-
const resources = await this.selectResources();
49+
const resources = this.selectResources();
5050
for (const role of Role) {
5151
const dumped = resources.flatMap((res) =>
5252
this.dumpRes(role, res, { props: true }),
@@ -74,7 +74,7 @@ export class PolicyDumper {
7474
resourcesIn: Many<ResourceLike & string>,
7575
) {
7676
const roles = search(rolesIn, [...Role], 'role');
77-
const map = await this.resources.getEnhancedMap();
77+
const map = this.resources.getEnhancedMap();
7878
const resources = searchResources(resourcesIn, map);
7979

8080
const data = roles.flatMap((role) =>
@@ -140,10 +140,10 @@ export class PolicyDumper {
140140
console.log(table.toString());
141141
}
142142

143-
private async selectResources(...filtered: AnyResource[]) {
143+
private selectResources(...filtered: AnyResource[]) {
144144
const selectedResources = filtered.length
145145
? filtered
146-
: Object.values<AnyResource>(await this.resources.getEnhancedMap());
146+
: Object.values<AnyResource>(this.resources.getEnhancedMap());
147147
return sortBy(selectedResources, (r) => r.name);
148148
}
149149

src/components/authorization/policy/granters.factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class GrantersFactory {
3939
),
4040
);
4141

42-
const ResourceMap = await this.resourcesHost.getEnhancedMap();
42+
const ResourceMap = this.resourcesHost.getEnhancedMap();
4343

4444
const resGranter: ResourcesGranter = mapValues(
4545
ResourceMap,

src/components/authorization/policy/policy.factory.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ export class PolicyFactory implements OnModuleInit {
108108
}
109109
}
110110

111-
await this.defaultInterfacesFromImplementationsIntersection(grants);
112-
await this.defaultImplementationsFromInterfaces(grants);
113-
await this.defaultRelationEdgesToResourceLevel(grants);
111+
this.defaultInterfacesFromImplementationsIntersection(grants);
112+
this.defaultImplementationsFromInterfaces(grants);
113+
this.defaultRelationEdgesToResourceLevel(grants);
114114

115-
const powers = await this.determinePowers(grants);
115+
const powers = this.determinePowers(grants);
116116

117117
const name = startCase(discoveredClass.name.replace(/Policy$/, ''));
118118
const policy: Policy = { name, roles, grants, powers };
@@ -125,17 +125,13 @@ export class PolicyFactory implements OnModuleInit {
125125
* Declare permissions of missing interfaces based on the intersection of
126126
* the permissions of its implementations
127127
*/
128-
private async defaultInterfacesFromImplementationsIntersection(
128+
private defaultInterfacesFromImplementationsIntersection(
129129
grantMap: WritableGrants,
130130
) {
131131
const interfaceCandidates = new Set(
132-
(
133-
await Promise.all(
134-
[...grantMap.keys()].map((res) =>
135-
this.resourcesHost.getInterfaces(res),
136-
),
137-
)
138-
).flat(),
132+
[...grantMap.keys()]
133+
.map((res) => this.resourcesHost.getInterfaces(res))
134+
.flat(),
139135
);
140136

141137
const allKeysOf = (list: Array<object | undefined>) =>
@@ -156,7 +152,7 @@ export class PolicyFactory implements OnModuleInit {
156152
continue;
157153
}
158154

159-
const impls = await this.resourcesHost.getImplementations(interfaceRes);
155+
const impls = this.resourcesHost.getImplementations(interfaceRes);
160156

161157
// Skip if policy doesn't specify all implementations of the interface
162158
if (!(impls.length > 0 && impls.every((impl) => grantMap.has(impl)))) {
@@ -213,18 +209,18 @@ export class PolicyFactory implements OnModuleInit {
213209
}
214210
}
215211

216-
private async defaultImplementationsFromInterfaces(grants: WritableGrants) {
212+
private defaultImplementationsFromInterfaces(grants: WritableGrants) {
217213
for (const resource of grants.keys()) {
218-
const impls = await this.resourcesHost.getImplementations(resource);
214+
const impls = this.resourcesHost.getImplementations(resource);
219215
for (const impl of impls) {
220216
if (grants.has(impl)) {
221217
continue;
222218
}
223219
// If policy doesn't specify this implementation then use most specific
224220
// interface given.
225-
const interfaceToApply = (
226-
await this.resourcesHost.getInterfaces(impl)
227-
).find((i) => grants.has(i));
221+
const interfaceToApply = this.resourcesHost
222+
.getInterfaces(impl)
223+
.find((i) => grants.has(i));
228224
const interfacePerms = interfaceToApply && grants.get(interfaceToApply);
229225
if (!interfacePerms) {
230226
// Safety check, but this shouldn't ever happen, since we only got
@@ -236,7 +232,7 @@ export class PolicyFactory implements OnModuleInit {
236232
}
237233
}
238234

239-
private async defaultRelationEdgesToResourceLevel(grants: WritableGrants) {
235+
private defaultRelationEdgesToResourceLevel(grants: WritableGrants) {
240236
for (const [resource, { childRelations }] of grants.entries()) {
241237
for (const rel of resource.relations.values()) {
242238
const type = rel.resource;
@@ -297,7 +293,7 @@ export class PolicyFactory implements OnModuleInit {
297293
return mergeConditions(...conditions);
298294
}
299295

300-
private async determinePowers(grants: Grants) {
296+
private determinePowers(grants: Grants) {
301297
const powers = new Set<Power>();
302298
const pushPower = (str: string) => {
303299
const power = `Create${str}`;
@@ -313,7 +309,7 @@ export class PolicyFactory implements OnModuleInit {
313309
}
314310
pushPower(res.name);
315311

316-
const implementations = await this.resourcesHost.getImplementations(res);
312+
const implementations = this.resourcesHost.getImplementations(res);
317313
for (const implementation of implementations) {
318314
if (grants.has(implementation)) {
319315
// If policy specifies this implementation then defer to its entry.

src/components/comments/comment.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class CommentService {
6767

6868
async getPermissionsFromResource(resource: CommentableRef, session: Session) {
6969
const parent = await this.loadCommentable(resource);
70-
const parentType = await this.resourcesHost.getByName(
70+
const parentType = this.resourcesHost.getByName(
7171
// I'd like to type this prop as this but somehow blows everything up.
7272
parent.__typename as 'Commentable',
7373
);
@@ -93,7 +93,7 @@ export class CommentService {
9393
: parentNode;
9494

9595
try {
96-
await this.resourcesHost.verifyImplements(parent.__typename, Commentable);
96+
this.resourcesHost.verifyImplements(parent.__typename, Commentable);
9797
} catch (e) {
9898
throw new NonCommentableType(e.message);
9999
}

src/components/file/media/events/can-update-event.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ export class CanUpdateMediaUserMetadataEvent {
1919
@Optional() readonly allowUpdate: PollVoter<boolean>,
2020
) {}
2121

22-
@Once() async getAttachedResource() {
22+
@Once() getAttachedResource() {
2323
const attachedResName = this.resourceResolver.resolveTypeByBaseNode(
2424
this.media.attachedTo[0],
2525
);
26-
const attachedResource = await this.resourceHost.getByName(attachedResName);
26+
const attachedResource = this.resourceHost.getByName(attachedResName);
2727
return attachedResource;
2828
}
2929
}

src/components/post/post.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class PostService {
120120

121121
async getPermissionsFromPostable(resource: PostableRef, session: Session) {
122122
const parent = await this.loadPostable(resource);
123-
const parentType = await this.resourcesHost.getByName(
123+
const parentType = this.resourcesHost.getByName(
124124
parent.__typename as 'Postable',
125125
);
126126
return this.privileges.for(session, parentType, parent).forEdge('posts');
@@ -138,7 +138,7 @@ export class PostService {
138138
: parentNode;
139139

140140
try {
141-
await this.resourcesHost.verifyImplements(parent.__typename, Postable);
141+
this.resourcesHost.verifyImplements(parent.__typename, Postable);
142142
} catch (e) {
143143
throw new NonPostableType(e.message);
144144
}

src/components/progress-report/media/handlers/update-media-metadata-check.handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class ProgressReportUpdateMediaMetadataCheckHandler {
1212
) {}
1313

1414
async handle(event: CanUpdateMediaUserMetadataEvent) {
15-
const attached = await event.getAttachedResource();
15+
const attached = event.getAttachedResource();
1616
if (!attached.is(ReportMedia)) {
1717
return;
1818
}

src/components/search/search.service.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class SearchService {
5555
// which is based on their first valid search label.
5656
const results = await this.repo.search({ ...input, type: types });
5757

58-
const ResourceMap = await this.resourceHost.getMap();
58+
const ResourceMap = this.resourceHost.getMap();
5959

6060
// Individually convert each result (id & type) to its search result
6161
// based on this.hydrators
@@ -94,9 +94,7 @@ export class SearchService {
9494
return null;
9595
}
9696

97-
const resource = await this.resourceHost.getByName(
98-
hydrated.__typename,
99-
);
97+
const resource = this.resourceHost.getByName(hydrated.__typename);
10098
const perms = this.privileges.for(session, resource, hydrated).all;
10199
return matchedProps.some((key) =>
102100
// @ts-expect-error strict typing is hard for this dynamic use case.

src/core/edgedb/common.repository.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class CommonRepository implements PublicOf<Neo4jCommonRepository> {
3737
): Promise<readonly BaseNode[]> {
3838
const res = fqn
3939
? typeof fqn === 'string'
40-
? await this.resources.getByEdgeDB(fqn)
40+
? this.resources.getByEdgeDB(fqn)
4141
: EnhancedResource.of(fqn)
4242
: undefined;
4343
const query = e.params({ ids: e.array(e.uuid) }, ({ ids }) =>
@@ -51,17 +51,14 @@ export class CommonRepository implements PublicOf<Neo4jCommonRepository> {
5151
);
5252
const nodes = await this.db.run(query, { ids });
5353

54-
return await Promise.all(
55-
nodes.map(async (node): Promise<BaseNode> => {
56-
const res = await this.resources.getByEdgeDB(node._typeFQN_);
57-
return {
58-
identity: node.id,
59-
labels: [res.dbLabel],
60-
properties: {
61-
id: node.id,
62-
createdAt: node.createdAt,
63-
},
64-
};
54+
return nodes.map(
55+
(node): BaseNode => ({
56+
identity: node.id,
57+
labels: [this.resources.getByEdgeDB(node._typeFQN_).dbLabel],
58+
properties: {
59+
id: node.id,
60+
createdAt: node.createdAt,
61+
},
6562
}),
6663
);
6764
}

src/core/edgedb/dto.repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export const RepoFor = <
147147
* failed insert, after we finish migration.
148148
*/
149149
async isUnique(value: string, fqn?: string) {
150-
const res = fqn ? await this.resources.getByEdgeDB(fqn) : resource;
150+
const res = fqn ? this.resources.getByEdgeDB(fqn) : resource;
151151
const query = e.select(e.Mixin.Named, (obj) => ({
152152
filter: e.op(
153153
e.op(obj.name, '=', value),

0 commit comments

Comments
 (0)