77 * - Use Convex `auth` to authenticate users rather than passing up a "user"
88 * - Check that the user is allowed to be in a given room.
99 */
10+ import { v } from "convex/values" ;
1011import { query , mutation } from "./_generated/server" ;
1112
1213const LIST_LIMIT = 20 ;
@@ -21,11 +22,13 @@ const LIST_LIMIT = 20;
2122 * page, chat channel, game instance.
2223 * @param user - The user associated with the presence data.
2324 */
24- export const update = mutation (
25- async (
26- { db } ,
27- { room, user, data } : { room : string ; user : string ; data : any } ,
28- ) => {
25+ export const update = mutation ( {
26+ args : {
27+ room : v . string ( ) ,
28+ user : v . string ( ) ,
29+ data : v . any ( ) ,
30+ } ,
31+ handler : async ( { db } , { room, user, data } ) => {
2932 const existing = await db
3033 . query ( "presence" )
3134 . withIndex ( "user_room" , ( q ) => q . eq ( "user" , user ) . eq ( "room" , room ) )
@@ -41,7 +44,7 @@ export const update = mutation(
4144 } ) ;
4245 }
4346 } ,
44- ) ;
47+ } ) ;
4548
4649/**
4750 * Updates the "updated" timestamp for a given user's presence in a room.
@@ -50,8 +53,12 @@ export const update = mutation(
5053 * page, chat channel, game instance.
5154 * @param user - The user associated with the presence data.
5255 */
53- export const heartbeat = mutation (
54- async ( { db } , { room, user } : { room : string ; user : string } ) => {
56+ export const heartbeat = mutation ( {
57+ args : {
58+ room : v . string ( ) ,
59+ user : v . string ( ) ,
60+ } ,
61+ handler : async ( { db } , { room, user } ) => {
5562 const existing = await db
5663 . query ( "presence" )
5764 . withIndex ( "user_room" , ( q ) => q . eq ( "user" , user ) . eq ( "room" , room ) )
@@ -60,7 +67,7 @@ export const heartbeat = mutation(
6067 await db . patch ( existing . _id , { updated : Date . now ( ) } ) ;
6168 }
6269 } ,
63- ) ;
70+ } ) ;
6471
6572/**
6673 * Lists the presence data for N users in a room, ordered by recent update.
@@ -70,16 +77,21 @@ export const heartbeat = mutation(
7077 * @returns A list of presence objects, ordered by recent update, limited to
7178 * the most recent N.
7279 */
73- export const list = query ( async ( { db } , { room } : { room : string } ) => {
74- const presence = await db
75- . query ( "presence" )
76- . withIndex ( "room_updated" , ( q ) => q . eq ( "room" , room ) )
77- . order ( "desc" )
78- . take ( LIST_LIMIT ) ;
79- return presence . map ( ( { _creationTime, updated, user, data } ) => ( {
80- created : _creationTime ,
81- updated,
82- user,
83- data,
84- } ) ) ;
80+ export const list = query ( {
81+ args : {
82+ room : v . string ( ) ,
83+ } ,
84+ handler : async ( { db } , { room } ) => {
85+ const presence = await db
86+ . query ( "presence" )
87+ . withIndex ( "room_updated" , ( q ) => q . eq ( "room" , room ) )
88+ . order ( "desc" )
89+ . take ( LIST_LIMIT ) ;
90+ return presence . map ( ( { _creationTime, updated, user, data } ) => ( {
91+ created : _creationTime ,
92+ updated,
93+ user,
94+ data,
95+ } ) ) ;
96+ } ,
8597} ) ;
0 commit comments