All files / packages / shared/src channel.ts

100% Statements 8/8
100% Branches 0/0
100% Functions 0/0
100% Lines 8/8

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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95          4x   4x                                                     4x       4x               4x                               4x                   4x               4x                            
import { z } from 'zod'
import type { Endpoint } from './endpoint'
 
// Creating and browsing channels (design 5.1, tickets #18 and #20).
 
const kinds = ['public', 'private'] as const
 
export const CHANNEL = {
  nameMax: 80,
  descriptionMax: 250,
  // ASCII only (owner's decision, 2026-11-13), so fold(name) equals the name.
  namePattern: /^[a-z0-9-]+$/,
  // Value lists the D1 columns (schema.ts, their CHECKs) and the zod schemas both read. Adding a
  // value still needs a migration: migrations only add (4.17), and the CHECK is in the SQL.
  kinds,
  conversationKinds: [...kinds, 'dm'],
  memberRoles: ['admin', 'member'],
  /** workspaces.member_create: which channels a Member may create (design 2.5). */
  memberCreate: ['both', ...kinds, 'none'],
  /** Browse (#20): page size bounds, and the filters of GET /api/channels/browse. */
  pageDefault: 50,
  pageMax: 100,
  browseScopes: ['all', 'mine', 'other'],
  browseTypes: ['any', ...kinds, 'archived'],
} as const
 
export type ChannelKind = (typeof CHANNEL.kinds)[number]
export type ConversationKind = (typeof CHANNEL.conversationKinds)[number]
export type MemberRole = (typeof CHANNEL.memberRoles)[number]
export type MemberCreate = (typeof CHANNEL.memberCreate)[number]
export type ChannelBrowseScope = (typeof CHANNEL.browseScopes)[number]
export type ChannelBrowseType = (typeof CHANNEL.browseTypes)[number]
 
// Codes the web switches on, beside ERROR_CODE (design G19).
export const CHANNEL_ERROR_CODE = {
  nameTaken: 'channel_name_taken',
} as const
 
export const CreateChannelRequestSchema = z.object({
  name: z.string().min(1).max(CHANNEL.nameMax).regex(CHANNEL.namePattern),
  kind: z.enum(CHANNEL.kinds),
  description: z.string().max(CHANNEL.descriptionMax).optional(),
})
 
export type CreateChannelRequest = z.infer<typeof CreateChannelRequestSchema>
 
export const ChannelSchema = z.object({
  id: z.string(),
  name: z.string(),
  // Open, not z.enum: a kind added later must not break older web clients.
  kind: z.string(),
  topic: z.string().nullable(),
  description: z.string().nullable(),
  isGeneral: z.boolean(),
  archivedAt: z.number().int().nullable(),
  memberCount: z.number().int(),
  joined: z.boolean(),
  createdAt: z.number().int(),
})
 
export type Channel = z.infer<typeof ChannelSchema>
 
export const ChannelBrowseQuerySchema = z.object({
  scope: z.enum(CHANNEL.browseScopes).default('all'),
  type: z.enum(CHANNEL.browseTypes).default('any'),
  limit: z.coerce.number().int().min(1).max(CHANNEL.pageMax).default(CHANNEL.pageDefault),
  // The last name of the previous page: channels are ordered by their folded name.
  cursor: z.string().max(CHANNEL.nameMax).regex(CHANNEL.namePattern).optional(),
})
 
export type ChannelBrowseQuery = z.infer<typeof ChannelBrowseQuerySchema>
 
export const ChannelPageSchema = z.object({
  channels: z.array(ChannelSchema),
  nextCursor: z.string().nullable(),
  total: z.number().int(),
})
 
export type ChannelPage = z.infer<typeof ChannelPageSchema>
 
export const CHANNEL_API = {
  create: {
    method: 'POST',
    path: '/api/channels',
    body: CreateChannelRequestSchema,
    response: ChannelSchema,
  },
  browse: {
    method: 'GET',
    path: '/api/channels/browse',
    query: ChannelBrowseQuerySchema,
    response: ChannelPageSchema,
  },
} as const satisfies Record<string, Endpoint>