2019-03-20 11:08:36 +01:00
|
|
|
{- 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
|
|
|
|
( getEncodeRouteFed
|
|
|
|
, getEncodeRouteLocal
|
2019-03-28 22:08:30 +01:00
|
|
|
, decodeRouteLocal
|
2019-03-20 11:08:36 +01:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
2019-03-28 22:08:30 +01:00
|
|
|
import Control.Monad
|
|
|
|
import Data.Text.Encoding
|
|
|
|
import Network.HTTP.Types.URI
|
2019-03-20 11:08:36 +01:00
|
|
|
import Yesod.Core
|
|
|
|
import Yesod.Core.Handler
|
|
|
|
|
2019-03-28 22:08:30 +01:00
|
|
|
import qualified Data.Text as T
|
|
|
|
|
2019-03-20 11:08:36 +01:00
|
|
|
import Network.FedURI
|
|
|
|
|
|
|
|
getEncodeRouteFed :: MonadHandler m => m (Route (HandlerSite m) -> FedURI)
|
|
|
|
getEncodeRouteFed = toFed <$> getUrlRender
|
|
|
|
where
|
|
|
|
toFed renderUrl route =
|
|
|
|
case parseFedURI $ renderUrl route of
|
|
|
|
Left e -> error $ "getUrlRender produced invalid FedURI: " ++ e
|
|
|
|
Right u -> u
|
|
|
|
|
|
|
|
getEncodeRouteLocal :: MonadHandler m => m (Route (HandlerSite m) -> LocalURI)
|
|
|
|
getEncodeRouteLocal = (\ f -> snd . f2l . f) <$> getEncodeRouteFed
|
2019-03-28 22:08:30 +01:00
|
|
|
|
|
|
|
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
|