git-annex/Assistant/WebApp/Notifications.hs

87 lines
3 KiB
Haskell
Raw Normal View History

2012-07-31 05:11:32 +00:00
{- git-annex assistant webapp notifications
-
- Copyright 2012 Joey Hess <id@joeyh.name>
2012-07-31 05:11:32 +00:00
-
- Licensed under the GNU AGPL version 3 or higher.
2012-07-31 05:11:32 +00:00
-}
{-# LANGUAGE QuasiQuotes, TemplateHaskell, OverloadedStrings #-}
2012-07-31 05:11:32 +00:00
module Assistant.WebApp.Notifications where
import Assistant.Common
import Assistant.WebApp
import Assistant.WebApp.Types
2012-11-03 01:13:06 +00:00
import Assistant.DaemonStatus
2012-07-31 05:11:32 +00:00
import Utility.NotificationBroadcaster
import Utility.Yesod
import Utility.AuthToken
2012-07-31 05:11:32 +00:00
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Aeson.Types as Aeson
2012-07-31 05:11:32 +00:00
{- Add to any widget to make it auto-update using long polling.
-
- The widget should have a html element with an id=ident, which will be
- replaced when it's updated.
-
- The geturl route should return the notifier url to use for polling.
-
- ms_delay is how long to delay between AJAX updates
- ms_startdelay is how long to delay before updating with AJAX at the start
-}
autoUpdate :: Text -> Route WebApp -> Int -> Int -> Widget
autoUpdate tident geturl ms_delay ms_startdelay = do
let delay = Aeson.String (T.pack (show ms_delay))
let startdelay = Aeson.String (T.pack (show ms_startdelay))
let ident = Aeson.String tident
2012-07-31 05:24:49 +00:00
$(widgetFile "notifications/longpolling")
2012-07-31 05:11:32 +00:00
{- Notifier urls are requested by the javascript, to avoid allocation
- of NotificationIds when noscript pages are loaded. This constructs a
- notifier url for a given Route and NotificationBroadcaster.
-}
2012-11-03 01:13:06 +00:00
notifierUrl :: (NotificationId -> Route WebApp) -> Assistant NotificationBroadcaster -> Handler RepPlain
notifierUrl route broadcaster = do
(urlbits, _params) <- renderRoute . route <$> newNotifier broadcaster
2012-07-31 05:11:32 +00:00
webapp <- getYesod
return $ RepPlain $ toContent $ T.concat
[ "/"
, T.intercalate "/" urlbits
, "?auth="
, fromAuthToken (authToken webapp)
2012-07-31 05:11:32 +00:00
]
getNotifierTransfersR :: Handler RepPlain
2012-11-03 01:13:06 +00:00
getNotifierTransfersR = notifierUrl TransfersR getTransferBroadcaster
2012-07-31 05:11:32 +00:00
getNotifierSideBarR :: Handler RepPlain
2012-11-03 01:13:06 +00:00
getNotifierSideBarR = notifierUrl SideBarR getAlertBroadcaster
2012-11-13 21:50:54 +00:00
getNotifierRepoListR :: RepoSelector -> Handler RepPlain
getNotifierRepoListR reposelector = notifierUrl route getRepoListBroadcaster
where
route nid = RepoListR nid reposelector
2012-11-13 21:50:54 +00:00
getNotifierGlobalRedirR :: Handler RepPlain
getNotifierGlobalRedirR = notifierUrl GlobalRedirR getGlobalRedirBroadcaster
2012-11-03 01:13:06 +00:00
getTransferBroadcaster :: Assistant NotificationBroadcaster
getTransferBroadcaster = transferNotifier <$> getDaemonStatus
getAlertBroadcaster :: Assistant NotificationBroadcaster
getAlertBroadcaster = alertNotifier <$> getDaemonStatus
2012-11-13 21:50:54 +00:00
getRepoListBroadcaster :: Assistant NotificationBroadcaster
getRepoListBroadcaster = syncRemotesNotifier <$> getDaemonStatus
getGlobalRedirBroadcaster :: Assistant NotificationBroadcaster
getGlobalRedirBroadcaster = globalRedirNotifier <$> getDaemonStatus
2013-11-25 19:14:34 +00:00
getGlobalRedirR :: NotificationId -> Handler RepPlain
getGlobalRedirR nid = do
waitNotifier getGlobalRedirBroadcaster nid
2013-11-25 19:14:34 +00:00
maybe (getGlobalRedirR nid) (return . RepPlain . toContent . T.pack)
=<< globalRedirUrl <$> liftAssistant getDaemonStatus