mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-07-07 12:36:57 +00:00
add relation from user->user_profile to speed up UserEntityService.pack and packMany
This commit is contained in:
parent
71f60d519b
commit
23302fe7d8
3 changed files with 32 additions and 8 deletions
|
@ -487,7 +487,10 @@ export class UserEntityService implements OnModuleInit {
|
||||||
includeSecrets: false,
|
includeSecrets: false,
|
||||||
}, options);
|
}, options);
|
||||||
|
|
||||||
const user = typeof src === 'object' ? src : await this.usersRepository.findOneByOrFail({ id: src });
|
const user = typeof src === 'object' ? src : await this.usersRepository.findOneOrFail({
|
||||||
|
where: { id: src },
|
||||||
|
relations: { userProfile: true },
|
||||||
|
});
|
||||||
|
|
||||||
// migration
|
// migration
|
||||||
if (user.avatarId != null && user.avatarUrl === null) {
|
if (user.avatarId != null && user.avatarUrl === null) {
|
||||||
|
@ -521,7 +524,7 @@ export class UserEntityService implements OnModuleInit {
|
||||||
const iAmModerator = me ? await this.roleService.isModerator(me as MiUser) : false;
|
const iAmModerator = me ? await this.roleService.isModerator(me as MiUser) : false;
|
||||||
|
|
||||||
const profile = isDetailed
|
const profile = isDetailed
|
||||||
? (opts.userProfile ?? await this.userProfilesRepository.findOneByOrFail({ userId: user.id }))
|
? (opts.userProfile ?? user.userProfile ?? await this.userProfilesRepository.findOneByOrFail({ userId: user.id }))
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
let relation: UserRelation | null = null;
|
let relation: UserRelation | null = null;
|
||||||
|
@ -556,7 +559,7 @@ export class UserEntityService implements OnModuleInit {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const mastoapi = !isDetailed ? opts.userProfile ?? await this.userProfilesRepository.findOneByOrFail({ userId: user.id }) : null;
|
const mastoapi = !isDetailed ? opts.userProfile ?? user.userProfile ?? await this.userProfilesRepository.findOneByOrFail({ userId: user.id }) : null;
|
||||||
|
|
||||||
const followingCount = profile == null ? null :
|
const followingCount = profile == null ? null :
|
||||||
(profile.followingVisibility === 'public') || isMe || iAmModerator ? user.followingCount :
|
(profile.followingVisibility === 'public') || isMe || iAmModerator ? user.followingCount :
|
||||||
|
@ -785,8 +788,13 @@ export class UserEntityService implements OnModuleInit {
|
||||||
const _users = users.filter((user): user is MiUser => typeof user !== 'string');
|
const _users = users.filter((user): user is MiUser => typeof user !== 'string');
|
||||||
if (_users.length !== users.length) {
|
if (_users.length !== users.length) {
|
||||||
_users.push(
|
_users.push(
|
||||||
...await this.usersRepository.findBy({
|
...await this.usersRepository.find({
|
||||||
id: In(users.filter((user): user is string => typeof user === 'string')),
|
where: {
|
||||||
|
id: In(users.filter((user): user is string => typeof user === 'string')),
|
||||||
|
},
|
||||||
|
relations: {
|
||||||
|
userProfile: true,
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -800,8 +808,20 @@ export class UserEntityService implements OnModuleInit {
|
||||||
let pinNotes: Map<MiUser['id'], MiUserNotePining[]> = new Map();
|
let pinNotes: Map<MiUser['id'], MiUserNotePining[]> = new Map();
|
||||||
|
|
||||||
if (options?.schema !== 'UserLite') {
|
if (options?.schema !== 'UserLite') {
|
||||||
profilesMap = await this.userProfilesRepository.findBy({ userId: In(_userIds) })
|
const _profiles: MiUserProfile[] = [];
|
||||||
.then(profiles => new Map(profiles.map(p => [p.userId, p])));
|
const _profilesToFetch: string[] = [];
|
||||||
|
for (const user of _users) {
|
||||||
|
if (user.userProfile) {
|
||||||
|
_profiles.push(user.userProfile);
|
||||||
|
} else {
|
||||||
|
_profilesToFetch.push(user.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (_profilesToFetch.length > 0) {
|
||||||
|
const fetched = await this.userProfilesRepository.findBy({ userId: In(_profilesToFetch) });
|
||||||
|
_profiles.push(...fetched);
|
||||||
|
}
|
||||||
|
profilesMap = new Map(_profiles.map(p => [p.userId, p]));
|
||||||
|
|
||||||
const meId = me ? me.id : null;
|
const meId = me ? me.id : null;
|
||||||
if (meId) {
|
if (meId) {
|
||||||
|
|
|
@ -8,6 +8,7 @@ import { type UserUnsignedFetchOption, userUnsignedFetchOptions } from '@/const.
|
||||||
import { MiInstance } from '@/models/Instance.js';
|
import { MiInstance } from '@/models/Instance.js';
|
||||||
import { id } from './util/id.js';
|
import { id } from './util/id.js';
|
||||||
import { MiDriveFile } from './DriveFile.js';
|
import { MiDriveFile } from './DriveFile.js';
|
||||||
|
import type { MiUserProfile } from './UserProfile.js';
|
||||||
|
|
||||||
@Entity('user')
|
@Entity('user')
|
||||||
@Index(['usernameLower', 'host'], { unique: true })
|
@Index(['usernameLower', 'host'], { unique: true })
|
||||||
|
@ -395,6 +396,9 @@ export class MiUser {
|
||||||
})
|
})
|
||||||
public attributionDomains: string[];
|
public attributionDomains: string[];
|
||||||
|
|
||||||
|
@OneToOne('user_profile', (profile: MiUserProfile) => profile.user)
|
||||||
|
public userProfile: MiUserProfile | null;
|
||||||
|
|
||||||
constructor(data: Partial<MiUser>) {
|
constructor(data: Partial<MiUser>) {
|
||||||
if (data == null) return;
|
if (data == null) return;
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ export class MiUserProfile {
|
||||||
@PrimaryColumn(id())
|
@PrimaryColumn(id())
|
||||||
public userId: MiUser['id'];
|
public userId: MiUser['id'];
|
||||||
|
|
||||||
@OneToOne(type => MiUser, {
|
@OneToOne(() => MiUser, user => user.userProfile, {
|
||||||
onDelete: 'CASCADE',
|
onDelete: 'CASCADE',
|
||||||
})
|
})
|
||||||
@JoinColumn()
|
@JoinColumn()
|
||||||
|
|
Loading…
Add table
Reference in a new issue