-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.ts
More file actions
199 lines (186 loc) ยท 5.17 KB
/
Copy pathexample.ts
File metadata and controls
199 lines (186 loc) ยท 5.17 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* Example usage of the Reactions component.
*
* This file demonstrates the core reactions functionality including:
* - Adding and removing reactions with validation
* - Querying reaction counts and user reactions
* - Checking if users have reacted
*
* See posts.ts for post management and cascade delete examples.
* See http.ts for an example of exposing reactions via HTTP endpoints.
*/
import { mutation, query } from "./_generated/server.js";
import { components } from "./_generated/api.js";
import { Reactions } from "@convex/reactions";
import { v } from "convex/values";
export const reactions = new Reactions(components.reactions, {});
// Define the set of allowed emoji reactions
const ALLOWED_EMOJIS = [
"๐",
"โค๏ธ",
"๐",
"๐ฎ",
"๐ข",
"๐",
"๐",
"๐",
"๐",
] as const;
/**
* Example: Add a reaction to a post
* If the user has already reacted with a different emoji, it will be replaced.
* If the user already has this exact reaction, this is a no-op.
*
* This demonstrates client-side validation to ensure only allowed emojis are accepted.
*/
export const addReaction = mutation({
args: {
postId: v.string(),
emoji: v.string(),
userId: v.string(),
},
returns: v.null(),
handler: async (ctx, args) => {
// Validate that the emoji is one of the allowed ones
if (!ALLOWED_EMOJIS.includes(args.emoji as any)) {
throw new Error(
`Invalid emoji: "${args.emoji}". Allowed emojis are: ${ALLOWED_EMOJIS.join(", ")}`,
);
}
await reactions.add(ctx, args.postId, args.emoji, args.userId);
},
});
/**
* Example: Add a reaction to a post allowing multiple reactions per user
* Unlike addReaction, this allows a user to have multiple different reactions on the same post.
* If the user already has this exact reaction, this is a no-op.
*
* This is useful for scenarios where you want users to be able to express
* multiple emotions or reactions on the same content.
*/
export const addMultipleReaction = mutation({
args: {
postId: v.string(),
emoji: v.string(),
userId: v.string(),
},
returns: v.null(),
handler: async (ctx, args) => {
// Validate that the emoji is one of the allowed ones
if (!ALLOWED_EMOJIS.includes(args.emoji as any)) {
throw new Error(
`Invalid emoji: "${args.emoji}". Allowed emojis are: ${ALLOWED_EMOJIS.join(", ")}`,
);
}
await reactions.add(
ctx,
args.postId,
args.emoji,
args.userId,
undefined, // namespace
true, // allowMultipleReactions
);
},
});
/**
* Example: Remove a reaction from a post
*/
export const removeReaction = mutation({
args: {
postId: v.string(),
emoji: v.string(),
userId: v.string(),
},
returns: v.null(),
handler: async (ctx, args) => {
// Validate that the emoji is one of the allowed ones
if (!ALLOWED_EMOJIS.includes(args.emoji as any)) {
throw new Error(
`Invalid emoji: "${args.emoji}". Allowed emojis are: ${ALLOWED_EMOJIS.join(", ")}`,
);
}
await reactions.remove(ctx, args.postId, args.emoji, args.userId);
},
});
/**
* Example: Get the list of allowed emojis
* Clients can use this to display available reactions to users
*/
export const getAllowedEmojis = query({
args: {},
returns: v.array(v.string()),
handler: async () => {
return [...ALLOWED_EMOJIS];
},
});
/**
* Example: Get all reaction counts for a post
* Returns an array like: [{ label: "๐", count: 5 }, { label: "โค๏ธ", count: 3 }]
*/
export const getPostReactions = query({
args: {
postId: v.string(),
},
returns: v.array(
v.object({
label: v.string(),
count: v.number(),
}),
),
handler: async (ctx, args) => {
return await reactions.getCounts(ctx, args.postId);
},
});
/**
* Example: Get reaction counts for multiple posts in a single query
* This is more efficient than calling getPostReactions multiple times as it reduces
* overhead from crossing the component isolation boundary.
* Returns a record mapping postId to its reaction counts.
*/
export const getBatchPostReactions = query({
args: {
postIds: v.array(v.string()),
},
returns: v.array(
v.object({
targetId: v.string(),
namespace: v.optional(v.string()),
counts: v.array(
v.object({
label: v.string(),
count: v.number(),
}),
),
}),
),
handler: async (ctx, args) => {
const targets = args.postIds.map((postId) => ({ targetId: postId }));
return await reactions.getBatchCounts(ctx, targets);
},
});
/**
* Example: Check which reactions a specific user has made on a post
*/
export const getUserPostReactions = query({
args: {
postId: v.string(),
userId: v.string(),
},
returns: v.array(v.string()),
handler: async (ctx, args) => {
return await reactions.getUserReactions(ctx, args.postId, args.userId);
},
});
/**
* Example: Check if a user has reacted with a specific emoji
*/
export const hasUserLikedPost = query({
args: {
postId: v.string(),
userId: v.string(),
},
returns: v.boolean(),
handler: async (ctx, args) => {
return await reactions.hasUserReacted(ctx, args.postId, "๐", args.userId);
},
});