feat(*): send images from website
This commit is contained in:
95
frontend/src/lib/convex/photoDrafts.ts
Normal file
95
frontend/src/lib/convex/photoDrafts.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import { v } from 'convex/values';
|
||||
import { mutation, query } from './_generated/server';
|
||||
|
||||
const photoValidator = v.object({
|
||||
base64: v.string(),
|
||||
mediaType: v.string()
|
||||
});
|
||||
|
||||
export const get = query({
|
||||
args: { chatId: v.id('chats'), deviceId: v.string() },
|
||||
returns: v.object({
|
||||
photos: v.array(
|
||||
v.object({
|
||||
_id: v.id('photoDrafts'),
|
||||
base64: v.string(),
|
||||
mediaType: v.string()
|
||||
})
|
||||
)
|
||||
}),
|
||||
handler: async (ctx, args) => {
|
||||
const drafts = await ctx.db
|
||||
.query('photoDrafts')
|
||||
.withIndex('by_chat_id_and_device_id', (q) =>
|
||||
q.eq('chatId', args.chatId).eq('deviceId', args.deviceId)
|
||||
)
|
||||
.collect();
|
||||
|
||||
return {
|
||||
photos: drafts.map((d) => ({
|
||||
_id: d._id,
|
||||
base64: d.base64,
|
||||
mediaType: d.mediaType
|
||||
}))
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
export const addPhoto = mutation({
|
||||
args: {
|
||||
chatId: v.id('chats'),
|
||||
deviceId: v.string(),
|
||||
photo: photoValidator
|
||||
},
|
||||
returns: v.null(),
|
||||
handler: async (ctx, args) => {
|
||||
await ctx.db.insert('photoDrafts', {
|
||||
chatId: args.chatId,
|
||||
deviceId: args.deviceId,
|
||||
base64: args.photo.base64,
|
||||
mediaType: args.photo.mediaType,
|
||||
createdAt: Date.now()
|
||||
});
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
export const removePhoto = mutation({
|
||||
args: {
|
||||
chatId: v.id('chats'),
|
||||
deviceId: v.string(),
|
||||
index: v.number()
|
||||
},
|
||||
returns: v.null(),
|
||||
handler: async (ctx, args) => {
|
||||
const drafts = await ctx.db
|
||||
.query('photoDrafts')
|
||||
.withIndex('by_chat_id_and_device_id', (q) =>
|
||||
q.eq('chatId', args.chatId).eq('deviceId', args.deviceId)
|
||||
)
|
||||
.collect();
|
||||
|
||||
if (drafts[args.index]) {
|
||||
await ctx.db.delete(drafts[args.index]._id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
export const clear = mutation({
|
||||
args: { chatId: v.id('chats'), deviceId: v.string() },
|
||||
returns: v.null(),
|
||||
handler: async (ctx, args) => {
|
||||
const drafts = await ctx.db
|
||||
.query('photoDrafts')
|
||||
.withIndex('by_chat_id_and_device_id', (q) =>
|
||||
q.eq('chatId', args.chatId).eq('deviceId', args.deviceId)
|
||||
)
|
||||
.collect();
|
||||
|
||||
for (const draft of drafts) {
|
||||
await ctx.db.delete(draft._id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user