-
Notifications
You must be signed in to change notification settings - Fork 21
[DUX-3058]: implement support for App Home tab #138
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
5 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,160 @@ | ||
| {-# LANGUAGE DuplicateRecordFields #-} | ||
| {-# LANGUAGE OverloadedLists #-} | ||
| {-# LANGUAGE TemplateHaskell #-} | ||
| {-# LANGUAGE TypeOperators #-} | ||
|
|
||
| -- | Publishing views using the Blocks API. | ||
| -- | ||
| -- This is used for App Home and modals. | ||
| -- | ||
| -- <https://api.slack.com/methods/views.publish> | ||
| -- | ||
| -- @since 2.1.0.0 | ||
| module Web.Slack.Experimental.Views ( | ||
| -- * Types | ||
| SlackView (..), | ||
| HomeTabView (..), | ||
| ModalView (..), | ||
| Expected (..), | ||
|
|
||
| -- * Requests and responses | ||
| PublishReq (..), | ||
| PublishResp (..), | ||
| Api, | ||
| viewsPublish, | ||
| ) where | ||
|
|
||
| import Data.Aeson qualified as A | ||
| import Data.Aeson.KeyMap qualified as KM | ||
| import Servant.API (AuthProtect, FormUrlEncoded, JSON, Post, ReqBody, (:>)) | ||
| import Servant.Client (ClientM, client) | ||
| import Servant.Client.Core (AuthenticatedRequest) | ||
| import Web.FormUrlEncoded (ToForm (..)) | ||
| import Web.HttpApiData (ToHttpApiData (..)) | ||
| import Web.Slack.AesonUtils (Expected (..), snakeCaseOptions, snakeCaseOptionsEatTrailingUnderscore, (.=!), (.=?)) | ||
| import Web.Slack.Experimental.Blocks qualified as B | ||
| import Web.Slack.Experimental.Blocks.Types (SlackPlainTextOnly) | ||
| import Web.Slack.Internal (ResponseJSON (..), SlackConfig (..), mkSlackAuthenticateReq, run) | ||
| import Web.Slack.Pager (Response) | ||
| import Web.Slack.Prelude | ||
| import Web.Slack.Types (UserId) | ||
|
|
||
| -- | View definition for some Slack surface. Has an inner type of either | ||
| -- 'ModalView' or 'HomeTabView'. | ||
| -- | ||
| -- <https://api.slack.com/reference/surfaces/views> | ||
| -- | ||
| -- @since 2.1.0.0 | ||
| data SlackView inner = SlackView | ||
| { blocks :: Vector B.SlackBlock | ||
| , privateMetadata :: Maybe Text | ||
| , callbackId :: Maybe Text | ||
| , externalId :: Maybe Text | ||
| , inner :: inner | ||
| } | ||
| deriving stock (Show, Eq, Generic) | ||
|
|
||
| type role SlackView representational | ||
|
|
||
| instance (FromJSON inner) => FromJSON (SlackView inner) where | ||
| parseJSON = withObject "SlackView" \o -> do | ||
| blocks <- o .: "blocks" | ||
| privateMetadata <- o .:? "private_metadata" | ||
| callbackId <- o .:? "callback_id" | ||
| externalId <- o .:? "external_id" | ||
| inner <- parseJSON @inner (A.Object o) | ||
| pure SlackView {..} | ||
|
|
||
| instance (ToJSON inner) => ToJSON (SlackView inner) where | ||
| toJSON SlackView {..} = case toJSON inner of | ||
| A.Object innerObj -> | ||
| A.Object | ||
| ( KM.fromList | ||
| ( catMaybes | ||
| [ "blocks" .=! blocks | ||
| , "private_metadata" .=? privateMetadata | ||
| , "callback_id" .=? callbackId | ||
| , "external_id" .=? externalId | ||
| ] | ||
| ) | ||
| <> innerObj | ||
| ) | ||
| _ -> error "inner of SlackView is not an object" | ||
|
|
||
| -- | @since 2.1.0.0 | ||
| data HomeTabView = HomeTabView | ||
| { type_ :: Expected "home" | ||
| } | ||
| deriving stock (Show, Eq, Generic) | ||
|
|
||
| $(deriveJSON snakeCaseOptionsEatTrailingUnderscore ''HomeTabView) | ||
|
|
||
| -- | Modal view | ||
| -- | ||
| -- <https://api.slack.com/reference/surfaces/views#modal> | ||
| -- | ||
| -- @since 2.1.0.0 | ||
| data ModalView = ModalView | ||
| { type_ :: Expected "modal" | ||
| , title :: SlackPlainTextOnly | ||
| -- ^ Title of the modal on the top left. Maximum length of 24 characters. | ||
| , close :: Maybe SlackPlainTextOnly | ||
| -- ^ Text appearing on the close button on the bottom-right of the modal. | ||
| -- Maximum length of 24 characters. | ||
| , submit :: Maybe SlackPlainTextOnly | ||
| -- ^ Text appearing on the submit buttonn. Maximum length of 24 characters. | ||
| -- Must be 'SlackPlainTextOnly'. | ||
| , submitDisabled :: Maybe Bool | ||
| -- ^ Whether one or more inputs must be filled before enabling the submit button. | ||
| -- For configuration modals: <https://api.slack.com/reference/workflows/configuration-view> | ||
| } | ||
| deriving stock (Show, Eq, Generic) | ||
|
|
||
| $(deriveJSON snakeCaseOptionsEatTrailingUnderscore ''ModalView) | ||
|
|
||
| -- | Publishes the App Home view for a user. | ||
| -- | ||
| -- <https://api.slack.com/methods/views.publish> | ||
| -- | ||
| -- @since 2.1.0.0 | ||
| data PublishReq = PublishReq | ||
| { userId :: UserId | ||
| -- ^ User to whom the view is being published. | ||
| , view :: SlackView HomeTabView | ||
| -- ^ View payload. | ||
| } | ||
| deriving stock (Show) | ||
|
|
||
| instance ToForm PublishReq where | ||
| toForm PublishReq {..} = | ||
| [ ("user_id" :: Text, toQueryParam userId) | ||
| , ("view", toQueryParam . decodeUtf8 $ A.encode view) | ||
| ] | ||
|
|
||
| -- | @since 2.1.0.0 | ||
| data PublishResp = PublishResp | ||
| { view :: SlackView HomeTabView | ||
| } | ||
| deriving stock (Show) | ||
|
|
||
| $(deriveJSON snakeCaseOptions ''PublishResp) | ||
|
|
||
| -- | @since 2.1.0.0 | ||
| type Api = | ||
| "views.publish" | ||
| :> AuthProtect "token" | ||
| :> ReqBody '[FormUrlEncoded] PublishReq | ||
| :> Post '[JSON] (ResponseJSON PublishResp) | ||
lf- marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| -- | Publishes the App Home view for a user. | ||
| -- | ||
| -- <https://api.slack.com/methods/views.publish> | ||
| -- | ||
| -- @since 2.1.0.0 | ||
| viewsPublish :: SlackConfig -> PublishReq -> IO (Response PublishResp) | ||
| viewsPublish slackConfig req = do | ||
| let authR = mkSlackAuthenticateReq slackConfig | ||
| run (viewsPublish_ authR req) . slackConfigManager $ slackConfig | ||
|
|
||
| viewsPublish_ :: AuthenticatedRequest (AuthProtect "token") -> PublishReq -> ClientM (ResponseJSON PublishResp) | ||
| viewsPublish_ = client (Proxy @Api) | ||
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.