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.Types (PathId(..))
44 import Gargantext.API.Admin.Auth (withAccess)
45 import Gargantext.API.Metrics
46 import Gargantext.API.Ngrams (TableNgramsApi, apiNgramsTableCorpus)
47 import Gargantext.API.Ngrams.Types (TabType(..))
48 import Gargantext.API.Node.File
49 import Gargantext.API.Node.FrameCalcUpload (FrameCalcUploadAPI, frameCalcUploadAPI)
50 import Gargantext.API.Node.New
51 import Gargantext.API.Prelude
52 import Gargantext.API.Table
53 import Gargantext.Core.Types (NodeTableResult)
54 import Gargantext.Core.Types.Individu (User(..))
55 import Gargantext.Core.Types.Main (Tree, NodeTree)
56 import Gargantext.Core.Utils.Prefix (unPrefix)
57 import Gargantext.Database.Action.Flow.Pairing (pairing)
58 import Gargantext.Database.Admin.Types.Hyperdata
59 import Gargantext.Database.Admin.Types.Node
60 import Gargantext.Database.Prelude -- (Cmd, CmdM)
61 import Gargantext.Database.Query.Facet (FacetDoc, OrderBy(..))
62 import Gargantext.Database.Query.Table.Node
63 import Gargantext.Database.Query.Table.Node.Children (getChildren)
64 import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..))
65 import Gargantext.Database.Query.Table.Node.Update (Update(..), update)
66 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
67 import Gargantext.Database.Query.Table.NodeNode
68 import Gargantext.Database.Query.Tree (tree, TreeMode(..))
69 import Gargantext.Prelude
70 import Gargantext.Core.Viz.Phylo.Legacy.LegacyAPI (PhyloAPI, phyloAPI)
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(..))
78 import qualified Gargantext.Core.Text.List.Learn as Learn
79 import qualified Data.Vector as Vec
84 type NodesAPI = Delete '[JSON] Int
87 -- Be careful: really delete nodes
88 -- Access by admin only
89 nodesAPI :: [NodeId] -> GargServer NodesAPI
90 nodesAPI = deleteNodes
92 ------------------------------------------------------------------------
93 -- | TODO-ACCESS: access by admin only.
94 -- At first let's just have an isAdmin check.
95 -- Later: CanAccessAnyNode or (CanGetAnyNode, CanPutAnyNode)
96 -- To manage the Users roots
99 -- TODO needs design discussion.
100 type Roots = Get '[JSON] [Node HyperdataUser]
101 :<|> Put '[JSON] Int -- TODO
103 -- | TODO: access by admin only
104 roots :: GargServer Roots
105 roots = getNodesWithParentId Nothing
106 :<|> pure (panic "not implemented yet") -- TODO use patch map to update what we need
108 -------------------------------------------------------------------
109 -- | Node API Types management
110 -- TODO-ACCESS : access by users
111 -- No ownership check is needed if we strictly follow the capability model.
113 -- CanGetNode (Node, Children, TableApi, TableNgramsApiGet, PairingApi, ChartApi,
115 -- CanRenameNode (or part of CanEditNode?)
116 -- CanCreateChildren (PostNodeApi)
117 -- CanEditNode / CanPutNode TODO not implemented yet
119 -- CanPatch (TableNgramsApi)
123 type NodeAPI a = Get '[JSON] (Node a)
124 :<|> "rename" :> RenameApi
125 :<|> PostNodeApi -- TODO move to children POST
127 :<|> FrameCalcUploadAPI
128 :<|> ReqBody '[JSON] a :> Put '[JSON] Int
129 :<|> "update" :> Update.API
130 :<|> Delete '[JSON] Int
131 :<|> "children" :> ChildrenApi a
134 :<|> "table" :> TableApi
135 :<|> "ngrams" :> TableNgramsApi
137 :<|> "category" :> CatApi
138 :<|> "score" :> ScoreApi
139 :<|> "search" :> (Search.API Search.SearchResult)
140 :<|> "share" :> Share.API
143 :<|> "pairwith" :> PairWith
144 :<|> "pairs" :> Pairs
145 :<|> "pairing" :> PairingApi
148 :<|> "metrics" :> ScatterAPI
149 :<|> "chart" :> ChartApi
151 :<|> "tree" :> TreeApi
152 :<|> "phylo" :> PhyloAPI
153 -- :<|> "add" :> NodeAddAPI
154 :<|> "move" :> MoveAPI
155 :<|> "unpublish" :> Share.Unpublish
157 :<|> "file" :> FileApi
158 :<|> "async" :> FileAsyncApi
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.
198 , MimeUnrender JSON a
202 -> GargServer (NodeAPI a)
203 nodeAPI p uId id' = withAccess (Proxy :: Proxy (NodeAPI a)) Proxy uId (PathNode id') nodeAPI'
205 nodeAPI' :: GargServer (NodeAPI a)
206 nodeAPI' = getNodeWith id' p
208 :<|> postNode uId id'
209 :<|> postNodeAsyncAPI uId id'
210 :<|> frameCalcUploadAPI uId id'
212 :<|> Update.api uId id'
213 :<|> Action.deleteNode (RootId $ NodeId uId) id'
214 :<|> getChildren id' p
218 :<|> apiNgramsTableCorpus id'
223 :<|> Share.api (RootId $ NodeId uId) id'
234 :<|> phyloAPI id' uId
235 :<|> moveNode (RootId $ NodeId uId) id'
236 -- :<|> nodeAddAPI id'
237 -- :<|> postUpload id'
238 :<|> Share.unPublish id'
241 :<|> fileAsyncApi 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
267 putCat :: CorpusId -> NodesToCategory -> Cmd err [Int]
268 putCat cId cs' = nodeNodesCategory $ map (\n -> (cId, n, ntc_category cs')) (ntc_nodesId cs')
270 ------------------------------------------------------------------------
271 type ScoreApi = Summary " To Score NodeNodes"
272 :> ReqBody '[JSON] NodesToScore
275 data NodesToScore = NodesToScore { nts_nodesId :: [NodeId]
280 -- TODO unPrefix "ntc_" FromJSON, ToJSON, ToSchema, adapt frontend.
281 instance FromJSON NodesToScore
282 instance ToJSON NodesToScore
283 instance ToSchema NodesToScore
285 scoreApi :: CorpusId -> GargServer ScoreApi
288 putScore :: CorpusId -> NodesToScore -> Cmd err [Int]
289 putScore cId cs' = nodeNodesScore $ map (\n -> (cId, n, nts_score cs')) (nts_nodesId cs')
291 ------------------------------------------------------------------------
292 -- TODO adapt FacetDoc -> ListDoc (and add type of document as column)
293 -- Pairing utilities to move elsewhere
294 type PairingApi = Summary " Pairing API"
295 :> QueryParam "view" TabType
296 -- TODO change TabType -> DocType (CorpusId for pairing)
297 :> QueryParam "offset" Int
298 :> QueryParam "limit" Int
299 :> QueryParam "order" OrderBy
300 :> Get '[JSON] [FacetDoc]
303 type Pairs = Summary "List of Pairs"
304 :> Get '[JSON] [AnnuaireId]
305 pairs :: CorpusId -> GargServer Pairs
307 ns <- getNodeNode cId
308 pure $ map _nn_node2_id ns
310 type PairWith = Summary "Pair a Corpus with an Annuaire"
311 :> "annuaire" :> Capture "annuaire_id" AnnuaireId
312 :> QueryParam "list_id" ListId
315 pairWith :: CorpusId -> GargServer PairWith
316 pairWith cId aId lId = do
317 r <- pairing cId aId lId
318 _ <- insertNodeNode [ NodeNode cId aId Nothing Nothing]
322 ------------------------------------------------------------------------
323 type TreeAPI = QueryParams "type" NodeType
324 :> Get '[JSON] (Tree NodeTree)
326 :> QueryParams "type" NodeType
327 :> Get '[JSON] (Tree NodeTree)
329 treeAPI :: NodeId -> GargServer TreeAPI
330 treeAPI id = tree TreeAdvanced id
331 :<|> tree TreeFirstLevel id
333 ------------------------------------------------------------------------
334 -- | TODO Check if the name is less than 255 char
335 rename :: NodeId -> RenameNode -> Cmd err [Int]
336 rename nId (RenameNode name') = U.update (U.Rename nId name')
338 putNode :: forall err a. (HasNodeError err, JSONB a, ToJSON a)
342 putNode n h = fromIntegral <$> updateHyperdata n h
344 -------------------------------------------------------------
345 type MoveAPI = Summary "Move Node endpoint"
346 :> Capture "parent_id" ParentId
353 moveNode _u n p = update (Move n p)
354 -------------------------------------------------------------
357 $(deriveJSON (unPrefix "r_" ) ''RenameNode )
358 instance ToSchema RenameNode
359 instance Arbitrary RenameNode where
360 arbitrary = elements [RenameNode "test"]
363 -------------------------------------------------------------