Repository navigation
feat(pins): add pins add, remove and list #218
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
Merged
Changes from all commits
Commits
Show all changes
2 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
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
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,63 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/url" | ||
| ) | ||
|
|
||
| // PinnedItem is one entry of pins.list. Type is "message" for a pinned | ||
| // message; Slack can also return "file" and "file_comment" items, which carry | ||
| // File instead of Message. | ||
| type PinnedItem struct { | ||
| Type string `json:"type"` | ||
| Channel string `json:"channel"` | ||
| Created int64 `json:"created"` | ||
| CreatedBy string `json:"created_by"` | ||
| Message *Message `json:"message,omitempty"` | ||
| File *File `json:"file,omitempty"` | ||
| } | ||
|
|
||
| // AddPin pins a message to a channel. It reports whether the message was | ||
| // already pinned, which Slack signals with an already_pinned error. | ||
| func (c *Client) AddPin(channel, timestamp string) (alreadyPinned bool, err error) { | ||
| _, err = c.post("pins.add", map[string]interface{}{ | ||
| "channel": channel, | ||
| "timestamp": timestamp, | ||
| }) | ||
| if IsSlackError(err, "already_pinned") { | ||
| return true, nil | ||
| } | ||
| return false, err | ||
| } | ||
|
|
||
| // RemovePin unpins a message from a channel. It reports whether the message | ||
| // was not pinned, which Slack signals with a no_pin or not_pinned error. | ||
| func (c *Client) RemovePin(channel, timestamp string) (notPinned bool, err error) { | ||
| _, err = c.post("pins.remove", map[string]interface{}{ | ||
| "channel": channel, | ||
| "timestamp": timestamp, | ||
| }) | ||
| if IsSlackError(err, "no_pin") || IsSlackError(err, "not_pinned") { | ||
| return true, nil | ||
| } | ||
| return false, err | ||
| } | ||
|
|
||
| // ListPins returns the items pinned to a channel. | ||
| func (c *Client) ListPins(channel string) ([]PinnedItem, error) { | ||
| params := url.Values{} | ||
| params.Set("channel", channel) | ||
|
|
||
| body, err := c.get("pins.list", params) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var result struct { | ||
| Items []PinnedItem `json:"items"` | ||
| } | ||
| if err := json.Unmarshal(body, &result); err != nil { | ||
| return nil, err | ||
| } | ||
| return result.Items, nil | ||
| } |
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,12 @@ | ||
| // Package cmdutil holds cobra helpers shared by the command packages. | ||
| package cmdutil | ||
|
|
||
| import "github.com/spf13/cobra" | ||
|
|
||
| // Quiet stops cobra from printing a runtime error and the usage text once the | ||
| // arguments parsed. The root command then prints the error alone, or nothing | ||
| // for an error that reports itself as silent. Call it first in RunE. | ||
| func Quiet(cmd *cobra.Command) { | ||
| cmd.SilenceUsage = true | ||
| cmd.SilenceErrors = true | ||
| } |
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,60 @@ | ||
| package pins | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/open-cli-collective/slack-chat-api/internal/client" | ||
| "github.com/open-cli-collective/slack-chat-api/internal/cmd/cmdutil" | ||
| "github.com/open-cli-collective/slack-chat-api/internal/output" | ||
| "github.com/open-cli-collective/slack-chat-api/internal/validate" | ||
| ) | ||
|
|
||
| func newAddCmd() *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "add <channel> <timestamp>", | ||
| Short: "Pin a message to a channel", | ||
| Long: `Pin a message to a channel. | ||
|
|
||
| The timestamp is the message's ts (1234567890.123456), its p-prefixed form, | ||
| or the message's Slack URL. Pinning a message that is already pinned | ||
| succeeds and says so. Requires the pins:write scope.`, | ||
| Example: ` slck pins add C1234567890 1234567890.123456 | ||
| slck pins add general 1234567890.123456 --as-user`, | ||
| Args: cobra.ExactArgs(2), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| cmdutil.Quiet(cmd) | ||
| return runAdd(args[0], args[1], nil) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func runAdd(channel, timestamp string, c *client.Client) error { | ||
| if err := validate.Timestamp(timestamp); err != nil { | ||
| return err | ||
| } | ||
| timestamp = validate.NormalizeTimestamp(timestamp) | ||
|
|
||
| if c == nil { | ||
| var err error | ||
| c, err = client.New() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| channelID, err := c.ResolveMessageDestination(channel) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| alreadyPinned, err := c.AddPin(channelID, timestamp) | ||
| if err != nil { | ||
| return client.WrapScopeError("pin message", writeScope, err) | ||
| } | ||
| if alreadyPinned { | ||
| output.Printf("Message %s is already pinned in %s\n", timestamp, channel) | ||
| return nil | ||
| } | ||
| output.Printf("Pinned message %s in %s\n", timestamp, channel) | ||
| return nil | ||
| } |
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.