Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gamius00 committed Jun 15, 2024
2 parents f2bc7eb + 2bda4e2 commit 90d9a49
Show file tree
Hide file tree
Showing 14 changed files with 863 additions and 714 deletions.
59 changes: 55 additions & 4 deletions convex/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,61 @@ export const getChats = query({
.table("users")
.getX("clerkId", identity.tokenIdentifier)
.edge("privateChats")
.map(async (chat) => ({
...chat,
users: await chat.edge("users"),
}));
.map(async (chat) => {
const messages = await chat.edge("messages");
const sortedMessages = messages.sort(
(a, b) => b._creationTime - a._creationTime,
);
const latestMessage = sortedMessages[0];
const readBy = latestMessage ? await latestMessage.edge("readBy") : [];

const extendedMessagesPromises = sortedMessages.map(async (message) => {
return {
...message,
readBy: await message.edge("readBy"),
deleted: message.deleted,
};
});

const extendedMessages = await Promise.all(extendedMessagesPromises);

const sortedMessagesAgain = extendedMessages.sort(
(a, b) => b._creationTime - a._creationTime,
);

let deletedCount = 0;
const firstReadMessageIndex = sortedMessagesAgain.findIndex(
(message) => {
if (message.deleted) {
deletedCount++;
}
return (
message.readBy.some(
(user) => user.clerkId === identity.tokenIdentifier,
) && !message.deleted
);
},
);

let numberOfUnreadMessages;
if (firstReadMessageIndex === -1) {
numberOfUnreadMessages = sortedMessages.length - deletedCount;
} else {
numberOfUnreadMessages = firstReadMessageIndex - deletedCount;
}

return {
...chat,
users: await chat.edge("users"),
numberOfUnreadMessages: numberOfUnreadMessages,
lastMessage: latestMessage
? {
...latestMessage,
readBy,
}
: null,
};
});
},
});

Expand Down
7 changes: 7 additions & 0 deletions convex/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const createMessage = mutation({
privateChatId: parsedChatId,
content: args.content.trim(),
deleted: false,
readBy: [convexUser._id],
});
},
});
Expand All @@ -68,11 +69,17 @@ export const deleteMessage = mutation({
throw new ConvexError("chatId was invalid");
}

const message = await ctx.table("messages").getX(parsedMessageId);
const chatId = message.privateChatId;
const chat = await ctx.table("privateChats").getX(chatId);
const users = await chat.edge("users");

await (
await ctx.table("messages").getX(parsedMessageId)
).patch({
content: "",
deleted: true,
readBy: { add: users.map((user) => user._id) },
});
},
});
Expand Down
49 changes: 26 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
"convex:deploy": "convex deploy"
},
"dependencies": {
"@clerk/nextjs": "^5.1.3",
"@clerk/shared": "^2.2.1",
"@floating-ui/react": "^0.26.16",
"@hookform/resolvers": "^3.4.2",
"@legendapp/state": "3.0.0-alpha.11",
"@clerk/nextjs": "^5.1.5",
"@clerk/shared": "^2.3.0",
"@floating-ui/react": "^0.26.17",
"@hookform/resolvers": "^3.6.0",
"@legendapp/state": "3.0.0-alpha.13",
"@radix-ui/react-avatar": "^1.0.4",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
Expand All @@ -29,57 +29,60 @@
"@radix-ui/react-select": "^2.0.0",
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-tooltip": "^1.0.7",
"@serwist/next": "9.0.2",
"@serwist/precaching": "9.0.2",
"@serwist/sw": "9.0.2",
"@serwist/next": "9.0.3",
"@t3-oss/env-nextjs": "^0.10.1",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"cmdk": "^1.0.0",
"convex": "^1.12.1",
"convex-ents": "^0.7.6",
"convex-ents": "^0.7.7",
"convex-helpers": "^0.1.41",
"dayjs": "^1.11.11",
"framer-motion": "^11.2.10",
"geist": "^1.3.0",
"jiti": "^1.21.0",
"lucide-react": "^0.390.0",
"next": "^14.2.3",
"jiti": "^1.21.6",
"lucide-react": "^0.395.0",
"next": "^14.2.4",
"next-themes": "^0.3.0",
"npm-run-all2": "^6.2.0",
"providers": "link:providers",
"react": "18.3.1",
"react-dom": "18.3.1",
"react-hook-form": "^7.51.5",
"react-icons": "^5.2.1",
"react-intersection-observer": "^9.10.2",
"react-intersection-observer": "^9.10.3",
"react-resizable-panels": "^2.0.19",
"react-responsive": "^10.0.0",
"sonner": "^1.4.41",
"sonner": "^1.5.0",
"tailwind-merge": "^2.3.0",
"tailwindcss-animate": "^1.0.7",
"zod": "^3.23.8"
},
"devDependencies": {
"@total-typescript/ts-reset": "^0.5.1",
"@types/eslint": "^8.56.10",
"@types/node": "^20.12.13",
"@types/node": "^20.14.2",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@typescript-eslint/eslint-plugin": "^7.11.0",
"@typescript-eslint/parser": "^7.11.0",
"@typescript-eslint/eslint-plugin": "^7.13.0",
"@typescript-eslint/parser": "^7.13.0",
"autoprefixer": "^10.4.19",
"eslint": "^9.3.0",
"eslint-config-next": "^14.2.3",
"eslint": "^9.4.0",
"eslint-config-next": "^14.2.4",
"eslint-plugin-react-hooks": "^4.6.2",
"postcss": "^8.4.38",
"prettier": "^3.2.5",
"prettier-plugin-tailwindcss": "^0.6.0",
"tailwindcss": "^3.4.3",
"prettier": "^3.3.2",
"prettier-plugin-tailwindcss": "^0.6.4",
"serwist": "^9.0.3",
"tailwindcss": "^3.4.4",
"typescript": "^5.4.5"
},
"ct3aMetadata": {
"initVersion": "7.30.0"
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"engineStrict": true,
"engines": {
"pnpm": "9.3.0"
}
}
Loading

0 comments on commit 90d9a49

Please sign in to comment.