Skip to content

Commit

Permalink
Merge pull request #765 from FleetAdmiralJakob/reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
FleetAdmiralJakob authored Dec 2, 2024
2 parents 3738da3 + d16d940 commit c14fa60
Show file tree
Hide file tree
Showing 9 changed files with 1,038 additions and 331 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ public/swe-worker*

# Sentry Config File
.env.sentry-build-plugin

certificates
71 changes: 70 additions & 1 deletion convex/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConvexError, v } from "convex/values";
import emojiRegex from "emoji-regex";
import { mutation, query } from "./lib/functions";

export const getMessages = query({
Expand Down Expand Up @@ -31,7 +32,7 @@ export const getMessages = query({

const [messages, requests] = await Promise.all([
chat.edge("messages").map(async (message) => {
const [from, readBy, replyTo] = await Promise.all([
const [from, readBy, replyTo, reactions] = await Promise.all([
ctx.table("users").getX(message.userId),
message.edge("readBy"),
message.replyTo
Expand All @@ -53,6 +54,7 @@ export const getMessages = query({
return null;
})
: null,
message.edge("reactions").docs(),
]);

return {
Expand All @@ -62,6 +64,7 @@ export const getMessages = query({
from,
readBy,
replyTo,
reactions,
sent: true,
};
}),
Expand Down Expand Up @@ -299,3 +302,69 @@ export const editMessage = mutation({
});
},
});

export const reactToMessage = mutation({
args: { messageId: v.id("messages"), reaction: v.string() },
handler: async (ctx, args) => {
const identity = await ctx.auth.getUserIdentity();

if (identity === null) {
console.error("Unauthenticated call to 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.",
);
}

// Check if emoji
if (
!emojiRegex().test(args.reaction) ||
args.reaction.match(emojiRegex())!.length > 1
) {
throw new ConvexError("Reaction must be a single emoji");
}

const trimmedReaction = args.reaction.trim();

// Check if message exists
const messageId = ctx.table("messages").normalizeId(args.messageId);

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

// Check if user already reacted to this message
const existingReaction = await ctx
.table("reactions", "messageId", (q) => q.eq("messageId", messageId))
.filter((q) => q.eq(q.field("userId"), convexUser._id))
.first();

if (existingReaction && existingReaction.emoji === trimmedReaction) {
// Remove existing reaction
await existingReaction.delete();
return null;
} else if (existingReaction) {
// Update existing reaction
await existingReaction.patch({
emoji: args.reaction,
});
return existingReaction;
}

// Create new reaction if none exists
const reaction = await ctx.table("reactions").insert({
emoji: trimmedReaction,
userId: convexUser._id,
messageId,
});

return reaction;
},
});
7 changes: 7 additions & 0 deletions convex/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const schema = defineEntSchema({
.field("firstName", v.optional(v.string()))
.field("email", v.optional(v.string()))
.field("lastName", v.optional(v.string()))
.edges("reactions", { ref: true })
.edges("privateChats")
.edges("messages", { ref: true })
.edges("clearRequests", { ref: true })
Expand Down Expand Up @@ -54,11 +55,17 @@ const schema = defineEntSchema({
.field("replyTo", v.optional(v.id("messages")))
.edge("privateChat")
.edge("user")
.edges("reactions", { ref: true })
.edges("readBy", {
to: "users",
inverseField: "readMessages",
table: "readMessages",
}),

reactions: defineEnt({})
.field("emoji", v.string())
.edge("user")
.edge("message"),
});

export default schema;
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"dependencies": {
"@clerk/nextjs": "^6.5.1",
"@clerk/shared": "^2.17.0",
"@emoji-mart/data": "^1.2.1",
"@emoji-mart/react": "^1.1.1",
"@floating-ui/react": "^0.26.28",
"@hookform/resolvers": "^3.9.1",
"@ianvs/prettier-plugin-sort-imports": "^4.4.0",
Expand All @@ -31,6 +33,7 @@
"@radix-ui/react-select": "^2.1.2",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-tooltip": "^1.1.4",
"@reactuses/core": "^5.0.23",
"@sentry/nextjs": "^8",
"@serwist/next": "9.0.10",
"@t3-oss/env-nextjs": "^0.11.1",
Expand All @@ -42,6 +45,8 @@
"convex-ents": "^0.13.0",
"convex-helpers": "^0.1.65",
"dayjs": "^1.11.13",
"emoji-mart": "^5.6.0",
"emoji-regex": "^10.4.0",
"framer-motion": "^11.11.17",
"geist": "^1.3.1",
"import-in-the-middle": "^1.11.2",
Expand Down
75 changes: 75 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c14fa60

Please sign in to comment.