feat(frontend): optimize photo requesting for slower internet
This commit is contained in:
@@ -43,6 +43,17 @@ export const create = mutation({
|
|||||||
},
|
},
|
||||||
returns: v.id('photoRequests'),
|
returns: v.id('photoRequests'),
|
||||||
handler: async (ctx, args) => {
|
handler: async (ctx, args) => {
|
||||||
|
const oldRequests = await ctx.db
|
||||||
|
.query('photoRequests')
|
||||||
|
.withIndex('by_chat_id', (q) => q.eq('chatId', args.chatId))
|
||||||
|
.take(20);
|
||||||
|
|
||||||
|
for (const req of oldRequests) {
|
||||||
|
if (req.status === 'pending' || req.status === 'countdown' || req.status === 'capture_now') {
|
||||||
|
await ctx.db.patch(req._id, { status: 'rejected' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return await ctx.db.insert('photoRequests', {
|
return await ctx.db.insert('photoRequests', {
|
||||||
chatId: args.chatId,
|
chatId: args.chatId,
|
||||||
requesterId: args.requesterId,
|
requesterId: args.requesterId,
|
||||||
@@ -69,15 +80,20 @@ export const submitPhoto = mutation({
|
|||||||
photoMediaType: v.string(),
|
photoMediaType: v.string(),
|
||||||
thumbnailBase64: v.optional(v.string())
|
thumbnailBase64: v.optional(v.string())
|
||||||
},
|
},
|
||||||
returns: v.null(),
|
returns: v.boolean(),
|
||||||
handler: async (ctx, args) => {
|
handler: async (ctx, args) => {
|
||||||
|
const req = await ctx.db.get(args.requestId);
|
||||||
|
if (!req || req.status !== 'capture_now') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
await ctx.db.patch(args.requestId, {
|
await ctx.db.patch(args.requestId, {
|
||||||
status: 'captured',
|
status: 'captured',
|
||||||
photoBase64: args.photoBase64,
|
photoBase64: args.photoBase64,
|
||||||
photoMediaType: args.photoMediaType,
|
photoMediaType: args.photoMediaType,
|
||||||
thumbnailBase64: args.thumbnailBase64
|
thumbnailBase64: args.thumbnailBase64
|
||||||
});
|
});
|
||||||
return null;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -92,10 +108,14 @@ export const markAccepted = mutation({
|
|||||||
|
|
||||||
export const markRejected = mutation({
|
export const markRejected = mutation({
|
||||||
args: { requestId: v.id('photoRequests') },
|
args: { requestId: v.id('photoRequests') },
|
||||||
returns: v.null(),
|
returns: v.boolean(),
|
||||||
handler: async (ctx, args) => {
|
handler: async (ctx, args) => {
|
||||||
await ctx.db.patch(args.requestId, { status: 'rejected' });
|
const req = await ctx.db.get(args.requestId);
|
||||||
return null;
|
if (!req || req.status === 'accepted' || req.status === 'rejected') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
await ctx.db.patch(req._id, { status: 'rejected' });
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -108,13 +128,16 @@ export const getCaptureNowRequest = query({
|
|||||||
args: { chatId: v.id('chats'), deviceId: v.optional(v.string()) },
|
args: { chatId: v.id('chats'), deviceId: v.optional(v.string()) },
|
||||||
returns: v.union(captureNowLightValidator, v.null()),
|
returns: v.union(captureNowLightValidator, v.null()),
|
||||||
handler: async (ctx, args) => {
|
handler: async (ctx, args) => {
|
||||||
|
const now = Date.now();
|
||||||
|
const maxAge = 60 * 1000;
|
||||||
|
|
||||||
const requests = await ctx.db
|
const requests = await ctx.db
|
||||||
.query('photoRequests')
|
.query('photoRequests')
|
||||||
.withIndex('by_chat_id', (q) => q.eq('chatId', args.chatId))
|
.withIndex('by_chat_id', (q) => q.eq('chatId', args.chatId))
|
||||||
.order('desc')
|
.order('desc')
|
||||||
.take(50);
|
.take(50);
|
||||||
|
|
||||||
const found = requests.find((r) => r.status === 'capture_now');
|
const found = requests.find((r) => r.status === 'capture_now' && now - r.createdAt < maxAge);
|
||||||
if (!found) return null;
|
if (!found) return null;
|
||||||
|
|
||||||
return { _id: found._id, status: 'capture_now' as const };
|
return { _id: found._id, status: 'capture_now' as const };
|
||||||
@@ -142,6 +165,7 @@ export const getMyActiveRequest = query({
|
|||||||
const requests = await ctx.db
|
const requests = await ctx.db
|
||||||
.query('photoRequests')
|
.query('photoRequests')
|
||||||
.withIndex('by_chat_id', (q) => q.eq('chatId', args.chatId))
|
.withIndex('by_chat_id', (q) => q.eq('chatId', args.chatId))
|
||||||
|
.order('desc')
|
||||||
.take(100);
|
.take(100);
|
||||||
|
|
||||||
if (!args.deviceId) return null;
|
if (!args.deviceId) return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user