All files / json-crdt-server/src/routes/presence/methods update.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 3/3
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          4x   4x 4x 93x 93x                                                                     93x                       93x                 93x 4x 4x            
import type {ResolveType} from '@jsonjoy.com/json-type';
import type {PresenceEntry} from '../schema';
import type {RouteDeps, Router, RouterBase} from '../../types';
 
/** Entry TTL in seconds. */
const ttl = 30;
 
export const update =
  ({t, services}: RouteDeps) =>
  <R extends RouterBase>(r: Router<R>) => {
    const Request = t
      .Object(
        t.Key('room', t.str).options({
          title: 'Room ID',
          description: 'The ID of the room to update.',
        }),
        t.Key('id', t.str).options({
          title: 'ID of the entry',
          description: 'The ID of the entry to update.',
        }),
        t.Key('data', t.any).options({
          title: 'Entry data',
          description: 'A map of key-value pairs to update. The object is merged with the existing entry data, if any.',
        }),
      )
      .options({
        examples: [
          {
            title: 'Update user entry',
            description:
              'The data section of the entry is merged with the existing data. ' +
              'It can contain any key-value pairs. For example, the `cursor` property is used to store the ' +
              'current cursor position of the user in the room.',
            value: {
              room: 'my-room',
              id: 'user-1',
              data: {
                name: 'John Doe',
                cursor: [123, 456],
              },
            },
          },
        ],
      });
 
    const Response = t
      .Object(
        t.Key('entry', t.Ref<typeof PresenceEntry>('PresenceEntry')),
        t.Key('time', t.num).options({
          title: 'Current time',
          description: 'The current server time in milliseconds since the UNIX epoch.',
        }),
      )
      .options({
        title: 'Presence update response',
      });
 
    const Func = t.Function(Request, Response).options({
      title: 'Update presence entry',
      intro: 'Update a presence entry in a room.',
      description:
        'This method updates a presence entry in a room. ' +
        `The entry is automatically removed after ${ttl} seconds. ` +
        `Every time the entry is updated, the TTL is reset to ${ttl} seconds.`,
    });
 
    return r.add('presence.update', Func, async ({room, id, data}) => {
      const entry = (await services.presence.update(room, id, ttl * 1000, data)) as ResolveType<typeof PresenceEntry>;
      return {
        entry,
        time: Date.now(),
      };
    });
  };