@ably/cli
Version:
Ably CLI for Pub/Sub, Chat and Spaces
123 lines (122 loc) • 5.89 kB
JavaScript
import { Args, Flags } from "@oclif/core";
import { RoomStatus, MessageReactionType, } from "@ably/chat";
import chalk from "chalk";
import { ChatBaseCommand } from "../../../../chat-base-command.js";
// Map CLI-friendly type names to SDK MessageReactionType values
const REACTION_TYPE_MAP = {
unique: MessageReactionType.Unique,
distinct: MessageReactionType.Distinct,
multiple: MessageReactionType.Multiple,
};
export default class MessagesReactionsRemove extends ChatBaseCommand {
static args = {
room: Args.string({
description: "The room where the message is located",
required: true,
}),
messageSerial: Args.string({
description: "The serial ID of the message to remove reaction from",
required: true,
}),
reaction: Args.string({
description: "The reaction to remove (e.g. 👍, ❤️, 😂)",
required: true,
}),
};
static description = "Remove a reaction from a message in a chat room";
static examples = [
"$ ably rooms messages reactions remove my-room message-serial 👍",
'$ ably rooms messages reactions remove --api-key "YOUR_API_KEY" my-room message-serial ❤️',
"$ ably rooms messages reactions remove my-room message-serial 👍 --type unique",
"$ ably rooms messages reactions remove my-room message-serial 👍 --json",
];
static flags = {
...ChatBaseCommand.globalFlags,
type: Flags.string({
description: "The type of reaction (unique, distinct, or multiple)",
options: Object.keys(REACTION_TYPE_MAP),
}),
};
async run() {
const { args, flags } = await this.parse(MessagesReactionsRemove);
const { room, messageSerial, reaction } = args;
try {
// Create Chat client
const chatClient = await this.createChatClient(flags);
if (!chatClient) {
this.error("Failed to create Chat client");
return;
}
// Add listeners for connection state changes
chatClient.connection.onStatusChange((stateChange) => {
this.logCliEvent(flags, "connection", stateChange.current, `Realtime connection state changed to ${stateChange.current}`, { error: stateChange.error });
});
// Get the room
this.logCliEvent(flags, "room", "gettingRoom", `Getting room handle for ${room}`);
const chatRoom = await chatClient.rooms.get(room);
this.logCliEvent(flags, "room", "gotRoom", `Got room handle for ${room}`);
// Subscribe to room status changes
this.logCliEvent(flags, "room", "subscribingToStatus", "Subscribing to room status changes");
chatRoom.onStatusChange((statusChange) => {
let reason;
if (statusChange.current === RoomStatus.Failed) {
reason = chatRoom.error; // Get reason from chatRoom.error on failure
}
const reasonMsg = reason instanceof Error ? reason.message : reason;
this.logCliEvent(flags, "room", `status-${statusChange.current}`, `Room status changed to ${statusChange.current}`, { reason: reasonMsg });
if (statusChange.current === RoomStatus.Failed &&
!this.shouldOutputJson(flags)) {
this.error(`Failed to attach to room: ${reasonMsg || "Unknown error"}`);
}
});
this.logCliEvent(flags, "room", "subscribedToStatus", "Successfully subscribed to room status changes");
// Attach to the room
this.logCliEvent(flags, "room", "attaching", `Attaching to room ${room}`);
await chatRoom.attach();
this.logCliEvent(flags, "room", "attached", `Successfully attached to room ${room}`);
// Remove the reaction
this.logCliEvent(flags, "reaction", "removing", `Removing reaction ${reaction} from message`, {
messageSerial,
reaction,
...(flags.type && { type: flags.type }),
});
// Use delete method instead of remove
await chatRoom.messages.reactions.delete(messageSerial, {
name: reaction,
...(flags.type && { type: REACTION_TYPE_MAP[flags.type] }),
});
this.logCliEvent(flags, "reaction", "removed", `Successfully removed reaction ${reaction} from message`);
// Format the response
const resultData = {
messageSerial,
reaction,
room,
success: true,
...(flags.type && { type: flags.type }),
};
if (this.shouldOutputJson(flags)) {
this.log(this.formatJsonOutput(resultData, flags));
}
else {
this.log(`${chalk.green("✓")} Removed reaction ${chalk.yellow(reaction)} from message ${chalk.cyan(messageSerial)} in room ${chalk.cyan(room)}`);
}
}
catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
this.logCliEvent(flags, "reaction", "error", `Failed to remove reaction: ${errorMsg}`, { error: errorMsg, room, messageSerial, reaction });
if (this.shouldOutputJson(flags)) {
this.log(this.formatJsonOutput({
error: errorMsg,
room,
messageSerial,
reaction,
...(flags.type && { type: flags.type }),
success: false,
}, flags));
}
else {
this.error(`Failed to remove reaction: ${errorMsg}`);
}
}
}
}