[backend] Don't fetch more notes than requested

This commit is contained in:
Laura Hausmann 2023-11-22 20:07:59 +01:00
parent 735fd37707
commit fd6ee32832
Signed by: zotan
GPG key ID: D044E84C5BE01605
10 changed files with 22 additions and 170 deletions

View file

@ -106,25 +106,10 @@ export default define(meta, paramDef, async (ps, 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;
}
const notes = await query.take(ps.limit).getMany();
return await Notes.packMany(notes, user);
} catch (error) {
throw new ApiError(meta.errors.queryError);
}
if (found.length > ps.limit) {
found.length = ps.limit;
}
return found;
});

View file

@ -165,25 +165,10 @@ export default define(meta, paramDef, async (ps, 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;
}
const notes = await query.take(ps.limit).getMany();
return await Notes.packMany(notes, user);
} catch (error) {
throw new ApiError(meta.errors.queryError);
}
if (found.length > ps.limit) {
found.length = ps.limit;
}
return found;
});

View file

@ -135,25 +135,10 @@ export default define(meta, paramDef, async (ps, 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;
}
const notes = await query.take(ps.limit).getMany();
return await Notes.packMany(notes, user);
} catch (error) {
throw new ApiError(meta.errors.queryError);
}
if (found.length > ps.limit) {
found.length = ps.limit;
}
return found;
});

View file

@ -80,23 +80,10 @@ export default define(meta, paramDef, async (ps, user) => {
query.setParameters(followingQuery.getParameters());
}
// 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;
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;
}
const notes = await query.take(ps.limit).getMany();
const packed = await Notes.packMany(notes, user);
if (found.length > ps.limit) {
found.length = ps.limit;
}
read(user.id, packed);
read(user.id, found);
return found;
return packed;
});

View file

@ -135,25 +135,10 @@ export default define(meta, paramDef, async (ps, 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;
}
const notes = await query.take(ps.limit).getMany();
return await Notes.packMany(notes, user);
} catch (error) {
throw new ApiError(meta.errors.queryError);
}
if (found.length > ps.limit) {
found.length = ps.limit;
}
return found;
});

View file

@ -75,21 +75,6 @@ export default define(meta, paramDef, async (ps, user) => {
if (user) generateMutedUserQuery(query, user);
if (user) generateBlockedUserQuery(query, 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;
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;
}
if (found.length > ps.limit) {
found.length = ps.limit;
}
return found;
const notes = await query.take(ps.limit).getMany();
return await Notes.packMany(notes, user);
});

View file

@ -52,21 +52,6 @@ export default define(meta, paramDef, async (ps, user) => {
if (user) generateMutedUserQuery(query, user);
if (user) generateBlockedUserQuery(query, 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;
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;
}
if (found.length > ps.limit) {
found.length = ps.limit;
}
return found;
const notes = await query.take(ps.limit).getMany();
return await Notes.packMany(notes, user);
});

View file

@ -139,21 +139,6 @@ export default define(meta, paramDef, async (ps, me) => {
}
}
// 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;
while (found.length < ps.limit) {
const notes = await query.take(take).skip(skip).getMany();
found.push(...(await Notes.packMany(notes, me)));
skip += take;
if (notes.length < take) break;
}
if (found.length > ps.limit) {
found.length = ps.limit;
}
return found;
const notes = await query.take(ps.limit).getMany();
return await Notes.packMany(notes, me);
});

View file

@ -143,25 +143,10 @@ export default define(meta, paramDef, async (ps, 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;
}
const notes = await query.take(ps.limit).getMany();
return await Notes.packMany(notes, user);
} catch (error) {
throw new ApiError(meta.errors.queryError);
}
if (found.length > ps.limit) {
found.length = ps.limit;
}
return found;
});

View file

@ -143,25 +143,10 @@ export default define(meta, paramDef, async (ps, 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;
}
const notes = await query.take(ps.limit).getMany();
return await Notes.packMany(notes, user);
} catch (error) {
throw new ApiError(meta.errors.queryError);
}
if (found.length > ps.limit) {
found.length = ps.limit;
}
return found;
});