<commit_before>module Protocols.Slack.Request
 ( Request (..)
 ) where

-- Haskell platform libraries
--
import Control.Applicative ((<$>), (<*>))

-- foreign libraries

import Happstack.Server (FromData (..), body, look)

data Request = Request { secretToken :: String
                       , channelName :: String
                       , timeStamp   :: String
                       , userName    :: String
                       , text        :: String
                       } deriving (Eq, Show)

instance FromData Request where
  fromData = Request <$> bl "token"
                     <*> bl "channel_name"
                     <*> bl "timestamp"
                     <*> bl "user_name"
                     <*> bl "text"
    where bl = body . look<commit_msg>moved authorization into data request<commit_after>module Protocols.Slack.Request
 ( Request (..)
 ) where

-- Haskell platform libraries
--
import           Control.Applicative    ((<$>), (<*>))
import qualified Control.Monad.IO.Class as IO
import           System.Environment     (getEnv)

-- foreign libraries

import Happstack.Server (FromData (..), RqData, body, look)

-- native libraries

import Settings (slackTokenEnvVar)

data Request = Unauthorized
             | Authorized { userName  :: String
                          , text      :: String
                          , timeStamp :: String
                          } deriving (Eq, Show)

instance IO.MonadIO RqData where
  liftIO = IO.liftIO

instance FromData Request where
  fromData = do
    can <- isAuthorized
    if can then newRequest else denyRequest

newRequest :: RqData Request
newRequest = Authorized <$> bl "user_name"
                        <*> bl "text"
                        <*> bl "timestamp"

denyRequest :: RqData Request
denyRequest = return Unauthorized

isAuthorized :: RqData Bool
isAuthorized = do
    yourToken <- bl "token"
    myToken   <- IO.liftIO $ getEnv slackTokenEnvVar
    return $ yourToken == myToken

bl :: String -> RqData String
bl = body . look
{'fraction_non_alphanumeric': 0.09950495049504951, 'fraction_numerical': 0.0, 'mean_word_length': 1.8666666666666667, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2465572', 'n_tokens_mistral': 557, 'n_tokens_neox': 527, 'n_words': 157}