Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Fixed reaction to deleted message bug #961

Merged
merged 10 commits into from
Jan 25, 2025
22 changes: 22 additions & 0 deletions convex/messages.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { defineEnt, defineEntSchema } from "convex-ents";
import { ConvexError, v } from "convex/values";
import emojiRegex from "emoji-regex";
import { mutation, query } from "./lib/functions";
Expand Down Expand Up @@ -164,12 +165,33 @@ export const deleteMessage = mutation({
return null;
}

const convexUser = await ctx
.table("users")
.get("clerkId", identity.tokenIdentifier);

if (!convexUser?._id) {
throw new ConvexError(
"Mismatch between Clerk and Convex. This is an error by us.",
);
}

const parsedMessageId = ctx.table("messages").normalizeId(args.messageId);

if (!parsedMessageId) {
throw new ConvexError("chatId was invalid");
}

const existingReaction = await ctx
.table("reactions", "messageId", (q) =>
q.eq("messageId", parsedMessageId),
)
.filter((q) => q.eq(q.field("userId"), convexUser._id))
.first();

if (existingReaction) {
await existingReaction.delete();
}

const message = await ctx.table("messages").getX(parsedMessageId);

if ((await message.edge("user")).clerkId !== identity.tokenIdentifier) {
Expand Down
Loading