-
Notifications
You must be signed in to change notification settings - Fork 1
/
messages.ts
30 lines (28 loc) · 920 Bytes
/
messages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { getAuthUserId } from "@convex-dev/auth/server";
import { v } from "convex/values";
import { mutation, query } from "./_generated/server";
export const list = query({
args: {},
handler: async (ctx) => {
// Grab the most recent messages.
const messages = await ctx.db.query("messages").order("desc").take(100);
// Add the author's name to each message.
return Promise.all(
messages.map(async (message) => {
const { name, email } = (await ctx.db.get(message.userId))!;
return { ...message, author: name ?? email! };
}),
);
},
});
export const send = mutation({
args: { body: v.string(), author: v.string() },
handler: async (ctx, { body }) => {
const userId = await getAuthUserId(ctx);
if (userId === null) {
throw new Error("Not signed in");
}
// Send a new message.
await ctx.db.insert("messages", { body, userId });
},
});