2 Module : Gargantext.Database.Schema.Node
3 Description : Main requests of Node to the database
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 DeriveGeneric #-}
16 {-# LANGUAGE ConstraintKinds #-}
17 {-# LANGUAGE FlexibleContexts #-}
18 {-# LANGUAGE FlexibleInstances #-}
19 {-# LANGUAGE FunctionalDependencies #-}
20 {-# LANGUAGE OverloadedStrings #-}
21 {-# LANGUAGE MultiParamTypeClasses #-}
22 {-# LANGUAGE NoImplicitPrelude #-}
23 {-# LANGUAGE RankNTypes #-}
24 {-# LANGUAGE TemplateHaskell #-}
26 module Gargantext.Database.Schema.Node where
28 import Control.Arrow (returnA)
29 import Control.Lens (Prism', set, view, (#), (^?))
30 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
31 import Control.Monad.Error.Class (MonadError(..))
33 import Data.Maybe (Maybe(..), fromMaybe)
34 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
35 import Data.Text (Text, pack)
36 import Database.PostgreSQL.Simple.FromField (FromField, fromField)
37 import GHC.Int (Int64)
38 import Gargantext.Core (Lang(..))
39 import Gargantext.Core.Types
40 import Gargantext.Core.Types.Individu (Username)
41 import Gargantext.Database.Config (nodeTypeId)
42 import Gargantext.Database.Queries.Filter (limit', offset')
43 import Gargantext.Database.Types.Node (NodeType, defaultCorpus, Hyperdata)
44 import Gargantext.Database.Utils
45 import Gargantext.Prelude hiding (sum, head)
46 import Opaleye hiding (FromField)
47 import Opaleye.Internal.QueryArr (Query)
48 import Prelude hiding (null, id, map, sum)
50 ------------------------------------------------------------------------
52 data NodeError = NoListFound
65 class HasNodeError e where
66 _NodeError :: Prism' e NodeError
68 nodeError :: (MonadError e m, HasNodeError e) => NodeError -> m a
69 nodeError ne = throwError $ _NodeError # ne
71 catchNodeError :: (MonadError e m, HasNodeError e) => m a -> (NodeError -> m a) -> m a
72 catchNodeError f g = catchError f (\e -> maybe (throwError e) g (e ^? _NodeError))
74 ------------------------------------------------------------------------
75 instance FromField HyperdataAny where
76 fromField = fromField'
78 instance FromField HyperdataCorpus
80 fromField = fromField'
82 instance FromField HyperdataDocument
84 fromField = fromField'
86 instance FromField HyperdataDocumentV3
88 fromField = fromField'
90 instance FromField HyperdataUser
92 fromField = fromField'
94 instance FromField HyperdataList
96 fromField = fromField'
98 instance FromField HyperdataGraph
100 fromField = fromField'
102 instance FromField HyperdataAnnuaire
104 fromField = fromField'
105 ------------------------------------------------------------------------
106 instance QueryRunnerColumnDefault PGJsonb HyperdataAny
108 queryRunnerColumnDefault = fieldQueryRunnerColumn
110 instance QueryRunnerColumnDefault PGJsonb HyperdataDocument
112 queryRunnerColumnDefault = fieldQueryRunnerColumn
114 instance QueryRunnerColumnDefault PGJsonb HyperdataDocumentV3
116 queryRunnerColumnDefault = fieldQueryRunnerColumn
118 instance QueryRunnerColumnDefault PGJsonb HyperdataCorpus
120 queryRunnerColumnDefault = fieldQueryRunnerColumn
122 instance QueryRunnerColumnDefault PGJsonb HyperdataUser
124 queryRunnerColumnDefault = fieldQueryRunnerColumn
126 instance QueryRunnerColumnDefault PGJsonb HyperdataList
128 queryRunnerColumnDefault = fieldQueryRunnerColumn
130 instance QueryRunnerColumnDefault PGJsonb HyperdataGraph
132 queryRunnerColumnDefault = fieldQueryRunnerColumn
134 instance QueryRunnerColumnDefault PGJsonb HyperdataAnnuaire
136 queryRunnerColumnDefault = fieldQueryRunnerColumn
138 instance QueryRunnerColumnDefault PGTSVector (Maybe TSVector)
140 queryRunnerColumnDefault = fieldQueryRunnerColumn
142 instance QueryRunnerColumnDefault PGInt4 (Maybe NodeId)
144 queryRunnerColumnDefault = fieldQueryRunnerColumn
146 instance QueryRunnerColumnDefault PGInt4 NodeId
148 queryRunnerColumnDefault = fieldQueryRunnerColumn
151 ------------------------------------------------------------------------
153 -- TODO Classe HasDefault where
154 -- default NodeType = Hyperdata
155 ------------------------------------------------------------------------
156 $(makeAdaptorAndInstance "pNode" ''NodePoly)
157 $(makeLensesWith abbreviatedFields ''NodePoly)
158 $(makeAdaptorAndInstance "pNodeSearch" ''NodePolySearch)
159 $(makeLensesWith abbreviatedFields ''NodePolySearch)
161 type NodeWrite = NodePoly (Maybe (Column PGInt4 ))
164 (Maybe (Column PGInt4 ))
166 (Maybe (Column PGTimestamptz))
169 type NodeRead = NodePoly (Column PGInt4 )
174 (Column PGTimestamptz )
177 type NodeReadNull = NodePoly (Column (Nullable PGInt4 ))
178 (Column (Nullable PGInt4 ))
179 (Column (Nullable PGInt4 ))
180 (Column (Nullable PGInt4 ))
181 (Column (Nullable PGText ))
182 (Column (Nullable PGTimestamptz ))
183 (Column (Nullable PGJsonb))
185 nodeTable :: Table NodeWrite NodeRead
186 nodeTable = Table "nodes" (pNode Node { _node_id = optional "id"
187 , _node_typename = required "typename"
188 , _node_userId = required "user_id"
190 , _node_parentId = optional "parent_id"
191 , _node_name = required "name"
192 , _node_date = optional "date"
194 , _node_hyperdata = required "hyperdata"
198 queryNodeTable :: Query NodeRead
199 queryNodeTable = queryTable nodeTable
201 ------------------------------------------------------------------------
202 -- | Node(Read|Write)Search is slower than Node(Write|Read) use it
203 -- for full text search only
204 type NodeSearchWrite = NodePolySearch (Maybe (Column PGInt4 ))
207 (Column (Nullable PGInt4 ))
209 (Maybe (Column PGTimestamptz))
211 (Maybe (Column PGTSVector))
213 type NodeSearchRead = NodePolySearch (Column PGInt4 )
216 (Column (Nullable PGInt4 ))
218 (Column PGTimestamptz )
222 type NodeSearchReadNull = NodePolySearch (Column (Nullable PGInt4 ))
223 (Column (Nullable PGInt4 ))
224 (Column (Nullable PGInt4 ))
225 (Column (Nullable PGInt4 ))
226 (Column (Nullable PGText ))
227 (Column (Nullable PGTimestamptz ))
228 (Column (Nullable PGJsonb))
229 (Column (Nullable PGTSVector))
232 nodeTableSearch :: Table NodeSearchWrite NodeSearchRead
233 nodeTableSearch = Table "nodes" (pNodeSearch NodeSearch { _ns_id = optional "id"
234 , _ns_typename = required "typename"
235 , _ns_userId = required "user_id"
237 , _ns_parentId = required "parent_id"
238 , _ns_name = required "name"
239 , _ns_date = optional "date"
241 , _ns_hyperdata = required "hyperdata"
242 , _ns_search = optional "search"
247 queryNodeSearchTable :: Query NodeSearchRead
248 queryNodeSearchTable = queryTable nodeTableSearch
250 selectNode :: Column PGInt4 -> Query NodeRead
251 selectNode id = proc () -> do
252 row <- queryNodeTable -< ()
253 restrict -< _node_id row .== id
256 runGetNodes :: Query NodeRead -> Cmd err [NodeAny]
257 runGetNodes = runOpaQuery
259 ------------------------------------------------------------------------
260 ------------------------------------------------------------------------
262 -- | order by publication date
263 -- Favorites (Bool), node_ngrams
264 selectNodesWith :: ParentId -> Maybe NodeType
265 -> Maybe Offset -> Maybe Limit -> Query NodeRead
266 selectNodesWith parentId maybeNodeType maybeOffset maybeLimit =
267 --offset' maybeOffset $ limit' maybeLimit $ orderBy (asc (hyperdataDocument_Publication_date . node_hyperdata)) $ selectNodesWith' parentId typeId
268 limit' maybeLimit $ offset' maybeOffset
269 $ orderBy (asc _node_id)
270 $ selectNodesWith' parentId maybeNodeType
272 selectNodesWith' :: ParentId -> Maybe NodeType -> Query NodeRead
273 selectNodesWith' parentId maybeNodeType = proc () -> do
274 node <- (proc () -> do
275 row@(Node _ typeId _ parentId' _ _ _) <- queryNodeTable -< ()
276 restrict -< parentId' .== (pgNodeId parentId)
278 let typeId' = maybe 0 nodeTypeId maybeNodeType
280 restrict -< if typeId' > 0
281 then typeId .== (pgInt4 (typeId' :: Int))
283 returnA -< row ) -< ()
287 deleteNode :: NodeId -> Cmd err Int
288 deleteNode n = mkCmd $ \conn ->
289 fromIntegral <$> runDelete conn nodeTable
290 (\(Node n_id _ _ _ _ _ _) -> n_id .== pgNodeId n)
292 deleteNodes :: [NodeId] -> Cmd err Int
293 deleteNodes ns = mkCmd $ \conn ->
294 fromIntegral <$> runDelete conn nodeTable
295 (\(Node n_id _ _ _ _ _ _) -> in_ ((map pgNodeId ns)) n_id)
297 -- TODO: NodeType should match with `a'
298 getNodesWith :: JSONB a => NodeId -> proxy a -> Maybe NodeType
299 -> Maybe Offset -> Maybe Limit -> Cmd err [Node a]
300 getNodesWith parentId _ nodeType maybeOffset maybeLimit =
301 runOpaQuery $ selectNodesWith parentId nodeType maybeOffset maybeLimit
303 -- TODO: Why is the second parameter ignored?
304 -- TODO: Why not use getNodesWith?
305 getNodesWithParentId :: NodeId -> Maybe Text -> Cmd err [NodeAny]
306 getNodesWithParentId n _ = runOpaQuery $ selectNodesWithParentID n
308 ------------------------------------------------------------------------
309 getDocumentsV3WithParentId :: NodeId -> Cmd err [Node HyperdataDocumentV3]
310 getDocumentsV3WithParentId n = runOpaQuery $ selectNodesWith' n (Just NodeDocument)
312 -- TODO: merge with getDocumentsWithParentId by having a class IsHyperdataDocument
313 getDocumentsWithParentId :: NodeId -> Cmd err [Node HyperdataDocument]
314 getDocumentsWithParentId n = runOpaQuery $ selectNodesWith' n (Just NodeDocument)
316 getListsWithParentId :: NodeId -> Cmd err [Node HyperdataList]
317 getListsWithParentId n = runOpaQuery $ selectNodesWith' n (Just NodeList)
319 getCorporaWithParentId :: NodeId -> Cmd err [Node HyperdataCorpus]
320 getCorporaWithParentId n = runOpaQuery $ selectNodesWith' n (Just NodeCorpus)
322 ------------------------------------------------------------------------
323 selectNodesWithParentID :: NodeId -> Query NodeRead
324 selectNodesWithParentID n = proc () -> do
325 row@(Node _ _ _ parent_id _ _ _) <- queryNodeTable -< ()
326 restrict -< parent_id .== (pgNodeId n)
329 selectNodesWithType :: Column PGInt4 -> Query NodeRead
330 selectNodesWithType type_id = proc () -> do
331 row@(Node _ tn _ _ _ _ _) <- queryNodeTable -< ()
332 restrict -< tn .== type_id
335 type JSONB = QueryRunnerColumnDefault PGJsonb
337 getNode :: JSONB a => NodeId -> proxy a -> Cmd err (Node a)
339 fromMaybe (error $ "Node does node exist: " <> show nId) . headMay <$> runOpaQuery (limit 1 $ selectNode (pgNodeId nId))
341 getNodesWithType :: Column PGInt4 -> Cmd err [Node HyperdataDocument]
342 getNodesWithType = runOpaQuery . selectNodesWithType
344 ------------------------------------------------------------------------
345 ------------------------------------------------------------------------
346 defaultUser :: HyperdataUser
347 defaultUser = HyperdataUser (Just $ (pack . show) EN)
349 nodeUserW :: Maybe Name -> Maybe HyperdataUser -> UserId -> NodeWrite
350 nodeUserW maybeName maybeHyperdata = node NodeUser name user Nothing
352 name = maybe "User" identity maybeName
353 user = maybe defaultUser identity maybeHyperdata
354 ------------------------------------------------------------------------
355 defaultFolder :: HyperdataFolder
356 defaultFolder = HyperdataFolder (Just "Markdown Description")
358 nodeFolderW :: Maybe Name -> Maybe HyperdataFolder -> ParentId -> UserId -> NodeWrite
359 nodeFolderW maybeName maybeFolder pid = node NodeFolder name folder (Just pid)
361 name = maybe "Folder" identity maybeName
362 folder = maybe defaultFolder identity maybeFolder
363 ------------------------------------------------------------------------
364 nodeCorpusW :: Maybe Name -> Maybe HyperdataCorpus -> ParentId -> UserId -> NodeWrite
365 nodeCorpusW maybeName maybeCorpus pId = node NodeCorpus name corpus (Just pId)
367 name = maybe "Corpus" identity maybeName
368 corpus = maybe defaultCorpus identity maybeCorpus
369 --------------------------
370 defaultDocument :: HyperdataDocument
371 defaultDocument = hyperdataDocument
373 nodeDocumentW :: Maybe Name -> Maybe HyperdataDocument -> CorpusId -> UserId -> NodeWrite
374 nodeDocumentW maybeName maybeDocument cId = node NodeDocument name doc (Just cId)
376 name = maybe "Document" identity maybeName
377 doc = maybe defaultDocument identity maybeDocument
378 ------------------------------------------------------------------------
379 defaultAnnuaire :: HyperdataAnnuaire
380 defaultAnnuaire = HyperdataAnnuaire (Just "Title") (Just "Description")
382 nodeAnnuaireW :: Maybe Name -> Maybe HyperdataAnnuaire -> ParentId -> UserId -> NodeWrite
383 nodeAnnuaireW maybeName maybeAnnuaire pId = node NodeAnnuaire name annuaire (Just pId)
385 name = maybe "Annuaire" identity maybeName
386 annuaire = maybe defaultAnnuaire identity maybeAnnuaire
387 --------------------------
389 ------------------------------------------------------------------------
390 arbitraryList :: HyperdataList
391 arbitraryList = HyperdataList (Just "Preferences")
393 nodeListW :: Maybe Name -> Maybe HyperdataList -> ParentId -> UserId -> NodeWrite
394 nodeListW maybeName maybeList pId = node NodeList name list (Just pId)
396 name = maybe "Listes" identity maybeName
397 list = maybe arbitraryList identity maybeList
399 ------------------------------------------------------------------------
400 arbitraryGraph :: HyperdataGraph
401 arbitraryGraph = HyperdataGraph (Just "Preferences")
403 nodeGraphW :: Maybe Name -> Maybe HyperdataGraph -> ParentId -> UserId -> NodeWrite
404 nodeGraphW maybeName maybeGraph pId = node NodeGraph name graph (Just pId)
406 name = maybe "Graph" identity maybeName
407 graph = maybe arbitraryGraph identity maybeGraph
409 ------------------------------------------------------------------------
411 arbitraryDashboard :: HyperdataDashboard
412 arbitraryDashboard = HyperdataDashboard (Just "Preferences")
414 nodeDashboardW :: Maybe Name -> Maybe HyperdataDashboard -> ParentId -> UserId -> NodeWrite
415 nodeDashboardW maybeName maybeDashboard pId = node NodeDashboard name dashboard (Just pId)
417 name = maybe "Dashboard" identity maybeName
418 dashboard = maybe arbitraryDashboard identity maybeDashboard
420 ------------------------------------------------------------------------
421 node :: (ToJSON a, Hyperdata a) => NodeType -> Name -> a -> Maybe ParentId -> UserId -> NodeWrite
422 node nodeType name hyperData parentId userId = Node Nothing (pgInt4 typeId) (pgInt4 userId) (pgNodeId <$> parentId) (pgStrictText name) Nothing (pgJSONB $ cs $ encode hyperData)
424 typeId = nodeTypeId nodeType
426 -------------------------------
427 insertNodes :: [NodeWrite] -> Cmd err Int64
428 insertNodes ns = mkCmd $ \conn -> runInsertMany conn nodeTable ns
430 insertNodesR :: [NodeWrite] -> Cmd err [NodeId]
431 insertNodesR ns = mkCmd $ \conn ->
432 runInsert_ conn (Insert nodeTable ns (rReturning (\(Node i _ _ _ _ _ _) -> i)) Nothing)
434 insertNodesWithParent :: Maybe ParentId -> [NodeWrite] -> Cmd err Int64
435 insertNodesWithParent pid ns = insertNodes (set node_parentId (pgNodeId <$> pid) <$> ns)
437 insertNodesWithParentR :: Maybe ParentId -> [NodeWrite] -> Cmd err [NodeId]
438 insertNodesWithParentR pid ns = insertNodesR (set node_parentId (pgNodeId <$> pid) <$> ns)
439 ------------------------------------------------------------------------
440 -- TODO Hierachy of Nodes
441 -- post and get same types Node' and update if changes
443 {- TODO semantic to achieve
444 post c uid pid [ Node' NodeCorpus "name" "{}" []
445 , Node' NodeFolder "name" "{}" [Node' NodeCorpus "test 2" "" [ Node' NodeDocument "title" "metaData" []
446 , Node' NodeDocument "title" "jsonData" []
451 ------------------------------------------------------------------------
454 -- currently this function removes the child relation
455 -- needs a Temporary type between Node' and NodeWriteT
456 node2table :: UserId -> Maybe ParentId -> Node' -> NodeWrite
457 node2table uid pid (Node' nt txt v []) = Node Nothing (pgInt4 $ nodeTypeId nt) (pgInt4 uid) (fmap pgNodeId pid) (pgStrictText txt) Nothing (pgStrictJSONB $ cs $ encode v)
458 node2table _ _ (Node' _ _ _ _) = panic "node2table: should not happen, Tree insert not implemented yet"
461 data Node' = Node' { _n_type :: NodeType
464 , _n_children :: [Node']
467 mkNode :: [NodeWrite] -> Cmd err Int64
468 mkNode ns = mkCmd $ \conn -> runInsertMany conn nodeTable ns
470 mkNodeR :: [NodeWrite] -> Cmd err [NodeId]
471 mkNodeR ns = mkCmd $ \conn -> runInsertManyReturning conn nodeTable ns (_node_id)
473 ------------------------------------------------------------------------
475 data NewNode = NewNode { _newNodeId :: NodeId
476 , _newNodeChildren :: [NodeId] }
478 postNode :: HasNodeError err => UserId -> Maybe ParentId -> Node' -> Cmd err NewNode
479 postNode uid pid (Node' nt txt v []) = do
480 pids <- mkNodeR [node2table uid pid (Node' nt txt v [])]
482 [pid'] -> pure $ NewNode pid' []
483 _ -> nodeError ManyParents
485 postNode uid pid (Node' NodeCorpus txt v ns) = do
486 NewNode pid' _ <- postNode uid pid (Node' NodeCorpus txt v [])
487 pids <- mkNodeR (concat $ map (\n -> [childWith uid pid' n]) ns)
488 pure $ NewNode pid' pids
490 postNode uid pid (Node' NodeAnnuaire txt v ns) = do
491 NewNode pid' _ <- postNode uid pid (Node' NodeAnnuaire txt v [])
492 pids <- mkNodeR (concat $ map (\n -> [childWith uid pid' n]) ns)
493 pure $ NewNode pid' pids
494 postNode _ _ (Node' _ _ _ _) = nodeError NotImplYet
497 childWith :: UserId -> ParentId -> Node' -> NodeWrite
498 childWith uId pId (Node' NodeDocument txt v []) = node2table uId (Just pId) (Node' NodeDocument txt v [])
499 childWith uId pId (Node' NodeContact txt v []) = node2table uId (Just pId) (Node' NodeContact txt v [])
500 childWith _ _ (Node' _ _ _ _) = panic "This NodeType can not be a child"
503 -- | TODO Use right userId
504 mk :: NodeType -> Maybe ParentId -> Text -> Cmd err [NodeId]
505 mk nt pId name = mk' nt userId pId name
509 mk' :: NodeType -> UserId -> Maybe ParentId -> Text -> Cmd err [NodeId]
510 mk' nt uId pId name = insertNodesWithParentR pId [node nt name hd pId uId]
512 hd = HyperdataUser . Just . pack $ show EN
516 mk'' :: HasNodeError err => NodeType -> Maybe ParentId -> UserId -> Name -> Cmd err [NodeId]
517 mk'' NodeUser Nothing uId name = mk' NodeUser uId Nothing name
518 mk'' NodeUser _ _ _ = nodeError UserNoParent
519 mk'' _ Nothing _ _ = nodeError HasParent
520 mk'' nt pId uId name = mk' nt uId pId name
522 mkRoot :: HasNodeError err => Username -> UserId -> Cmd err [RootId]
523 mkRoot uname uId = case uId > 0 of
524 False -> nodeError NegativeId
525 True -> mk'' NodeUser Nothing uId uname
527 mkCorpus :: Maybe Name -> Maybe HyperdataCorpus -> ParentId -> UserId -> Cmd err [CorpusId]
528 mkCorpus n h p u = insertNodesR [nodeCorpusW n h p u]
530 getOrMkList :: HasNodeError err => ParentId -> UserId -> Cmd err NodeId
531 getOrMkList pId uId =
532 maybe (mkList' pId uId) (pure . view node_id) . headMay =<< getListsWithParentId pId
534 mkList' pId uId = maybe (nodeError MkNode) pure . headMay =<< mkList pId uId
536 -- | TODO remove defaultList
537 defaultList :: HasNodeError err => CorpusId -> Cmd err ListId
539 maybe (nodeError NoListFound) (pure . view node_id) . headMay =<< getListsWithParentId cId
541 mkList :: HasNodeError err => ParentId -> UserId -> Cmd err [NodeId]
542 mkList p u = insertNodesR [nodeListW Nothing Nothing p u]
544 mkGraph :: ParentId -> UserId -> Cmd err [GraphId]
545 mkGraph p u = insertNodesR [nodeGraphW Nothing Nothing p u]
547 mkDashboard :: ParentId -> UserId -> Cmd err [NodeId]
548 mkDashboard p u = insertNodesR [nodeDashboardW Nothing Nothing p u]
550 mkAnnuaire :: ParentId -> UserId -> Cmd err [NodeId]
551 mkAnnuaire p u = insertNodesR [nodeAnnuaireW Nothing Nothing p u]
553 -- | Default CorpusId Master and ListId Master
555 pgNodeId :: NodeId -> Column PGInt4
556 pgNodeId = pgInt4 . id2int