Vervis/src/Yesod/FedURI.hs

94 lines
3 KiB
Haskell
Raw Normal View History

{- This file is part of Vervis.
-
- Written 2019 by fr33domlover <fr33domlover@riseup.net>.
-
- Copying is an act of love. Please copy, reuse and share.
-
- The author(s) have dedicated all copyright and related and neighboring
- rights to this software to the public domain worldwide. This software is
- distributed without any warranty.
-
- You should have received a copy of the CC0 Public Domain Dedication along
- with this software. If not, see
- <http://creativecommons.org/publicdomain/zero/1.0/>.
-}
module Yesod.FedURI
( getEncodeRouteLocal
, getEncodeRouteHome
, getEncodeRouteFed
, decodeRouteLocal
, getEncodeRoutePageLocal
, getEncodeRoutePageHome
, getEncodeRoutePageFed
)
where
import Prelude
import Control.Monad
import Data.Text (Text)
import Data.Text.Encoding
import Network.HTTP.Types.URI
import Yesod.Core
import Yesod.Core.Handler
import qualified Data.Text as T
import Network.FedURI
import Yesod.Paginate.Local
getEncodeRouteLocal :: MonadHandler m => m (Route (HandlerSite m) -> LocalURI)
getEncodeRouteLocal = (\ f -> snd . f2l . f) <$> getEncodeRouteHome
getEncodeRouteHome :: MonadHandler m => m (Route (HandlerSite m) -> FedURI)
getEncodeRouteHome = toFed <$> getUrlRender
where
toFed renderUrl route =
case parseFedURI $ renderUrl route of
Left e -> error $ "getUrlRender produced invalid FedURI: " ++ e
Right u -> u
getEncodeRouteFed :: MonadHandler m => m (Text -> Route (HandlerSite m) -> FedURI)
getEncodeRouteFed = toFed <$> getUrlRender
where
toFed renderUrl host route =
case parseFedURI $ renderUrl route of
Left e -> error $ "getUrlRender produced invalid FedURI: " ++ e
Right u -> u { furiHost = host }
decodeRouteLocal :: ParseRoute site => LocalURI -> Maybe (Route site)
decodeRouteLocal =
parseRoute . (,[]) . decodePathSegments . encodeUtf8 . luriPath <=< noFrag
where
noFrag lu =
if T.null $ luriFragment lu
then Just lu
else Nothing
getEncodeRoutePageLocal
:: (MonadHandler m, YesodPaginate (HandlerSite m))
=> m (Route (HandlerSite m) -> Int -> LocalPageURI)
getEncodeRoutePageLocal = do
encodeRouteLocal <- getEncodeRouteLocal
param <- getsYesod sitePageParamName
return $ \ route page -> LocalPageURI (encodeRouteLocal route) param page
getEncodeRoutePageHome
:: (MonadHandler m, YesodPaginate (HandlerSite m))
=> m (Route (HandlerSite m) -> Int -> FedPageURI)
getEncodeRoutePageHome = do
encodeRouteHome <- getEncodeRouteHome
param <- getsYesod sitePageParamName
return $ \ route page -> FedPageURI (encodeRouteHome route) param page
getEncodeRoutePageFed
:: (MonadHandler m, YesodPaginate (HandlerSite m))
=> m (Text -> Route (HandlerSite m) -> Int -> FedPageURI)
getEncodeRoutePageFed = do
encodeRouteFed <- getEncodeRouteFed
param <- getsYesod sitePageParamName
return $
\ host route page -> FedPageURI (encodeRouteFed host route) param page