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 #-}
25 {-# LANGUAGE TypeFamilies #-}
27 module Gargantext.Database.Schema.Node where
29 import Control.Arrow (returnA)
30 import Control.Lens (Prism', set, view, (#), (^?))
31 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
32 import Control.Monad.Error.Class (MonadError(..))
34 import Data.Maybe (Maybe(..), fromMaybe)
35 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
36 import Data.Text (Text)
37 import Database.PostgreSQL.Simple.FromField (FromField, fromField)
38 import GHC.Int (Int64)
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, HyperData(..))
44 import Gargantext.Database.Node.User (HyperdataUser(..), fake_HyperdataUser)
45 import Gargantext.Database.Node.Contact (HyperdataContact(..), arbitraryHyperdataContact)
46 import Gargantext.Database.Utils
47 import Gargantext.Prelude hiding (sum, head)
48 import Gargantext.Viz.Graph (HyperdataGraph(..))
50 import Opaleye hiding (FromField)
51 import Opaleye.Internal.QueryArr (Query)
52 import Prelude hiding (null, id, map, sum)
54 ------------------------------------------------------------------------
56 data NodeError = NoListFound
69 class HasNodeError e where
70 _NodeError :: Prism' e NodeError
72 nodeError :: (MonadError e m, HasNodeError e) => NodeError -> m a
73 nodeError ne = throwError $ _NodeError # ne
75 catchNodeError :: (MonadError e m, HasNodeError e) => m a -> (NodeError -> m a) -> m a
76 catchNodeError f g = catchError f (\e -> maybe (throwError e) g (e ^? _NodeError))
78 ------------------------------------------------------------------------
79 instance FromField HyperdataAny where
80 fromField = fromField'
82 instance FromField HyperdataCorpus
84 fromField = fromField'
86 instance FromField HyperdataDocument
88 fromField = fromField'
90 instance FromField HyperdataDocumentV3
92 fromField = fromField'
94 instance FromField HyperData
96 fromField = fromField'
98 instance FromField HyperdataListModel
100 fromField = fromField'
102 instance FromField HyperdataGraph
104 fromField = fromField'
106 instance FromField HyperdataPhylo
108 fromField = fromField'
110 instance FromField HyperdataAnnuaire
112 fromField = fromField'
114 instance FromField HyperdataList
116 fromField = fromField'
118 instance FromField (NodeId, Text)
120 fromField = fromField'
121 ------------------------------------------------------------------------
122 instance QueryRunnerColumnDefault PGJsonb HyperdataAny
124 queryRunnerColumnDefault = fieldQueryRunnerColumn
126 instance QueryRunnerColumnDefault PGJsonb HyperdataList
128 queryRunnerColumnDefault = fieldQueryRunnerColumn
130 instance QueryRunnerColumnDefault PGJsonb HyperData
132 queryRunnerColumnDefault = fieldQueryRunnerColumn
135 instance QueryRunnerColumnDefault PGJsonb HyperdataDocument
137 queryRunnerColumnDefault = fieldQueryRunnerColumn
139 instance QueryRunnerColumnDefault PGJsonb HyperdataDocumentV3
141 queryRunnerColumnDefault = fieldQueryRunnerColumn
143 instance QueryRunnerColumnDefault PGJsonb HyperdataCorpus
145 queryRunnerColumnDefault = fieldQueryRunnerColumn
147 instance QueryRunnerColumnDefault PGJsonb HyperdataListModel
149 queryRunnerColumnDefault = fieldQueryRunnerColumn
151 instance QueryRunnerColumnDefault PGJsonb HyperdataGraph
153 queryRunnerColumnDefault = fieldQueryRunnerColumn
155 instance QueryRunnerColumnDefault PGJsonb HyperdataPhylo
157 queryRunnerColumnDefault = fieldQueryRunnerColumn
159 instance QueryRunnerColumnDefault PGJsonb HyperdataAnnuaire
161 queryRunnerColumnDefault = fieldQueryRunnerColumn
163 instance QueryRunnerColumnDefault PGTSVector (Maybe TSVector)
165 queryRunnerColumnDefault = fieldQueryRunnerColumn
167 instance QueryRunnerColumnDefault PGInt4 (Maybe NodeId)
169 queryRunnerColumnDefault = fieldQueryRunnerColumn
171 instance QueryRunnerColumnDefault PGInt4 NodeId
173 queryRunnerColumnDefault = fieldQueryRunnerColumn
175 instance QueryRunnerColumnDefault (Nullable PGInt4) NodeId
177 queryRunnerColumnDefault = fieldQueryRunnerColumn
180 ------------------------------------------------------------------------
181 $(makeAdaptorAndInstance "pNode" ''NodePoly)
182 $(makeLensesWith abbreviatedFields ''NodePoly)
184 $(makeAdaptorAndInstance "pNodeSearch" ''NodePolySearch)
185 $(makeLensesWith abbreviatedFields ''NodePolySearch)
187 type NodeWrite = NodePoly (Maybe (Column PGInt4) )
190 (Maybe (Column PGInt4) )
192 (Maybe (Column PGTimestamptz))
195 type NodeRead = NodePoly (Column PGInt4 )
200 (Column PGTimestamptz )
203 type NodeReadNull = NodePoly (Column (Nullable PGInt4))
204 (Column (Nullable PGInt4))
205 (Column (Nullable PGInt4))
206 (Column (Nullable PGInt4))
207 (Column (Nullable PGText))
208 (Column (Nullable PGTimestamptz))
209 (Column (Nullable PGJsonb))
211 nodeTable :: Table NodeWrite NodeRead
212 nodeTable = Table "nodes" (pNode Node { _node_id = optional "id"
213 , _node_typename = required "typename"
214 , _node_userId = required "user_id"
216 , _node_parentId = optional "parent_id"
217 , _node_name = required "name"
218 , _node_date = optional "date"
220 , _node_hyperdata = required "hyperdata"
224 queryNodeTable :: Query NodeRead
225 queryNodeTable = queryTable nodeTable
227 ------------------------------------------------------------------------
228 -- | Node(Read|Write)Search is slower than Node(Write|Read) use it
229 -- for full text search only
230 type NodeSearchWrite =
232 (Maybe (Column PGInt4) )
235 (Column (Nullable PGInt4) )
237 (Maybe (Column PGTimestamptz))
239 (Maybe (Column PGTSVector) )
241 type NodeSearchRead =
246 (Column (Nullable PGInt4 ))
248 (Column PGTimestamptz )
252 type NodeSearchReadNull =
254 (Column (Nullable PGInt4) )
255 (Column (Nullable PGInt4) )
256 (Column (Nullable PGInt4) )
257 (Column (Nullable PGInt4) )
258 (Column (Nullable PGText) )
259 (Column (Nullable PGTimestamptz))
260 (Column (Nullable PGJsonb) )
261 (Column (Nullable PGTSVector) )
263 nodeTableSearch :: Table NodeSearchWrite NodeSearchRead
264 nodeTableSearch = Table "nodes" (pNodeSearch NodeSearch { _ns_id = optional "id"
265 , _ns_typename = required "typename"
266 , _ns_userId = required "user_id"
268 , _ns_parentId = required "parent_id"
269 , _ns_name = required "name"
270 , _ns_date = optional "date"
272 , _ns_hyperdata = required "hyperdata"
273 , _ns_search = optional "search"
278 queryNodeSearchTable :: Query NodeSearchRead
279 queryNodeSearchTable = queryTable nodeTableSearch
281 selectNode :: Column PGInt4 -> Query NodeRead
282 selectNode id = proc () -> do
283 row <- queryNodeTable -< ()
284 restrict -< _node_id row .== id
289 runGetNodes :: Query NodeRead -> Cmd err [Node HyperdataAny]
290 runGetNodes = runOpaQuery
292 ------------------------------------------------------------------------
293 ------------------------------------------------------------------------
294 -- | order by publication date
295 -- Favorites (Bool), node_ngrams
296 selectNodesWith :: ParentId -> Maybe NodeType
297 -> Maybe Offset -> Maybe Limit -> Query NodeRead
298 selectNodesWith parentId maybeNodeType maybeOffset maybeLimit =
299 --offset' maybeOffset $ limit' maybeLimit $ orderBy (asc (hyperdataDocument_Publication_date . node_hyperdata)) $ selectNodesWith' parentId typeId
300 limit' maybeLimit $ offset' maybeOffset
301 $ orderBy (asc _node_id)
302 $ selectNodesWith' parentId maybeNodeType
304 selectNodesWith' :: ParentId -> Maybe NodeType -> Query NodeRead
305 selectNodesWith' parentId maybeNodeType = proc () -> do
306 node <- (proc () -> do
307 row@(Node _ typeId _ parentId' _ _ _) <- queryNodeTable -< ()
308 restrict -< parentId' .== (pgNodeId parentId)
310 let typeId' = maybe 0 nodeTypeId maybeNodeType
312 restrict -< if typeId' > 0
313 then typeId .== (pgInt4 (typeId' :: Int))
315 returnA -< row ) -< ()
318 deleteNode :: NodeId -> Cmd err Int
319 deleteNode n = mkCmd $ \conn ->
320 fromIntegral <$> runDelete conn nodeTable
321 (\(Node n_id _ _ _ _ _ _) -> n_id .== pgNodeId n)
323 deleteNodes :: [NodeId] -> Cmd err Int
324 deleteNodes ns = mkCmd $ \conn ->
325 fromIntegral <$> runDelete conn nodeTable
326 (\(Node n_id _ _ _ _ _ _) -> in_ ((map pgNodeId ns)) n_id)
328 -- TODO: NodeType should match with `a'
329 getNodesWith :: JSONB a => NodeId -> proxy a -> Maybe NodeType
330 -> Maybe Offset -> Maybe Limit -> Cmd err [Node a]
331 getNodesWith parentId _ nodeType maybeOffset maybeLimit =
332 runOpaQuery $ selectNodesWith parentId nodeType maybeOffset maybeLimit
334 -- TODO: Why is the second parameter ignored?
335 -- TODO: Why not use getNodesWith?
336 getNodesWithParentId :: (Hyperdata a, QueryRunnerColumnDefault PGJsonb a)
339 getNodesWithParentId n = runOpaQuery $ selectNodesWithParentID n'
345 ------------------------------------------------------------------------
346 getDocumentsV3WithParentId :: NodeId -> Cmd err [Node HyperdataDocumentV3]
347 getDocumentsV3WithParentId n = runOpaQuery $ selectNodesWith' n (Just NodeDocument)
349 -- TODO: merge with getDocumentsWithParentId by having a class IsHyperdataDocument
350 getDocumentsWithParentId :: NodeId -> Cmd err [Node HyperdataDocument]
351 getDocumentsWithParentId n = runOpaQuery $ selectNodesWith' n (Just NodeDocument)
353 getListsModelWithParentId :: NodeId -> Cmd err [Node HyperdataListModel]
354 getListsModelWithParentId n = runOpaQuery $ selectNodesWith' n (Just NodeListModel)
356 getCorporaWithParentId :: NodeId -> Cmd err [Node HyperdataCorpus]
357 getCorporaWithParentId n = runOpaQuery $ selectNodesWith' n (Just NodeCorpus)
359 ------------------------------------------------------------------------
360 selectNodesWithParentID :: NodeId -> Query NodeRead
361 selectNodesWithParentID n = proc () -> do
362 row@(Node _ _ _ parent_id _ _ _) <- queryNodeTable -< ()
363 restrict -< parent_id .== (pgNodeId n)
366 selectNodesWithType :: Column PGInt4 -> Query NodeRead
367 selectNodesWithType type_id = proc () -> do
368 row@(Node _ tn _ _ _ _ _) <- queryNodeTable -< ()
369 restrict -< tn .== type_id
372 type JSONB = QueryRunnerColumnDefault PGJsonb
375 getNode :: NodeId -> Cmd err (Node Value)
376 getNode nId = fromMaybe (error $ "Node does not exist: " <> show nId) . headMay
377 <$> runOpaQuery (limit 1 $ selectNode (pgNodeId nId))
379 getNodeWith :: JSONB a => NodeId -> proxy a -> Cmd err (Node a)
380 getNodeWith nId _ = do
381 fromMaybe (error $ "Node does not exist: " <> show nId) . headMay
382 <$> runOpaQuery (limit 1 $ selectNode (pgNodeId nId))
384 getNodeUser :: NodeId -> Cmd err (Node HyperdataUser)
386 fromMaybe (error $ "Node does not exist: " <> show nId) . headMay
387 <$> runOpaQuery (limit 1 $ selectNode (pgNodeId nId))
390 getNodePhylo :: NodeId -> Cmd err (Node HyperdataPhylo)
391 getNodePhylo nId = do
392 fromMaybe (error $ "Node Phylo does not exist: " <> show nId) . headMay
393 <$> runOpaQuery (limit 1 $ selectNode (pgNodeId nId))
396 getNodesWithType :: Column PGInt4 -> Cmd err [Node HyperdataDocument]
397 getNodesWithType = runOpaQuery . selectNodesWithType
399 ------------------------------------------------------------------------
400 nodeUserW :: Maybe Name -> Maybe HyperdataUser -> UserId -> NodeWrite
401 nodeUserW maybeName maybeHyperdata = node NodeUser name user Nothing
403 name = maybe "User" identity maybeName
404 user = maybe fake_HyperdataUser identity maybeHyperdata
406 nodeContactW :: Maybe Name -> Maybe HyperdataContact
407 -> AnnuaireId -> UserId -> NodeWrite
408 nodeContactW maybeName maybeContact aId =
409 node NodeContact name contact (Just aId)
411 name = maybe "Contact" identity maybeName
412 contact = maybe arbitraryHyperdataContact identity maybeContact
413 ------------------------------------------------------------------------
414 defaultFolder :: HyperdataCorpus
415 defaultFolder = defaultCorpus
417 nodeFolderW :: Maybe Name -> Maybe HyperdataCorpus -> ParentId -> UserId -> NodeWrite
418 nodeFolderW maybeName maybeFolder pid = node NodeFolder name folder (Just pid)
420 name = maybe "Folder" identity maybeName
421 folder = maybe defaultFolder identity maybeFolder
422 ------------------------------------------------------------------------
423 nodeCorpusW :: Maybe Name -> Maybe HyperdataCorpus -> ParentId -> UserId -> NodeWrite
424 nodeCorpusW maybeName maybeCorpus pId = node NodeCorpus name corpus (Just pId)
426 name = maybe "Corpus" identity maybeName
427 corpus = maybe defaultCorpus identity maybeCorpus
428 --------------------------
429 defaultDocument :: HyperdataDocument
430 defaultDocument = hyperdataDocument
432 nodeDocumentW :: Maybe Name -> Maybe HyperdataDocument -> CorpusId -> UserId -> NodeWrite
433 nodeDocumentW maybeName maybeDocument cId = node NodeDocument name doc (Just cId)
435 name = maybe "Document" identity maybeName
436 doc = maybe defaultDocument identity maybeDocument
437 ------------------------------------------------------------------------
438 defaultAnnuaire :: HyperdataAnnuaire
439 defaultAnnuaire = HyperdataAnnuaire (Just "Title") (Just "Description")
441 nodeAnnuaireW :: Maybe Name -> Maybe HyperdataAnnuaire -> ParentId -> UserId -> NodeWrite
442 nodeAnnuaireW maybeName maybeAnnuaire pId = node NodeAnnuaire name annuaire (Just pId)
444 name = maybe "Annuaire" identity maybeName
445 annuaire = maybe defaultAnnuaire identity maybeAnnuaire
447 ------------------------------------------------------------------------
450 class IsNodeDb a where
454 instance IsNodeDb NodeType where
457 instance HasHyperdata NodeType where
458 data Hyper NodeType = HyperList HyperdataList
459 | HyperCorpus HyperdataCorpus
461 hasHyperdata nt = case nt of
462 NodeList -> HyperList $ HyperdataList (Just "list")
464 unHyper h = case h of
470 class HasDefault a where
471 hasDefaultData :: a -> HyperData
472 hasDefaultName :: a -> Text
474 instance HasDefault NodeType where
475 hasDefaultData nt = case nt of
476 NodeTexts -> HyperdataTexts (Just "Preferences")
477 NodeList -> HyperdataList' (Just "Preferences")
478 NodeListCooc -> HyperdataList' (Just "Preferences")
480 --NodeAnnuaire -> HyperdataAnnuaire (Just "Title") (Just "Description")
482 hasDefaultName nt = case nt of
485 NodeListCooc -> "Cooc"
488 ------------------------------------------------------------------------
490 nodeDefault :: NodeType -> ParentId -> UserId -> NodeWrite
491 nodeDefault nt parent = node nt name hyper (Just parent)
493 name = (hasDefaultName nt)
494 hyper = (hasDefaultData nt)
496 ------------------------------------------------------------------------
497 arbitraryListModel :: HyperdataListModel
498 arbitraryListModel = HyperdataListModel (400,500) "data/models/test.model" (Just 0.83)
500 mkListModelNode :: HasNodeError err => ParentId -> UserId -> Cmd err [NodeId]
501 mkListModelNode p u = insertNodesR [nodeListModelW Nothing Nothing p u]
503 nodeListModelW :: Maybe Name -> Maybe HyperdataListModel -> ParentId -> UserId -> NodeWrite
504 nodeListModelW maybeName maybeListModel pId = node NodeListModel name list (Just pId)
506 name = maybe "List Model" identity maybeName
507 list = maybe arbitraryListModel identity maybeListModel
509 ------------------------------------------------------------------------
510 arbitraryGraph :: HyperdataGraph
511 arbitraryGraph = HyperdataGraph Nothing
513 nodeGraphW :: Maybe Name -> Maybe HyperdataGraph -> ParentId -> UserId -> NodeWrite
514 nodeGraphW maybeName maybeGraph pId = node NodeGraph name graph (Just pId)
516 name = maybe "Graph" identity maybeName
517 graph = maybe arbitraryGraph identity maybeGraph
519 mkGraph :: ParentId -> UserId -> Cmd err [GraphId]
520 mkGraph p u = insertNodesR [nodeGraphW Nothing Nothing p u]
522 insertGraph :: ParentId -> UserId -> HyperdataGraph -> Cmd err [GraphId]
523 insertGraph p u h = insertNodesR [nodeGraphW Nothing (Just h) p u]
525 ------------------------------------------------------------------------
526 arbitraryPhylo :: HyperdataPhylo
527 arbitraryPhylo = HyperdataPhylo Nothing Nothing
529 nodePhyloW :: Maybe Name -> Maybe HyperdataPhylo -> ParentId -> UserId -> NodeWrite
530 nodePhyloW maybeName maybePhylo pId = node NodePhylo name graph (Just pId)
532 name = maybe "Phylo" identity maybeName
533 graph = maybe arbitraryPhylo identity maybePhylo
536 ------------------------------------------------------------------------
537 arbitraryDashboard :: HyperdataDashboard
538 arbitraryDashboard = HyperdataDashboard (Just "Preferences") []
539 ------------------------------------------------------------------------
541 node :: (ToJSON a, Hyperdata a) => NodeType -> Name -> a -> Maybe ParentId -> UserId -> NodeWrite
542 node nodeType name hyperData parentId userId =
546 (pgNodeId <$> parentId)
549 (pgJSONB $ cs $ encode hyperData)
551 typeId = nodeTypeId nodeType
553 -------------------------------
554 insertNodes :: [NodeWrite] -> Cmd err Int64
555 insertNodes ns = mkCmd $ \conn -> runInsert_ conn $ Insert nodeTable ns rCount Nothing
557 insertNodesR :: [NodeWrite] -> Cmd err [NodeId]
558 insertNodesR ns = mkCmd $ \conn ->
559 runInsert_ conn (Insert nodeTable ns (rReturning (\(Node i _ _ _ _ _ _) -> i)) Nothing)
561 insertNodesWithParent :: Maybe ParentId -> [NodeWrite] -> Cmd err Int64
562 insertNodesWithParent pid ns = insertNodes (set node_parentId (pgNodeId <$> pid) <$> ns)
564 insertNodesWithParentR :: Maybe ParentId -> [NodeWrite] -> Cmd err [NodeId]
565 insertNodesWithParentR pid ns = insertNodesR (set node_parentId (pgNodeId <$> pid) <$> ns)
566 ------------------------------------------------------------------------
567 -- TODO Hierachy of Nodes
568 -- post and get same types Node' and update if changes
570 {- TODO semantic to achieve
571 post c uid pid [ Node' NodeCorpus "name" "{}" []
572 , Node' NodeFolder "name" "{}" [Node' NodeCorpus "test 2" "" [ Node' NodeDocument "title" "metaData" []
573 , Node' NodeDocument "title" "jsonData" []
578 ------------------------------------------------------------------------
581 -- currently this function removes the child relation
582 -- needs a Temporary type between Node' and NodeWriteT
583 node2table :: UserId -> Maybe ParentId -> Node' -> NodeWrite
584 node2table uid pid (Node' nt txt v []) = Node Nothing (pgInt4 $ nodeTypeId nt) (pgInt4 uid) (fmap pgNodeId pid) (pgStrictText txt) Nothing (pgStrictJSONB $ cs $ encode v)
585 node2table _ _ (Node' _ _ _ _) = panic "node2table: should not happen, Tree insert not implemented yet"
588 data Node' = Node' { _n_type :: NodeType
591 , _n_children :: [Node']
594 mkNodes :: [NodeWrite] -> Cmd err Int64
595 mkNodes ns = mkCmd $ \conn -> runInsert_ conn $ Insert nodeTable ns rCount Nothing
597 mkNodeR :: [NodeWrite] -> Cmd err [NodeId]
598 mkNodeR ns = mkCmd $ \conn -> runInsert_ conn $ Insert nodeTable ns (rReturning _node_id) Nothing
600 ------------------------------------------------------------------------
602 data NewNode = NewNode { _newNodeId :: NodeId
603 , _newNodeChildren :: [NodeId] }
605 postNode :: HasNodeError err => UserId -> Maybe ParentId -> Node' -> Cmd err NewNode
607 postNode uid pid (Node' nt txt v []) = do
608 pids <- mkNodeR [node2table uid pid (Node' nt txt v [])]
610 [pid'] -> pure $ NewNode pid' []
611 _ -> nodeError ManyParents
613 postNode uid pid (Node' NodeCorpus txt v ns) = do
614 NewNode pid' _ <- postNode uid pid (Node' NodeCorpus txt v [])
615 pids <- mkNodeR (concat $ map (\n -> [childWith uid pid' n]) ns)
616 pure $ NewNode pid' pids
618 postNode uid pid (Node' NodeAnnuaire txt v ns) = do
619 NewNode pid' _ <- postNode uid pid (Node' NodeAnnuaire txt v [])
620 pids <- mkNodeR (concat $ map (\n -> [childWith uid pid' n]) ns)
621 pure $ NewNode pid' pids
623 postNode uid pid (Node' NodeDashboard txt v ns) = do
624 NewNode pid' _ <- postNode uid pid (Node' NodeDashboard txt v [])
625 pids <- mkNodeR (concat $ map (\n -> [childWith uid pid' n]) ns)
626 pure $ NewNode pid' pids
628 postNode _ _ (Node' _ _ _ _) = nodeError NotImplYet
631 childWith :: UserId -> ParentId -> Node' -> NodeWrite
632 childWith uId pId (Node' NodeDocument txt v []) = node2table uId (Just pId) (Node' NodeDocument txt v [])
633 childWith uId pId (Node' NodeContact txt v []) = node2table uId (Just pId) (Node' NodeContact txt v [])
634 childWith _ _ (Node' _ _ _ _) = panic "This NodeType can not be a child"
637 -- =================================================================== --
638 ------------------------------------------------------------------------
639 -- | TODO mk all others nodes
640 mkNodeWithParent :: HasNodeError err => NodeType -> Maybe ParentId -> UserId -> Name -> Cmd err [NodeId]
641 mkNodeWithParent NodeUser (Just _) _ _ = nodeError UserNoParent
643 ------------------------------------------------------------------------
644 mkNodeWithParent NodeUser Nothing uId name =
645 insertNodesWithParentR Nothing [node NodeUser name fake_HyperdataUser Nothing uId]
647 mkNodeWithParent _ Nothing _ _ = nodeError HasParent
648 ------------------------------------------------------------------------
649 mkNodeWithParent NodeFolder (Just i) uId name =
650 insertNodesWithParentR (Just i) [node NodeFolder name hd Nothing uId]
654 mkNodeWithParent NodeFolderPrivate (Just i) uId _ =
655 insertNodesWithParentR (Just i) [node NodeFolderPrivate "Private" hd Nothing uId]
659 mkNodeWithParent NodeFolderShared (Just i) uId _ =
660 insertNodesWithParentR (Just i) [node NodeFolderShared "Shared" hd Nothing uId]
664 mkNodeWithParent NodeFolderPublic (Just i) uId _ =
665 insertNodesWithParentR (Just i) [node NodeFolderPublic "Public" hd Nothing uId]
669 mkNodeWithParent NodeTeam (Just i) uId _ =
670 insertNodesWithParentR (Just i) [node NodeTeam "Team" hd Nothing uId]
673 ------------------------------------------------------------------------
674 mkNodeWithParent NodeCorpus (Just i) uId name =
675 insertNodesWithParentR (Just i) [node NodeCorpus name hd Nothing uId]
679 mkNodeWithParent NodeAnnuaire (Just i) uId name =
680 insertNodesWithParentR (Just i) [node NodeAnnuaire name hd Nothing uId]
684 mkNodeWithParent _ _ _ _ = nodeError NotImplYet
685 ------------------------------------------------------------------------
686 -- =================================================================== --
690 mkRoot :: HasNodeError err => Username -> UserId -> Cmd err [RootId]
691 mkRoot uname uId = case uId > 0 of
692 False -> nodeError NegativeId
694 rs <- mkNodeWithParent NodeUser Nothing uId uname
697 _ <- mkNodeWithParent NodeFolderPrivate (Just r) uId uname
698 _ <- mkNodeWithParent NodeFolderShared (Just r) uId uname
699 _ <- mkNodeWithParent NodeFolderPublic (Just r) uId uname
705 -- CorpusDocument is a corpus made from a set of documents
706 -- CorpusContact is a corpus made from a set of contacts (syn of Annuaire)
707 data CorpusType = CorpusDocument | CorpusContact
711 mk :: Maybe Name -> Maybe a -> ParentId -> UserId -> Cmd err [NodeId]
713 instance MkCorpus HyperdataCorpus
715 mk n h p u = insertNodesR [nodeCorpusW n h p u]
718 instance MkCorpus HyperdataAnnuaire
720 mk n h p u = insertNodesR [nodeAnnuaireW n h p u]
723 getOrMkList :: HasNodeError err => ParentId -> UserId -> Cmd err ListId
724 getOrMkList pId uId =
725 maybe (mkList' pId uId) (pure . view node_id) . headMay =<< getListsWithParentId pId
727 mkList' pId uId = maybe (nodeError MkNode) pure . headMay =<< mkNode NodeList pId uId
729 -- | TODO remove defaultList
730 defaultList :: HasNodeError err => CorpusId -> Cmd err ListId
732 maybe (nodeError NoListFound) (pure . view node_id) . headMay =<< getListsWithParentId cId
734 mkNode :: NodeType -> ParentId -> UserId -> Cmd err [NodeId]
735 mkNode nt p u = insertNodesR [nodeDefault nt p u]
737 mkDashboard :: ParentId -> UserId -> Cmd err [NodeId]
738 mkDashboard p u = insertNodesR [nodeDashboardW Nothing Nothing p u]
740 nodeDashboardW :: Maybe Name -> Maybe HyperdataDashboard -> ParentId -> UserId -> NodeWrite
741 nodeDashboardW maybeName maybeDashboard pId = node NodeDashboard name dashboard (Just pId)
743 name = maybe "Board" identity maybeName
744 dashboard = maybe arbitraryDashboard identity maybeDashboard
747 mkPhylo :: ParentId -> UserId -> Cmd err [NodeId]
748 mkPhylo p u = insertNodesR [nodePhyloW Nothing Nothing p u]
750 -- | Default CorpusId Master and ListId Master
752 pgNodeId :: NodeId -> Column PGInt4
753 pgNodeId = pgInt4 . id2int
755 getListsWithParentId :: NodeId -> Cmd err [Node HyperdataList]
756 getListsWithParentId n = runOpaQuery $ selectNodesWith' n (Just NodeList)
759 -- import Gargantext.Database.Node.UpdateOpaleye (updateHyperdata)
760 -- updateNodeUser_fake :: NodeId -> Cmd err Int64
761 -- updateNodeUser_fake n = updateHyperdata n fake_HyperdataUser