62 lines
1.4 KiB
TypeScript
62 lines
1.4 KiB
TypeScript
import { mutation } from "./_generated/server";
|
|
import { v } from "convex/values";
|
|
|
|
// Add reaction (upsert - no duplicates)
|
|
export const add = mutation({
|
|
args: {
|
|
messageId: v.id("messages"),
|
|
userId: v.id("userProfiles"),
|
|
emoji: v.string(),
|
|
},
|
|
returns: v.null(),
|
|
handler: async (ctx, args) => {
|
|
// Check if already exists
|
|
const existing = await ctx.db
|
|
.query("messageReactions")
|
|
.withIndex("by_message_user_emoji", (q) =>
|
|
q
|
|
.eq("messageId", args.messageId)
|
|
.eq("userId", args.userId)
|
|
.eq("emoji", args.emoji)
|
|
)
|
|
.unique();
|
|
|
|
if (!existing) {
|
|
await ctx.db.insert("messageReactions", {
|
|
messageId: args.messageId,
|
|
userId: args.userId,
|
|
emoji: args.emoji,
|
|
});
|
|
}
|
|
|
|
return null;
|
|
},
|
|
});
|
|
|
|
// Remove reaction
|
|
export const remove = mutation({
|
|
args: {
|
|
messageId: v.id("messages"),
|
|
userId: v.id("userProfiles"),
|
|
emoji: v.string(),
|
|
},
|
|
returns: v.null(),
|
|
handler: async (ctx, args) => {
|
|
const existing = await ctx.db
|
|
.query("messageReactions")
|
|
.withIndex("by_message_user_emoji", (q) =>
|
|
q
|
|
.eq("messageId", args.messageId)
|
|
.eq("userId", args.userId)
|
|
.eq("emoji", args.emoji)
|
|
)
|
|
.unique();
|
|
|
|
if (existing) {
|
|
await ctx.db.delete(existing._id);
|
|
}
|
|
|
|
return null;
|
|
},
|
|
});
|