Files
remark42/backend/vendor/github.com/slack-go/slack/views.go
T
dependabot[bot]andUmputun 7529aa7e17 Bump the go-modules-updates group in /backend with 9 updates
Bumps the go-modules-updates group in /backend with 9 updates:

| Package | From | To |
| --- | --- | --- |
| [github.com/PuerkitoBio/goquery](https://github.com/PuerkitoBio/goquery) | `1.9.2` | `1.10.0` |
| [github.com/alecthomas/chroma/v2](https://github.com/alecthomas/chroma) | `2.13.0` | `2.14.0` |
| [github.com/go-pkgz/notify](https://github.com/go-pkgz/notify) | `1.1.1` | `1.2.0` |
| [github.com/go-pkgz/repeater](https://github.com/go-pkgz/repeater) | `1.1.3` | `1.2.0` |
| [github.com/rs/xid](https://github.com/rs/xid) | `1.5.0` | `1.6.0` |
| [go.etcd.io/bbolt](https://github.com/etcd-io/bbolt) | `1.3.10` | `1.3.11` |
| [golang.org/x/crypto](https://github.com/golang/crypto) | `0.25.0` | `0.27.0` |
| [golang.org/x/image](https://github.com/golang/image) | `0.18.0` | `0.22.0` |
| [golang.org/x/net](https://github.com/golang/net) | `0.27.0` | `0.29.0` |

Updates `github.com/PuerkitoBio/goquery` from 1.9.2 to 1.10.0
- [Release notes](https://github.com/PuerkitoBio/goquery/releases)
- [Commits](https://github.com/PuerkitoBio/goquery/compare/v1.9.2...v1.10.0)

Updates `github.com/alecthomas/chroma/v2` from 2.13.0 to 2.14.0
- [Release notes](https://github.com/alecthomas/chroma/releases)
- [Changelog](https://github.com/alecthomas/chroma/blob/master/.goreleaser.yml)
- [Commits](https://github.com/alecthomas/chroma/compare/v2.13.0...v2.14.0)

Updates `github.com/go-pkgz/notify` from 1.1.1 to 1.2.0
- [Release notes](https://github.com/go-pkgz/notify/releases)
- [Commits](https://github.com/go-pkgz/notify/compare/v1.1.1...v1.2.0)

Updates `github.com/go-pkgz/repeater` from 1.1.3 to 1.2.0
- [Release notes](https://github.com/go-pkgz/repeater/releases)
- [Commits](https://github.com/go-pkgz/repeater/compare/v1.1.3...v1.2.0)

Updates `github.com/rs/xid` from 1.5.0 to 1.6.0
- [Release notes](https://github.com/rs/xid/releases)
- [Commits](https://github.com/rs/xid/compare/v1.5.0...v1.6.0)

Updates `go.etcd.io/bbolt` from 1.3.10 to 1.3.11
- [Release notes](https://github.com/etcd-io/bbolt/releases)
- [Commits](https://github.com/etcd-io/bbolt/compare/v1.3.10...v1.3.11)

Updates `golang.org/x/crypto` from 0.25.0 to 0.27.0
- [Commits](https://github.com/golang/crypto/compare/v0.25.0...v0.27.0)

Updates `golang.org/x/image` from 0.18.0 to 0.22.0
- [Commits](https://github.com/golang/image/compare/v0.18.0...v0.22.0)

Updates `golang.org/x/net` from 0.27.0 to 0.29.0
- [Commits](https://github.com/golang/net/compare/v0.27.0...v0.29.0)

---
updated-dependencies:
- dependency-name: github.com/PuerkitoBio/goquery
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: go-modules-updates
- dependency-name: github.com/alecthomas/chroma/v2
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: go-modules-updates
- dependency-name: github.com/go-pkgz/notify
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: go-modules-updates
- dependency-name: github.com/go-pkgz/repeater
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: go-modules-updates
- dependency-name: github.com/rs/xid
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: go-modules-updates
- dependency-name: go.etcd.io/bbolt
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: go-modules-updates
- dependency-name: golang.org/x/crypto
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: go-modules-updates
- dependency-name: golang.org/x/image
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: go-modules-updates
- dependency-name: golang.org/x/net
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: go-modules-updates
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-11-08 16:19:58 -06:00

315 lines
8.9 KiB
Go

package slack
import (
"context"
"encoding/json"
)
const (
VTModal ViewType = "modal"
VTHomeTab ViewType = "home"
)
type ViewType string
type ViewState struct {
Values map[string]map[string]BlockAction `json:"values"`
}
type View struct {
SlackResponse
ID string `json:"id"`
TeamID string `json:"team_id"`
Type ViewType `json:"type"`
Title *TextBlockObject `json:"title"`
Close *TextBlockObject `json:"close"`
Submit *TextBlockObject `json:"submit"`
Blocks Blocks `json:"blocks"`
PrivateMetadata string `json:"private_metadata"`
CallbackID string `json:"callback_id"`
State *ViewState `json:"state"`
Hash string `json:"hash"`
ClearOnClose bool `json:"clear_on_close"`
NotifyOnClose bool `json:"notify_on_close"`
RootViewID string `json:"root_view_id"`
PreviousViewID string `json:"previous_view_id"`
AppID string `json:"app_id"`
ExternalID string `json:"external_id"`
BotID string `json:"bot_id"`
AppInstalledTeamID string `json:"app_installed_team_id"`
}
type ViewSubmissionCallbackResponseURL struct {
BlockID string `json:"block_id"`
ActionID string `json:"action_id"`
ChannelID string `json:"channel_id"`
ResponseURL string `json:"response_url"`
}
type ViewSubmissionCallback struct {
Hash string `json:"hash"`
ResponseURLs []ViewSubmissionCallbackResponseURL `json:"response_urls,omitempty"`
}
type ViewClosedCallback struct {
IsCleared bool `json:"is_cleared"`
}
const (
RAClear ViewResponseAction = "clear"
RAUpdate ViewResponseAction = "update"
RAPush ViewResponseAction = "push"
RAErrors ViewResponseAction = "errors"
)
type ViewResponseAction string
type ViewSubmissionResponse struct {
ResponseAction ViewResponseAction `json:"response_action"`
View *ModalViewRequest `json:"view,omitempty"`
Errors map[string]string `json:"errors,omitempty"`
}
func NewClearViewSubmissionResponse() *ViewSubmissionResponse {
return &ViewSubmissionResponse{
ResponseAction: RAClear,
}
}
func NewUpdateViewSubmissionResponse(view *ModalViewRequest) *ViewSubmissionResponse {
return &ViewSubmissionResponse{
ResponseAction: RAUpdate,
View: view,
}
}
func NewPushViewSubmissionResponse(view *ModalViewRequest) *ViewSubmissionResponse {
return &ViewSubmissionResponse{
ResponseAction: RAPush,
View: view,
}
}
func NewErrorsViewSubmissionResponse(errors map[string]string) *ViewSubmissionResponse {
return &ViewSubmissionResponse{
ResponseAction: RAErrors,
Errors: errors,
}
}
type ModalViewRequest struct {
Type ViewType `json:"type"`
Title *TextBlockObject `json:"title,omitempty"`
Blocks Blocks `json:"blocks"`
Close *TextBlockObject `json:"close,omitempty"`
Submit *TextBlockObject `json:"submit,omitempty"`
PrivateMetadata string `json:"private_metadata,omitempty"`
CallbackID string `json:"callback_id,omitempty"`
ClearOnClose bool `json:"clear_on_close,omitempty"`
NotifyOnClose bool `json:"notify_on_close,omitempty"`
ExternalID string `json:"external_id,omitempty"`
}
func (v *ModalViewRequest) ViewType() ViewType {
return v.Type
}
type HomeTabViewRequest struct {
Type ViewType `json:"type"`
Blocks Blocks `json:"blocks"`
PrivateMetadata string `json:"private_metadata,omitempty"`
CallbackID string `json:"callback_id,omitempty"`
ExternalID string `json:"external_id,omitempty"`
}
func (v *HomeTabViewRequest) ViewType() ViewType {
return v.Type
}
type openViewRequest struct {
TriggerID string `json:"trigger_id"`
View ModalViewRequest `json:"view"`
}
type publishViewRequest struct {
UserID string `json:"user_id"`
View HomeTabViewRequest `json:"view"`
Hash string `json:"hash,omitempty"`
}
type pushViewRequest struct {
TriggerID string `json:"trigger_id"`
View ModalViewRequest `json:"view"`
}
type updateViewRequest struct {
View ModalViewRequest `json:"view"`
ExternalID string `json:"external_id,omitempty"`
Hash string `json:"hash,omitempty"`
ViewID string `json:"view_id,omitempty"`
}
type ViewResponse struct {
SlackResponse
View `json:"view"`
}
// OpenView opens a view for a user.
// For more information see the OpenViewContext documentation.
func (api *Client) OpenView(triggerID string, view ModalViewRequest) (*ViewResponse, error) {
return api.OpenViewContext(context.Background(), triggerID, view)
}
// ValidateUniqueBlockID will verify if each input block has a unique block ID if set
func ValidateUniqueBlockID(view ModalViewRequest) bool {
uniqueBlockID := map[string]bool{}
for _, b := range view.Blocks.BlockSet {
if inputBlock, ok := b.(*InputBlock); ok {
if _, ok := uniqueBlockID[inputBlock.BlockID]; ok {
return false
}
uniqueBlockID[inputBlock.BlockID] = true
}
}
return true
}
// OpenViewContext opens a view for a user with a custom context.
// Slack API docs: https://api.slack.com/methods/views.open
func (api *Client) OpenViewContext(
ctx context.Context,
triggerID string,
view ModalViewRequest,
) (*ViewResponse, error) {
if triggerID == "" {
return nil, ErrParametersMissing
}
if !ValidateUniqueBlockID(view) {
return nil, ErrBlockIDNotUnique
}
req := openViewRequest{
TriggerID: triggerID,
View: view,
}
encoded, err := json.Marshal(req)
if err != nil {
return nil, err
}
endpoint := api.endpoint + "views.open"
resp := &ViewResponse{}
err = postJSON(ctx, api.httpclient, endpoint, api.token, encoded, resp, api)
if err != nil {
return nil, err
}
return resp, resp.Err()
}
// PublishView publishes a static view for a user.
// For more information see the PublishViewContext documentation.
func (api *Client) PublishView(userID string, view HomeTabViewRequest, hash string) (*ViewResponse, error) {
return api.PublishViewContext(context.Background(), userID, view, hash)
}
// PublishViewContext publishes a static view for a user with a custom context.
// Slack API docs: https://api.slack.com/methods/views.publish
func (api *Client) PublishViewContext(
ctx context.Context,
userID string,
view HomeTabViewRequest,
hash string,
) (*ViewResponse, error) {
if userID == "" {
return nil, ErrParametersMissing
}
req := publishViewRequest{
UserID: userID,
View: view,
Hash: hash,
}
encoded, err := json.Marshal(req)
if err != nil {
return nil, err
}
endpoint := api.endpoint + "views.publish"
resp := &ViewResponse{}
err = postJSON(ctx, api.httpclient, endpoint, api.token, encoded, resp, api)
if err != nil {
return nil, err
}
return resp, resp.Err()
}
// PushView pushes a view onto the stack of a root view.
// For more information see the PushViewContext documentation.
func (api *Client) PushView(triggerID string, view ModalViewRequest) (*ViewResponse, error) {
return api.PushViewContext(context.Background(), triggerID, view)
}
// PushViewContext pushes a view onto the stack of a root view with a custom context.
// Slack API docs: https://api.slack.com/methods/views.push
func (api *Client) PushViewContext(
ctx context.Context,
triggerID string,
view ModalViewRequest,
) (*ViewResponse, error) {
if triggerID == "" {
return nil, ErrParametersMissing
}
req := pushViewRequest{
TriggerID: triggerID,
View: view,
}
encoded, err := json.Marshal(req)
if err != nil {
return nil, err
}
endpoint := api.endpoint + "views.push"
resp := &ViewResponse{}
err = postJSON(ctx, api.httpclient, endpoint, api.token, encoded, resp, api)
if err != nil {
return nil, err
}
return resp, resp.Err()
}
// UpdateView updates an existing view.
// For more information see the UpdateViewContext documentation.
func (api *Client) UpdateView(view ModalViewRequest, externalID, hash, viewID string) (*ViewResponse, error) {
return api.UpdateViewContext(context.Background(), view, externalID, hash, viewID)
}
// UpdateViewContext updates an existing view with a custom context.
// Slack API docs: https://api.slack.com/methods/views.update
func (api *Client) UpdateViewContext(
ctx context.Context,
view ModalViewRequest,
externalID, hash,
viewID string,
) (*ViewResponse, error) {
if externalID == "" && viewID == "" {
return nil, ErrParametersMissing
}
req := updateViewRequest{
View: view,
ExternalID: externalID,
Hash: hash,
ViewID: viewID,
}
encoded, err := json.Marshal(req)
if err != nil {
return nil, err
}
endpoint := api.endpoint + "views.update"
resp := &ViewResponse{}
err = postJSON(ctx, api.httpclient, endpoint, api.token, encoded, resp, api)
if err != nil {
return nil, err
}
return resp, resp.Err()
}