fix: properly update cache

This commit is contained in:
Namekuji 2023-09-20 09:27:28 -04:00
parent 8e0b0c7cb9
commit 0b23a5adaa
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532
8 changed files with 74 additions and 11 deletions

View file

@ -48,7 +48,10 @@ export default async (
if (isNativeToken(token)) { if (isNativeToken(token)) {
const user = await localUserByNativeTokenCache.fetch( const user = await localUserByNativeTokenCache.fetch(
token, token,
() => Users.findOneBy({ token }) as Promise<ILocalUser | null>, () =>
Users.findOneBy({
token: token as string,
}) as Promise<ILocalUser | null>,
true, true,
); );
@ -83,14 +86,11 @@ export default async (
Users.findOneBy({ Users.findOneBy({
id: accessToken.userId, id: accessToken.userId,
}) as Promise<ILocalUser>, }) as Promise<ILocalUser>,
true,
); );
if (accessToken.appId) { if (accessToken.appId) {
const app = await appCache.fetch( const app = await appCache.fetch(accessToken.appId, () =>
accessToken.appId, Apps.findOneByOrFail({ id: accessToken.appId as string }),
() => Apps.findOneByOrFail({ id: accessToken.appId! }),
true,
); );
return [ return [

View file

@ -1,6 +1,11 @@
import define from "../../../define.js"; import define from "../../../define.js";
import { Users } from "@/models/index.js"; import { Users } from "@/models/index.js";
import { publishInternalEvent } from "@/services/stream.js"; import { publishInternalEvent } from "@/services/stream.js";
import {
localUserByIdCache,
userByIdCache,
userDenormalizedCache,
} from "@/services/user-cache.js";
export const meta = { export const meta = {
tags: ["admin"], tags: ["admin"],
@ -28,6 +33,9 @@ export default define(meta, paramDef, async (ps) => {
throw new Error("cannot mark as moderator if admin user"); throw new Error("cannot mark as moderator if admin user");
} }
await userDenormalizedCache.delete(user.id);
await userByIdCache.delete(user.id);
await localUserByIdCache.delete(user.id);
await Users.update(user.id, { await Users.update(user.id, {
isModerator: true, isModerator: true,
}); });

View file

@ -1,6 +1,11 @@
import define from "../../../define.js"; import define from "../../../define.js";
import { Users } from "@/models/index.js"; import { Users } from "@/models/index.js";
import { publishInternalEvent } from "@/services/stream.js"; import { publishInternalEvent } from "@/services/stream.js";
import {
localUserByIdCache,
userByIdCache,
userDenormalizedCache,
} from "@/services/user-cache.js";
export const meta = { export const meta = {
tags: ["admin"], tags: ["admin"],
@ -24,6 +29,9 @@ export default define(meta, paramDef, async (ps) => {
throw new Error("user not found"); throw new Error("user not found");
} }
await userDenormalizedCache.delete(user.id);
await userByIdCache.delete(user.id);
await localUserByIdCache.delete(user.id);
await Users.update(user.id, { await Users.update(user.id, {
isModerator: false, isModerator: false,
}); });

View file

@ -2,6 +2,11 @@ import define from "../../define.js";
import { Users } from "@/models/index.js"; import { Users } from "@/models/index.js";
import { insertModerationLog } from "@/services/insert-moderation-log.js"; import { insertModerationLog } from "@/services/insert-moderation-log.js";
import { publishInternalEvent } from "@/services/stream.js"; import { publishInternalEvent } from "@/services/stream.js";
import {
localUserByIdCache,
userByIdCache,
userDenormalizedCache,
} from "@/services/user-cache.js";
export const meta = { export const meta = {
tags: ["admin"], tags: ["admin"],
@ -29,6 +34,9 @@ export default define(meta, paramDef, async (ps, me) => {
throw new Error("cannot silence admin"); throw new Error("cannot silence admin");
} }
await userDenormalizedCache.delete(user.id);
await userByIdCache.delete(user.id);
await localUserByIdCache.delete(user.id);
await Users.update(user.id, { await Users.update(user.id, {
isSilenced: true, isSilenced: true,
}); });

View file

@ -7,6 +7,11 @@ import { doPostSuspend } from "@/services/suspend-user.js";
import { publishUserEvent } from "@/services/stream.js"; import { publishUserEvent } from "@/services/stream.js";
import { scyllaClient } from "@/db/scylla.js"; import { scyllaClient } from "@/db/scylla.js";
import { SuspendedUsersCache } from "@/misc/cache.js"; import { SuspendedUsersCache } from "@/misc/cache.js";
import {
localUserByIdCache,
userByIdCache,
userDenormalizedCache,
} from "@/services/user-cache.js";
export const meta = { export const meta = {
tags: ["admin"], tags: ["admin"],
@ -38,6 +43,8 @@ export default define(meta, paramDef, async (ps, me) => {
throw new Error("cannot suspend moderator"); throw new Error("cannot suspend moderator");
} }
await userDenormalizedCache.delete(user.id);
await userByIdCache.delete(user.id);
await SuspendedUsersCache.init().then((cache) => cache.add(user.id)); await SuspendedUsersCache.init().then((cache) => cache.add(user.id));
await Users.update(user.id, { await Users.update(user.id, {
isSuspended: true, isSuspended: true,
@ -49,6 +56,7 @@ export default define(meta, paramDef, async (ps, me) => {
// Terminate streaming // Terminate streaming
if (Users.isLocalUser(user)) { if (Users.isLocalUser(user)) {
await localUserByIdCache.delete(user.id);
publishUserEvent(user.id, "terminate", {}); publishUserEvent(user.id, "terminate", {});
} }

View file

@ -2,6 +2,11 @@ import define from "../../define.js";
import { Users } from "@/models/index.js"; import { Users } from "@/models/index.js";
import { insertModerationLog } from "@/services/insert-moderation-log.js"; import { insertModerationLog } from "@/services/insert-moderation-log.js";
import { publishInternalEvent } from "@/services/stream.js"; import { publishInternalEvent } from "@/services/stream.js";
import {
localUserByIdCache,
userByIdCache,
userDenormalizedCache,
} from "@/services/user-cache.js";
export const meta = { export const meta = {
tags: ["admin"], tags: ["admin"],
@ -25,6 +30,9 @@ export default define(meta, paramDef, async (ps, me) => {
throw new Error("user not found"); throw new Error("user not found");
} }
await userDenormalizedCache.delete(user.id);
await userByIdCache.delete(user.id);
await localUserByIdCache.delete(user.id);
await Users.update(user.id, { await Users.update(user.id, {
isSilenced: false, isSilenced: false,
}); });

View file

@ -3,6 +3,11 @@ import define from "../../define.js";
import { Users } from "@/models/index.js"; import { Users } from "@/models/index.js";
import { insertModerationLog } from "@/services/insert-moderation-log.js"; import { insertModerationLog } from "@/services/insert-moderation-log.js";
import { doPostUnsuspend } from "@/services/unsuspend-user.js"; import { doPostUnsuspend } from "@/services/unsuspend-user.js";
import {
localUserByIdCache,
userByIdCache,
userDenormalizedCache,
} from "@/services/user-cache.js";
export const meta = { export const meta = {
tags: ["admin"], tags: ["admin"],
@ -26,6 +31,9 @@ export default define(meta, paramDef, async (ps, me) => {
throw new Error("user not found"); throw new Error("user not found");
} }
await userDenormalizedCache.delete(user.id);
await userByIdCache.delete(user.id);
await localUserByIdCache.delete(user.id);
await SuspendedUsersCache.init().then((cache) => cache.delete(user.id)); await SuspendedUsersCache.init().then((cache) => cache.delete(user.id));
await Users.update(user.id, { await Users.update(user.id, {
isSuspended: false, isSuspended: false,

View file

@ -35,11 +35,26 @@ export default async function (
searchCriteria.replyId = IsNull(); searchCriteria.replyId = IsNull();
} }
const notes = await Notes.find({ let notes: Note[] = [];
where: searchCriteria, if (scyllaClient) {
order: { createdAt: -1 }, const query = [prepared.note.select.byUserId, "LIMIT ?"];
take: history, notes = await scyllaClient
}); .execute(
query.join(" "),
[user.id, Math.min(history, config.scylla?.queryLimit ?? 100)],
{ prepare: true },
)
.then((result) => result.rows.map(parseScyllaNote));
notes = notes.filter(
(note) => !(!renotes && note.renoteId) && !(!replies && note.replyId),
);
} else {
notes = await Notes.find({
where: searchCriteria,
order: { createdAt: -1 },
take: history,
});
}
const feed = new Feed({ const feed = new Feed({
id: author.link, id: author.link,