Weird stopping point but whatever
This commit is contained in:
@@ -26,6 +26,12 @@ type StatusRow = {
|
||||
} | null,
|
||||
};
|
||||
|
||||
type Paginated<T> = {
|
||||
page: T[],
|
||||
isDone: boolean,
|
||||
continueCursor: string | null,
|
||||
};
|
||||
|
||||
// CHANGED: typed helpers
|
||||
const ensureUser = async (ctx: RWCtx, userId: Id<'users'>) => {
|
||||
const user = await ctx.db.get(userId);
|
||||
@@ -228,35 +234,76 @@ export const getCurrentForAll = query({
|
||||
});
|
||||
|
||||
/**
|
||||
* Paginated history for a specific user (newest first).
|
||||
* Paginated history for all users or for a specific user.
|
||||
*/
|
||||
export const listHistoryByUser = query({
|
||||
export const listHistory = query({
|
||||
args: {
|
||||
userId: v.id('users'),
|
||||
userId: v.optional(v.id('users')),
|
||||
paginationOpts: paginationOptsValidator,
|
||||
},
|
||||
handler: async (ctx, { userId, paginationOpts }) => {
|
||||
await ensureUser(ctx, userId);
|
||||
handler: async (ctx, { userId, paginationOpts }): Promise<
|
||||
Paginated<StatusRow>
|
||||
> => {
|
||||
// Query statuses newest-first, optionally filtered by user
|
||||
const result = userId
|
||||
? await ctx.db
|
||||
.query('statuses')
|
||||
.withIndex('by_user_updatedAt', (q) => q.eq('userId', userId))
|
||||
.order('desc')
|
||||
.paginate(paginationOpts)
|
||||
: await ctx.db
|
||||
.query('statuses')
|
||||
.order('desc')
|
||||
.paginate(paginationOpts);
|
||||
|
||||
return await ctx.db
|
||||
.query('statuses')
|
||||
.withIndex('by_user_updatedAt', (q) => q.eq('userId', userId))
|
||||
.order('desc')
|
||||
.paginate(paginationOpts);
|
||||
// Cache user display objects to avoid refetching repeatedly
|
||||
const displayCache = new Map<string, StatusRow['user']>();
|
||||
|
||||
const getDisplay = async (
|
||||
uid: Id<'users'>,
|
||||
): Promise<StatusRow['user']> => {
|
||||
const key = uid as unknown as string;
|
||||
const cached = displayCache.get(key);
|
||||
if (cached) return cached;
|
||||
|
||||
const user = await ctx.db.get(uid);
|
||||
if (!user) throw new ConvexError('User not found.');
|
||||
|
||||
const imgId = getImageId(user);
|
||||
const imgUrl = imgId ? await ctx.storage.getUrl(imgId) : null;
|
||||
|
||||
const display: StatusRow['user'] = {
|
||||
id: user._id,
|
||||
name: getName(user),
|
||||
imageUrl: imgUrl,
|
||||
};
|
||||
displayCache.set(key, display);
|
||||
return display;
|
||||
};
|
||||
|
||||
const page: StatusRow[] = [];
|
||||
for (const s of result.page) {
|
||||
const owner = await getDisplay(s.userId);
|
||||
const updatedBy =
|
||||
s.updatedBy !== s.userId ? await getDisplay(s.updatedBy) : null;
|
||||
|
||||
page.push({
|
||||
user: owner,
|
||||
status: {
|
||||
id: s._id,
|
||||
message: s.message,
|
||||
updatedAt: s.updatedAt,
|
||||
updatedBy,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
page,
|
||||
isDone: result.isDone,
|
||||
continueCursor: result.continueCursor,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Global paginated history (all users, newest first).
|
||||
* - Add an index on updatedAt if you want to avoid full-table scans
|
||||
* when the collection grows large.
|
||||
*/
|
||||
export const listHistoryAll = query({
|
||||
args: { paginationOpts: paginationOptsValidator },
|
||||
handler: async (ctx, { paginationOpts }) => {
|
||||
return await ctx.db
|
||||
.query('statuses')
|
||||
.order('desc')
|
||||
.paginate(paginationOpts);
|
||||
},
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user