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.UpdateOpaleye (updateHyperdata)
30 import Gargantext.Prelude
31 import Gargantext.Prelude.Utils (hash)
32 import Gargantext.Database.Prelude
33 import Control.Lens (view)
34 import Gargantext.Config (GargConfig(..))
36 ------------------------------------------------------------------------
37 -- | TODO mk all others nodes
38 mkNodeWithParent :: (HasNodeError err)
44 mkNodeWithParent NodeUser (Just _) _ _ = nodeError UserNoParent
46 ------------------------------------------------------------------------
47 -- | MkNode, insert and eventually configure Hyperdata
48 mkNodeWithParent NodeUser Nothing uId name =
49 insertNodesWithParentR Nothing [node NodeUser name defaultHyperdataUser Nothing uId]
51 mkNodeWithParent _ Nothing _ _ = nodeError HasParent
52 ------------------------------------------------------------------------
53 mkNodeWithParent NodeFrameWrite i u n =
54 mkNodeWithParent_ConfigureHyperdata NodeFrameWrite i u n
56 mkNodeWithParent NodeFrameCalc i u n =
57 mkNodeWithParent_ConfigureHyperdata NodeFrameCalc i u n
59 mkNodeWithParent nt (Just pId) uId name = insertNode nt (Just name) Nothing pId uId
60 -- mkNodeWithParent _ _ _ _ = errorWith "[G.D.A.Node.mkNodeWithParent] nees parent"
63 -- | Sugar to create a node, get its NodeId and update its Hyperdata after
64 mkNodeWithParent_ConfigureHyperdata :: (HasNodeError err)
70 mkNodeWithParent_ConfigureHyperdata NodeFrameWrite (Just i) uId name =
71 mkNodeWithParent_ConfigureHyperdata' NodeFrameWrite (Just i) uId name
73 mkNodeWithParent_ConfigureHyperdata NodeFrameCalc (Just i) uId name =
74 mkNodeWithParent_ConfigureHyperdata' NodeFrameCalc (Just i) uId name
76 mkNodeWithParent_ConfigureHyperdata _ _ _ _ = nodeError NotImplYet
79 -- | Function not exposed
80 mkNodeWithParent_ConfigureHyperdata' :: (HasNodeError err)
86 mkNodeWithParent_ConfigureHyperdata' nt (Just i) uId name = do
87 maybeNodeId <- case nt of
88 NodeFrameWrite -> insertNode NodeFrameWrite (Just name) Nothing i uId
89 NodeFrameCalc -> insertNode NodeFrameCalc (Just name) Nothing i uId
90 _ -> nodeError NeedsConfiguration
93 [] -> nodeError (DoesNotExist i)
95 config <- view hasConfig
97 NodeFrameWrite -> pure $ _gc_frame_write_url config
98 NodeFrameCalc -> pure $ _gc_frame_calc_url config
99 _ -> nodeError NeedsConfiguration
101 s = _gc_secretkey config
102 hd = HyperdataFrame u (hash $ s <> (cs $ show n))
103 _ <- updateHyperdata n hd
105 (_:_:_) -> nodeError MkNode
106 mkNodeWithParent_ConfigureHyperdata' _ _ _ _ = nodeError HasParent