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
|
2019-07-23 15:59:48 +02:00
|
|
|
( SiteFedURI (..)
|
|
|
|
, getEncodeRouteLocal
|
2019-05-21 01:51:06 +02:00
|
|
|
, getEncodeRouteHome
|
|
|
|
, getEncodeRouteFed
|
2019-03-28 22:08:30 +01:00
|
|
|
, decodeRouteLocal
|
2019-05-21 01:51:06 +02:00
|
|
|
, getEncodeRoutePageLocal
|
|
|
|
, getEncodeRoutePageHome
|
|
|
|
, getEncodeRoutePageFed
|
2019-03-20 11:08:36 +01:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2019-03-28 22:08:30 +01:00
|
|
|
import Data.Text.Encoding
|
|
|
|
import Network.HTTP.Types.URI
|
2019-03-20 11:08:36 +01:00
|
|
|
import Yesod.Core
|
|
|
|
|
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
|
2019-06-14 19:21:38 +02:00
|
|
|
import Yesod.MonadSite
|
2019-03-20 11:08:36 +01:00
|
|
|
|
2019-05-21 01:51:06 +02:00
|
|
|
import Yesod.Paginate.Local
|
|
|
|
|
2019-07-23 15:59:48 +02:00
|
|
|
class UriMode (SiteFedURIMode site) => SiteFedURI site where
|
|
|
|
type SiteFedURIMode site
|
2019-05-21 01:51:06 +02:00
|
|
|
|
2019-07-23 15:59:48 +02:00
|
|
|
getEncodeRouteHome
|
|
|
|
:: (MonadSite m, SiteEnv m ~ site, SiteFedURI site)
|
|
|
|
=> m (Route site -> ObjURI (SiteFedURIMode site))
|
2019-06-14 19:21:38 +02:00
|
|
|
getEncodeRouteHome = toFed <$> askUrlRender
|
2019-03-20 11:08:36 +01:00
|
|
|
where
|
|
|
|
toFed renderUrl route =
|
2019-07-23 15:59:48 +02:00
|
|
|
case parseObjURI $ renderUrl route of
|
|
|
|
Left e -> error $ "askUrlRender produced invalid ObjURI: " ++ e
|
2019-03-20 11:08:36 +01:00
|
|
|
Right u -> u
|
|
|
|
|
2019-07-23 15:59:48 +02:00
|
|
|
getEncodeRouteLocal
|
|
|
|
:: (MonadSite m, SiteEnv m ~ site, SiteFedURI site)
|
|
|
|
=> m (Route site -> LocalURI)
|
|
|
|
getEncodeRouteLocal = (objUriLocal .) <$> getEncodeRouteHome
|
|
|
|
|
|
|
|
getEncodeRouteFed
|
|
|
|
:: ( MonadSite m
|
|
|
|
, SiteEnv m ~ site
|
|
|
|
, SiteFedURI site
|
|
|
|
, SiteFedURIMode site ~ u
|
|
|
|
)
|
|
|
|
=> m (Authority u -> Route site -> ObjURI u)
|
|
|
|
getEncodeRouteFed = (\ f a -> ObjURI a . f) <$> getEncodeRouteLocal
|
2019-03-28 22:08:30 +01:00
|
|
|
|
|
|
|
decodeRouteLocal :: ParseRoute site => LocalURI -> Maybe (Route site)
|
|
|
|
decodeRouteLocal =
|
2019-07-23 15:59:48 +02:00
|
|
|
parseRoute . (,[]) . decodePathSegments . encodeUtf8 . localUriPath
|
2019-05-21 01:51:06 +02:00
|
|
|
|
|
|
|
getEncodeRoutePageLocal
|
2019-07-23 15:59:48 +02:00
|
|
|
:: (MonadSite m, SiteEnv m ~ site, SiteFedURI site, YesodPaginate site)
|
|
|
|
=> m (Route site -> Int -> LocalPageURI)
|
|
|
|
getEncodeRoutePageLocal =
|
|
|
|
(\ f r n -> pageUriLocal $ f r n) <$> getEncodeRoutePageHome
|
2019-05-21 01:51:06 +02:00
|
|
|
|
|
|
|
getEncodeRoutePageHome
|
2019-07-23 15:59:48 +02:00
|
|
|
:: (MonadSite m, SiteEnv m ~ site, SiteFedURI site, YesodPaginate site)
|
|
|
|
=> m (Route site -> Int -> PageURI (SiteFedURIMode site))
|
2019-05-21 01:51:06 +02:00
|
|
|
getEncodeRoutePageHome = do
|
|
|
|
encodeRouteHome <- getEncodeRouteHome
|
2019-06-14 19:21:38 +02:00
|
|
|
param <- asksSite sitePageParamName
|
2019-07-23 15:59:48 +02:00
|
|
|
return $ \ route page ->
|
|
|
|
let ObjURI a l = encodeRouteHome route
|
|
|
|
in PageURI a $ LocalPageURI l param page
|
2019-05-21 01:51:06 +02:00
|
|
|
|
|
|
|
getEncodeRoutePageFed
|
2019-07-23 15:59:48 +02:00
|
|
|
:: ( MonadSite m
|
|
|
|
, SiteEnv m ~ site
|
|
|
|
, SiteFedURI site
|
|
|
|
, YesodPaginate site
|
|
|
|
, SiteFedURIMode site ~ u
|
|
|
|
)
|
|
|
|
=> m (Authority u -> Route site -> Int -> PageURI u)
|
|
|
|
getEncodeRoutePageFed =
|
|
|
|
(\ f a r n -> PageURI a $ f r n) <$> getEncodeRoutePageLocal
|