From 55da02e98d007056ba9f73aa751dcf95c5ec480a Mon Sep 17 00:00:00 2001 From: Alexander Zinn Date: Sun, 7 Sep 2025 13:34:15 -0400 Subject: [PATCH] Update version to 2025.1.8 and implement fork and kill channel methods in Channels API --- package.json | 2 +- src/apis/Channels.ts | 46 +++++++++++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 5e5ad86..33be845 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@techniker-me/pcast-api", - "version": "2025.1.7", + "version": "2025.1.8", "type": "module", "scripts": { "ci-build": "bun run build", diff --git a/src/apis/Channels.ts b/src/apis/Channels.ts index 8d1c959..8b49b54 100644 --- a/src/apis/Channels.ts +++ b/src/apis/Channels.ts @@ -46,15 +46,23 @@ export class ChannelError extends Error { } } +type KillChannelResponse = { + status: string; + killedMembers: Member[]; +}; + +type ForkChannelResponse = { + status: string; + members: Member[]; +}; + export class Channels { private readonly _httpRequests: PCastHttpRequests; private readonly _channelsByAlias: Map = new Map(); - constructor(pcastHttpRequests: PCastHttpRequests, skipInitialization = false) { + constructor(pcastHttpRequests: PCastHttpRequests) { this._httpRequests = pcastHttpRequests; - if (!skipInitialization) { - this.initialize(); - } + this.initialize(); } public async create(name: string, description: string, channelOptions: string[] = []): Promise { @@ -186,7 +194,7 @@ export class Channels { return response; } - async getPublishSourceStreamId(channelId: string, retryCount: number = 3): Promise { + public async getPublishSourceStreamId(channelId: string, retryCount: number = 3): Promise { const retryCountRemaining = retryCount || 3; const channelMembers = await this.getMembers(channelId); @@ -213,14 +221,30 @@ export class Channels { return presenter.streams[0].uri.match(publishSourceStreamIdRegExp)?.[1] ?? null; } - // TODO(AZ): Implement this - // public async fork(channelId: string): Promise + public async fork(sourceChannelId: string, destinationChannelId: string): Promise { + const response = await this._httpRequests.request( + HttpMethod.PUT, + `/channel/${encodeURIComponent(destinationChannelId)}/fork/${encodeURIComponent(sourceChannelId)}` + ); - // TODO(AZ): Implement this - // public async killChannel(channelId: string): Promise + if (response.status !== 'ok') { + console.error('Fork response [%o]', response); + throw new ChannelError(`Failed to fork channel [${sourceChannelId}] to [${destinationChannelId}]`, 'FORK_FAILED'); + } - // TODO(AZ): Implement this - // public async publishViaUrl(mediaUriToPublish: string, token: string): Promise + return response; + } + + public async killChannel(channelId: string): Promise { + const response = await this._httpRequests.request(HttpMethod.PUT, `/channel/${encodeURIComponent(channelId)}/kill`); + + if (response.status !== 'ok') { + console.error('Kill response [%o]', response); + throw new ChannelError(`Failed to kill channel [${channelId}]`, 'KILL_FAILED'); + } + + return response; + } private async initialize(): Promise { try {