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 Control.Arrow (returnA)
23 import Data.Either (Either, fromLeft, fromRight)
24 import Gargantext.Core.Types.Individu (User(..))
25 import Gargantext.Core.Types.Main (CorpusName)
26 import Gargantext.Database.Action.Node
27 import Gargantext.Database.Action.User (getUserId, getUsername)
28 import Gargantext.Database.Admin.Config (nodeTypeId, userMaster)
29 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataUser)
30 import Gargantext.Database.Admin.Types.Node
31 import Gargantext.Database.Prelude (Cmd, runOpaQuery)
32 import Gargantext.Database.Query.Table.Node
33 import Gargantext.Database.Query.Table.Node.Error
34 import Gargantext.Database.Query.Table.User (queryUserTable, UserPoly(..))
35 import Gargantext.Database.Schema.Node (NodePoly(..), NodeRead)
36 import Gargantext.Database.Schema.Node (queryNodeTable)
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
52 getOrMkRoot :: (HasNodeError err)
54 -> Cmd err (UserId, RootId)
56 userId <- getUserId user
58 rootId' <- map _node_id <$> getRoot user
60 rootId'' <- case rootId' of
62 n -> case length n >= 2 of
63 True -> nodeError ManyNodeUsers
66 rootId <- maybe (nodeError NoRootFound) pure (head rootId'')
70 getOrMk_RootWithCorpus :: (HasNodeError err, MkCorpus a)
72 -> Either CorpusName [CorpusId]
74 -> Cmd err (UserId, RootId, CorpusId)
75 getOrMk_RootWithCorpus user cName c = do
76 (userId, rootId) <- getOrMkRoot user
77 corpusId'' <- if user == UserName userMaster
79 ns <- getCorporaWithParentId rootId
80 pure $ map _node_id ns
82 pure $ fromRight [] cName
84 corpusId' <- if corpusId'' /= []
87 c' <- mk (Just $ fromLeft "Default" cName) c rootId userId
88 _tId <- case head c' of
89 Nothing -> nodeError $ NodeError "[G.D.Q.T.Root.getOrMk...] mk Corpus failed"
90 Just c'' -> insertDefaultNode NodeTexts c'' userId
93 corpusId <- maybe (nodeError NoCorpusFound) pure (head corpusId')
94 pure (userId, rootId, corpusId)
97 mkRoot :: HasNodeError err
103 -- udb <- getUserDb user
104 -- let uid = user_id udb
105 uid <- getUserId user
107 -- TODO ? Which name for user Node ?
108 una <- getUsername user
111 False -> nodeError NegativeId
113 rs <- mkNodeWithParent NodeUser Nothing uid una
116 _ <- insertNode NodeFolderPrivate Nothing Nothing r uid
117 _ <- insertNode NodeFolderShared Nothing Nothing r uid
118 _ <- insertNode NodeFolderPublic Nothing Nothing r uid
123 selectRoot :: User -> Query NodeRead
124 selectRoot (UserName username) = proc () -> do
125 row <- queryNodeTable -< ()
126 users <- queryUserTable -< ()
127 restrict -< _node_typename row .== (pgInt4 $ nodeTypeId NodeUser)
128 restrict -< user_username users .== (pgStrictText username)
129 restrict -< _node_userId row .== (user_id users)
132 selectRoot (UserDBId uid) = proc () -> do
133 row <- queryNodeTable -< ()
134 restrict -< _node_typename row .== (pgInt4 $ nodeTypeId NodeUser)
135 restrict -< _node_userId row .== (pgInt4 uid)
138 selectRoot (RootId nid) =
140 row <- queryNodeTable -< ()
141 restrict -< _node_typename row .== (pgInt4 $ nodeTypeId NodeUser)
142 restrict -< _node_id row .== (pgNodeId nid)
144 selectRoot UserPublic = panic {-nodeError $ NodeError-} "[G.D.Q.T.Root.selectRoot] No root for Public"