2 Module : Gargantext.Database.Action.Node
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 #-}
18 {-# LANGUAGE TypeFamilies #-}
20 module Gargantext.Database.Action.Node
23 import Gargantext.Core.Types (Name)
24 import Gargantext.Database.Admin.Types.Hyperdata
25 import Gargantext.Database.Admin.Types.Node
26 import Gargantext.Database.Prelude (Cmd)
27 import Gargantext.Database.Query.Table.Node
28 import Gargantext.Database.Query.Table.Node.Error
29 import Gargantext.Database.Query.Table.Node.User
30 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
31 import Gargantext.Viz.Graph (defaultHyperdataGraph)
32 import Gargantext.Prelude
33 import Gargantext.Prelude.Utils (sha)
34 import Gargantext.Database.Prelude
35 import Control.Lens (view)
36 import Gargantext.Config (GargConfig(..))
38 ------------------------------------------------------------------------
39 -- | TODO mk all others nodes
40 mkNodeWithParent :: (HasNodeError err)
46 mkNodeWithParent NodeUser (Just _) _ _ = nodeError UserNoParent
48 ------------------------------------------------------------------------
49 mkNodeWithParent NodeUser Nothing uId name =
50 insertNodesWithParentR Nothing [node NodeUser name fake_HyperdataUser Nothing uId]
52 mkNodeWithParent _ Nothing _ _ = nodeError HasParent
53 ------------------------------------------------------------------------
54 mkNodeWithParent NodeFolder (Just i) uId name =
55 insertNodesWithParentR (Just i) [node NodeFolder name hd Nothing uId]
57 hd = defaultHyperdataFolder
59 mkNodeWithParent NodeFolderPrivate (Just i) uId _ =
60 insertNodesWithParentR (Just i) [node NodeFolderPrivate "Private" hd Nothing uId]
62 hd = defaultHyperdataFolder
64 mkNodeWithParent NodeFolderShared (Just i) uId _ =
65 insertNodesWithParentR (Just i) [node NodeFolderShared "Shared" hd Nothing uId]
67 hd = defaultHyperdataFolder
69 mkNodeWithParent NodeFolderPublic (Just i) uId _ =
70 insertNodesWithParentR (Just i) [node NodeFolderPublic "Public" hd Nothing uId]
72 hd = defaultHyperdataFolder
74 mkNodeWithParent NodeTeam (Just i) uId name =
75 insertNodesWithParentR (Just i) [node NodeTeam name hd Nothing uId]
77 hd = defaultHyperdataFolder
78 ------------------------------------------------------------------------
79 mkNodeWithParent NodeCorpus (Just i) uId name =
80 insertNodesWithParentR (Just i) [node NodeCorpus name hd Nothing uId]
82 hd = defaultHyperdataCorpus
84 mkNodeWithParent NodeAnnuaire (Just i) uId name =
85 insertNodesWithParentR (Just i) [node NodeAnnuaire name hd Nothing uId]
87 hd = defaultHyperdataAnnuaire
89 mkNodeWithParent NodeList (Just i) uId name =
90 insertNodesWithParentR (Just i) [node NodeList name hd Nothing uId]
92 hd = defaultHyperdataAnnuaire
94 mkNodeWithParent NodeGraph (Just i) uId name =
95 insertNodesWithParentR (Just i) [node NodeGraph name hd Nothing uId]
97 hd = defaultHyperdataGraph
99 mkNodeWithParent NodeFrameWrite i u n =
100 mkNodeWithParent_ConfigureHyperdata NodeFrameWrite i u n
102 mkNodeWithParent NodeFrameCalc i u n =
103 mkNodeWithParent_ConfigureHyperdata NodeFrameCalc i u n
106 mkNodeWithParent n (Just i) uId name =
107 insertNodesWithParentR (Just i) [node NodeDashboard name (hasDefaultData n) Nothing uId]
110 mkNodeWithParent _ _ _ _ = nodeError NotImplYet
113 -- | Sugar to create a node, get his NodeId and update his Hyperdata after
114 mkNodeWithParent_ConfigureHyperdata :: (HasNodeError err)
120 mkNodeWithParent_ConfigureHyperdata NodeFrameWrite (Just i) uId name =
121 mkNodeWithParent_ConfigureHyperdata' NodeFrameWrite (Just i) uId name
123 mkNodeWithParent_ConfigureHyperdata NodeFrameCalc (Just i) uId name =
124 mkNodeWithParent_ConfigureHyperdata' NodeFrameCalc (Just i) uId name
126 mkNodeWithParent_ConfigureHyperdata _ _ _ _ = nodeError NotImplYet
129 -- | Function not exposed
130 mkNodeWithParent_ConfigureHyperdata' :: (HasNodeError err)
136 mkNodeWithParent_ConfigureHyperdata' nt (Just i) uId name = do
137 maybeNodeId <- insertNodesWithParentR (Just i) [node nt name defaultFolder Nothing uId]
139 [] -> nodeError (DoesNotExist i)
141 config <- view hasConfig
143 NodeFrameWrite -> pure $ _gc_frame_write_url config
144 NodeFrameCalc -> pure $ _gc_frame_calc_url config
145 _ -> nodeError NeedsConfiguration
147 s = _gc_secretkey config
148 hd = HyperdataFrame u (sha $ s <> (cs $ show n))
149 _ <- updateHyperdata n hd
151 (_:_:_) -> nodeError MkNode
152 mkNodeWithParent_ConfigureHyperdata' _ _ _ _ = nodeError HasParent