summaryrefslogtreecommitdiff
path: root/fig-monitor-twitch/src/Fig/Monitor/Twitch.hs
blob: 497e123abe33998f441a8811ca844ec50964c304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{-# Language ApplicativeDo #-}

module Fig.Monitor.Twitch
  ( twitchChatbot
  , twitchEventMonitor
  , twitchChannelLiveChecker
  , userTokenRedirectServer
  ) where

import Fig.Prelude

import qualified Data.Text.Lazy as Text.Lazy

import Data.Default.Class (def)

import Network.Wai.Handler.Warp (setPort)
import qualified Web.Scotty as Scotty

import Fig.Monitor.Twitch.Utils
import Fig.Monitor.Twitch.LiveChecker
import Fig.Monitor.Twitch.EventMonitor
import Fig.Monitor.Twitch.Chatbot

userTokenRedirectServer :: Config -> Bool -> IO ()
userTokenRedirectServer cfg rw = do
  log "Starting token redirect server on port 4444"
  Scotty.scottyOpts opts do
    Scotty.get "/" do
      Scotty.html $ mconcat
        [ "<a href=\"https://id.twitch.tv/oauth2/authorize?response_type=token"
        , "&client_id=", Text.Lazy.fromStrict cfg.clientId
        , "&redirect_uri=http://localhost:4444"
        , "&scope=", Text.Lazy.replace ":" "%3A" $ Text.Lazy.intercalate "+" scopes
        , "\">Authenticate</a>"
        ]
  where
    opts = Scotty.Options
      { Scotty.verbose = 0
      , Scotty.settings = setPort 4444 (Scotty.settings def)
      }
    scopes = if rw then scopesReadWrite else scopesReadOnly
    scopesReadWrite =
      [ "channel:manage:polls"
      , "channel:manage:predictions"
      , "channel:manage:redemptions"
      , "channel:manage:vips"
      , "channel:read:polls"
      , "channel:read:predictions"
      , "channel:read:redemptions"
      , "channel:read:subscriptions"
      , "channel:read:vips"
      , "channel:moderate"
      , "moderator:read:followers"
      , "moderator:read:chatters"
      , "moderator:manage:shoutouts"
      , "chat:edit"
      , "chat:read"
      , "bits:read"
      ]
    scopesReadOnly =
      [
      ]