-
Notifications
You must be signed in to change notification settings - Fork 0
Add channel flag to push publish
#184
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,15 +5,22 @@ import * as path from "node:path"; | |
| import { AblyBaseCommand } from "../../base-command.js"; | ||
| import { productApiFlags } from "../../flags.js"; | ||
| import { BaseFlags } from "../../types/cli.js"; | ||
| import { formatProgress, formatSuccess } from "../../utils/output.js"; | ||
| import { | ||
| formatProgress, | ||
| formatResource, | ||
| formatSuccess, | ||
| formatWarning, | ||
| } from "../../utils/output.js"; | ||
| import { promptForConfirmation } from "../../utils/prompt-confirmation.js"; | ||
|
|
||
| export default class PushPublish extends AblyBaseCommand { | ||
| static override description = | ||
| "Publish a push notification to a device or client"; | ||
| "Publish a push notification to a device, client, or channel"; | ||
|
|
||
| static override examples = [ | ||
| "<%= config.bin %> <%= command.id %> --device-id device-123 --title Hello --body World", | ||
| "<%= config.bin %> <%= command.id %> --client-id client-1 --title Hello --body World", | ||
| "<%= config.bin %> <%= command.id %> --channel my-channel --title Hello --body World", | ||
| '<%= config.bin %> <%= command.id %> --device-id device-123 --payload \'{"notification":{"title":"Hello","body":"World"}}\'', | ||
| '<%= config.bin %> <%= command.id %> --recipient \'{"transportType":"apns","deviceToken":"token123"}\' --title Hello --body World', | ||
| "<%= config.bin %> <%= command.id %> --device-id device-123 --title Hello --body World --json", | ||
|
|
@@ -33,6 +40,10 @@ export default class PushPublish extends AblyBaseCommand { | |
| description: "Raw recipient JSON for advanced targeting", | ||
| exclusive: ["device-id", "client-id"], | ||
| }), | ||
| channel: Flags.string({ | ||
| description: | ||
| "Target channel name (publishes push notification via the channel using extras.push; ignored if --device-id, --client-id, or --recipient is also provided)", | ||
| }), | ||
| title: Flags.string({ | ||
| description: "Notification title", | ||
| }), | ||
|
|
@@ -70,32 +81,49 @@ export default class PushPublish extends AblyBaseCommand { | |
| web: Flags.string({ | ||
| description: "Web push-specific override as JSON", | ||
| }), | ||
| force: Flags.boolean({ | ||
| char: "f", | ||
| description: "Skip confirmation prompt when publishing to a channel", | ||
| }), | ||
| }; | ||
|
|
||
| async run(): Promise<void> { | ||
| const { flags } = await this.parse(PushPublish); | ||
|
|
||
| if (!flags["device-id"] && !flags["client-id"] && !flags.recipient) { | ||
| const hasDirectRecipient = | ||
| flags["device-id"] || flags["client-id"] || flags.recipient; | ||
|
|
||
| if (!hasDirectRecipient && !flags.channel) { | ||
| this.fail( | ||
| "A recipient is required: --device-id, --client-id, or --recipient", | ||
| "A target is required: --device-id, --client-id, --recipient, or --channel", | ||
| flags as BaseFlags, | ||
| "pushPublish", | ||
| ); | ||
| } | ||
|
|
||
| if (hasDirectRecipient && flags.channel) { | ||
| const channelIgnoredWarning = | ||
| "--channel is ignored when --device-id, --client-id, or --recipient is provided."; | ||
| if (this.shouldOutputJson(flags)) { | ||
| this.logJsonStatus("warning", channelIgnoredWarning, flags); | ||
| } else { | ||
| this.log(formatWarning(channelIgnoredWarning)); | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| const rest = await this.createAblyRestClient(flags as BaseFlags); | ||
| if (!rest) return; | ||
|
|
||
| // Build recipient | ||
| let recipient: Record<string, unknown>; | ||
| let recipient: Record<string, unknown> | undefined; | ||
| if (flags["device-id"]) { | ||
| recipient = { deviceId: flags["device-id"] }; | ||
| } else if (flags["client-id"]) { | ||
| recipient = { clientId: flags["client-id"] }; | ||
| } else { | ||
| } else if (flags.recipient) { | ||
| recipient = this.parseJsonObjectFlag( | ||
| flags.recipient!, | ||
| flags.recipient, | ||
| "--recipient", | ||
| flags as BaseFlags, | ||
| ); | ||
|
|
@@ -202,12 +230,40 @@ export default class PushPublish extends AblyBaseCommand { | |
| this.log(formatProgress("Publishing push notification")); | ||
| } | ||
|
|
||
| await rest.push.admin.publish(recipient!, payload); | ||
| if (recipient) { | ||
| await rest.push.admin.publish(recipient, payload); | ||
|
|
||
| if (this.shouldOutputJson(flags)) { | ||
| this.logJsonResult({ published: true, recipient: recipient! }, flags); | ||
| if (this.shouldOutputJson(flags)) { | ||
| this.logJsonResult({ published: true, recipient }, flags); | ||
| } else { | ||
| this.log(formatSuccess("Push notification published.")); | ||
| } | ||
| } else { | ||
| this.log(formatSuccess("Push notification published.")); | ||
| const channelName = flags.channel!; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw I feel, if possible we should also introduce confirmation dialogue saying
using promptForConfirmation in the code. Generally devs tend to test push notifications, if there are lots of users present on the channel in prod environment, all of them will receive test notifications. So, we can make it explicit in the prompt.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same, goes for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call. Added |
||
|
|
||
| if (!this.shouldOutputJson(flags) && !flags.force) { | ||
| const confirmed = await promptForConfirmation( | ||
| `This will send a push notification to all devices subscribed to channel ${formatResource(channelName)}. You can check channel occupancy using \`ably channels occupancy get ${formatResource(channelName)}\`. Continue?`, | ||
| ); | ||
| if (!confirmed) { | ||
| this.log("Publish cancelled."); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| await rest.channels | ||
| .get(channelName) | ||
| .publish({ extras: { push: payload } }); | ||
|
|
||
| if (this.shouldOutputJson(flags)) { | ||
| this.logJsonResult({ published: true, channel: channelName }, flags); | ||
| } else { | ||
| this.log( | ||
| formatSuccess( | ||
| `Push notification published to channel: ${formatResource(channelName)}.`, | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| this.fail(error, flags as BaseFlags, "pushPublish"); | ||
|
|
||
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.
push publishnow includes a--forceflag (and channel publishing prompts for confirmation when not using JSON), but the README section forably push publishdoesn’t list--forcein USAGE/FLAGS. Please update the generated command docs so help/README stay in sync with the command’s actual flags and behavior.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.
@copilot apply changes based on this feedback
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.
Done — regenerated the README in commit 38e102a. The
ably push publishsection now includes[-f]in the USAGE line and-f, --force Skip confirmation prompt when publishing to a channelin the FLAGS section.