2 Module : Gargantext.API.Node
3 Description : Server API
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 -- TODO-SECURITY: Critical
12 -- TODO-ACCESS: CanGetNode
13 -- TODO-EVENTS: No events as this is a read only query.
15 -------------------------------------------------------------------
16 -- TODO-ACCESS: access by admin only.
17 -- At first let's just have an isAdmin check.
18 -- Later: check userId CanDeleteNodes Nothing
19 -- TODO-EVENTS: DeletedNodes [NodeId]
20 -- {"tag": "DeletedNodes", "nodes": [Int*]}
24 {-# OPTIONS_GHC -fno-warn-orphans #-}
26 {-# LANGUAGE ScopedTypeVariables #-}
27 {-# LANGUAGE TemplateHaskell #-}
28 {-# LANGUAGE TypeOperators #-}
30 module Gargantext.API.Node
33 import Data.Aeson (FromJSON, ToJSON)
34 import Data.Aeson.TH (deriveJSON)
37 import Data.Text (Text())
38 import GHC.Generics (Generic)
40 import Test.QuickCheck (elements)
41 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
43 import Gargantext.API.Admin.Auth (withAccess, PathId(..))
44 import Gargantext.API.Metrics
45 import Gargantext.API.Ngrams (TableNgramsApi, apiNgramsTableCorpus)
46 import Gargantext.API.Ngrams.Types (TabType(..))
47 import Gargantext.API.Node.File
48 import Gargantext.API.Node.New
49 import Gargantext.API.Prelude
50 import Gargantext.API.Table
51 import Gargantext.Core.Types (NodeTableResult)
52 import Gargantext.Core.Types.Individu (User(..))
53 import Gargantext.Core.Types.Main (Tree, NodeTree)
54 import Gargantext.Core.Utils.Prefix (unPrefix)
55 import Gargantext.Database.Action.Flow.Pairing (pairing)
56 import Gargantext.Database.Admin.Types.Hyperdata
57 import Gargantext.Database.Admin.Types.Node
58 import Gargantext.Database.Prelude -- (Cmd, CmdM)
59 import Gargantext.Database.Query.Facet (FacetDoc, OrderBy(..))
60 import Gargantext.Database.Query.Table.Node
61 import Gargantext.Database.Query.Table.Node.Children (getChildren)
62 import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..))
63 import Gargantext.Database.Query.Table.Node.Update (Update(..), update)
64 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
65 import Gargantext.Database.Query.Table.NodeNode
66 import Gargantext.Database.Query.Tree (tree, TreeMode(..))
67 import Gargantext.Prelude
68 import Gargantext.Core.Viz.Phylo.API (PhyloAPI, phyloAPI)
69 import qualified Gargantext.API.Node.Share as Share
70 import qualified Gargantext.API.Node.Update as Update
71 import qualified Gargantext.API.Search as Search
72 import qualified Gargantext.Database.Action.Delete as Action (deleteNode)
73 import qualified Gargantext.Database.Query.Table.Node.Update as U (update, Update(..))
76 import qualified Gargantext.Core.Text.List.Learn as Learn
77 import qualified Data.Vector as Vec
82 type NodesAPI = Delete '[JSON] Int
85 -- Be careful: really delete nodes
86 -- Access by admin only
87 nodesAPI :: [NodeId] -> GargServer NodesAPI
88 nodesAPI = deleteNodes
90 ------------------------------------------------------------------------
91 -- | TODO-ACCESS: access by admin only.
92 -- At first let's just have an isAdmin check.
93 -- Later: CanAccessAnyNode or (CanGetAnyNode, CanPutAnyNode)
94 -- To manage the Users roots
97 -- TODO needs design discussion.
98 type Roots = Get '[JSON] [Node HyperdataUser]
99 :<|> Put '[JSON] Int -- TODO
101 -- | TODO: access by admin only
102 roots :: GargServer Roots
103 roots = getNodesWithParentId Nothing
104 :<|> pure (panic "not implemented yet") -- TODO use patch map to update what we need
106 -------------------------------------------------------------------
107 -- | Node API Types management
108 -- TODO-ACCESS : access by users
109 -- No ownership check is needed if we strictly follow the capability model.
111 -- CanGetNode (Node, Children, TableApi, TableNgramsApiGet, PairingApi, ChartApi,
113 -- CanRenameNode (or part of CanEditNode?)
114 -- CanCreateChildren (PostNodeApi)
115 -- CanEditNode / CanPutNode TODO not implemented yet
117 -- CanPatch (TableNgramsApi)
121 type NodeAPI a = Get '[JSON] (Node a)
122 :<|> "rename" :> RenameApi
123 :<|> PostNodeApi -- TODO move to children POST
125 :<|> ReqBody '[JSON] a :> Put '[JSON] Int
126 :<|> "update" :> Update.API
127 :<|> Delete '[JSON] Int
128 :<|> "children" :> ChildrenApi a
131 :<|> "table" :> TableApi
132 :<|> "ngrams" :> TableNgramsApi
134 :<|> "category" :> CatApi
135 :<|> "search" :> (Search.API Search.SearchResult)
136 :<|> "share" :> Share.API
139 :<|> "pairwith" :> PairWith
140 :<|> "pairs" :> Pairs
141 :<|> "pairing" :> PairingApi
144 :<|> "metrics" :> ScatterAPI
145 :<|> "chart" :> ChartApi
147 :<|> "tree" :> TreeApi
148 :<|> "phylo" :> PhyloAPI
149 -- :<|> "add" :> NodeAddAPI
150 :<|> "move" :> MoveAPI
151 :<|> "unpublish" :> Share.Unpublish
153 :<|> "file" :> FileApi
154 :<|> "async" :> FileAsyncApi
156 -- TODO-ACCESS: check userId CanRenameNode nodeId
157 -- TODO-EVENTS: NodeRenamed RenameNode or re-use some more general NodeEdited...
158 type RenameApi = Summary " Rename Node"
159 :> ReqBody '[JSON] RenameNode
162 type PostNodeApi = Summary " PostNode Node with ParentId as {id}"
163 :> ReqBody '[JSON] PostNode
164 :> Post '[JSON] [NodeId]
166 type ChildrenApi a = Summary " Summary children"
167 :> QueryParam "type" NodeType
168 :> QueryParam "offset" Int
169 :> QueryParam "limit" Int
170 -- :> Get '[JSON] [Node a]
171 :> Get '[JSON] (NodeTableResult a)
173 ------------------------------------------------------------------------
174 type NodeNodeAPI a = Get '[JSON] (Node a)
176 nodeNodeAPI :: forall proxy a. (JSONB a, ToJSON a)
181 -> GargServer (NodeNodeAPI a)
182 nodeNodeAPI p uId cId nId = withAccess (Proxy :: Proxy (NodeNodeAPI a)) Proxy uId (PathNodeNode cId nId) nodeNodeAPI'
184 nodeNodeAPI' :: GargServer (NodeNodeAPI a)
185 nodeNodeAPI' = getNodeWith nId p
187 ------------------------------------------------------------------------
188 -- TODO: make the NodeId type indexed by `a`, then we no longer need the proxy.
189 nodeAPI :: forall proxy a.
196 -> GargServer (NodeAPI a)
197 nodeAPI p uId id' = withAccess (Proxy :: Proxy (NodeAPI a)) Proxy uId (PathNode id') nodeAPI'
199 nodeAPI' :: GargServer (NodeAPI a)
200 nodeAPI' = getNodeWith id' p
202 :<|> postNode uId id'
203 :<|> postNodeAsyncAPI uId id'
205 :<|> Update.api uId id'
206 :<|> Action.deleteNode (RootId $ NodeId uId) id'
207 :<|> getChildren id' p
211 :<|> apiNgramsTableCorpus id'
226 :<|> phyloAPI id' uId
227 :<|> moveNode (RootId $ NodeId uId) id'
228 -- :<|> nodeAddAPI id'
229 -- :<|> postUpload id'
230 :<|> Share.unPublish id'
233 :<|> fileAsyncApi uId id'
236 ------------------------------------------------------------------------
237 data RenameNode = RenameNode { r_name :: Text }
240 ------------------------------------------------------------------------
241 ------------------------------------------------------------------------
242 type CatApi = Summary " To Categorize NodeNodes: 0 for delete, 1/null neutral, 2 favorite"
243 :> ReqBody '[JSON] NodesToCategory
246 data NodesToCategory = NodesToCategory { ntc_nodesId :: [NodeId]
247 , ntc_category :: Int
251 -- TODO unPrefix "ntc_" FromJSON, ToJSON, ToSchema, adapt frontend.
252 instance FromJSON NodesToCategory
253 instance ToJSON NodesToCategory
254 instance ToSchema NodesToCategory
256 catApi :: CorpusId -> GargServer CatApi
259 putCat :: CorpusId -> NodesToCategory -> Cmd err [Int]
260 putCat cId cs' = nodeNodesCategory $ map (\n -> (cId, n, ntc_category cs')) (ntc_nodesId cs')
262 ------------------------------------------------------------------------
263 -- TODO adapt FacetDoc -> ListDoc (and add type of document as column)
264 -- Pairing utilities to move elsewhere
265 type PairingApi = Summary " Pairing API"
266 :> QueryParam "view" TabType
267 -- TODO change TabType -> DocType (CorpusId for pairing)
268 :> QueryParam "offset" Int
269 :> QueryParam "limit" Int
270 :> QueryParam "order" OrderBy
271 :> Get '[JSON] [FacetDoc]
274 type Pairs = Summary "List of Pairs"
275 :> Get '[JSON] [AnnuaireId]
276 pairs :: CorpusId -> GargServer Pairs
278 ns <- getNodeNode cId
279 pure $ map _nn_node2_id ns
281 type PairWith = Summary "Pair a Corpus with an Annuaire"
282 :> "annuaire" :> Capture "annuaire_id" AnnuaireId
283 :> QueryParam "list_id" ListId
286 pairWith :: CorpusId -> GargServer PairWith
287 pairWith cId aId lId = do
288 r <- pairing cId aId lId
289 _ <- insertNodeNode [ NodeNode cId aId Nothing Nothing]
293 ------------------------------------------------------------------------
294 type TreeAPI = QueryParams "type" NodeType :> Get '[JSON] (Tree NodeTree)
296 treeAPI :: NodeId -> GargServer TreeAPI
297 treeAPI = tree TreeAdvanced
299 ------------------------------------------------------------------------
300 -- | TODO Check if the name is less than 255 char
301 rename :: NodeId -> RenameNode -> Cmd err [Int]
302 rename nId (RenameNode name') = U.update (U.Rename nId name')
304 putNode :: forall err a. (HasNodeError err, JSONB a, ToJSON a)
308 putNode n h = fromIntegral <$> updateHyperdata n h
310 -------------------------------------------------------------
311 type MoveAPI = Summary "Move Node endpoint"
312 :> Capture "parent_id" ParentId
319 moveNode _u n p = update (Move n p)
320 -------------------------------------------------------------
323 $(deriveJSON (unPrefix "r_" ) ''RenameNode )
324 instance ToSchema RenameNode
325 instance Arbitrary RenameNode where
326 arbitrary = elements [RenameNode "test"]
329 -------------------------------------------------------------