Skip to content

Commit

Permalink
✨ delete youtube stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Vondee committed Jul 31, 2024
1 parent c9d5835 commit 6fb9d92
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
24 changes: 20 additions & 4 deletions packages/server/src/services/stage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { ILiveStream, IStage } from '@interfaces/stage.interface';
import Stage from '@models/stage.model';
import Events from '@models/event.model';
import { Types } from 'mongoose';
import { createStream, deleteStream, getStreamInfo } from '@utils/livepeer';
import {
createMultiStream,
createStream,
deleteStream,
getStreamInfo,
} from '@utils/livepeer';
import { config } from '@config';
import Organization from '@models/organization.model';
import { refreshAccessToken } from '@utils/oauth';
Expand Down Expand Up @@ -129,11 +134,22 @@ export default class StageService {
const token = org.socials.find(
(e) => e.type == data.socialType && e._id == data.socialId,
);
const refeshToken = await refreshAccessToken(token.refreshToken);
return await createYoutubeLiveStream({
accessToken: refeshToken,
const accessToken = await refreshAccessToken(token.refreshToken);
const stream = await createYoutubeLiveStream({
accessToken: accessToken,
title: stage.name,
streamDate: stage.streamDate.toString(),
});
await createMultiStream({
name: stage.name,
streamId: stage.streamSettings.streamId,
targetStreamKey: stream.streamKey,
targetURL: stream.ingestUrl,
organizationId: stage.organizationId.toString(),
socialId: data.socialId,
socialType: data.socialType,
broadcastId: stream.broadcastId,
});
return stream;
}
}
20 changes: 20 additions & 0 deletions packages/server/src/utils/livepeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { Session, Stream } from 'livepeer/dist/models/components';
import SessionModel from '@models/session.model';
import { SessionType } from '@interfaces/session.interface';
import { createEventVideoById } from './firebase';
import { deleteYoutubeLiveStream } from './youtube';
import Organization from '@models/organization.model';
import { refreshAccessToken } from './oauth';
const livepeer = new Livepeer({
apiKey: secretKey,
});
Expand Down Expand Up @@ -254,6 +257,9 @@ export const createMultiStream = async (data: IMultiStream): Promise<void> => {
'streamSettings.targets': {
id: multistream.id,
name: multistream.name,
socialId: data.socialId ?? '',
socialType: data.socialType ?? '',
broadcastId: data.broadcastId ?? '',
},
},
});
Expand All @@ -280,6 +286,20 @@ export const deleteMultiStream = async (data: {
const stage = await Stage.findOne({
'streamSettings.streamId': data.streamId,
});
const target = stage.streamSettings.targets.find(
(e) => e.id == data.targetId,
);
if (target.socialType == 'youtube') {
const org = await Organization.findOne({ _id: stage.organizationId });
const token = org.socials.find(
(e) => e.type == target.socialType && e._id == target.socialId,
);
const accessToken = await refreshAccessToken(token.refreshToken);
await deleteYoutubeLiveStream({
broadcastId: target.broadcastId,
accessToken: accessToken,
});
}
await stage.updateOne({
$pull: { 'streamSettings.targets': { id: data.targetId } },
});
Expand Down
15 changes: 12 additions & 3 deletions packages/server/src/utils/youtube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,26 @@ export const createYoutubeLiveStream = async (data: {
accessToken: string;
title: string;
streamDate: string;
}): Promise<{ streamKey: string; ingestUrl: string }> => {
}): Promise<{ streamKey: string; ingestUrl: string; broadcastId: string }> => {
const youtube = await getYoutubeClient(data.accessToken);
const broadCastId = await createLiveBroadcast(
const broadcastId = await createLiveBroadcast(
youtube,
data.title,
data.streamDate,
);
const stream = await createLiveStream(youtube, data.title);
await bindBroadCastToStream(youtube, broadCastId, stream.id);
await bindBroadCastToStream(youtube, broadcastId, stream.id);
return {
streamKey: stream.streamKey,
ingestUrl: stream.ingestUrl,
broadcastId: broadcastId,
};
};

export const deleteYoutubeLiveStream = async (data: {
accessToken: string;
broadcastId: string;
}): Promise<void> => {
const youtube = await getYoutubeClient(data.accessToken);
await youtube.liveBroadcasts.delete({ id: data.broadcastId });
};

0 comments on commit 6fb9d92

Please sign in to comment.