-
Notifications
You must be signed in to change notification settings - Fork 0
[DX-789] Implement pubsub message annotations #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
sacOO7
wants to merge
3
commits into
fix/pubsub-subscribe-history-formatting
from
feature/pubsub-message-annotations
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { BaseTopicCommand } from "../../base-topic-command.js"; | ||
|
|
||
| export default class ChannelsAnnotations extends BaseTopicCommand { | ||
| protected topicName = "channels:annotations"; | ||
| protected commandGroup = "channel annotations"; | ||
|
|
||
| static override description = "Manage annotations on Ably channel messages"; | ||
|
|
||
| static override examples = [ | ||
| "$ ably channels annotations publish my-channel msg-serial-123 reactions:flag.v1", | ||
| "$ ably channels annotations delete my-channel msg-serial-123 reactions:flag.v1", | ||
| "$ ably channels annotations get my-channel msg-serial-123", | ||
| "$ ably channels annotations subscribe my-channel", | ||
| ]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { Args, Flags } from "@oclif/core"; | ||
| import * as Ably from "ably"; | ||
|
|
||
| import { AblyBaseCommand } from "../../../base-command.js"; | ||
| import { clientIdFlag, productApiFlags } from "../../../flags.js"; | ||
| import { resource, success } from "../../../utils/output.js"; | ||
| import { | ||
| extractSummarizationType, | ||
| validateAnnotationParams, | ||
| } from "../../../utils/annotation-validation.js"; | ||
|
|
||
| export default class ChannelsAnnotationsDelete extends AblyBaseCommand { | ||
| static override args = { | ||
| channel: Args.string({ | ||
| description: "Channel name", | ||
| required: true, | ||
| }), | ||
| msgSerial: Args.string({ | ||
| description: "Message serial of the annotated message", | ||
| required: true, | ||
| }), | ||
| annotationType: Args.string({ | ||
| description: "Annotation type (e.g., reactions:flag.v1)", | ||
| required: true, | ||
| }), | ||
| }; | ||
|
|
||
| static override description = "Delete an annotation from a message"; | ||
|
|
||
| static override examples = [ | ||
| "$ ably channels annotations delete my-channel msg-serial-123 reactions:flag.v1", | ||
| '$ ably channels annotations delete my-channel msg-serial-123 reactions:distinct.v1 --name "thumbsup"', | ||
| "$ ably channels annotations delete my-channel msg-serial-123 reactions:flag.v1 --json", | ||
| ]; | ||
|
|
||
| static override flags = { | ||
| ...productApiFlags, | ||
| ...clientIdFlag, | ||
| name: Flags.string({ | ||
| description: | ||
| "Annotation name (required for distinct/unique/multiple types)", | ||
| }), | ||
| data: Flags.string({ | ||
| description: "Optional data payload (JSON string)", | ||
| }), | ||
| }; | ||
|
|
||
| async run(): Promise<void> { | ||
| const { args, flags } = await this.parse(ChannelsAnnotationsDelete); | ||
|
|
||
| try { | ||
| // 1. Validate (same as publish, but count not needed for delete via isDelete flag) | ||
| const summarization = extractSummarizationType(args.annotationType); | ||
| const errors = validateAnnotationParams(summarization, { | ||
| name: flags.name, | ||
| isDelete: true, | ||
| }); | ||
| if (errors.length > 0) { | ||
| this.error(errors.join("\n")); | ||
| } | ||
|
|
||
| // 2. Build OutboundAnnotation | ||
| const annotation: Ably.OutboundAnnotation = { | ||
| type: args.annotationType, | ||
| }; | ||
| if (flags.name) annotation.name = flags.name; | ||
| if (flags.data) { | ||
| try { | ||
| annotation.data = JSON.parse(flags.data); | ||
| } catch { | ||
| this.error("Invalid JSON in --data flag. Please provide valid JSON."); | ||
| } | ||
| } | ||
|
|
||
| // 3. Create client and delete | ||
| const client = await this.createAblyRealtimeClient(flags); | ||
| if (!client) return; | ||
|
|
||
| const channel = client.channels.get(args.channel); | ||
| await channel.annotations.delete(args.msgSerial, annotation); | ||
|
|
||
| // 4. Output success | ||
| if (this.shouldOutputJson(flags)) { | ||
| this.log( | ||
| this.formatJsonOutput( | ||
| { | ||
| success: true, | ||
| channel: args.channel, | ||
| messageSerial: args.msgSerial, | ||
| annotationType: args.annotationType, | ||
| name: flags.name || null, | ||
| }, | ||
| flags, | ||
| ), | ||
| ); | ||
| } else { | ||
| this.log( | ||
| success(`Annotation deleted from channel ${resource(args.channel)}.`), | ||
| ); | ||
| } | ||
| } catch (error) { | ||
| const errorMsg = `Error deleting annotation: ${error instanceof Error ? error.message : String(error)}`; | ||
| if (this.shouldOutputJson(flags)) { | ||
| this.jsonError({ error: errorMsg, success: false }, flags); | ||
| } else { | ||
| this.error(errorMsg); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import { Args, Flags } from "@oclif/core"; | ||
| import * as Ably from "ably"; | ||
| import chalk from "chalk"; | ||
|
|
||
| import { AblyBaseCommand } from "../../../base-command.js"; | ||
| import { productApiFlags } from "../../../flags.js"; | ||
| import { formatTimestamp, resource } from "../../../utils/output.js"; | ||
|
|
||
| export default class ChannelsAnnotationsGet extends AblyBaseCommand { | ||
| static override args = { | ||
| channel: Args.string({ | ||
| description: "Channel name", | ||
| required: true, | ||
| }), | ||
| msgSerial: Args.string({ | ||
| description: "Message serial to get annotations for", | ||
| required: true, | ||
| }), | ||
| }; | ||
|
|
||
| static override description = "Get annotations for a message"; | ||
|
|
||
| static override examples = [ | ||
| "$ ably channels annotations get my-channel msg-serial-123", | ||
| "$ ably channels annotations get my-channel msg-serial-123 --limit 50", | ||
| "$ ably channels annotations get my-channel msg-serial-123 --json", | ||
| ]; | ||
|
|
||
| static override flags = { | ||
| ...productApiFlags, | ||
| limit: Flags.integer({ | ||
| default: 100, | ||
| description: "Maximum number of results to return (default: 100)", | ||
| }), | ||
| }; | ||
|
|
||
| async run(): Promise<void> { | ||
| const { args, flags } = await this.parse(ChannelsAnnotationsGet); | ||
|
|
||
| try { | ||
| // 1. Create REST client (get is a REST operation) | ||
| const client = await this.createAblyRestClient(flags); | ||
| if (!client) return; | ||
|
|
||
| // 2. Get channel and fetch annotations | ||
| const channel = client.channels.get(args.channel); | ||
| const params: Ably.GetAnnotationsParams = {}; | ||
| if (flags.limit !== undefined) { | ||
| params.limit = flags.limit; | ||
| } | ||
|
|
||
| const result = await channel.annotations.get(args.msgSerial, params); | ||
| const annotations = result.items; | ||
|
|
||
| // 3. Output results | ||
| if (this.shouldOutputJson(flags)) { | ||
| this.log( | ||
| this.formatJsonOutput( | ||
| annotations.map((annotation, index) => ({ | ||
| index: index + 1, | ||
| id: annotation.id, | ||
| action: annotation.action, | ||
| type: annotation.type, | ||
| name: annotation.name || null, | ||
| clientId: annotation.clientId || null, | ||
| count: annotation.count ?? null, | ||
| data: annotation.data ?? null, | ||
| messageSerial: annotation.messageSerial, | ||
| serial: annotation.serial, | ||
| timestamp: annotation.timestamp | ||
| ? new Date(annotation.timestamp).toISOString() | ||
| : null, | ||
| })), | ||
| flags, | ||
| ), | ||
| ); | ||
| } else { | ||
| if (annotations.length === 0) { | ||
| this.log( | ||
| `No annotations found for message ${resource(args.msgSerial)} on channel ${resource(args.channel)}.`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| this.log( | ||
| `Annotations for message ${resource(args.msgSerial)} on channel ${resource(args.channel)}:\n`, | ||
| ); | ||
|
|
||
| for (const [index, annotation] of annotations.entries()) { | ||
| const timestamp = annotation.timestamp | ||
| ? new Date(annotation.timestamp).toISOString() | ||
| : new Date().toISOString(); | ||
|
|
||
| const actionLabel = | ||
| annotation.action === "annotation.create" | ||
| ? chalk.green("CREATE") | ||
| : chalk.red("DELETE"); | ||
|
|
||
| this.log( | ||
| `${chalk.dim(`[${index + 1}]`)} ${formatTimestamp(timestamp)} ${actionLabel} | ${chalk.dim("Type:")} ${annotation.type} | ${chalk.dim("Name:")} ${annotation.name || "(none)"} | ${chalk.dim("Client:")} ${annotation.clientId ? chalk.blue(annotation.clientId) : "(none)"}`, | ||
| ); | ||
| if (annotation.count !== undefined) { | ||
| this.log(` ${chalk.dim("Count:")} ${annotation.count}`); | ||
| } | ||
|
|
||
| if (annotation.data) { | ||
| this.log( | ||
| ` ${chalk.dim("Data:")} ${JSON.stringify(annotation.data)}`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (annotations.length === flags.limit) { | ||
| this.log(""); | ||
| this.log( | ||
| chalk.yellow( | ||
| `Showing maximum of ${flags.limit} annotations. Use --limit to show more.`, | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| const errorMsg = `Error retrieving annotations: ${error instanceof Error ? error.message : String(error)}`; | ||
| if (this.shouldOutputJson(flags)) { | ||
| this.jsonError({ error: errorMsg, success: false }, flags); | ||
| } else { | ||
| this.error(errorMsg); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
channels.get()is called without setting the Ably channel mode required for annotation publishing. The Ably SDK expectsANNOTATION_PUBLISHmode for annotation publish/delete; without it,channel.annotations.delete()can fail at runtime. Request the channel with{ modes: ["ANNOTATION_PUBLISH"] }.