Skip to content

Instantly share code, notes, and snippets.

@recursivecodes
Created June 15, 2023 14:59
Show Gist options
  • Save recursivecodes/ca464a29fc38ef0399d8f2b64b838817 to your computer and use it in GitHub Desktop.
Save recursivecodes/ca464a29fc38ef0399d8f2b64b838817 to your computer and use it in GitHub Desktop.
import { IvsClient, StopStreamCommand } from "@aws-sdk/client-ivs";
import { IvschatClient, SendEventCommand } from "@aws-sdk/client-ivschat";
import { RekognitionClient, DetectLabelsCommand, DetectModerationLabelsCommand } from "@aws-sdk/client-rekognition";
const chatArn = process.env.DEMO_CHAT_ARN;
const channelArn = process.env.DEMO_CHANNEL_ARN;
const responseObject = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,GET,PUT,POST,DELETE",
"Content-Type": "application/json"
},
body: '',
isBase64Encoded: false
};
const ivsClient = new IvsClient();
const ivsChatClient = new IvschatClient();
const rekognitionClient = new RekognitionClient();
export const stopStreamHandler = async (event) => {
console.log('stopStreamHandler:', JSON.stringify(event, null, 2));
try {
const stopStreamResponse = stopStream();
responseObject.body = JSON.stringify(stopStreamResponse);
}
catch (err) {
responseObject.statusCode = err?.name === 'ChannelNotBroadcasting' ? 404 : 500;
responseObject.body = JSON.stringify(err);
}
return responseObject;
};
export const moderateImage = async (event) => {
console.log('moderateImage:', JSON.stringify(event, null, 2));
const bucket = event.detail.bucket.name;
const key = event.detail.object.key;
const commandInput = {
Image: {
S3Object: {
Bucket: bucket,
Name: key,
}
},
};
const detectLabelsRequest = new DetectLabelsCommand({ ...commandInput, MaxLabels: 5 });
const detectLabelsResponse = await rekognitionClient.send(detectLabelsRequest);
console.log('labels:');
console.log(detectLabelsResponse);
if (detectLabelsResponse.Labels) {
sendEvent('STREAM_MODERATION', detectLabelsResponse.Labels.map((label) => {
return {
Name: label.Name,
Confidence: label.Confidence
};
}));
}
const detectModerationLabelsRequest = new DetectModerationLabelsCommand(commandInput);
const detectModerationLabelsResponse = await rekognitionClient.send(detectModerationLabelsRequest);
console.log('moderation:');
console.log(detectModerationLabelsResponse);
if (detectModerationLabelsResponse.ModerationLabels) {
sendEvent('STREAM_MODERATION', detectModerationLabelsResponse.ModerationLabels);
}
};
const stopStream = async () => {
const stopStreamRequest = new StopStreamCommand({ channelArn });
const stopStreamResponse = await ivsClient.send(stopStreamRequest);
return stopStreamResponse;
};
const sendEvent = async (eventName, eventDetails) => {
const sendEventInput = {
roomIdentifier: chatArn,
attributes: {
streamModerationEvent: JSON.stringify(eventDetails),
},
eventName,
};
const sendEventRequest = new SendEventCommand(sendEventInput);
await ivsChatClient.send(sendEventRequest);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment