2 Module : Gargantext.Database.Root
3 Description : Main requests to get root of users
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
11 {-# LANGUAGE Arrows #-}
13 module Gargantext.Database.Query.Tree.Root
16 import Control.Arrow (returnA)
17 import Data.Either (Either, fromLeft, fromRight)
18 import Gargantext.Core.Types.Individu (User(..))
19 import Gargantext.Core.Types.Main (CorpusName)
20 import Gargantext.Database.Action.Node
21 import Gargantext.Database.Action.User (getUserId, getUsername)
22 import Gargantext.Database.Admin.Config (nodeTypeId, userMaster)
23 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataUser)
24 import Gargantext.Database.Admin.Types.Node
25 import Gargantext.Database.Prelude (Cmd, runOpaQuery)
26 import Gargantext.Database.Query.Table.Node
27 import Gargantext.Database.Query.Table.Node.Error
28 import Gargantext.Database.Query.Table.User (queryUserTable, UserPoly(..))
29 import Gargantext.Database.Schema.Node (NodePoly(..), NodeRead)
30 import Gargantext.Database.Schema.Node (queryNodeTable)
31 import Gargantext.Prelude
32 import Opaleye (restrict, (.==), Query)
33 import Opaleye.PGTypes (pgStrictText, pgInt4)
36 getRootId :: (HasNodeError err) => User -> Cmd err NodeId
38 maybeRoot <- head <$> getRoot u
40 Nothing -> nodeError $ NodeError "[G.D.Q.T.R.getRootId] No root id"
41 Just r -> pure (_node_id r)
43 getRoot :: User -> Cmd err [Node HyperdataUser]
44 getRoot = runOpaQuery . selectRoot
46 getOrMkRoot :: (HasNodeError err)
48 -> Cmd err (UserId, RootId)
50 userId <- getUserId user
52 rootId' <- map _node_id <$> getRoot user
54 rootId'' <- case rootId' of
56 n -> case length n >= 2 of
57 True -> nodeError ManyNodeUsers
60 rootId <- maybe (nodeError NoRootFound) pure (head rootId'')
64 getOrMk_RootWithCorpus :: (HasNodeError err, MkCorpus a)
66 -> Either CorpusName [CorpusId]
68 -> Cmd err (UserId, RootId, CorpusId)
69 getOrMk_RootWithCorpus user cName c = do
70 (userId, rootId) <- getOrMkRoot user
71 corpusId'' <- if user == UserName userMaster
73 ns <- getCorporaWithParentId rootId
74 pure $ map _node_id ns
76 pure $ fromRight [] cName
78 corpusId' <- if corpusId'' /= []
81 c' <- mk (Just $ fromLeft "Default" cName) c rootId userId
82 _tId <- case head c' of
83 Nothing -> nodeError $ NodeError "[G.D.Q.T.Root.getOrMk...] mk Corpus failed"
84 Just c'' -> insertDefaultNode NodeTexts c'' userId
87 corpusId <- maybe (nodeError NoCorpusFound) pure (head corpusId')
88 pure (userId, rootId, corpusId)
91 mkRoot :: HasNodeError err
97 -- udb <- getUserDb user
98 -- let uid = user_id udb
101 -- TODO ? Which name for user Node ?
102 una <- getUsername user
105 False -> nodeError NegativeId
107 rs <- mkNodeWithParent NodeUser Nothing uid una
110 _ <- insertNode NodeFolderPrivate Nothing Nothing r uid
111 _ <- insertNode NodeFolderShared Nothing Nothing r uid
112 _ <- insertNode NodeFolderPublic Nothing Nothing r uid
117 selectRoot :: User -> Query NodeRead
118 selectRoot (UserName username) = proc () -> do
119 row <- queryNodeTable -< ()
120 users <- queryUserTable -< ()
121 restrict -< _node_typename row .== (pgInt4 $ nodeTypeId NodeUser)
122 restrict -< user_username users .== (pgStrictText username)
123 restrict -< _node_userId row .== (user_id users)
126 selectRoot (UserDBId uid) = proc () -> do
127 row <- queryNodeTable -< ()
128 restrict -< _node_typename row .== (pgInt4 $ nodeTypeId NodeUser)
129 restrict -< _node_userId row .== (pgInt4 uid)
132 selectRoot (RootId nid) =
134 row <- queryNodeTable -< ()
135 restrict -< _node_typename row .== (pgInt4 $ nodeTypeId NodeUser)
136 restrict -< _node_id row .== (pgNodeId nid)
138 selectRoot UserPublic = panic {-nodeError $ NodeError-} "[G.D.Q.T.Root.selectRoot] No root for Public"