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

View file

@ -1,6 +1,11 @@
import define from "../../../define.js";
import { Users } from "@/models/index.js";
import { publishInternalEvent } from "@/services/stream.js";
import {
localUserByIdCache,
userByIdCache,
userDenormalizedCache,
} from "@/services/user-cache.js";
export const meta = {
tags: ["admin"],
@ -28,6 +33,9 @@ export default define(meta, paramDef, async (ps) => {
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, {
isModerator: true,
});

View file

@ -1,6 +1,11 @@
import define from "../../../define.js";
import { Users } from "@/models/index.js";
import { publishInternalEvent } from "@/services/stream.js";
import {
localUserByIdCache,
userByIdCache,
userDenormalizedCache,
} from "@/services/user-cache.js";
export const meta = {
tags: ["admin"],
@ -24,6 +29,9 @@ export default define(meta, paramDef, async (ps) => {
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, {
isModerator: false,
});

View file

@ -2,6 +2,11 @@ import define from "../../define.js";
import { Users } from "@/models/index.js";
import { insertModerationLog } from "@/services/insert-moderation-log.js";
import { publishInternalEvent } from "@/services/stream.js";
import {
localUserByIdCache,
userByIdCache,
userDenormalizedCache,
} from "@/services/user-cache.js";
export const meta = {
tags: ["admin"],
@ -29,6 +34,9 @@ export default define(meta, paramDef, async (ps, me) => {
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, {
isSilenced: true,
});

View file

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

View file

@ -2,6 +2,11 @@ import define from "../../define.js";
import { Users } from "@/models/index.js";
import { insertModerationLog } from "@/services/insert-moderation-log.js";
import { publishInternalEvent } from "@/services/stream.js";
import {
localUserByIdCache,
userByIdCache,
userDenormalizedCache,
} from "@/services/user-cache.js";
export const meta = {
tags: ["admin"],
@ -25,6 +30,9 @@ export default define(meta, paramDef, async (ps, me) => {
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, {
isSilenced: false,
});

View file

@ -3,6 +3,11 @@ import define from "../../define.js";
import { Users } from "@/models/index.js";
import { insertModerationLog } from "@/services/insert-moderation-log.js";
import { doPostUnsuspend } from "@/services/unsuspend-user.js";
import {
localUserByIdCache,
userByIdCache,
userDenormalizedCache,
} from "@/services/user-cache.js";
export const meta = {
tags: ["admin"],
@ -26,6 +31,9 @@ export default define(meta, paramDef, async (ps, me) => {
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 Users.update(user.id, {
isSuspended: false,

View file

@ -35,11 +35,26 @@ export default async function (
searchCriteria.replyId = IsNull();
}
const notes = await Notes.find({
let notes: Note[] = [];
if (scyllaClient) {
const query = [prepared.note.select.byUserId, "LIMIT ?"];
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({
id: author.link,