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 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
14 {-# LANGUAGE Arrows #-}
15 {-# LANGUAGE ConstraintKinds #-}
16 {-# LANGUAGE FunctionalDependencies #-}
17 {-# LANGUAGE TemplateHaskell #-}
19 module Gargantext.Database.Query.Tree.Root
22 import Data.Either (Either, fromLeft, fromRight)
23 import Control.Arrow (returnA)
24 import Gargantext.Core.Types.Main (CorpusName)
25 import Gargantext.Core.Types.Individu (User(..))
26 import Gargantext.Database.Admin.Config (nodeTypeId, userMaster)
27 import Gargantext.Database.Query.Table.Node.Error
28 import Gargantext.Database.Admin.Types.Node
29 import Gargantext.Database.Query.Table.Node
30 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataUser)
31 import Gargantext.Database.Action.Flow.Utils (getUserId)
32 import Gargantext.Database.Schema.Node (NodePoly(..), NodeRead)
33 import Gargantext.Database.Schema.Node (queryNodeTable)
34 import Gargantext.Database.Action.Node
35 import Gargantext.Database.Query.Table.User (queryUserTable, UserPoly(..))
36 import Gargantext.Database.Prelude (Cmd, runOpaQuery)
37 import Gargantext.Prelude
38 import Opaleye (restrict, (.==), Query)
39 import Opaleye.PGTypes (pgStrictText, pgInt4)
42 getRootId :: (HasNodeError err) => User -> Cmd err NodeId
44 maybeRoot <- head <$> getRoot u
46 Nothing -> nodeError $ NodeError "[G.D.Q.T.R.getRootId] No root id"
47 Just r -> pure (_node_id r)
49 getRoot :: User -> Cmd err [Node HyperdataUser]
50 getRoot = runOpaQuery . selectRoot
53 getOrMkRoot :: (HasNodeError err)
55 -> Cmd err (UserId, RootId)
57 userId <- getUserId user
59 rootId' <- map _node_id <$> getRoot user
61 rootId'' <- case rootId' of
63 n -> case length n >= 2 of
64 True -> nodeError ManyNodeUsers
67 rootId <- maybe (nodeError NoRootFound) pure (head rootId'')
71 getOrMk_RootWithCorpus :: (HasNodeError err, MkCorpus a)
73 -> Either CorpusName [CorpusId]
75 -> Cmd err (UserId, RootId, CorpusId)
76 getOrMk_RootWithCorpus user cName c = do
77 (userId, rootId) <- getOrMkRoot user
78 corpusId'' <- if user == UserName userMaster
80 ns <- getCorporaWithParentId rootId
81 pure $ map _node_id ns
83 pure $ fromRight [] cName
85 corpusId' <- if corpusId'' /= []
88 c' <- mk (Just $ fromLeft "Default" cName) c rootId userId
89 _tId <- case head c' of
90 Nothing -> nodeError $ NodeError "[G.D.Q.T.Root.getOrMk...] mk Corpus failed"
91 Just c'' -> insertDefaultNode NodeTexts c'' userId
94 corpusId <- maybe (nodeError NoCorpusFound) pure (head corpusId')
95 pure (userId, rootId, corpusId)
98 mkRoot :: HasNodeError err
104 -- udb <- getUserDb user
105 -- let uid = user_id udb
106 uid <- getUserId user
108 -- TODO ? Which name for user Node ?
112 False -> nodeError NegativeId
114 rs <- mkNodeWithParent NodeUser Nothing uid una
117 _ <- insertNode NodeFolderPrivate Nothing Nothing r uid
118 _ <- insertNode NodeFolderShared Nothing Nothing r uid
119 _ <- insertNode NodeFolderPublic Nothing Nothing r uid
124 selectRoot :: User -> Query NodeRead
125 selectRoot (UserName username) = proc () -> do
126 row <- queryNodeTable -< ()
127 users <- queryUserTable -< ()
128 restrict -< _node_typename row .== (pgInt4 $ nodeTypeId NodeUser)
129 restrict -< user_username users .== (pgStrictText username)
130 restrict -< _node_userId row .== (user_id users)
133 selectRoot (UserDBId uid) = proc () -> do
134 row <- queryNodeTable -< ()
135 restrict -< _node_typename row .== (pgInt4 $ nodeTypeId NodeUser)
136 restrict -< _node_userId row .== (pgInt4 uid)
139 selectRoot (RootId nid) =
141 row <- queryNodeTable -< ()
142 restrict -< _node_typename row .== (pgInt4 $ nodeTypeId NodeUser)
143 restrict -< _node_id row .== (pgNodeId nid)
145 selectRoot UserPublic = panic {-nodeError $ NodeError-} "[G.D.Q.T.Root.selectRoot] No root for Public"