Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remote avatar deco cache #1

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "misskey",
"version": "2023.11.1+munochi.4",
"version": "2023.11.1+munochi.5",
"codename": "nasubi",
"repository": {
"type": "git",
Expand Down
7 changes: 6 additions & 1 deletion packages/backend/src/core/AvatarDecorationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
import { HttpRequestService } from "@/core/HttpRequestService.js";
import { appendQuery, query } from '@/misc/prelude/url.js';
import type { Config } from '@/config.js';
import {IsNull} from "typeorm";

Check failure on line 19 in packages/backend/src/core/AvatarDecorationService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

A space is required after '{'

Check failure on line 19 in packages/backend/src/core/AvatarDecorationService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

A space is required before '}'

@Injectable()
export class AvatarDecorationService implements OnApplicationShutdown {
public cache: MemorySingleCache<MiAvatarDecoration[]>;
public cacheWithRemote: MemorySingleCache<MiAvatarDecoration[]>;

constructor(
@Inject(DI.config)
Expand All @@ -44,6 +45,7 @@
private httpRequestService: HttpRequestService,
) {
this.cache = new MemorySingleCache<MiAvatarDecoration[]>(1000 * 60 * 30);
this.cacheWithRemote = new MemorySingleCache<MiAvatarDecoration[]>(1000 * 60 * 30);

this.redisForSub.on('message', this.onMessage);
}
Expand Down Expand Up @@ -142,7 +144,7 @@
if (!avatarDecorations) {
const updates = {} as Partial<MiUser>;
updates.avatarDecorations = [];
await this.usersRepository.update({id: user.id}, updates);

Check failure on line 147 in packages/backend/src/core/AvatarDecorationService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

A space is required after '{'

Check failure on line 147 in packages/backend/src/core/AvatarDecorationService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

A space is required before '}'
return;
}

Expand All @@ -151,14 +153,14 @@
const decorationApiUrl = `https://${instanceHost}/api/get-avatar-decorations`;
const allRes = await this.httpRequestService.send(decorationApiUrl, {
method: 'POST',
headers: {"Content-Type": "application/json"},

Check failure on line 156 in packages/backend/src/core/AvatarDecorationService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

A space is required after '{'

Check failure on line 156 in packages/backend/src/core/AvatarDecorationService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

A space is required before '}'
body: JSON.stringify({}),
});
const allDecorations: any = await allRes.json();
let name;
let description;
for (const decoration of allDecorations) {
if (decoration.id == avatarDecorationId) {

Check failure on line 163 in packages/backend/src/core/AvatarDecorationService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

Expected '===' and instead saw '=='
name = decoration.name;
description = decoration.description;
break;
Expand All @@ -177,8 +179,10 @@
};
if (existingDecoration == null) {
await this.create(decorationData);
this.cacheWithRemote.delete();
} else {
await this.update(existingDecoration.id, decorationData);
this.cacheWithRemote.delete();
}
const findDecoration = await this.avatarDecorationsRepository.findOneBy({
host: userHost,
Expand All @@ -190,7 +194,7 @@
angle: avatarDecorations.angle ?? 0,
flipH: avatarDecorations.flipH ?? false,
}];
await this.usersRepository.update({id: user.id}, updates);

Check failure on line 197 in packages/backend/src/core/AvatarDecorationService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

A space is required after '{'

Check failure on line 197 in packages/backend/src/core/AvatarDecorationService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

A space is required before '}'
}

@bindThis
Expand All @@ -212,11 +216,12 @@
public async getAll(noCache = false, withRemote = false): Promise<MiAvatarDecoration[]> {
if (noCache) {
this.cache.delete();
this.cacheWithRemote.delete();
}
if (!withRemote) {
return this.cache.fetch(() => this.avatarDecorationsRepository.find({ where: { host: IsNull() } }));
} else {
return this.cache.fetch(() => this.avatarDecorationsRepository.find());
return this.cacheWithRemote.fetch(() => this.avatarDecorationsRepository.find());
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/core/entities/UserEntityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export class UserEntityService implements OnModuleInit {
host: user.host,
avatarUrl: user.avatarUrl ?? this.getIdenticonUrl(user),
avatarBlurhash: user.avatarBlurhash,
avatarDecorations: user.avatarDecorations.length > 0 ? this.avatarDecorationService.getAll(true, true).then(decorations => user.avatarDecorations.filter(ud => decorations.some(d => d.id === ud.id)).map(ud => ({
avatarDecorations: user.avatarDecorations.length > 0 ? this.avatarDecorationService.getAll(false, true).then(decorations => user.avatarDecorations.filter(ud => decorations.some(d => d.id === ud.id)).map(ud => ({
id: ud.id,
angle: ud.angle || undefined,
flipH: ud.flipH || undefined,
Expand Down
Loading