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.Admin.Types.Node (Node, NodeType(NodeUser), pgNodeId)
37 import Gargantext.Database.Prelude (Cmd, runOpaQuery)
38 import Gargantext.Prelude
39 import Opaleye (restrict, (.==), Query)
40 import Opaleye.PGTypes (pgStrictText, pgInt4)
43 getRootId :: User -> Cmd err NodeId
45 maybeRoot <- head <$> getRoot u
47 Nothing -> panic "no root id"
48 Just r -> pure (_node_id r)
50 getRoot :: User -> Cmd err [Node HyperdataUser]
51 getRoot = runOpaQuery . selectRoot
54 getOrMkRoot :: (HasNodeError err)
56 -> Cmd err (UserId, RootId)
58 userId <- getUserId user
60 rootId' <- map _node_id <$> getRoot user
62 rootId'' <- case rootId' of
64 n -> case length n >= 2 of
65 True -> nodeError ManyNodeUsers
68 rootId <- maybe (nodeError NoRootFound) pure (head rootId'')
72 getOrMk_RootWithCorpus :: (HasNodeError err, MkCorpus a)
74 -> Either CorpusName [CorpusId]
76 -> Cmd err (UserId, RootId, CorpusId)
77 getOrMk_RootWithCorpus user cName c = do
78 (userId, rootId) <- getOrMkRoot user
79 corpusId'' <- if user == UserName userMaster
81 ns <- getCorporaWithParentId rootId
82 pure $ map _node_id ns
84 pure $ fromRight [] cName
86 corpusId' <- if corpusId'' /= []
89 c' <- mk (Just $ fromLeft "Default" cName) c rootId userId
90 _tId <- case head c' of
92 Just c'' -> insertDefaultNode NodeTexts c'' userId
95 corpusId <- maybe (nodeError NoCorpusFound) pure (head corpusId')
96 pure (userId, rootId, corpusId)
99 mkRoot :: HasNodeError err
105 -- udb <- getUserDb user
106 -- let uid = user_id udb
107 uid <- getUserId user
109 -- TODO ? Which name for user Node ?
113 False -> nodeError NegativeId
115 rs <- mkNodeWithParent NodeUser Nothing uid una
118 _ <- mkNodeWithParent NodeFolderPrivate (Just r) uid una
119 _ <- mkNodeWithParent NodeFolderShared (Just r) uid una
120 _ <- mkNodeWithParent NodeFolderPublic (Just r) uid una
125 selectRoot :: User -> Query NodeRead
126 selectRoot (UserName username) = proc () -> do
127 row <- queryNodeTable -< ()
128 users <- queryUserTable -< ()
129 restrict -< _node_typename row .== (pgInt4 $ nodeTypeId NodeUser)
130 restrict -< user_username users .== (pgStrictText username)
131 restrict -< _node_userId row .== (user_id users)
134 selectRoot (UserDBId uid) = proc () -> do
135 row <- queryNodeTable -< ()
136 restrict -< _node_typename row .== (pgInt4 $ nodeTypeId NodeUser)
137 restrict -< _node_userId row .== (pgInt4 uid)
140 selectRoot (RootId nid) =
142 row <- queryNodeTable -< ()
143 restrict -< _node_typename row .== (pgInt4 $ nodeTypeId NodeUser)
144 restrict -< _node_id row .== (pgNodeId nid)
146 selectRoot UserPublic = panic "No root for Public"