Skip to content

Commit

Permalink
keep cached avatar&banner when refresh fails to get new values (missk…
Browse files Browse the repository at this point in the history
…ey-dev#13145)

* keep cached avatar&banner when refresh fails to get new values

when the remote explicitly tells us a user image is gone, we remove
our cached value, but if we fail to get the image, we keep whatever
value we already have

this should minimise the problem of avatars randomly disappearing

* autogen bits

* pnpm run build-misskey-js-with-types

---------

Co-authored-by: tamaina <tamaina@hotmail.co.jp>
  • Loading branch information
dakkar and tamaina authored Feb 4, 2024
1 parent bafef1f commit dabf186
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
35 changes: 26 additions & 9 deletions packages/backend/src/core/activitypub/models/ApPersonService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,37 @@ export class ApPersonService implements OnModuleInit {
return null;
}

private async resolveAvatarAndBanner(user: MiRemoteUser, icon: any, image: any): Promise<Pick<MiRemoteUser, 'avatarId' | 'bannerId' | 'avatarUrl' | 'bannerUrl' | 'avatarBlurhash' | 'bannerBlurhash'>> {
private async resolveAvatarAndBanner(user: MiRemoteUser, icon: any, image: any): Promise<Partial<Pick<MiRemoteUser, 'avatarId' | 'bannerId' | 'avatarUrl' | 'bannerUrl' | 'avatarBlurhash' | 'bannerBlurhash'>>> {
if (user == null) throw new Error('failed to create user: user is null');

const [avatar, banner] = await Promise.all([icon, image].map(img => {
if (img == null) return null;
if (user == null) throw new Error('failed to create user: user is null');
// if we have an explicitly missing image, return an
// explicitly-null set of values
if ((img == null) || (typeof img === 'object' && img.url == null)) {
return { id: null, url: null, blurhash: null };
}

return this.apImageService.resolveImage(user, img).catch(() => null);
}));

/*
we don't want to return nulls on errors! if the database fields
are already null, nothing changes; if the database has old
values, we should keep those. The exception is if the remote has
actually removed the images: in that case, the block above
returns the special {id:null}&c value, and we return those
*/
return {
avatarId: avatar?.id ?? null,
bannerId: banner?.id ?? null,
avatarUrl: avatar ? this.driveFileEntityService.getPublicUrl(avatar, 'avatar') : null,
bannerUrl: banner ? this.driveFileEntityService.getPublicUrl(banner) : null,
avatarBlurhash: avatar?.blurhash ?? null,
bannerBlurhash: banner?.blurhash ?? null,
...( avatar ? {
avatarId: avatar.id,
avatarUrl: avatar.url ? this.driveFileEntityService.getPublicUrl(avatar, 'avatar') : null,
avatarBlurhash: avatar.blurhash,
} : {}),
...( banner ? {
bannerId: banner.id,
bannerUrl: banner.url ? this.driveFileEntityService.getPublicUrl(banner) : null,
bannerBlurhash: banner.blurhash,
} : {}),
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/misskey-js/src/autogen/apiClientJSDoc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* version: 2024.2.0-beta.8
* generatedAt: 2024-02-02T14:18:15.716Z
* generatedAt: 2024-02-04T11:51:13.598Z
*/

import type { SwitchCaseResponseType } from '../api.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/misskey-js/src/autogen/endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* version: 2024.2.0-beta.8
* generatedAt: 2024-02-02T14:18:15.712Z
* generatedAt: 2024-02-04T11:51:13.595Z
*/

import type {
Expand Down
2 changes: 1 addition & 1 deletion packages/misskey-js/src/autogen/entities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* version: 2024.2.0-beta.8
* generatedAt: 2024-02-02T14:18:15.709Z
* generatedAt: 2024-02-04T11:51:13.593Z
*/

import { operations } from './types.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/misskey-js/src/autogen/models.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* version: 2024.2.0-beta.8
* generatedAt: 2024-02-02T14:18:15.708Z
* generatedAt: 2024-02-04T11:51:13.592Z
*/

import { components } from './types.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/misskey-js/src/autogen/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/*
* version: 2024.2.0-beta.8
* generatedAt: 2024-02-02T14:18:15.529Z
* generatedAt: 2024-02-04T11:51:13.473Z
*/

/**
Expand Down

0 comments on commit dabf186

Please sign in to comment.