mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-21 17:37:29 -07:00
[mastodon-client] Refresh user data in background on UserConverter.encode
This commit is contained in:
parent
2575588fa3
commit
7c56ee348b
3 changed files with 20 additions and 13 deletions
|
@ -12,6 +12,7 @@ import { Cache } from "@/misc/cache.js";
|
|||
import { IMentionedRemoteUsers } from "@/models/entities/note.js";
|
||||
import { UserProfile } from "@/models/entities/user-profile.js";
|
||||
import { RecursionLimiter } from "@/models/repositories/user-profile.js";
|
||||
import { promiseEarlyReturn } from "@/prelude/promise.js";
|
||||
|
||||
const logger = remoteLogger.createSubLogger("resolve-user");
|
||||
const uriHostCache = new Cache<string>("resolveUserUriHost", 60 * 60 * 24);
|
||||
|
@ -27,11 +28,12 @@ type ProfileMention = {
|
|||
};
|
||||
};
|
||||
|
||||
type refreshType = 'refresh' | 'refresh-in-background' | 'refresh-timeout-1500ms' | 'no-refresh';
|
||||
|
||||
export async function resolveUser(
|
||||
username: string,
|
||||
host: string | null,
|
||||
refresh: boolean = true,
|
||||
awaitRefresh: boolean = true,
|
||||
refresh: refreshType = 'refresh',
|
||||
limiter: RecursionLimiter = new RecursionLimiter(20)
|
||||
): Promise<User> {
|
||||
const usernameLower = username.toLowerCase();
|
||||
|
@ -118,7 +120,7 @@ export async function resolveUser(
|
|||
|
||||
// If user information is out of date, return it by starting over from WebFinger
|
||||
if (
|
||||
refresh && awaitRefresh && (
|
||||
(refresh === 'refresh' || refresh === 'refresh-timeout-1500ms') && (
|
||||
user.lastFetchedAt == null ||
|
||||
Date.now() - user.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24
|
||||
)
|
||||
|
@ -176,9 +178,15 @@ export async function resolveUser(
|
|||
);
|
||||
}
|
||||
|
||||
await updatePerson(fingerRes.self.href);
|
||||
if (refresh === 'refresh') {
|
||||
await updatePerson(fingerRes.self.href);
|
||||
logger.info(`return resynced remote user: ${finalAcctLower}`);
|
||||
}
|
||||
else if (refresh === 'refresh-timeout-1500ms') {
|
||||
const res = await promiseEarlyReturn(updatePerson(fingerRes.self.href), 1500);
|
||||
logger.info(`return possibly resynced remote user: ${finalAcctLower}`);
|
||||
}
|
||||
|
||||
logger.info(`return resynced remote user: ${finalAcctLower}`);
|
||||
return await Users.findOneBy({ uri: fingerRes.self.href }).then((u) => {
|
||||
if (u == null) {
|
||||
throw new Error("user not found");
|
||||
|
@ -186,10 +194,10 @@ export async function resolveUser(
|
|||
return u;
|
||||
}
|
||||
});
|
||||
} else if (refresh && !awaitRefresh && (user.lastFetchedAt == null || Date.now() - user.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24)) {
|
||||
} else if (refresh === 'refresh-in-background' && (user.lastFetchedAt == null || Date.now() - user.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24)) {
|
||||
// Run the refresh in the background
|
||||
// noinspection ES6MissingAwait
|
||||
resolveUser(username, host, true, true, limiter);
|
||||
resolveUser(username, host, 'refresh', limiter);
|
||||
}
|
||||
|
||||
logger.info(`return existing remote user: ${acctLower}`);
|
||||
|
@ -199,7 +207,7 @@ export async function resolveUser(
|
|||
export async function resolveMentionToUserAndProfile(username: string, host: string | null, objectHost: string | null, limiter: RecursionLimiter) {
|
||||
return profileMentionCache.fetch(`${username}@${host ?? objectHost}`, async () => {
|
||||
try {
|
||||
const user = await resolveUser(username, host ?? objectHost, false, false, limiter);
|
||||
const user = await resolveUser(username, host ?? objectHost, 'no-refresh', limiter);
|
||||
const profile = await UserProfiles.findOneBy({ userId: user.id });
|
||||
const data = { username, host: host ?? objectHost };
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ import { populateEmojis } from "@/misc/populate-emojis.js";
|
|||
import { escapeMFM } from "@/server/api/mastodon/converters/mfm.js";
|
||||
import mfm from "mfm-js";
|
||||
import { awaitAll } from "@/prelude/await-all.js";
|
||||
import { AccountCache } from "@/server/api/mastodon/helpers/user.js";
|
||||
import { AccountCache, UserHelpers } from "@/server/api/mastodon/helpers/user.js";
|
||||
import { MfmHelpers } from "@/server/api/mastodon/helpers/mfm.js";
|
||||
import { MastoContext } from "@/server/api/mastodon/index.js";
|
||||
import { IMentionedRemoteUsers } from "@/models/entities/note.js";
|
||||
|
@ -99,6 +99,8 @@ export class UserConverter {
|
|||
bot: u.isBot,
|
||||
discoverable: u.isExplorable
|
||||
}).then(p => {
|
||||
// noinspection ES6MissingAwait
|
||||
UserHelpers.updateUserInBackground(u);
|
||||
cache.accounts.push(p);
|
||||
return p;
|
||||
});
|
||||
|
|
|
@ -245,7 +245,7 @@ export class UserHelpers {
|
|||
if (p) return p;
|
||||
throw new MastoApiError(404);
|
||||
})
|
||||
: resolveUser(split[0], split[1], true, false).catch(() => {
|
||||
: resolveUser(split[0], split[1], 'no-refresh').catch(() => {
|
||||
throw new MastoApiError(404);
|
||||
});
|
||||
}
|
||||
|
@ -522,9 +522,6 @@ export class UserHelpers {
|
|||
public static async getUserOr404(id: string): Promise<User> {
|
||||
return getUser(id).catch(_ => {
|
||||
throw new MastoApiError(404);
|
||||
}).then(u => {
|
||||
this.updateUserInBackground(u);
|
||||
return u;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue