forked from mirrors/iceshrimp
Laura Hausmann
302b112f05
This drastically improves timeline performance due to the many (2-6 per query) database joins that are now no longer required
168 lines
4.2 KiB
TypeScript
168 lines
4.2 KiB
TypeScript
import { Brackets } from "typeorm";
|
|
import { UserLists, UserListJoinings, Notes } from "@/models/index.js";
|
|
import { activeUsersChart } from "@/services/chart/index.js";
|
|
import define from "../../define.js";
|
|
import { ApiError } from "../../error.js";
|
|
import { makePaginationQuery } from "../../common/make-pagination-query.js";
|
|
import { generateVisibilityQuery } from "../../common/generate-visibility-query.js";
|
|
|
|
export const meta = {
|
|
tags: ["notes", "lists"],
|
|
|
|
requireCredential: true,
|
|
|
|
res: {
|
|
type: "array",
|
|
optional: false,
|
|
nullable: false,
|
|
items: {
|
|
type: "object",
|
|
optional: false,
|
|
nullable: false,
|
|
ref: "Note",
|
|
},
|
|
},
|
|
|
|
errors: {
|
|
noSuchList: {
|
|
message: "No such list.",
|
|
code: "NO_SUCH_LIST",
|
|
id: "8fb1fbd5-e476-4c37-9fb0-43d55b63a2ff",
|
|
},
|
|
queryError: {
|
|
message: "Please follow more users.",
|
|
code: "QUERY_ERROR",
|
|
id: "620763f4-f621-4533-ab33-0577a1a3c343",
|
|
},
|
|
},
|
|
} as const;
|
|
|
|
export const paramDef = {
|
|
type: "object",
|
|
properties: {
|
|
listId: { type: "string", format: "misskey:id" },
|
|
limit: { type: "integer", minimum: 1, maximum: 100, default: 10 },
|
|
sinceId: { type: "string", format: "misskey:id" },
|
|
untilId: { type: "string", format: "misskey:id" },
|
|
sinceDate: { type: "integer" },
|
|
untilDate: { type: "integer" },
|
|
includeMyRenotes: { type: "boolean", default: true },
|
|
includeRenotedMyNotes: { type: "boolean", default: true },
|
|
includeLocalRenotes: { type: "boolean", default: true },
|
|
withFiles: {
|
|
type: "boolean",
|
|
default: false,
|
|
description: "Only show notes that have attached files.",
|
|
},
|
|
},
|
|
required: ["listId"],
|
|
} as const;
|
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
|
const list = await UserLists.findOneBy({
|
|
id: ps.listId,
|
|
userId: user.id,
|
|
});
|
|
|
|
if (list == null) {
|
|
throw new ApiError(meta.errors.noSuchList);
|
|
}
|
|
|
|
//#region Construct query
|
|
const query = makePaginationQuery(
|
|
Notes.createQueryBuilder("note"),
|
|
ps.sinceId,
|
|
ps.untilId,
|
|
)
|
|
.innerJoin(
|
|
UserListJoinings.metadata.targetName,
|
|
"userListJoining",
|
|
"userListJoining.userId = note.userId",
|
|
)
|
|
.innerJoinAndSelect("note.user", "user")
|
|
.leftJoinAndSelect("note.reply", "reply")
|
|
.leftJoinAndSelect("note.renote", "renote")
|
|
.leftJoinAndSelect("reply.user", "replyUser")
|
|
.leftJoinAndSelect("renote.user", "renoteUser")
|
|
.andWhere("userListJoining.userListId = :userListId", {
|
|
userListId: list.id,
|
|
});
|
|
|
|
generateVisibilityQuery(query, user);
|
|
|
|
if (ps.includeMyRenotes === false) {
|
|
query.andWhere(
|
|
new Brackets((qb) => {
|
|
qb.orWhere("note.userId != :meId", { meId: user.id });
|
|
qb.orWhere("note.renoteId IS NULL");
|
|
qb.orWhere("note.text IS NOT NULL");
|
|
qb.orWhere("note.fileIds != '{}'");
|
|
qb.orWhere(
|
|
'0 < (SELECT COUNT(*) FROM poll WHERE poll."noteId" = note.id)',
|
|
);
|
|
}),
|
|
);
|
|
}
|
|
|
|
if (ps.includeRenotedMyNotes === false) {
|
|
query.andWhere(
|
|
new Brackets((qb) => {
|
|
qb.orWhere("note.renoteUserId != :meId", { meId: user.id });
|
|
qb.orWhere("note.renoteId IS NULL");
|
|
qb.orWhere("note.text IS NOT NULL");
|
|
qb.orWhere("note.fileIds != '{}'");
|
|
qb.orWhere(
|
|
'0 < (SELECT COUNT(*) FROM poll WHERE poll."noteId" = note.id)',
|
|
);
|
|
}),
|
|
);
|
|
}
|
|
|
|
if (ps.includeLocalRenotes === false) {
|
|
query.andWhere(
|
|
new Brackets((qb) => {
|
|
qb.orWhere("note.renoteUserHost IS NOT NULL");
|
|
qb.orWhere("note.renoteId IS NULL");
|
|
qb.orWhere("note.text IS NOT NULL");
|
|
qb.orWhere("note.fileIds != '{}'");
|
|
qb.orWhere(
|
|
'0 < (SELECT COUNT(*) FROM poll WHERE poll."noteId" = note.id)',
|
|
);
|
|
}),
|
|
);
|
|
}
|
|
|
|
if (ps.withFiles) {
|
|
query.andWhere("note.fileIds != '{}'");
|
|
}
|
|
//#endregion
|
|
|
|
process.nextTick(() => {
|
|
if (user) {
|
|
activeUsersChart.read(user);
|
|
}
|
|
});
|
|
|
|
// We fetch more than requested because some may be filtered out, and if there's less than
|
|
// requested, the pagination stops.
|
|
const found = [];
|
|
const take = Math.floor(ps.limit * 1.5);
|
|
let skip = 0;
|
|
try {
|
|
while (found.length < ps.limit) {
|
|
const notes = await query.take(take).skip(skip).getMany();
|
|
found.push(...(await Notes.packMany(notes, user)));
|
|
skip += take;
|
|
if (notes.length < take) break;
|
|
}
|
|
} catch (error) {
|
|
throw new ApiError(meta.errors.queryError);
|
|
}
|
|
|
|
if (found.length > ps.limit) {
|
|
found.length = ps.limit;
|
|
}
|
|
|
|
return found;
|
|
});
|