-
Notifications
You must be signed in to change notification settings - Fork 0
[DX-1285] Implement get-message to retrieve latest version by serial
#385
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
Merged
+701
−10
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
93f42ef
feat(channels): add `get-message` to retrieve latest version of a mes…
sacOO7 37aeb39
fix(channels): address get-message PR review comments
sacOO7 b82baf4
Fixed output for channels get-message, updated description for annota…
sacOO7 97b55e1
Addressed review comments related to hint message on message serial f…
sacOO7 65d2c0e
Updated `MUTABLE_MESSAGES_HINT` message, added optional `hint` param …
sacOO7 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
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
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,143 @@ | ||
| import { Args, Flags } from "@oclif/core"; | ||
|
sacOO7 marked this conversation as resolved.
|
||
| import * as Ably from "ably"; | ||
|
|
||
| import { AblyBaseCommand } from "../../base-command.js"; | ||
| import { CommandError } from "../../errors/command-error.js"; | ||
| import { productApiFlags } from "../../flags.js"; | ||
| import { | ||
| formatMessageTimestamp, | ||
| formatMessagesOutput, | ||
| formatResource, | ||
| } from "../../utils/output.js"; | ||
| import type { MessageDisplayFields } from "../../utils/output.js"; | ||
|
|
||
| const MUTABLE_MESSAGES_HINT = | ||
| "Ensure the channel has a `mutableMessages` rule enabled (run `ably apps rules list`) and that the message serial is correct."; | ||
|
|
||
| export default class ChannelsGetMessage extends AblyBaseCommand { | ||
| static override args = { | ||
| channelName: Args.string({ | ||
| description: "The channel name", | ||
| required: true, | ||
| }), | ||
| messageSerial: Args.string({ | ||
| description: "The serial of the message to retrieve", | ||
| required: true, | ||
| }), | ||
| }; | ||
|
|
||
| static override description = | ||
| "Get the latest version of a message on an Ably channel. Requires `mutableMessages` enabled on the channel rule."; | ||
|
|
||
| static override examples = [ | ||
| '$ ably channels get-message my-channel "01234567890:0"', | ||
| '$ ably channels get-message my-channel "01234567890:0" --json', | ||
| '$ ably channels get-message my-channel "01234567890:0" --pretty-json', | ||
| '$ ably channels get-message my-channel "01234567890:0" --cipher YOUR_CIPHER_KEY', | ||
| ]; | ||
|
|
||
| static override flags = { | ||
| ...productApiFlags, | ||
| cipher: Flags.string({ | ||
| description: | ||
| "Decryption key for encrypted messages (base64-encoded or hex-encoded, supports AES-128-CBC and AES-256-CBC)", | ||
| }), | ||
| }; | ||
|
|
||
| async run(): Promise<void> { | ||
| const { args, flags } = await this.parse(ChannelsGetMessage); | ||
| const channelName = args.channelName; | ||
| const serial = args.messageSerial; | ||
|
|
||
| try { | ||
| const rest = await this.createAblyRestClient(flags); | ||
| if (!rest) return; | ||
|
|
||
| const channelOptions: Ably.ChannelOptions = {}; | ||
| if (flags.cipher) { | ||
| channelOptions.cipher = { key: flags.cipher }; | ||
| } | ||
|
|
||
| const channel = rest.channels.get(channelName, channelOptions); | ||
|
|
||
| this.logProgress( | ||
| `Fetching message ${formatResource(serial)} on channel ${formatResource(channelName)}`, | ||
| flags, | ||
| ); | ||
|
|
||
| const message = await channel.getMessage(serial); | ||
|
|
||
| const tracePayload = { | ||
| id: message.id, | ||
| timestamp: formatMessageTimestamp(message.timestamp), | ||
| channel: channelName, | ||
| event: message.name || undefined, | ||
| clientId: message.clientId, | ||
| connectionId: message.connectionId, | ||
| data: message.data as unknown, | ||
| encoding: message.encoding, | ||
| extras: message.extras as unknown, | ||
| action: | ||
| message.action === undefined ? undefined : String(message.action), | ||
| serial: message.serial, | ||
| version: message.version, | ||
| annotations: message.annotations, | ||
| }; | ||
| this.logCliEvent( | ||
| flags, | ||
| "channelGetMessage", | ||
| "messageRetrieved", | ||
| `Retrieved message ${message.serial ?? serial} on channel ${channelName}`, | ||
| tracePayload, | ||
| ); | ||
|
|
||
| if (this.shouldOutputJson(flags)) { | ||
| this.logJsonResult( | ||
| { | ||
| message: { | ||
| ...message, | ||
| // Stringify action for predictable JSON typing across commands | ||
| // (matches `channels subscribe`'s explicit normalisation). | ||
| action: | ||
| message.action === undefined | ||
| ? undefined | ||
| : String(message.action), | ||
| // Nullish-aware: a legitimate epoch-zero timestamp must not be | ||
| // dropped to undefined. | ||
| timestamp: | ||
| message.timestamp == null | ||
| ? undefined | ||
| : new Date(message.timestamp).toISOString(), | ||
| }, | ||
| }, | ||
| flags, | ||
| ); | ||
|
sacOO7 marked this conversation as resolved.
sacOO7 marked this conversation as resolved.
|
||
| } else { | ||
| const display: MessageDisplayFields = { | ||
| action: | ||
| message.action === undefined ? undefined : String(message.action), | ||
| channel: channelName, | ||
| clientId: message.clientId, | ||
| data: message.data, | ||
| event: message.name || undefined, | ||
| id: message.id, | ||
| serial: message.serial, | ||
| timestamp: message.timestamp ?? Date.now(), | ||
| version: message.version, | ||
| annotations: message.annotations, | ||
| }; | ||
| this.log(formatMessagesOutput([display])); | ||
| } | ||
| } catch (error) { | ||
| const cmdError = CommandError.from(error); | ||
| const hint = cmdError.code === 40400 ? MUTABLE_MESSAGES_HINT : undefined; | ||
| this.fail( | ||
| error, | ||
| flags, | ||
| "channelGetMessage", | ||
| { channel: channelName, serial }, | ||
| hint, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.