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 {-# LANGUAGE ScopedTypeVariables #-}
25 {-# LANGUAGE TemplateHaskell #-}
26 {-# LANGUAGE TypeOperators #-}
28 module Gargantext.API.Node
31 import Data.Aeson (FromJSON, ToJSON)
32 import Data.Aeson.TH (deriveJSON)
35 import Data.Text (Text())
36 import GHC.Generics (Generic)
37 import Gargantext.API.Admin.Auth (withAccess)
38 import Gargantext.API.Admin.Auth.Types (PathId(..))
39 import Gargantext.API.Metrics
40 import Gargantext.API.Ngrams (TableNgramsApi, apiNgramsTableCorpus)
41 import Gargantext.API.Ngrams.Types (TabType(..))
42 import Gargantext.API.Node.File
43 import Gargantext.API.Node.New
44 import Gargantext.API.Prelude
45 import Gargantext.API.Table
46 import Gargantext.Core.Types (NodeTableResult)
47 import Gargantext.Core.Types.Individu (User(..))
48 import Gargantext.Core.Types.Main (Tree, NodeTree)
49 import Gargantext.Core.Utils.Prefix (unPrefix)
50 import Gargantext.Core.Viz.Phylo.API (PhyloAPI, phyloAPI)
51 import Gargantext.Database.Action.Flow.Pairing (pairing)
52 import Gargantext.Database.Admin.Types.Hyperdata
53 import Gargantext.Database.Admin.Types.Node
54 import Gargantext.Database.Prelude -- (Cmd, CmdM)
55 import Gargantext.Database.Query.Facet (FacetDoc, OrderBy(..))
56 import Gargantext.Database.Query.Table.Node
57 import Gargantext.Database.Query.Table.Node.Children (getChildren)
58 import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..))
59 import Gargantext.Database.Query.Table.Node.Update (Update(..), update)
60 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
61 import Gargantext.Database.Query.Table.NodeContext (nodeContextsCategory, nodeContextsScore)
62 import Gargantext.Database.Query.Table.NodeNode
63 import Gargantext.Database.Query.Tree (tree, TreeMode(..))
64 import Gargantext.Prelude
66 import Test.QuickCheck (elements)
67 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
68 import qualified Gargantext.API.Node.DocumentUpload as DocumentUpload
69 import qualified Gargantext.API.Node.DocumentsFromWriteNodes as DocumentsFromWriteNodes
70 import qualified Gargantext.API.Node.FrameCalcUpload as FrameCalcUpload
71 import qualified Gargantext.API.Node.Share as Share
72 import qualified Gargantext.API.Node.Update as Update
73 import qualified Gargantext.API.Search as Search
74 import qualified Gargantext.Database.Action.Delete as Action (deleteNode)
75 import qualified Gargantext.Database.Query.Table.Node.Update as U (update, Update(..))
80 type NodesAPI = Delete '[JSON] Int
83 -- Be careful: really delete nodes
84 -- Access by admin only
85 nodesAPI :: [NodeId] -> GargServer NodesAPI
86 nodesAPI = deleteNodes
88 ------------------------------------------------------------------------
89 -- | TODO-ACCESS: access by admin only.
90 -- At first let's just have an isAdmin check.
91 -- Later: CanAccessAnyNode or (CanGetAnyNode, CanPutAnyNode)
92 -- To manage the Users roots
95 -- TODO needs design discussion.
96 type Roots = Get '[JSON] [Node HyperdataUser]
97 :<|> Put '[JSON] Int -- TODO
99 -- | TODO: access by admin only
100 roots :: GargServer Roots
101 roots = getNodesWithParentId Nothing
102 :<|> pure (panic "not implemented yet") -- TODO use patch map to update what we need
104 -------------------------------------------------------------------
105 -- | Node API Types management
106 -- TODO-ACCESS : access by users
107 -- No ownership check is needed if we strictly follow the capability model.
109 -- CanGetNode (Node, Children, TableApi, TableNgramsApiGet, PairingApi, ChartApi,
111 -- CanRenameNode (or part of CanEditNode?)
112 -- CanCreateChildren (PostNodeApi)
113 -- CanEditNode / CanPutNode TODO not implemented yet
115 -- CanPatch (TableNgramsApi)
119 type NodeAPI a = Get '[JSON] (Node a)
120 :<|> "rename" :> RenameApi
121 :<|> PostNodeApi -- TODO move to children POST
123 :<|> FrameCalcUpload.API
124 :<|> ReqBody '[JSON] a :> Put '[JSON] Int
125 :<|> "update" :> Update.API
126 :<|> Delete '[JSON] Int
127 :<|> "children" :> ChildrenApi a
130 :<|> "table" :> TableApi
131 :<|> "ngrams" :> TableNgramsApi
133 :<|> "category" :> CatApi
134 :<|> "score" :> ScoreApi
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 :<|> "documents-from-write-nodes" :> DocumentsFromWriteNodes.API
157 :<|> DocumentUpload.API
159 -- TODO-ACCESS: check userId CanRenameNode nodeId
160 -- TODO-EVENTS: NodeRenamed RenameNode or re-use some more general NodeEdited...
161 type RenameApi = Summary " Rename Node"
162 :> ReqBody '[JSON] RenameNode
165 type PostNodeApi = Summary " PostNode Node with ParentId as {id}"
166 :> ReqBody '[JSON] PostNode
167 :> Post '[JSON] [NodeId]
169 type ChildrenApi a = Summary " Summary children"
170 :> QueryParam "type" NodeType
171 :> QueryParam "offset" Int
172 :> QueryParam "limit" Int
173 -- :> Get '[JSON] [Node a]
174 :> Get '[JSON] (NodeTableResult a)
176 ------------------------------------------------------------------------
177 type NodeNodeAPI a = Get '[JSON] (Node a)
179 nodeNodeAPI :: forall proxy a. (JSONB a, ToJSON a)
184 -> GargServer (NodeNodeAPI a)
185 nodeNodeAPI p uId cId nId = withAccess (Proxy :: Proxy (NodeNodeAPI a)) Proxy uId (PathNodeNode cId nId) nodeNodeAPI'
187 nodeNodeAPI' :: GargServer (NodeNodeAPI a)
188 nodeNodeAPI' = getNodeWith nId p
190 ------------------------------------------------------------------------
191 -- TODO: make the NodeId type indexed by `a`, then we no longer need the proxy.
192 nodeAPI :: forall proxy a.
199 -> GargServer (NodeAPI a)
200 nodeAPI p uId id' = withAccess (Proxy :: Proxy (NodeAPI a)) Proxy uId (PathNode id') nodeAPI'
202 nodeAPI' :: GargServer (NodeAPI a)
203 nodeAPI' = getNodeWith id' p
205 :<|> postNode uId id'
206 :<|> postNodeAsyncAPI uId id'
207 :<|> FrameCalcUpload.api uId id'
209 :<|> Update.api uId id'
210 :<|> Action.deleteNode (RootId $ NodeId uId) id'
211 :<|> getChildren id' p
215 :<|> apiNgramsTableCorpus id'
220 :<|> Share.api (RootId $ NodeId uId) id'
231 :<|> phyloAPI id' uId
232 :<|> moveNode (RootId $ NodeId uId) id'
233 -- :<|> nodeAddAPI id'
234 -- :<|> postUpload id'
235 :<|> Share.unPublish id'
238 :<|> fileAsyncApi uId id'
240 :<|> DocumentsFromWriteNodes.api uId id'
241 :<|> DocumentUpload.api uId id'
244 ------------------------------------------------------------------------
245 data RenameNode = RenameNode { r_name :: Text }
248 ------------------------------------------------------------------------
249 ------------------------------------------------------------------------
250 type CatApi = Summary " To Categorize NodeNodes: 0 for delete, 1/null neutral, 2 favorite"
251 :> ReqBody '[JSON] NodesToCategory
254 data NodesToCategory = NodesToCategory { ntc_nodesId :: [NodeId]
255 , ntc_category :: Int
259 -- TODO unPrefix "ntc_" FromJSON, ToJSON, ToSchema, adapt frontend.
260 instance FromJSON NodesToCategory
261 instance ToJSON NodesToCategory
262 instance ToSchema NodesToCategory
264 catApi :: CorpusId -> GargServer CatApi
266 ret <- nodeContextsCategory $ map (\n -> (cId, n, ntc_category cs')) (ntc_nodesId cs')
267 lId <- defaultList cId
268 _ <- updateChart cId (Just lId) Docs Nothing
271 ------------------------------------------------------------------------
272 type ScoreApi = Summary " To Score NodeNodes"
273 :> ReqBody '[JSON] NodesToScore
276 data NodesToScore = NodesToScore { nts_nodesId :: [NodeId]
281 -- TODO unPrefix "ntc_" FromJSON, ToJSON, ToSchema, adapt frontend.
282 instance FromJSON NodesToScore
283 instance ToJSON NodesToScore
284 instance ToSchema NodesToScore
286 scoreApi :: CorpusId -> GargServer ScoreApi
289 putScore :: CorpusId -> NodesToScore -> Cmd err [Int]
290 putScore cId cs' = nodeContextsScore $ map (\n -> (cId, n, nts_score cs')) (nts_nodesId cs')
292 ------------------------------------------------------------------------
293 -- TODO adapt FacetDoc -> ListDoc (and add type of document as column)
294 -- Pairing utilities to move elsewhere
295 type PairingApi = Summary " Pairing API"
296 :> QueryParam "view" TabType
297 -- TODO change TabType -> DocType (CorpusId for pairing)
298 :> QueryParam "offset" Int
299 :> QueryParam "limit" Int
300 :> QueryParam "order" OrderBy
301 :> Get '[JSON] [FacetDoc]
304 type Pairs = Summary "List of Pairs"
305 :> Get '[JSON] [AnnuaireId]
306 pairs :: CorpusId -> GargServer Pairs
308 ns <- getNodeNode cId
309 pure $ map _nn_node2_id ns
311 type PairWith = Summary "Pair a Corpus with an Annuaire"
312 :> "annuaire" :> Capture "annuaire_id" AnnuaireId
313 :> QueryParam "list_id" ListId
314 :> Post '[JSON] [Int]
316 pairWith :: CorpusId -> GargServer PairWith
317 pairWith cId aId lId = do
318 r <- pairing cId aId lId
319 _ <- insertNodeNode [ NodeNode { _nn_node1_id = cId
321 , _nn_score = Nothing
322 , _nn_category = Nothing }]
326 ------------------------------------------------------------------------
327 type TreeAPI = QueryParams "type" NodeType
328 :> Get '[JSON] (Tree NodeTree)
330 :> QueryParams "type" NodeType
331 :> Get '[JSON] (Tree NodeTree)
333 treeAPI :: NodeId -> GargServer TreeAPI
334 treeAPI id = tree TreeAdvanced id
335 :<|> tree TreeFirstLevel id
337 ------------------------------------------------------------------------
338 -- | TODO Check if the name is less than 255 char
339 rename :: NodeId -> RenameNode -> Cmd err [Int]
340 rename nId (RenameNode name') = U.update (U.Rename nId name')
342 putNode :: forall err a. (HasNodeError err, JSONB a, ToJSON a)
346 putNode n h = fromIntegral <$> updateHyperdata n h
348 -------------------------------------------------------------
349 type MoveAPI = Summary "Move Node endpoint"
350 :> Capture "parent_id" ParentId
357 moveNode _u n p = update (Move n p)
358 -------------------------------------------------------------
361 $(deriveJSON (unPrefix "r_" ) ''RenameNode )
362 instance ToSchema RenameNode
363 instance Arbitrary RenameNode where
364 arbitrary = elements [RenameNode "test"]
367 -------------------------------------------------------------