mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-26 02:35:58 +08:00
feat(server): add user existence check and optimize permission queries (#10402)
This commit is contained in:
@@ -297,6 +297,14 @@ test('should paginate users', async t => {
|
||||
);
|
||||
});
|
||||
|
||||
test('should check if user exists', async t => {
|
||||
const user = await t.context.user.create({
|
||||
email: 'test@affine.pro',
|
||||
});
|
||||
t.true(await t.context.user.exists(user.id));
|
||||
t.false(await t.context.user.exists('non-existing-user'));
|
||||
});
|
||||
|
||||
// #region ConnectedAccount
|
||||
|
||||
test('should create, get, update, delete connected account', async t => {
|
||||
|
||||
@@ -102,7 +102,7 @@ export class PageInfo {
|
||||
hasPreviousPage!: boolean;
|
||||
}
|
||||
|
||||
export function Paginated<T>(classRef: Type<T>): any {
|
||||
export function Paginated<T>(classRef: Type<T>) {
|
||||
@ObjectType(`${classRef.name}Edge`)
|
||||
abstract class EdgeType {
|
||||
@Field(() => String)
|
||||
|
||||
@@ -498,22 +498,14 @@ export class DocResolver {
|
||||
]);
|
||||
});
|
||||
|
||||
const users = new Map<string, PublicUserType>(
|
||||
await Promise.all(
|
||||
permissions.map(
|
||||
async p =>
|
||||
[p.userId, await this.models.user.getPublicUser(p.userId)] as [
|
||||
string,
|
||||
PublicUserType,
|
||||
]
|
||||
)
|
||||
)
|
||||
const publicUsers = await this.models.user.getPublicUsers(
|
||||
permissions.map(p => p.userId)
|
||||
);
|
||||
|
||||
const publicUsersMap = new Map(publicUsers.map(pu => [pu.id, pu]));
|
||||
return paginate(
|
||||
permissions.map(p => ({
|
||||
...p,
|
||||
user: users.get(p.userId),
|
||||
user: publicUsersMap.get(p.userId) as PublicUserType,
|
||||
})),
|
||||
'createdAt',
|
||||
pagination,
|
||||
|
||||
@@ -61,6 +61,13 @@ export class UserModel extends BaseModel {
|
||||
});
|
||||
}
|
||||
|
||||
async exists(id: string) {
|
||||
const count = await this.db.user.count({
|
||||
where: { id },
|
||||
});
|
||||
return count > 0;
|
||||
}
|
||||
|
||||
async getPublicUser(id: string): Promise<PublicUser | null> {
|
||||
return this.db.user.findUnique({
|
||||
select: publicUserSelect,
|
||||
|
||||
Reference in New Issue
Block a user