Skip to content
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

Parse Slack email objects without exploding #124

Merged
merged 2 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.6.1.0 (2022-12-16)

* [#124](https://github.com/MercuryTechnologies/slack-web/pull/124)
Parse [Slack incoming emails](https://slack.com/help/articles/206819278-Send-emails-to-Slack)
without throwing an error.

# 1.6.0.0 (2022-12-14)

## Breaking changes
Expand Down
6 changes: 4 additions & 2 deletions slack-web.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ license: MIT
license-file: LICENSE.md

copyright: 2017 Juan Pedro Villa Isaza, 2022 Mercury Technologies, Inc
author: Juan Pedro Villa Isaza <jpvillaisaza@gmail.com>, Jade Lovelace <jadel@mercury.com>, Dennis Hennen <dennis@mercury.com>
maintainer: Jade Lovelace <jadel@mercury.com>
author: Juan Pedro Villa Isaza <jpvillaisaza@gmail.com>, Jade Lovelace <software at lfcode dot ca>, Dennis Hennen <dennis@mercury.com>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😭

maintainer: Jade Lovelace <software at lfcode dot ca>

homepage: https://github.com/MercuryTechnologies/slack-web
bug-reports: https://github.com/MercuryTechnologies/slack-web/issues
Expand Down Expand Up @@ -187,6 +187,7 @@ test-suite tests
Web.Slack.Experimental.Events.TypesSpec
Web.Slack.Experimental.BlocksSpec
TestImport
TestImport.Aeson
build-tool-depends:
hspec-discover:hspec-discover >=2.6.0 && <2.11
build-depends:
Expand All @@ -197,6 +198,7 @@ test-suite tests
, bytestring
, classy-prelude
, fakepull
, generic-arbitrary
, hspec
, hspec-core
, hspec-golden
Expand Down
38 changes: 35 additions & 3 deletions src/Web/Slack/Files/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,42 @@ newtype FileId = FileId {unFileId :: Text}
deriving stock (Show, Eq)
deriving newtype (FromJSON, ToJSON)

data FileMode = Hosted | External | Snippet | Post | FileAccess
deriving stock (Show, Eq)
data FileMode
= Hosted
| External
| Snippet
| Post
| FileAccess
| -- | <https://slack.com/help/articles/206819278-Send-emails-to-Slack>
--
-- @since 1.6.1.0
Email
| -- | Other file modes.
--
-- @since 1.6.1.0
Other Text
deriving stock (Show, Eq, Generic)

instance FromJSON FileMode where
parseJSON = A.withText "FileMode" \case
"hosted" -> pure Hosted
"external" -> pure External
"snippet" -> pure Snippet
"post" -> pure Post
"file_access" -> pure FileAccess
"email" -> pure Email
other -> pure . Other $ other

$(deriveJSON snakeCaseOptions ''FileMode)
instance ToJSON FileMode where
toJSON =
A.String . \case
Hosted -> "hosted"
External -> "external"
Snippet -> "snippet"
Post -> "post"
FileAccess -> "file_access"
Email -> "email"
Other s -> s

-- | <https://api.slack.com/types/file>
data FileObjectVisible = FileObjectVisible
Expand Down
5 changes: 5 additions & 0 deletions tests/TestImport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ module TestImport
( fromJust,
module Control.Monad.Fail,
module Test.Hspec,
module Test.Hspec.QuickCheck,
module ClassyPrelude,
module Data.Aeson,
module Data.Aeson.TH,
cs,
module Test.QuickCheck,
)
where

Expand All @@ -16,3 +18,6 @@ import Data.Aeson.TH (deriveJSON)
import Data.Maybe (fromJust)
import Data.String.Conversions (cs)
import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck
import Test.QuickCheck.Instances ()
14 changes: 14 additions & 0 deletions tests/TestImport/Aeson.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module TestImport.Aeson (aesonRoundtrips) where

import Data.Aeson.Types qualified as A
import TestImport

aesonRoundtrips :: forall a. (FromJSON a, ToJSON a, Eq a) => a -> Bool
aesonRoundtrips a =
let encoded = toJSON a
parsed = A.parse (parseJSON @a) encoded
roundTwo = fmap (toJSON @a) parsed
in -- The encoding is the same
A.Success encoded == roundTwo
-- AND the object itself is the same
&& A.Success a == parsed
2 changes: 2 additions & 0 deletions tests/Web/Slack/Experimental/Events/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ spec = describe "Types for Slack events" do
, "slackbotIm"
, "channel_left"
, "share_without_message"
, -- https://slack.com/help/articles/206819278-Send-emails-to-Slack
"email_message"
]
9 changes: 9 additions & 0 deletions tests/Web/Slack/Files/TypesSpec.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
{-# OPTIONS_GHC -Wno-orphans #-}
-- GHC told me to set it, and it compiles now 🤷
{-# OPTIONS_GHC -fconstraint-solver-iterations=10 #-}

module Web.Slack.Files.TypesSpec (spec) where

import JSONGolden
import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary (..))
import TestImport
import TestImport.Aeson
import Web.Slack.Files.Types

deriving via GenericArbitrary FileMode instance Arbitrary FileMode

spec :: Spec
spec = describe "Types for Slack files" do
describe "FileObject" do
prop "FileMode roundtrips" $ aesonRoundtrips @FileMode
describe "FromJSON" do
mapM_
(oneGoldenTestDecode @FileObject)
Expand Down
43 changes: 43 additions & 0 deletions tests/golden/SlackWebhookEvent/email_message.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
EventEventCallback
( EventCallback
{ eventId = EventId
{ unEventId = "Ev0123467899" }
, teamId = TeamId
{ unTeamId = "T012346789" }
, eventTime = MkSystemTime
{ systemSeconds = 1671217113
, systemNanoseconds = 0
}
, event = EventMessage
( MessageEvent
{ blocks = Nothing
, channel = ConversationId
{ unConversationId = "C012346789" }
, text = ""
, channelType = Channel
, files = Just
[ VisibleFileObject
( FileObjectVisible
{ id = FileId
{ unFileId = "F0123468789" }
, created = 2022-12-16 18:58:33 UTC
, name = "meow"
, title = "meow"
, mimetype = "text/html"
, urlPrivate = "https://example.com"
, isExternal = False
, size = 6212
, mode = Email
}
)
]
, user = UserId
{ unUserId = "USLACKBOT" }
, ts = "1671217113.292829"
, threadTs = Nothing
, appId = Nothing
, botId = Just "B012346789"
}
)
}
)
91 changes: 91 additions & 0 deletions tests/golden/SlackWebhookEvent/email_message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"token": "aaaa",
"team_id": "T012346789",
"api_app_id": "A012346789",
"event": {
"type": "message",
"text": "",
"files": [
{
"id": "F0123468789",
"created": 1671217113,
"timestamp": 1671217110,
"name": "meow",
"title": "meow",
"mimetype": "text/html",
"filetype": "email",
"pretty_type": "Email",
"user": "USLACKBOT",
"user_team": "T012346789",
"editable": true,
"size": 6212,
"mode": "email",
"is_external": false,
"external_type": "",
"is_public": true,
"public_url_shared": false,
"display_as_bot": true,
"username": "Email",
"url_private": "https://example.com",
"url_private_download": "https://example.com",
"permalink": "https://example.com",
"permalink_public": "https://example.com",
"subject": "meow",
"to": [
{
"address": "",
"name": "Email",
"original": "Email <>"
}
],
"from": [
{
"address": "addr@example.com",
"name": "meow",
"original": "meow <addr@example.com>"
}
],
"cc": [],
"attachments": [],
"original_attachment_count": 0,
"plain_text": "meow",
"preview": "meow",
"preview_plain_text": "meow",
"headers": {
"date": "Fri, 16 Dec 2022 18:58:30 +0000",
"in_reply_to": null,
"reply_to": null,
"message_id": "<meow@example.com>"
},
"has_more": false,
"sent_to_self": false,
"bot_id": "B012346789",
"has_rich_preview": false,
"file_access": "visible"
}
],
"upload": true,
"user": "USLACKBOT",
"display_as_bot": true,
"bot_id": "B012346789",
"ts": "1671217113.292829",
"channel": "C012346789",
"subtype": "file_share",
"event_ts": "1671217113.292829",
"channel_type": "channel"
},
"type": "event_callback",
"event_id": "Ev0123467899",
"event_time": 1671217113,
"authorizations": [
{
"enterprise_id": null,
"team_id": "T012346789",
"user_id": "U012346789",
"is_bot": true,
"is_enterprise_install": false
}
],
"is_ext_shared_channel": false,
"event_context": "aaaa"
}