fix: filter blocking ids out

This commit is contained in:
Namekuji 2023-08-17 02:23:28 -04:00
parent ef0d842298
commit ea94e5001e
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532
14 changed files with 180 additions and 79 deletions

View file

@ -11,6 +11,7 @@ import {
LocalFollowingsCache,
RenoteMutingsCache,
UserBlockedCache,
UserBlockingCache,
UserMutingsCache,
userWordMuteCache,
} from "@/misc/cache.js";
@ -499,17 +500,23 @@ export async function filterMutedNote(
return notes;
}
export async function filterBlockedUser(
export async function filterBlockUser(
notes: ScyllaNote[],
user: { id: User["id"] },
blockerIds?: User["id"][],
blockIds?: User["id"][],
): Promise<ScyllaNote[]> {
let ids: User["id"][];
if (blockerIds) {
ids = blockerIds;
if (blockIds) {
ids = blockIds;
} else {
ids = await UserBlockedCache.init(user.id).then((cache) => cache.getAll());
const blocked = await UserBlockedCache.init(user.id).then((cache) =>
cache.getAll(),
);
const blocking = await UserBlockingCache.init(user.id).then((cache) =>
cache.getAll(),
);
ids = [...blocked, ...blocking];
}
return notes.filter(

View file

@ -414,6 +414,25 @@ export class InstanceMutingsCache extends SetCache {
}
}
export class UserBlockingCache extends SetCache {
private constructor(userId: string) {
const fetcher = () =>
Blockings.find({
select: ["blockeeId"],
where: { blockerId: userId },
}).then((blocks) => blocks.map(({ blockeeId }) => blockeeId));
super("blocking", userId, fetcher);
}
public static async init(userId: string): Promise<UserBlockingCache> {
const cache = new UserBlockingCache(userId);
await cache.fetch();
return cache;
}
}
export class UserBlockedCache extends SetCache {
private constructor(userId: string) {
const fetcher = () =>

View file

@ -4,7 +4,7 @@ import { ApiError } from "../../error.js";
import { getUser } from "../../common/getters.js";
import { NoteWatchings, Users } from "@/models/index.js";
import { HOUR } from "@/const.js";
import { UserBlockedCache } from "@/misc/cache.js";
import { UserBlockingCache } from "@/misc/cache.js";
export const meta = {
tags: ["account"],
@ -70,8 +70,9 @@ export default define(meta, paramDef, async (ps, user) => {
});
// Check if already blocking
const cache = await UserBlockedCache.init(blockee.id);
const exist = await cache.has(blocker.id);
const exist = await UserBlockingCache.init(blocker.id).then((cache) =>
cache.has(blockee.id),
);
if (exist) {
throw new ApiError(meta.errors.alreadyBlocking);

View file

@ -4,7 +4,7 @@ import { ApiError } from "../../error.js";
import { getUser } from "../../common/getters.js";
import { Blockings, Users } from "@/models/index.js";
import { HOUR } from "@/const.js";
import { UserBlockedCache } from "@/misc/cache.js";
import { UserBlockedCache, UserBlockingCache } from "@/misc/cache.js";
export const meta = {
tags: ["account"],
@ -70,8 +70,9 @@ export default define(meta, paramDef, async (ps, user) => {
});
// Check not blocking
const cache = await UserBlockedCache.init(blockee.id);
const exist = await cache.has(blocker.id);
const exist = await UserBlockingCache.init(blocker.id).then((cache) =>
cache.has(blockee.id),
);
if (!exist) {
throw new ApiError(meta.errors.notBlocking);

View file

@ -6,7 +6,7 @@ import { generateMutedUserQuery } from "../../common/generate-muted-user-query.j
import { generateBlockedUserQuery } from "../../common/generate-block-query.js";
import {
ScyllaNote,
filterBlockedUser,
filterBlockUser,
filterMutedNote,
filterMutedUser,
filterVisibility,
@ -20,6 +20,7 @@ import {
InstanceMutingsCache,
LocalFollowingsCache,
UserBlockedCache,
UserBlockingCache,
UserMutingsCache,
userWordMuteCache,
} from "@/misc/cache.js";
@ -63,24 +64,32 @@ export default define(meta, paramDef, async (ps, user) => {
mutedUserIds,
mutedInstances,
blockerIds,
blockingIds,
]: string[][] = [];
let mutedWords: string[][] = [];
if (user) {
[followingUserIds, mutedUserIds, mutedInstances, mutedWords, blockerIds] =
await Promise.all([
LocalFollowingsCache.init(user.id).then((cache) => cache.getAll()),
UserMutingsCache.init(user.id).then((cache) => cache.getAll()),
InstanceMutingsCache.init(user.id).then((cache) => cache.getAll()),
userWordMuteCache
.fetchMaybe(user.id, () =>
UserProfiles.findOne({
select: ["mutedWords"],
where: { userId: user.id },
}).then((profile) => profile?.mutedWords),
)
.then((words) => words ?? []),
UserBlockedCache.init(user.id).then((cache) => cache.getAll()),
]);
[
followingUserIds,
mutedUserIds,
mutedInstances,
mutedWords,
blockerIds,
blockingIds,
] = await Promise.all([
LocalFollowingsCache.init(user.id).then((cache) => cache.getAll()),
UserMutingsCache.init(user.id).then((cache) => cache.getAll()),
InstanceMutingsCache.init(user.id).then((cache) => cache.getAll()),
userWordMuteCache
.fetchMaybe(user.id, () =>
UserProfiles.findOne({
select: ["mutedWords"],
where: { userId: user.id },
}).then((profile) => profile?.mutedWords),
)
.then((words) => words ?? []),
UserBlockedCache.init(user.id).then((cache) => cache.getAll()),
UserBlockingCache.init(user.id).then((cache) => cache.getAll()),
]);
}
const root = await getNote(ps.noteId, user, followingUserIds).catch(
@ -100,7 +109,10 @@ export default define(meta, paramDef, async (ps, user) => {
mutedInstances,
);
filtered = await filterMutedNote(filtered, user, mutedWords);
filtered = await filterBlockedUser(filtered, user, blockerIds);
filtered = await filterBlockUser(filtered, user, [
...blockerIds,
...blockingIds,
]);
}
return filtered;
};

View file

@ -12,7 +12,7 @@ import { generateMutedUserRenotesQueryForNotes } from "../../common/generated-mu
import {
ScyllaNote,
execNotePaginationQuery,
filterBlockedUser,
filterBlockUser,
filterMutedNote,
filterMutedRenotes,
filterMutedUser,
@ -23,6 +23,7 @@ import {
InstanceMutingsCache,
RenoteMutingsCache,
UserBlockedCache,
UserBlockingCache,
UserMutingsCache,
userWordMuteCache,
} from "@/misc/cache.js";
@ -94,25 +95,37 @@ export default define(meta, paramDef, async (ps, user) => {
});
if (scyllaClient) {
let [mutedUserIds, mutedInstances, blockerIds, renoteMutedIds]: string[][] =
[];
let [
mutedUserIds,
mutedInstances,
blockerIds,
blockingIds,
renoteMutedIds,
]: string[][] = [];
let mutedWords: string[][];
if (user) {
[mutedUserIds, mutedInstances, mutedWords, blockerIds, renoteMutedIds] =
await Promise.all([
UserMutingsCache.init(user.id).then((cache) => cache.getAll()),
InstanceMutingsCache.init(user.id).then((cache) => cache.getAll()),
userWordMuteCache
.fetchMaybe(user.id, () =>
UserProfiles.findOne({
select: ["mutedWords"],
where: { userId: user.id },
}).then((profile) => profile?.mutedWords),
)
.then((words) => words ?? []),
UserBlockedCache.init(user.id).then((cache) => cache.getAll()),
RenoteMutingsCache.init(user.id).then((cache) => cache.getAll()),
]);
[
mutedUserIds,
mutedInstances,
mutedWords,
blockerIds,
blockingIds,
renoteMutedIds,
] = await Promise.all([
UserMutingsCache.init(user.id).then((cache) => cache.getAll()),
InstanceMutingsCache.init(user.id).then((cache) => cache.getAll()),
userWordMuteCache
.fetchMaybe(user.id, () =>
UserProfiles.findOne({
select: ["mutedWords"],
where: { userId: user.id },
}).then((profile) => profile?.mutedWords),
)
.then((words) => words ?? []),
UserBlockedCache.init(user.id).then((cache) => cache.getAll()),
UserBlockingCache.init(user.id).then((cache) => cache.getAll()),
RenoteMutingsCache.init(user.id).then((cache) => cache.getAll()),
]);
}
const filter = async (notes: ScyllaNote[]) => {
@ -128,7 +141,10 @@ export default define(meta, paramDef, async (ps, user) => {
mutedInstances,
);
filtered = await filterMutedNote(filtered, user, mutedWords);
filtered = await filterBlockedUser(filtered, user, blockerIds);
filtered = await filterBlockUser(filtered, user, [
...blockerIds,
...blockingIds,
]);
filtered = await filterMutedRenotes(filtered, user, renoteMutedIds);
}
if (ps.withFiles) {
@ -140,10 +156,9 @@ export default define(meta, paramDef, async (ps, user) => {
const foundPacked = [];
while (foundPacked.length < ps.limit) {
const foundNotes = (await execNotePaginationQuery("global", ps, filter)).slice(
0,
ps.limit * 1.5,
); // Some may filtered out by Notes.packMany, thus we take more than ps.limit.
const foundNotes = (
await execNotePaginationQuery("global", ps, filter)
).slice(0, ps.limit * 1.5); // Some may filtered out by Notes.packMany, thus we take more than ps.limit.
foundPacked.push(
...(await Notes.packMany(foundNotes, user, { scyllaNote: true })),
);

View file

@ -15,7 +15,7 @@ import { generateMutedUserRenotesQueryForNotes } from "../../common/generated-mu
import {
ScyllaNote,
execNotePaginationQuery,
filterBlockedUser,
filterBlockUser,
filterChannel,
filterMutedNote,
filterMutedRenotes,
@ -30,6 +30,7 @@ import {
LocalFollowingsCache,
RenoteMutingsCache,
UserBlockedCache,
UserBlockingCache,
UserMutingsCache,
userWordMuteCache,
} from "@/misc/cache.js";
@ -108,6 +109,7 @@ export default define(meta, paramDef, async (ps, user) => {
mutedInstances,
mutedWords,
blockerIds,
blockingIds,
renoteMutedIds,
] = await Promise.all([
ChannelFollowingsCache.init(user.id).then((cache) => cache.getAll()),
@ -123,6 +125,7 @@ export default define(meta, paramDef, async (ps, user) => {
)
.then((words) => words ?? []),
UserBlockedCache.init(user.id).then((cache) => cache.getAll()),
UserBlockingCache.init(user.id).then((cache) => cache.getAll()),
RenoteMutingsCache.init(user.id).then((cache) => cache.getAll()),
]);
const homeUserIds = [user.id, ...followingUserIds];
@ -145,7 +148,10 @@ export default define(meta, paramDef, async (ps, user) => {
mutedInstances,
);
filtered = await filterMutedNote(filtered, user, mutedWords);
filtered = await filterBlockedUser(filtered, user, blockerIds);
filtered = await filterBlockUser(filtered, user, [
...blockerIds,
...blockingIds,
]);
filtered = await filterMutedRenotes(filtered, user, renoteMutedIds);
if (!ps.includeMyRenotes) {
filtered = filtered.filter((n) => n.userId !== user.id || optFilter(n));

View file

@ -15,7 +15,7 @@ import { generateMutedUserRenotesQueryForNotes } from "../../common/generated-mu
import {
ScyllaNote,
execNotePaginationQuery,
filterBlockedUser,
filterBlockUser,
filterChannel,
filterMutedNote,
filterMutedRenotes,
@ -30,6 +30,7 @@ import {
LocalFollowingsCache,
RenoteMutingsCache,
UserBlockedCache,
UserBlockingCache,
UserMutingsCache,
userWordMuteCache,
} from "@/misc/cache.js";
@ -115,6 +116,7 @@ export default define(meta, paramDef, async (ps, user) => {
mutedUserIds,
mutedInstances,
blockerIds,
blockingIds,
renoteMutedIds,
]: string[][] = [];
let mutedWords: string[][];
@ -126,6 +128,7 @@ export default define(meta, paramDef, async (ps, user) => {
mutedInstances,
mutedWords,
blockerIds,
blockingIds,
renoteMutedIds,
] = await Promise.all([
ChannelFollowingsCache.init(user.id).then((cache) => cache.getAll()),
@ -141,6 +144,7 @@ export default define(meta, paramDef, async (ps, user) => {
)
.then((words) => words ?? []),
UserBlockedCache.init(user.id).then((cache) => cache.getAll()),
UserBlockingCache.init(user.id).then((cache) => cache.getAll()),
RenoteMutingsCache.init(user.id).then((cache) => cache.getAll()),
]);
}
@ -157,7 +161,10 @@ export default define(meta, paramDef, async (ps, user) => {
mutedInstances,
);
filtered = await filterMutedNote(filtered, user, mutedWords);
filtered = await filterBlockedUser(filtered, user, blockerIds);
filtered = await filterBlockUser(filtered, user, [
...blockerIds,
...blockingIds,
]);
filtered = await filterMutedRenotes(filtered, user, renoteMutedIds);
}
if (ps.withFiles) {
@ -179,10 +186,9 @@ export default define(meta, paramDef, async (ps, user) => {
const foundPacked = [];
while (foundPacked.length < ps.limit) {
const foundNotes = (await execNotePaginationQuery("local", ps, filter)).slice(
0,
ps.limit * 1.5,
); // Some may filtered out by Notes.packMany, thus we take more than ps.limit.
const foundNotes = (
await execNotePaginationQuery("local", ps, filter)
).slice(0, ps.limit * 1.5); // Some may filtered out by Notes.packMany, thus we take more than ps.limit.
foundPacked.push(
...(await Notes.packMany(foundNotes, user, { scyllaNote: true })),
);

View file

@ -15,7 +15,7 @@ import { generateMutedUserRenotesQueryForNotes } from "../../common/generated-mu
import {
ScyllaNote,
execNotePaginationQuery,
filterBlockedUser,
filterBlockUser,
filterMutedNote,
filterMutedRenotes,
filterMutedUser,
@ -28,6 +28,7 @@ import {
LocalFollowingsCache,
RenoteMutingsCache,
UserBlockedCache,
UserBlockingCache,
UserMutingsCache,
userWordMuteCache,
} from "@/misc/cache.js";
@ -111,6 +112,7 @@ export default define(meta, paramDef, async (ps, user) => {
mutedUserIds,
mutedInstances,
blockerIds,
blockingIds,
renoteMutedIds,
]: string[][] = [];
let mutedWords: string[][];
@ -121,6 +123,7 @@ export default define(meta, paramDef, async (ps, user) => {
mutedInstances,
mutedWords,
blockerIds,
blockingIds,
renoteMutedIds,
] = await Promise.all([
LocalFollowingsCache.init(user.id).then((cache) => cache.getAll()),
@ -135,6 +138,7 @@ export default define(meta, paramDef, async (ps, user) => {
)
.then((words) => words ?? []),
UserBlockedCache.init(user.id).then((cache) => cache.getAll()),
UserBlockingCache.init(user.id).then((cache) => cache.getAll()),
RenoteMutingsCache.init(user.id).then((cache) => cache.getAll()),
]);
}
@ -157,7 +161,10 @@ export default define(meta, paramDef, async (ps, user) => {
mutedInstances,
);
filtered = await filterMutedNote(filtered, user, mutedWords);
filtered = await filterBlockedUser(filtered, user, blockerIds);
filtered = await filterBlockUser(filtered, user, [
...blockerIds,
...blockingIds,
]);
filtered = await filterMutedRenotes(filtered, user, renoteMutedIds);
}
if (ps.withFiles) {

View file

@ -9,7 +9,7 @@ import { generateBlockedUserQuery } from "../../common/generate-block-query.js";
import {
ScyllaNote,
execNotePaginationQuery,
filterBlockedUser,
filterBlockUser,
filterMutedUser,
filterVisibility,
scyllaClient,
@ -18,6 +18,7 @@ import {
InstanceMutingsCache,
LocalFollowingsCache,
UserBlockedCache,
UserBlockingCache,
UserMutingsCache,
} from "@/misc/cache.js";
@ -75,13 +76,16 @@ export default define(meta, paramDef, async (ps, user) => {
});
if (scyllaClient) {
let [mutedUserIds, mutedInstances, blockerIds]: string[][] = [];
let [mutedUserIds, mutedInstances, blockerIds, blockingIds]: string[][] =
[];
if (user) {
[mutedUserIds, mutedInstances, blockerIds] = await Promise.all([
UserMutingsCache.init(user.id).then((cache) => cache.getAll()),
InstanceMutingsCache.init(user.id).then((cache) => cache.getAll()),
UserBlockedCache.init(user.id).then((cache) => cache.getAll()),
]);
[mutedUserIds, mutedInstances, blockerIds, blockingIds] =
await Promise.all([
UserMutingsCache.init(user.id).then((cache) => cache.getAll()),
InstanceMutingsCache.init(user.id).then((cache) => cache.getAll()),
UserBlockedCache.init(user.id).then((cache) => cache.getAll()),
UserBlockingCache.init(user.id).then((cache) => cache.getAll()),
]);
}
const filter = async (notes: ScyllaNote[]) => {
@ -97,7 +101,10 @@ export default define(meta, paramDef, async (ps, user) => {
mutedUserIds,
mutedInstances,
);
filtered = await filterBlockedUser(filtered, user, blockerIds);
filtered = await filterBlockUser(filtered, user, [
...blockerIds,
...blockingIds,
]);
}
return filtered;
};

View file

@ -20,7 +20,7 @@ import {
execNotePaginationQuery,
filterMutedUser,
filterMutedNote,
filterBlockedUser,
filterBlockUser,
filterMutedRenotes,
} from "@/db/scylla.js";
import {
@ -29,6 +29,7 @@ import {
LocalFollowingsCache,
RenoteMutingsCache,
UserBlockedCache,
UserBlockingCache,
UserMutingsCache,
userWordMuteCache,
} from "@/misc/cache.js";
@ -99,6 +100,7 @@ export default define(meta, paramDef, async (ps, user) => {
mutedInstances,
mutedWords,
blockerIds,
blockingIds,
renoteMutedIds,
] = await Promise.all([
ChannelFollowingsCache.init(user.id).then((cache) => cache.getAll()),
@ -114,6 +116,7 @@ export default define(meta, paramDef, async (ps, user) => {
)
.then((words) => words ?? []),
UserBlockedCache.init(user.id).then((cache) => cache.getAll()),
UserBlockingCache.init(user.id).then((cache) => cache.getAll()),
RenoteMutingsCache.init(user.id).then((cache) => cache.getAll()),
]);
const validUserIds = [user.id, ...followingUserIds];
@ -132,7 +135,10 @@ export default define(meta, paramDef, async (ps, user) => {
mutedInstances,
);
filtered = await filterMutedNote(filtered, user, mutedWords);
filtered = await filterBlockedUser(filtered, user, blockerIds);
filtered = await filterBlockUser(filtered, user, [
...blockerIds,
...blockingIds,
]);
filtered = await filterMutedRenotes(filtered, user, renoteMutedIds);
if (!ps.includeMyRenotes) {
filtered = filtered.filter((n) => n.userId !== user.id || optFilter(n));

View file

@ -10,7 +10,7 @@ import { generateBlockedUserQuery } from "../../common/generate-block-query.js";
import {
ScyllaNote,
execNotePaginationQuery,
filterBlockedUser,
filterBlockUser,
filterMutedNote,
filterMutedUser,
filterVisibility,
@ -20,6 +20,7 @@ import {
InstanceMutingsCache,
LocalFollowingsCache,
UserBlockedCache,
UserBlockingCache,
UserMutingsCache,
userWordMuteCache,
} from "@/misc/cache.js";
@ -89,6 +90,7 @@ export default define(meta, paramDef, async (ps, me) => {
mutedInstances,
mutedWords,
blockerIds,
blockingIds,
] = await Promise.all([
LocalFollowingsCache.init(user.id).then((cache) => cache.getAll()),
UserMutingsCache.init(user.id).then((cache) => cache.getAll()),
@ -102,6 +104,7 @@ export default define(meta, paramDef, async (ps, me) => {
)
.then((words) => words ?? []),
UserBlockedCache.init(user.id).then((cache) => cache.getAll()),
UserBlockingCache.init(user.id).then((cache) => cache.getAll()),
]);
if (
@ -122,7 +125,10 @@ export default define(meta, paramDef, async (ps, me) => {
mutedInstances,
);
filtered = await filterMutedNote(filtered, user, mutedWords);
filtered = await filterBlockedUser(filtered, user, blockerIds);
filtered = await filterBlockUser(filtered, user, [
...blockerIds,
...blockingIds,
]);
if (ps.withFiles) {
filtered = filtered.filter((n) => n.files.length > 0);
}

View file

@ -19,7 +19,7 @@ import { perUserFollowingChart } from "@/services/chart/index.js";
import { genId } from "@/misc/gen-id.js";
import { getActiveWebhooks } from "@/misc/webhook-cache.js";
import { webhookDeliver } from "@/queue/index.js";
import { UserBlockedCache } from "@/misc/cache.js";
import { UserBlockedCache, UserBlockingCache } from "@/misc/cache.js";
export default async function (blocker: User, blockee: User) {
await Promise.all([
@ -41,8 +41,12 @@ export default async function (blocker: User, blockee: User) {
await Blockings.insert(blocking);
const cache = await UserBlockedCache.init(blockee.id);
await cache.add(blocker.id);
await UserBlockedCache.init(blockee.id).then((cache) =>
cache.add(blocker.id),
);
await UserBlockingCache.init(blocker.id).then((cache) =>
cache.add(blockee.id),
);
if (Users.isLocalUser(blocker) && Users.isRemoteUser(blockee)) {
const content = renderActivity(renderBlock(blocking));

View file

@ -5,7 +5,7 @@ import { deliver } from "@/queue/index.js";
import Logger from "../logger.js";
import type { CacheableUser } from "@/models/entities/user.js";
import { Blockings, Users } from "@/models/index.js";
import { UserBlockedCache } from "@/misc/cache.js";
import { UserBlockedCache, UserBlockingCache } from "@/misc/cache.js";
const logger = new Logger("blocking/delete");
@ -15,8 +15,12 @@ export default async function (blocker: CacheableUser, blockee: CacheableUser) {
blockeeId: blockee.id,
});
const cache = await UserBlockedCache.init(blockee.id);
await cache.delete(blocker.id);
await UserBlockedCache.init(blockee.id).then((cache) =>
cache.delete(blocker.id),
);
await UserBlockingCache.init(blocker.id).then((cache) =>
cache.delete(blockee.id),
);
if (blocking == null) {
logger.warn(