[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
No known key found for this signature in database
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 { try {
while (found.length < ps.limit) { const notes = await query.take(ps.limit).getMany();
const notes = await query.take(take).skip(skip).getMany(); return await Notes.packMany(notes, user);
found.push(...(await Notes.packMany(notes, user)));
skip += take;
if (notes.length < take) break;
}
} catch (error) { } catch (error) {
throw new ApiError(meta.errors.queryError); 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); 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 { try {
while (found.length < ps.limit) { const notes = await query.take(ps.limit).getMany();
const notes = await query.take(take).skip(skip).getMany(); return await Notes.packMany(notes, user);
found.push(...(await Notes.packMany(notes, user)));
skip += take;
if (notes.length < take) break;
}
} catch (error) { } catch (error) {
throw new ApiError(meta.errors.queryError); 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 { try {
while (found.length < ps.limit) { const notes = await query.take(ps.limit).getMany();
const notes = await query.take(take).skip(skip).getMany(); return await Notes.packMany(notes, user);
found.push(...(await Notes.packMany(notes, user)));
skip += take;
if (notes.length < take) break;
}
} catch (error) { } catch (error) {
throw new ApiError(meta.errors.queryError); 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()); query.setParameters(followingQuery.getParameters());
} }
// We fetch more than requested because some may be filtered out, and if there's less than const notes = await query.take(ps.limit).getMany();
// requested, the pagination stops. const packed = await Notes.packMany(notes, user);
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) { read(user.id, packed);
found.length = ps.limit;
}
read(user.id, found); return packed;
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 { try {
while (found.length < ps.limit) { const notes = await query.take(ps.limit).getMany();
const notes = await query.take(take).skip(skip).getMany(); return await Notes.packMany(notes, user);
found.push(...(await Notes.packMany(notes, user)));
skip += take;
if (notes.length < take) break;
}
} catch (error) { } catch (error) {
throw new ApiError(meta.errors.queryError); 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) generateMutedUserQuery(query, user);
if (user) generateBlockedUserQuery(query, user); if (user) generateBlockedUserQuery(query, user);
// We fetch more than requested because some may be filtered out, and if there's less than const notes = await query.take(ps.limit).getMany();
// requested, the pagination stops. return await Notes.packMany(notes, user);
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;
}); });

View file

@ -52,21 +52,6 @@ export default define(meta, paramDef, async (ps, user) => {
if (user) generateMutedUserQuery(query, user); if (user) generateMutedUserQuery(query, user);
if (user) generateBlockedUserQuery(query, user); if (user) generateBlockedUserQuery(query, user);
// We fetch more than requested because some may be filtered out, and if there's less than const notes = await query.take(ps.limit).getMany();
// requested, the pagination stops. return await Notes.packMany(notes, user);
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;
}); });

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 const notes = await query.take(ps.limit).getMany();
// requested, the pagination stops. return await Notes.packMany(notes, me);
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;
}); });

View file

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