import type { SavedResource } from '../database'; import type { ChannelInfo, Share, ShareSettings } from '../types'; import type { ChannelInfo as ChannelWire, ShareInfo as ShareWire, ShareItemInfo as ShareWireItem } from '../generated/api'; export type { ChannelWire, ShareWire, ShareWireItem }; export function toChannelInfo(raw: ChannelWire, saved: SavedResource): ChannelInfo { const joinPolicy = raw.settings.join_policy; if (joinPolicy !== 'open' && joinPolicy !== 'invite') throw new Error('空间加入规则无效'); return { id: raw.id, creatorAccount: raw.creator_account ?? undefined, joinPolicy, allowGuests: raw.settings.allow_guests, passwordRequired: raw.password_required, name: raw.settings.name ?? undefined, mode: raw.mode, role: raw.can_manage_structure ? 'owner' : 'member', canUpload: raw.can_upload, canManageStructure: raw.can_manage_structure, seats: raw.settings.seats, membersUsed: raw.seats_used, membersCanUpload: raw.settings.members_can_upload, ttlSeconds: raw.settings.ttl_seconds, maxDownloads: raw.settings.file_max_downloads, memberLink: saved.link, ownerLink: saved.ownerLink, createdAt: raw.created_at, expiresAt: raw.expires_at, }; } export function toShareInfo(raw: ShareWire, saved: SavedResource): Share { if (raw.state !== 'draft' && raw.state !== 'sealed') throw new Error('分享状态无效'); return { id: raw.id, state: raw.state, mine: saved.mine ?? (saved.role === 'owner' || saved.ownerLink !== undefined), link: saved.link, createdAt: raw.sealed_at ?? raw.created_at, expiresAt: raw.expires_at, ttlSeconds: raw.settings.ttl_seconds, maxVisitors: raw.settings.max_visitors, maxDownloads: raw.settings.file_max_downloads === 0 ? null : raw.settings.file_max_downloads, }; } export function apiShareSettings(settings: ShareSettings) { return { ttl_seconds: settings.ttlSeconds, max_visitors: settings.maxVisitors, file_max_downloads: settings.maxDownloads ?? 0 }; }