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.Admin.EnvTypes
40 import Gargantext.API.Metrics
41 import Gargantext.API.Ngrams (TableNgramsApi, apiNgramsTableCorpus)
42 import Gargantext.API.Ngrams.Types (TabType(..))
43 import Gargantext.API.Node.File
44 import Gargantext.API.Node.New
45 import Gargantext.API.Prelude
46 import Gargantext.API.Table
47 import Gargantext.Core.Types (NodeTableResult)
48 import Gargantext.Core.Types.Individu (User(..))
49 import Gargantext.Core.Types.Main (Tree, NodeTree)
50 import Gargantext.Core.Utils.Prefix (unPrefix)
51 import Gargantext.Core.Viz.Phylo.API (PhyloAPI, phyloAPI)
52 import Gargantext.Database.Action.Flow.Pairing (pairing)
53 import Gargantext.Database.Admin.Types.Hyperdata
54 import Gargantext.Database.Admin.Types.Node
55 import Gargantext.Database.Prelude -- (Cmd, CmdM)
56 import Gargantext.Database.Query.Facet (FacetDoc, OrderBy(..))
57 import Gargantext.Database.Query.Table.Node
58 import Gargantext.Database.Query.Table.Node.Children (getChildren)
59 import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..))
60 import Gargantext.Database.Query.Table.Node.Update (Update(..), update)
61 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
62 import Gargantext.Database.Query.Table.NodeContext (nodeContextsCategory, nodeContextsScore)
63 import Gargantext.Database.Query.Table.NodeNode
64 import Gargantext.Database.Query.Tree (tree, TreeMode(..))
65 import Gargantext.Prelude
67 import Test.QuickCheck (elements)
68 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
69 import qualified Gargantext.API.Node.DocumentUpload as DocumentUpload
70 import qualified Gargantext.API.Node.DocumentsFromWriteNodes as DocumentsFromWriteNodes
71 import qualified Gargantext.API.Node.FrameCalcUpload as FrameCalcUpload
72 import qualified Gargantext.API.Node.Share as Share
73 import qualified Gargantext.API.Node.Update as Update
74 import qualified Gargantext.API.Search as Search
75 import qualified Gargantext.Database.Action.Delete as Action (deleteNode)
76 import qualified Gargantext.Database.Query.Table.Node.Update as U (update, Update(..))
81 type NodesAPI = Delete '[JSON] Int
84 -- Be careful: really delete nodes
85 -- Access by admin only
86 nodesAPI :: [NodeId] -> GargServer NodesAPI
87 nodesAPI = deleteNodes
89 ------------------------------------------------------------------------
90 -- | TODO-ACCESS: access by admin only.
91 -- At first let's just have an isAdmin check.
92 -- Later: CanAccessAnyNode or (CanGetAnyNode, CanPutAnyNode)
93 -- To manage the Users roots
96 -- TODO needs design discussion.
97 type Roots = Get '[JSON] [Node HyperdataUser]
98 :<|> Put '[JSON] Int -- TODO
100 -- | TODO: access by admin only
101 roots :: GargServer Roots
102 roots = getNodesWithParentId Nothing
103 :<|> pure (panic "not implemented yet") -- TODO use patch map to update what we need
105 -------------------------------------------------------------------
106 -- | Node API Types management
107 -- TODO-ACCESS : access by users
108 -- No ownership check is needed if we strictly follow the capability model.
110 -- CanGetNode (Node, Children, TableApi, TableNgramsApiGet, PairingApi, ChartApi,
112 -- CanRenameNode (or part of CanEditNode?)
113 -- CanCreateChildren (PostNodeApi)
114 -- CanEditNode / CanPutNode TODO not implemented yet
116 -- CanPatch (TableNgramsApi)
120 type NodeAPI a = Get '[JSON] (Node a)
121 :<|> "rename" :> RenameApi
122 :<|> PostNodeApi -- TODO move to children POST
124 :<|> FrameCalcUpload.API
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 :<|> "score" :> ScoreApi
136 :<|> "search" :> (Search.API Search.SearchResult)
137 :<|> "share" :> Share.API
140 :<|> "pairwith" :> PairWith
141 :<|> "pairs" :> Pairs
142 :<|> "pairing" :> PairingApi
145 :<|> "metrics" :> ScatterAPI
146 :<|> "chart" :> ChartApi
148 :<|> "tree" :> TreeApi
149 :<|> "phylo" :> PhyloAPI
150 -- :<|> "add" :> NodeAddAPI
151 :<|> "move" :> MoveAPI
152 :<|> "unpublish" :> Share.Unpublish
154 :<|> "file" :> FileApi
155 :<|> "async" :> FileAsyncApi
157 :<|> "documents-from-write-nodes" :> DocumentsFromWriteNodes.API
158 :<|> DocumentUpload.API
160 -- TODO-ACCESS: check userId CanRenameNode nodeId
161 -- TODO-EVENTS: NodeRenamed RenameNode or re-use some more general NodeEdited...
162 type RenameApi = Summary " Rename Node"
163 :> ReqBody '[JSON] RenameNode
166 type PostNodeApi = Summary " PostNode Node with ParentId as {id}"
167 :> ReqBody '[JSON] PostNode
168 :> Post '[JSON] [NodeId]
170 type ChildrenApi a = Summary " Summary children"
171 :> QueryParam "type" NodeType
172 :> QueryParam "offset" Int
173 :> QueryParam "limit" Int
174 -- :> Get '[JSON] [Node a]
175 :> Get '[JSON] (NodeTableResult a)
177 ------------------------------------------------------------------------
178 type NodeNodeAPI a = Get '[JSON] (Node a)
180 nodeNodeAPI :: forall proxy a. (JSONB a, ToJSON a)
185 -> GargServer (NodeNodeAPI a)
186 nodeNodeAPI p uId cId nId = withAccess (Proxy :: Proxy (NodeNodeAPI a)) Proxy uId (PathNodeNode cId nId) nodeNodeAPI'
188 nodeNodeAPI' :: GargServer (NodeNodeAPI a)
189 nodeNodeAPI' = getNodeWith nId p
191 ------------------------------------------------------------------------
192 -- TODO: make the NodeId type indexed by `a`, then we no longer need the proxy.
193 nodeAPI :: forall proxy a.
200 -> ServerT (NodeAPI a) (GargM Env GargError)
201 nodeAPI p uId id' = withAccess (Proxy :: Proxy (NodeAPI a)) Proxy uId (PathNode id') nodeAPI'
203 nodeAPI' :: ServerT (NodeAPI a) (GargM Env GargError)
204 nodeAPI' = getNodeWith id' p
206 :<|> postNode uId id'
207 :<|> postNodeAsyncAPI uId id'
208 :<|> FrameCalcUpload.api uId id'
210 :<|> Update.api uId id'
211 :<|> Action.deleteNode (RootId $ NodeId uId) id'
212 :<|> getChildren id' p
216 :<|> apiNgramsTableCorpus id'
221 :<|> Share.api (RootId $ NodeId uId) id'
232 :<|> phyloAPI id' uId
233 :<|> moveNode (RootId $ NodeId uId) id'
234 -- :<|> nodeAddAPI id'
235 -- :<|> postUpload id'
236 :<|> Share.unPublish id'
239 :<|> fileAsyncApi uId id'
241 :<|> DocumentsFromWriteNodes.api uId id'
242 :<|> DocumentUpload.api uId id'
245 ------------------------------------------------------------------------
246 data RenameNode = RenameNode { r_name :: Text }
249 ------------------------------------------------------------------------
250 ------------------------------------------------------------------------
251 type CatApi = Summary " To Categorize NodeNodes: 0 for delete, 1/null neutral, 2 favorite"
252 :> ReqBody '[JSON] NodesToCategory
255 data NodesToCategory = NodesToCategory { ntc_nodesId :: [NodeId]
256 , ntc_category :: Int
260 -- TODO unPrefix "ntc_" FromJSON, ToJSON, ToSchema, adapt frontend.
261 instance FromJSON NodesToCategory
262 instance ToJSON NodesToCategory
263 instance ToSchema NodesToCategory
265 catApi :: CorpusId -> GargServer CatApi
267 ret <- nodeContextsCategory $ map (\n -> (cId, n, ntc_category cs')) (ntc_nodesId cs')
268 lId <- defaultList cId
269 _ <- updateChart cId (Just lId) Docs Nothing
272 ------------------------------------------------------------------------
273 type ScoreApi = Summary " To Score NodeNodes"
274 :> ReqBody '[JSON] NodesToScore
277 data NodesToScore = NodesToScore { nts_nodesId :: [NodeId]
282 -- TODO unPrefix "ntc_" FromJSON, ToJSON, ToSchema, adapt frontend.
283 instance FromJSON NodesToScore
284 instance ToJSON NodesToScore
285 instance ToSchema NodesToScore
287 scoreApi :: CorpusId -> GargServer ScoreApi
290 putScore :: CorpusId -> NodesToScore -> Cmd err [Int]
291 putScore cId cs' = nodeContextsScore $ map (\n -> (cId, n, nts_score cs')) (nts_nodesId cs')
293 ------------------------------------------------------------------------
294 -- TODO adapt FacetDoc -> ListDoc (and add type of document as column)
295 -- Pairing utilities to move elsewhere
296 type PairingApi = Summary " Pairing API"
297 :> QueryParam "view" TabType
298 -- TODO change TabType -> DocType (CorpusId for pairing)
299 :> QueryParam "offset" Int
300 :> QueryParam "limit" Int
301 :> QueryParam "order" OrderBy
302 :> Get '[JSON] [FacetDoc]
305 type Pairs = Summary "List of Pairs"
306 :> Get '[JSON] [AnnuaireId]
307 pairs :: CorpusId -> GargServer Pairs
309 ns <- getNodeNode cId
310 pure $ map _nn_node2_id ns
312 type PairWith = Summary "Pair a Corpus with an Annuaire"
313 :> "annuaire" :> Capture "annuaire_id" AnnuaireId
314 :> QueryParam "list_id" ListId
315 :> Post '[JSON] [Int]
317 pairWith :: CorpusId -> GargServer PairWith
318 pairWith cId aId lId = do
319 r <- pairing cId aId lId
320 _ <- insertNodeNode [ NodeNode { _nn_node1_id = cId
322 , _nn_score = Nothing
323 , _nn_category = Nothing }]
327 ------------------------------------------------------------------------
328 type TreeAPI = QueryParams "type" NodeType
329 :> Get '[JSON] (Tree NodeTree)
331 :> QueryParams "type" NodeType
332 :> Get '[JSON] (Tree NodeTree)
334 treeAPI :: NodeId -> GargServer TreeAPI
335 treeAPI id = tree TreeAdvanced id
336 :<|> tree TreeFirstLevel id
338 ------------------------------------------------------------------------
339 -- | TODO Check if the name is less than 255 char
340 rename :: NodeId -> RenameNode -> Cmd err [Int]
341 rename nId (RenameNode name') = U.update (U.Rename nId name')
343 putNode :: forall err a. (HasNodeError err, JSONB a, ToJSON a)
347 putNode n h = fromIntegral <$> updateHyperdata n h
349 -------------------------------------------------------------
350 type MoveAPI = Summary "Move Node endpoint"
351 :> Capture "parent_id" ParentId
358 moveNode _u n p = update (Move n p)
359 -------------------------------------------------------------
362 $(deriveJSON (unPrefix "r_" ) ''RenameNode )
363 instance ToSchema RenameNode
364 instance Arbitrary RenameNode where
365 arbitrary = elements [RenameNode "test"]
368 -------------------------------------------------------------