mirror of
https://code.sup39.dev/repos/Wqawg
synced 2025-01-07 08:36:47 +09:00
91 lines
3.1 KiB
Haskell
91 lines
3.1 KiB
Haskell
|
{- This file is part of Vervis.
|
||
|
-
|
||
|
- Written in 2016 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 Vervis.Role
|
||
|
( getRepoRoleGraph
|
||
|
, getProjectRoleGraph
|
||
|
)
|
||
|
where
|
||
|
|
||
|
import Prelude
|
||
|
|
||
|
import Control.Arrow (second, (&&&), (***))
|
||
|
import Data.Graph.Inductive.Graph (mkGraph)
|
||
|
import Data.Graph.Inductive.PatriciaTree (Gr)
|
||
|
import Data.Tuple (swap)
|
||
|
import Database.Esqueleto
|
||
|
import Yesod.Persist.Core (runDB)
|
||
|
|
||
|
import qualified Data.HashMap.Lazy as M (fromList, lookup)
|
||
|
import qualified Database.Persist as P
|
||
|
|
||
|
import Vervis.Foundation
|
||
|
import Vervis.Model
|
||
|
import Vervis.Model.Ident
|
||
|
|
||
|
getRepoRoleGraph :: AppDB SharerId -> Handler (Gr RlIdent ())
|
||
|
getRepoRoleGraph getsid = do
|
||
|
(roles, inhs) <- runDB $ do
|
||
|
sid <- getsid
|
||
|
rrs <- P.selectList [RepoRoleSharer P.==. sid] []
|
||
|
rrhs <- select $ from $ \ (rr `InnerJoin` rrh) -> do
|
||
|
on $ rr ^. RepoRoleId ==. rrh ^. RepoRoleInheritParent
|
||
|
where_ $ rr ^. RepoRoleSharer ==. val sid
|
||
|
return rrh
|
||
|
return (rrs, rrhs)
|
||
|
let numbered = zip [1..] roles
|
||
|
nodes = map (second $ repoRoleIdent . entityVal) numbered
|
||
|
nodeMap = M.fromList $ map (swap . second entityKey) numbered
|
||
|
rridToNode rrid =
|
||
|
case M.lookup rrid nodeMap of
|
||
|
Nothing -> error "Role graph: Node not found in node map"
|
||
|
Just n -> n
|
||
|
edges =
|
||
|
map
|
||
|
( (\ (c, p) -> (c, p, ()))
|
||
|
. (rridToNode *** rridToNode)
|
||
|
. (repoRoleInheritChild &&& repoRoleInheritParent)
|
||
|
. entityVal
|
||
|
)
|
||
|
inhs
|
||
|
return $ mkGraph nodes edges
|
||
|
|
||
|
getProjectRoleGraph :: AppDB SharerId -> Handler (Gr RlIdent ())
|
||
|
getProjectRoleGraph getsid = do
|
||
|
(roles, inhs) <- runDB $ do
|
||
|
sid <- getsid
|
||
|
prs <- P.selectList [ProjectRoleSharer P.==. sid] []
|
||
|
prhs <- select $ from $ \ (pr `InnerJoin` prh) -> do
|
||
|
on $ pr ^. ProjectRoleId ==. prh ^. ProjectRoleInheritParent
|
||
|
where_ $ pr ^. ProjectRoleSharer ==. val sid
|
||
|
return prh
|
||
|
return (prs, prhs)
|
||
|
let numbered = zip [1..] roles
|
||
|
nodes = map (second $ projectRoleIdent . entityVal) numbered
|
||
|
nodeMap = M.fromList $ map (swap . second entityKey) numbered
|
||
|
pridToNode prid =
|
||
|
case M.lookup prid nodeMap of
|
||
|
Nothing -> error "Role graph: Node not found in node map"
|
||
|
Just n -> n
|
||
|
edges =
|
||
|
map
|
||
|
( (\ (c, p) -> (c, p, ()))
|
||
|
. (pridToNode *** pridToNode)
|
||
|
. (projectRoleInheritChild &&& projectRoleInheritParent)
|
||
|
. entityVal
|
||
|
)
|
||
|
inhs
|
||
|
return $ mkGraph nodes edges
|