mirror of
https://activitypub.software/TransFem-org/Sharkey.git
synced 2025-08-21 05:44:48 +00:00
merge: Slightly optimize i/update-remote-user endpoint by fetching from cache (!1185)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/1185 Approved-by: dakkar <dakkar@thenautilus.net> Approved-by: Marie <github@yuugi.dev>
This commit is contained in:
commit
41be003d79
2 changed files with 29 additions and 2 deletions
|
@ -9,7 +9,7 @@ import { In, IsNull } from 'typeorm';
|
||||||
import type { BlockingsRepository, FollowingsRepository, MutingsRepository, RenoteMutingsRepository, MiUserProfile, UserProfilesRepository, UsersRepository, MiNote, MiFollowing, NoteThreadMutingsRepository } from '@/models/_.js';
|
import type { BlockingsRepository, FollowingsRepository, MutingsRepository, RenoteMutingsRepository, MiUserProfile, UserProfilesRepository, UsersRepository, MiNote, MiFollowing, NoteThreadMutingsRepository } from '@/models/_.js';
|
||||||
import { MemoryKVCache, RedisKVCache } from '@/misc/cache.js';
|
import { MemoryKVCache, RedisKVCache } from '@/misc/cache.js';
|
||||||
import { QuantumKVCache } from '@/misc/QuantumKVCache.js';
|
import { QuantumKVCache } from '@/misc/QuantumKVCache.js';
|
||||||
import type { MiLocalUser, MiUser } from '@/models/User.js';
|
import type { MiLocalUser, MiRemoteUser, MiUser } from '@/models/User.js';
|
||||||
import { DI } from '@/di-symbols.js';
|
import { DI } from '@/di-symbols.js';
|
||||||
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
||||||
import { bindThis } from '@/decorators.js';
|
import { bindThis } from '@/decorators.js';
|
||||||
|
@ -387,6 +387,17 @@ export class CacheService implements OnApplicationShutdown {
|
||||||
}) ?? null;
|
}) ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@bindThis
|
||||||
|
public async findRemoteUserById(userId: MiUser['id']): Promise<MiRemoteUser | null> {
|
||||||
|
const user = await this.findUserById(userId);
|
||||||
|
|
||||||
|
if (user.host == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return user as MiRemoteUser;
|
||||||
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public async getFollowStats(userId: MiUser['id']): Promise<FollowStats> {
|
public async getFollowStats(userId: MiUser['id']): Promise<FollowStats> {
|
||||||
return await this.userFollowStatsCache.fetch(userId, async () => {
|
return await this.userFollowStatsCache.fetch(userId, async () => {
|
||||||
|
|
|
@ -7,12 +7,22 @@ import { Injectable } from '@nestjs/common';
|
||||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||||
import { ApPersonService } from '@/core/activitypub/models/ApPersonService.js';
|
import { ApPersonService } from '@/core/activitypub/models/ApPersonService.js';
|
||||||
import { GetterService } from '@/server/api/GetterService.js';
|
import { GetterService } from '@/server/api/GetterService.js';
|
||||||
|
import { CacheService } from '@/core/CacheService.js';
|
||||||
|
import { ApiError } from '@/server/api/error.js';
|
||||||
|
|
||||||
export const meta = {
|
export const meta = {
|
||||||
tags: ['federation'],
|
tags: ['federation'],
|
||||||
|
|
||||||
requireCredential: false,
|
requireCredential: false,
|
||||||
|
|
||||||
|
errors: {
|
||||||
|
noSuchUser: {
|
||||||
|
message: 'No such user.',
|
||||||
|
code: 'NO_SUCH_USER',
|
||||||
|
id: '558ea170-f653-4700-94d0-5a818371d0df',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
// Up to 10 calls, then 4 / second.
|
// Up to 10 calls, then 4 / second.
|
||||||
// This allows for reliable automation.
|
// This allows for reliable automation.
|
||||||
limit: {
|
limit: {
|
||||||
|
@ -35,9 +45,15 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
constructor(
|
constructor(
|
||||||
private getterService: GetterService,
|
private getterService: GetterService,
|
||||||
private apPersonService: ApPersonService,
|
private apPersonService: ApPersonService,
|
||||||
|
private readonly cacheService: CacheService,
|
||||||
) {
|
) {
|
||||||
super(meta, paramDef, async (ps) => {
|
super(meta, paramDef, async (ps) => {
|
||||||
const user = await this.getterService.getRemoteUser(ps.userId);
|
const user = await this.cacheService.findRemoteUserById(ps.userId);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new ApiError(meta.errors.noSuchUser);
|
||||||
|
}
|
||||||
|
|
||||||
await this.apPersonService.updatePerson(user.uri!);
|
await this.apPersonService.updatePerson(user.uri!);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue