polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
279 lines • 16.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InteractionHandler = void 0;
const base_handler_1 = require("./base.handler");
const interaction_service_1 = require("../services/interaction-service");
const errors_1 = require("../utils/errors");
const api_command_1 = require("../utils/api-command");
class InteractionHandler extends base_handler_1.BaseHandler {
constructor(authConfig, serviceConfig) {
super();
this.service = new interaction_service_1.InteractionServiceSdk(authConfig, serviceConfig);
}
async sendFavor(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['channelId', 'viewerId']);
this.validateOptionalPositiveInteger('times', options.times);
await (0, api_command_1.confirmWrite)(options.force, `Send favor for viewer ${options.viewerId}?`);
const result = await this.service.sendFavor(this.compactOptions({
channelId: options.channelId,
viewerId: options.viewerId,
times: options.times,
}));
this.displayGenericResult(result, options.output, 'Favor sent successfully');
}, 'interaction.favor');
}
async sendRewardMsg(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['channelId', 'nickname', 'avatar', 'viewerId', 'donateType', 'content']);
this.validateYn('needUserImage', options.needUserImage);
await (0, api_command_1.confirmWrite)(options.force, `Send reward message for viewer ${options.viewerId}?`);
const result = await this.service.sendRewardMsg(this.compactOptions({
channelId: options.channelId,
nickname: options.nickname,
avatar: options.avatar,
viewerId: options.viewerId,
donateType: options.donateType,
content: options.content,
goodImage: options.goodImage,
sessionId: options.sessionId,
goodNum: options.goodNum,
needUserImage: options.needUserImage,
}));
this.displayGenericResult(result, options.output, 'Reward message sent successfully');
}, 'interaction.reward');
}
async getStudentQuestionWebhook(options) {
return this.executeWithErrorHandling(async () => {
this.validateOutputOption(options.output);
const result = await this.service.getStudentQuestionWebhook(this.compactOptions({
roomId: options.roomId,
}));
this.displayGenericResult(result, options.output);
}, 'interaction.webhook.get');
}
async setStudentQuestionWebhook(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['roomId', 'callbackUrl']);
await (0, api_command_1.confirmWrite)(options.force, `Set student question webhook for room ${options.roomId}?`);
const result = await this.service.setStudentQuestionWebhook({
roomId: options.roomId,
callbackUrl: options.callbackUrl,
});
this.displayGenericResult(result ?? { success: true }, options.output, 'Student question webhook saved successfully');
}, 'interaction.webhook.set');
}
async deleteStudentQuestionWebhook(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['roomId']);
await (0, api_command_1.confirmWrite)(options.force, `Delete student question webhook for room ${options.roomId}?`);
const result = await this.service.deleteStudentQuestionWebhook({
roomId: options.roomId,
});
this.displayGenericResult(result ?? { success: true }, options.output, 'Student question webhook deleted successfully');
}, 'interaction.webhook.delete');
}
async sendTeacherAnswer(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['roomId', 'content', 'viewerUserId']);
await (0, api_command_1.confirmWrite)(options.force, `Send teacher answer to viewer ${options.viewerUserId}?`);
const result = await this.service.sendTeacherAnswer(this.compactOptions({
roomId: options.roomId,
content: options.content,
viewerUserId: options.viewerUserId,
teacherNick: options.teacherNick,
teacherPic: options.teacherPic,
msgType: options.msgType,
}));
this.displayGenericResult(result, options.output, 'Teacher answer sent successfully');
}, 'interaction.teacher-answer');
}
async listInteractionEvents(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['roomId']);
const result = await this.service.listInteractionEvents(this.toV4Params(options));
this.displayGenericResult(result, options.output);
}, 'interaction.event.list');
}
async saveInteractionEvent(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['channelId', 'tasks', 'allDone']);
await (0, api_command_1.confirmWrite)(options.force, `Save interaction event for channel ${options.channelId}?`);
const result = await this.service.saveInteractionEvent(this.toV4Params(options));
this.displayGenericResult(result, options.output, 'Interaction event saved successfully');
}, 'interaction.event.save');
}
async deleteInteractionEvent(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['channelId', 'taskIds']);
await (0, api_command_1.confirmWrite)(options.force, `Delete interaction event for channel ${options.channelId}?`);
const result = await this.service.deleteInteractionEvent(this.toV4Params(options));
this.displayGenericResult(result, options.output, 'Interaction event deleted successfully');
}, 'interaction.event.delete');
}
async createInvitePoster(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['channelId', 'openId', 'nickname']);
await (0, api_command_1.confirmWrite)(options.force, `Create invite poster for ${options.nickname}?`);
const result = await this.service.createInvitePoster(this.toV4Params(options));
this.displayGenericResult(result, options.output, 'Invite poster created successfully');
}, 'interaction.invite-poster.create');
}
async queryDiskVideoCustomScript(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['channelId', 'diskVideoId']);
const result = await this.service.queryDiskVideoCustomScript(this.toV4Params(options));
this.displayGenericResult(result, options.output);
}, 'interaction.script.query');
}
async uploadDiskVideoCustomScript(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['channelId', 'diskVideoId', 'filePath']);
await (0, api_command_1.confirmWrite)(options.force, `Upload interaction script for disk video ${options.diskVideoId}?`);
const result = await this.service.uploadDiskVideoCustomScript(this.toV4Params(options));
this.displayGenericResult(result, options.output, 'Interaction script uploaded successfully');
}, 'interaction.script.upload');
}
async deleteInteractionScript(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['channelId', 'id']);
await (0, api_command_1.confirmWrite)(options.force, `Delete interaction script ${options.id}?`);
const result = await this.service.deleteInteractionScript(this.toV4Params(options));
this.displayGenericResult(result, options.output, 'Interaction script deleted successfully');
}, 'interaction.script.delete');
}
async createTaskRewardActivity(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['channelId', 'activityName', 'taskRule', 'startTime', 'endTime', 'tasks']);
this.validateArrayOption('tasks', options.tasks);
await (0, api_command_1.confirmWrite)(options.force, `Create task reward activity "${options.activityName}"?`);
const result = await this.service.createTaskRewardActivity(this.toV4Params(options));
this.displayGenericResult(result, options.output, 'Task reward activity created successfully');
}, 'interaction.task-reward.create');
}
async listTaskRewardActivities(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['channelId']);
this.validateOptionalPositiveInteger('page', options.page);
this.validateOptionalPositiveInteger('size', options.size);
const result = await this.service.listTaskRewardActivities(this.toV4Params(options));
this.displayGenericResult(result, options.output);
}, 'interaction.task-reward.list');
}
async listTaskRewardStats(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['channelId']);
this.validateOptionalPositiveInteger('page', options.page);
this.validateOptionalPositiveInteger('size', options.size);
const result = await this.service.listTaskRewardStats(this.toV4Params(options));
this.displayGenericResult(result, options.output);
}, 'interaction.task-reward.stats');
}
async listTaskRewardViewerDetails(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['channelId', 'activityId']);
this.validateOptionalPositiveInteger('page', options.page);
this.validateOptionalPositiveInteger('size', options.size);
const result = await this.service.listTaskRewardViewerDetails(this.toV4Params(options));
this.displayGenericResult(result, options.output);
}, 'interaction.task-reward.viewer-detail');
}
async updateTaskRewardActivity(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['channelId', 'activityId', 'tasks']);
this.validateArrayOption('tasks', options.tasks);
await (0, api_command_1.confirmWrite)(options.force, `Update task reward activity ${options.activityId}?`);
const result = await this.service.updateTaskRewardActivity(this.toV4Params(options));
this.displayGenericResult(result, options.output, 'Task reward activity updated successfully');
}, 'interaction.task-reward.update');
}
async deleteTaskRewardActivity(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['activityId']);
await (0, api_command_1.confirmWrite)(options.force, `Delete task reward activity ${options.activityId}?`);
const result = await this.service.deleteTaskRewardActivity(this.toV4Params(options));
this.displayGenericResult(result, options.output, 'Task reward activity deleted successfully');
}, 'interaction.task-reward.delete');
}
async stopTaskRewardActivity(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['activityId']);
await (0, api_command_1.confirmWrite)(options.force, `Stop task reward activity ${options.activityId}?`);
const result = await this.service.stopTaskRewardActivity(this.toV4Params(options));
this.displayGenericResult(result, options.output, 'Task reward activity stopped successfully');
}, 'interaction.task-reward.stop');
}
async listViewerTaskRewardDetails(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['viewerId']);
this.validateOptionalPositiveInteger('page', options.page);
this.validateOptionalPositiveInteger('size', options.size);
const result = await this.service.listViewerTaskRewardDetails(this.toV4Params(options));
this.displayGenericResult(result, options.output);
}, 'interaction.task-reward.viewer-list');
}
async submitViewerTaskRewardAcceptInfo(options) {
return this.executeWithErrorHandling(async () => {
this.validateRequiredOptions(options, ['id', 'viewerId', 'formInfo']);
this.validateArrayOption('formInfo', options.formInfo);
await (0, api_command_1.confirmWrite)(options.force, `Submit task reward accept info for viewer ${options.viewerId}?`);
const result = await this.service.submitViewerTaskRewardAcceptInfo(this.toV4Params(options));
this.displayGenericResult(result, options.output, 'Task reward accept info submitted successfully');
}, 'interaction.task-reward.submit-accept-info');
}
validateRequiredOptions(options, fields) {
const missing = fields.filter((field) => {
const value = options[field];
return value === undefined || value === null || (typeof value === 'string' && value.trim() === '');
});
if (missing.length > 0) {
throw new errors_1.PolyVValidationError(`Missing required options: ${missing.join(', ')}`, 'options', options, 'validation_failed');
}
this.validateOutputOption(options.output);
}
validateOutputOption(output) {
if (output && output !== 'table' && output !== 'json') {
throw new errors_1.PolyVValidationError('output must be either "table" or "json"', 'output', output, 'validation_failed');
}
}
validateOptionalPositiveInteger(field, value) {
if (value === undefined)
return;
if (!Number.isInteger(value) || value < 1) {
throw new errors_1.PolyVValidationError(`${field} must be a positive integer`, field, value, 'validation_failed');
}
}
validateYn(field, value) {
if (value !== undefined && value !== 'Y' && value !== 'N') {
throw new errors_1.PolyVValidationError(`${field} must be Y or N`, field, value, 'validation_failed');
}
}
validateArrayOption(field, value) {
if (!Array.isArray(value) || value.length === 0) {
throw new errors_1.PolyVValidationError(`${field} must be a non-empty JSON array`, field, value, 'validation_failed');
}
}
compactOptions(options) {
return Object.fromEntries(Object.entries(options).filter(([, value]) => value !== undefined));
}
toV4Params(options) {
const params = (0, api_command_1.apiParams)(options);
if (params.page !== undefined) {
params.pageNumber = params.page;
delete params.page;
}
if (params.size !== undefined) {
params.pageSize = params.size;
delete params.size;
}
return params;
}
displayGenericResult(result, format, successMessage) {
if (successMessage && format !== 'json') {
this.displaySuccess(successMessage);
}
this.displayData(result ?? { success: true }, format || 'table');
}
}
exports.InteractionHandler = InteractionHandler;
//# sourceMappingURL=interaction.handler.js.map