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.Types.Query (Limit, Offset)
51 import Gargantext.Core.Utils.Prefix (unPrefix)
52 import Gargantext.Core.Viz.Phylo.API (PhyloAPI, phyloAPI)
53 import Gargantext.Database.Action.Flow.Pairing (pairing)
54 import Gargantext.Database.Admin.Types.Hyperdata
55 import Gargantext.Database.Admin.Types.Node
56 import Gargantext.Database.Prelude -- (Cmd, CmdM)
57 import Gargantext.Database.Query.Facet (FacetDoc, OrderBy(..))
58 import Gargantext.Database.Query.Table.Node
59 import Gargantext.Database.Query.Table.Node.Children (getChildren)
60 import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..))
61 import Gargantext.Database.Query.Table.Node.Update (Update(..), update)
62 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
63 import Gargantext.Database.Query.Table.NodeContext (nodeContextsCategory, nodeContextsScore)
64 import Gargantext.Database.Query.Table.NodeNode
65 import Gargantext.Database.Query.Tree (tree, TreeMode(..))
66 import Gargantext.Prelude
68 import Test.QuickCheck (elements)
69 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
70 import qualified Gargantext.API.Node.DocumentUpload as DocumentUpload
71 import qualified Gargantext.API.Node.DocumentsFromWriteNodes as DocumentsFromWriteNodes
72 import qualified Gargantext.API.Node.FrameCalcUpload as FrameCalcUpload
73 import qualified Gargantext.API.Node.Share as Share
74 import qualified Gargantext.API.Node.Update as Update
75 import qualified Gargantext.API.Search as Search
76 import qualified Gargantext.Database.Action.Delete as Action (deleteNode)
77 import qualified Gargantext.Database.Query.Table.Node.Update as U (update, Update(..))
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 :<|> FrameCalcUpload.API
126 :<|> ReqBody '[JSON] a :> Put '[JSON] Int
127 :<|> "update" :> Update.API
128 :<|> Delete '[JSON] Int
129 :<|> "children" :> ChildrenApi a
132 :<|> "table" :> TableApi
133 :<|> "ngrams" :> TableNgramsApi
135 :<|> "category" :> CatApi
136 :<|> "score" :> ScoreApi
137 :<|> "search" :> (Search.API Search.SearchResult)
138 :<|> "share" :> Share.API
141 :<|> "pairwith" :> PairWith
142 :<|> "pairs" :> Pairs
143 :<|> "pairing" :> PairingApi
146 :<|> "metrics" :> ScatterAPI
147 :<|> "chart" :> ChartApi
149 :<|> "tree" :> TreeApi
150 :<|> "phylo" :> PhyloAPI
151 -- :<|> "add" :> NodeAddAPI
152 :<|> "move" :> MoveAPI
153 :<|> "unpublish" :> Share.Unpublish
155 :<|> "file" :> FileApi
156 :<|> "async" :> FileAsyncApi
158 :<|> "documents-from-write-nodes" :> DocumentsFromWriteNodes.API
159 :<|> DocumentUpload.API
161 -- TODO-ACCESS: check userId CanRenameNode nodeId
162 -- TODO-EVENTS: NodeRenamed RenameNode or re-use some more general NodeEdited...
163 type RenameApi = Summary " Rename Node"
164 :> ReqBody '[JSON] RenameNode
167 type PostNodeApi = Summary " PostNode Node with ParentId as {id}"
168 :> ReqBody '[JSON] PostNode
169 :> Post '[JSON] [NodeId]
171 type ChildrenApi a = Summary " Summary children"
172 :> QueryParam "type" NodeType
173 :> QueryParam "offset" Offset
174 :> QueryParam "limit" Limit
175 -- :> Get '[JSON] [Node a]
176 :> Get '[JSON] (NodeTableResult a)
178 ------------------------------------------------------------------------
179 type NodeNodeAPI a = Get '[JSON] (Node a)
181 nodeNodeAPI :: forall proxy a. (JSONB a, ToJSON a)
186 -> GargServer (NodeNodeAPI a)
187 nodeNodeAPI p uId cId nId = withAccess (Proxy :: Proxy (NodeNodeAPI a)) Proxy uId (PathNodeNode cId nId) nodeNodeAPI'
189 nodeNodeAPI' :: GargServer (NodeNodeAPI a)
190 nodeNodeAPI' = getNodeWith nId p
192 ------------------------------------------------------------------------
193 -- TODO: make the NodeId type indexed by `a`, then we no longer need the proxy.
194 nodeAPI :: forall proxy a.
201 -> ServerT (NodeAPI a) (GargM Env GargError)
202 nodeAPI p uId id' = withAccess (Proxy :: Proxy (NodeAPI a)) Proxy uId (PathNode id') nodeAPI'
204 nodeAPI' :: ServerT (NodeAPI a) (GargM Env GargError)
205 nodeAPI' = getNodeWith id' p
207 :<|> postNode uId id'
208 :<|> postNodeAsyncAPI uId id'
209 :<|> FrameCalcUpload.api uId id'
211 :<|> Update.api uId id'
212 :<|> Action.deleteNode (RootId $ NodeId uId) id'
213 :<|> getChildren id' p
217 :<|> apiNgramsTableCorpus id'
222 :<|> Share.api (RootId $ NodeId uId) id'
233 :<|> phyloAPI id' uId
234 :<|> moveNode (RootId $ NodeId uId) id'
235 -- :<|> nodeAddAPI id'
236 -- :<|> postUpload id'
237 :<|> Share.unPublish id'
240 :<|> fileAsyncApi uId id'
242 :<|> DocumentsFromWriteNodes.api uId id'
243 :<|> DocumentUpload.api uId id'
246 ------------------------------------------------------------------------
247 data RenameNode = RenameNode { r_name :: Text }
250 ------------------------------------------------------------------------
251 ------------------------------------------------------------------------
252 type CatApi = Summary " To Categorize NodeNodes: 0 for delete, 1/null neutral, 2 favorite"
253 :> ReqBody '[JSON] NodesToCategory
256 data NodesToCategory = NodesToCategory { ntc_nodesId :: [NodeId]
257 , ntc_category :: Int
261 -- TODO unPrefix "ntc_" FromJSON, ToJSON, ToSchema, adapt frontend.
262 instance FromJSON NodesToCategory
263 instance ToJSON NodesToCategory
264 instance ToSchema NodesToCategory
266 catApi :: CorpusId -> GargServer CatApi
268 ret <- nodeContextsCategory $ map (\n -> (cId, n, ntc_category cs')) (ntc_nodesId cs')
269 lId <- defaultList cId
270 _ <- updateChart cId (Just lId) Docs Nothing
273 ------------------------------------------------------------------------
274 type ScoreApi = Summary " To Score NodeNodes"
275 :> ReqBody '[JSON] NodesToScore
278 data NodesToScore = NodesToScore { nts_nodesId :: [NodeId]
283 -- TODO unPrefix "ntc_" FromJSON, ToJSON, ToSchema, adapt frontend.
284 instance FromJSON NodesToScore
285 instance ToJSON NodesToScore
286 instance ToSchema NodesToScore
288 scoreApi :: CorpusId -> GargServer ScoreApi
291 putScore :: CorpusId -> NodesToScore -> Cmd err [Int]
292 putScore cId cs' = nodeContextsScore $ map (\n -> (cId, n, nts_score cs')) (nts_nodesId cs')
294 ------------------------------------------------------------------------
295 -- TODO adapt FacetDoc -> ListDoc (and add type of document as column)
296 -- Pairing utilities to move elsewhere
297 type PairingApi = Summary " Pairing API"
298 :> QueryParam "view" TabType
299 -- TODO change TabType -> DocType (CorpusId for pairing)
300 :> QueryParam "offset" Offset
301 :> QueryParam "limit" Limit
302 :> QueryParam "order" OrderBy
303 :> Get '[JSON] [FacetDoc]
306 type Pairs = Summary "List of Pairs"
307 :> Get '[JSON] [AnnuaireId]
308 pairs :: CorpusId -> GargServer Pairs
310 ns <- getNodeNode cId
311 pure $ map _nn_node2_id ns
313 type PairWith = Summary "Pair a Corpus with an Annuaire"
314 :> "annuaire" :> Capture "annuaire_id" AnnuaireId
315 :> QueryParam "list_id" ListId
316 :> Post '[JSON] [Int]
318 pairWith :: CorpusId -> GargServer PairWith
319 pairWith cId aId lId = do
320 r <- pairing cId aId lId
321 _ <- insertNodeNode [ NodeNode { _nn_node1_id = cId
323 , _nn_score = Nothing
324 , _nn_category = Nothing }]
328 ------------------------------------------------------------------------
329 type TreeAPI = QueryParams "type" NodeType
330 :> Get '[JSON] (Tree NodeTree)
332 :> QueryParams "type" NodeType
333 :> Get '[JSON] (Tree NodeTree)
335 treeAPI :: NodeId -> GargServer TreeAPI
336 treeAPI id = tree TreeAdvanced id
337 :<|> tree TreeFirstLevel id
339 ------------------------------------------------------------------------
340 -- | TODO Check if the name is less than 255 char
341 rename :: NodeId -> RenameNode -> Cmd err [Int]
342 rename nId (RenameNode name') = U.update (U.Rename nId name')
344 putNode :: forall err a. (HasNodeError err, JSONB a, ToJSON a)
348 putNode n h = fromIntegral <$> updateHyperdata n h
350 -------------------------------------------------------------
351 type MoveAPI = Summary "Move Node endpoint"
352 :> Capture "parent_id" ParentId
359 moveNode _u n p = update (Move n p)
360 -------------------------------------------------------------
363 $(deriveJSON (unPrefix "r_" ) ''RenameNode )
364 instance ToSchema RenameNode
365 instance Arbitrary RenameNode where
366 arbitrary = elements [RenameNode "test"]
369 -------------------------------------------------------------