]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Node.hs
new strategy for choosing temporal matching candidates
[gargantext.git] / src / Gargantext / API / Node.hs
1 {-|
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
8 Portability : POSIX
9
10 -- TODO-SECURITY: Critical
11
12 -- TODO-ACCESS: CanGetNode
13 -- TODO-EVENTS: No events as this is a read only query.
14 Node API
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*]}
21
22 -}
23
24 {-# OPTIONS_GHC -fno-warn-orphans #-}
25
26 {-# LANGUAGE ScopedTypeVariables #-}
27 {-# LANGUAGE TemplateHaskell #-}
28 {-# LANGUAGE TypeOperators #-}
29
30 module Gargantext.API.Node
31 where
32
33 import Data.Aeson (FromJSON, ToJSON)
34 import Data.Aeson.TH (deriveJSON)
35 import Data.Maybe
36 import Data.Swagger
37 import Data.Text (Text())
38 import GHC.Generics (Generic)
39 import Gargantext.API.Admin.Auth (withAccess)
40 import Gargantext.API.Admin.Auth.Types (PathId(..))
41 import Gargantext.API.Metrics
42 import Gargantext.API.Ngrams (TableNgramsApi, apiNgramsTableCorpus)
43 import Gargantext.API.Ngrams.Types (TabType(..))
44 import Gargantext.API.Node.File
45 import Gargantext.API.Node.New
46 import Gargantext.API.Prelude
47 import Gargantext.API.Table
48 import Gargantext.Core.Types (NodeTableResult)
49 import Gargantext.Core.Types.Individu (User(..))
50 import Gargantext.Core.Types.Main (Tree, NodeTree)
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
67 import Servant
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(..))
78
79
80 -- | Admin NodesAPI
81 -- TODO
82 type NodesAPI = Delete '[JSON] Int
83
84 -- | Delete Nodes
85 -- Be careful: really delete nodes
86 -- Access by admin only
87 nodesAPI :: [NodeId] -> GargServer NodesAPI
88 nodesAPI = deleteNodes
89
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
95 -- TODO-EVENTS:
96 -- PutNode ?
97 -- TODO needs design discussion.
98 type Roots = Get '[JSON] [Node HyperdataUser]
99 :<|> Put '[JSON] Int -- TODO
100
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
105
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.
110 --
111 -- CanGetNode (Node, Children, TableApi, TableNgramsApiGet, PairingApi, ChartApi,
112 -- SearchAPI)
113 -- CanRenameNode (or part of CanEditNode?)
114 -- CanCreateChildren (PostNodeApi)
115 -- CanEditNode / CanPutNode TODO not implemented yet
116 -- CanDeleteNode
117 -- CanPatch (TableNgramsApi)
118 -- CanFavorite
119 -- CanMoveToTrash
120
121 type NodeAPI a = Get '[JSON] (Node a)
122 :<|> "rename" :> RenameApi
123 :<|> PostNodeApi -- TODO move to children POST
124 :<|> PostNodeAsync
125 :<|> FrameCalcUpload.API
126 :<|> ReqBody '[JSON] a :> Put '[JSON] Int
127 :<|> "update" :> Update.API
128 :<|> Delete '[JSON] Int
129 :<|> "children" :> ChildrenApi a
130
131 -- TODO gather it
132 :<|> "table" :> TableApi
133 :<|> "ngrams" :> TableNgramsApi
134
135 :<|> "category" :> CatApi
136 :<|> "score" :> ScoreApi
137 :<|> "search" :> (Search.API Search.SearchResult)
138 :<|> "share" :> Share.API
139
140 -- Pairing utilities
141 :<|> "pairwith" :> PairWith
142 :<|> "pairs" :> Pairs
143 :<|> "pairing" :> PairingApi
144
145 -- VIZ
146 :<|> "metrics" :> ScatterAPI
147 :<|> "chart" :> ChartApi
148 :<|> "pie" :> PieApi
149 :<|> "tree" :> TreeApi
150 :<|> "phylo" :> PhyloAPI
151 -- :<|> "add" :> NodeAddAPI
152 :<|> "move" :> MoveAPI
153 :<|> "unpublish" :> Share.Unpublish
154
155 :<|> "file" :> FileApi
156 :<|> "async" :> FileAsyncApi
157
158 :<|> "documents-from-write-nodes" :> DocumentsFromWriteNodes.API
159 :<|> DocumentUpload.API
160
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
165 :> Put '[JSON] [Int]
166
167 type PostNodeApi = Summary " PostNode Node with ParentId as {id}"
168 :> ReqBody '[JSON] PostNode
169 :> Post '[JSON] [NodeId]
170
171 type ChildrenApi a = Summary " Summary children"
172 :> QueryParam "type" NodeType
173 :> QueryParam "offset" Int
174 :> QueryParam "limit" Int
175 -- :> Get '[JSON] [Node a]
176 :> Get '[JSON] (NodeTableResult a)
177
178 ------------------------------------------------------------------------
179 type NodeNodeAPI a = Get '[JSON] (Node a)
180
181 nodeNodeAPI :: forall proxy a. (JSONB a, ToJSON a)
182 => proxy a
183 -> UserId
184 -> CorpusId
185 -> NodeId
186 -> GargServer (NodeNodeAPI a)
187 nodeNodeAPI p uId cId nId = withAccess (Proxy :: Proxy (NodeNodeAPI a)) Proxy uId (PathNodeNode cId nId) nodeNodeAPI'
188 where
189 nodeNodeAPI' :: GargServer (NodeNodeAPI a)
190 nodeNodeAPI' = getNodeWith nId p
191
192 ------------------------------------------------------------------------
193 -- TODO: make the NodeId type indexed by `a`, then we no longer need the proxy.
194 nodeAPI :: forall proxy a.
195 ( JSONB a
196 , FromJSON a
197 , ToJSON a
198 ) => proxy a
199 -> UserId
200 -> NodeId
201 -> GargServer (NodeAPI a)
202 nodeAPI p uId id' = withAccess (Proxy :: Proxy (NodeAPI a)) Proxy uId (PathNode id') nodeAPI'
203 where
204 nodeAPI' :: GargServer (NodeAPI a)
205 nodeAPI' = getNodeWith id' p
206 :<|> rename id'
207 :<|> postNode uId id'
208 :<|> postNodeAsyncAPI uId id'
209 :<|> FrameCalcUpload.api uId id'
210 :<|> putNode id'
211 :<|> Update.api uId id'
212 :<|> Action.deleteNode (RootId $ NodeId uId) id'
213 :<|> getChildren id' p
214
215 -- TODO gather it
216 :<|> tableApi id'
217 :<|> apiNgramsTableCorpus id'
218
219 :<|> catApi id'
220 :<|> scoreApi id'
221 :<|> Search.api id'
222 :<|> Share.api (RootId $ NodeId uId) id'
223 -- Pairing Tools
224 :<|> pairWith id'
225 :<|> pairs id'
226 :<|> getPair id'
227
228 -- VIZ
229 :<|> scatterApi id'
230 :<|> chartApi id'
231 :<|> pieApi id'
232 :<|> treeApi id'
233 :<|> phyloAPI id' uId
234 :<|> moveNode (RootId $ NodeId uId) id'
235 -- :<|> nodeAddAPI id'
236 -- :<|> postUpload id'
237 :<|> Share.unPublish id'
238
239 :<|> fileApi uId id'
240 :<|> fileAsyncApi uId id'
241
242 :<|> DocumentsFromWriteNodes.api uId id'
243 :<|> DocumentUpload.api uId id'
244
245
246 ------------------------------------------------------------------------
247 data RenameNode = RenameNode { r_name :: Text }
248 deriving (Generic)
249
250 ------------------------------------------------------------------------
251 ------------------------------------------------------------------------
252 type CatApi = Summary " To Categorize NodeNodes: 0 for delete, 1/null neutral, 2 favorite"
253 :> ReqBody '[JSON] NodesToCategory
254 :> Put '[JSON] [Int]
255
256 data NodesToCategory = NodesToCategory { ntc_nodesId :: [NodeId]
257 , ntc_category :: Int
258 }
259 deriving (Generic)
260
261 -- TODO unPrefix "ntc_" FromJSON, ToJSON, ToSchema, adapt frontend.
262 instance FromJSON NodesToCategory
263 instance ToJSON NodesToCategory
264 instance ToSchema NodesToCategory
265
266 catApi :: CorpusId -> GargServer CatApi
267 catApi = putCat
268 where
269 putCat :: CorpusId -> NodesToCategory -> Cmd err [Int]
270 putCat cId cs' = nodeContextsCategory $ map (\n -> (cId, n, ntc_category cs')) (ntc_nodesId cs')
271
272 ------------------------------------------------------------------------
273 type ScoreApi = Summary " To Score NodeNodes"
274 :> ReqBody '[JSON] NodesToScore
275 :> Put '[JSON] [Int]
276
277 data NodesToScore = NodesToScore { nts_nodesId :: [NodeId]
278 , nts_score :: Int
279 }
280 deriving (Generic)
281
282 -- TODO unPrefix "ntc_" FromJSON, ToJSON, ToSchema, adapt frontend.
283 instance FromJSON NodesToScore
284 instance ToJSON NodesToScore
285 instance ToSchema NodesToScore
286
287 scoreApi :: CorpusId -> GargServer ScoreApi
288 scoreApi = putScore
289 where
290 putScore :: CorpusId -> NodesToScore -> Cmd err [Int]
291 putScore cId cs' = nodeContextsScore $ map (\n -> (cId, n, nts_score cs')) (nts_nodesId cs')
292
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]
303
304 ----------
305 type Pairs = Summary "List of Pairs"
306 :> Get '[JSON] [AnnuaireId]
307 pairs :: CorpusId -> GargServer Pairs
308 pairs cId = do
309 ns <- getNodeNode cId
310 pure $ map _nn_node2_id ns
311
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
316
317 pairWith :: CorpusId -> GargServer PairWith
318 pairWith cId aId lId = do
319 r <- pairing cId aId lId
320 _ <- insertNodeNode [ NodeNode { _nn_node1_id = cId
321 , _nn_node2_id = aId
322 , _nn_score = Nothing
323 , _nn_category = Nothing }]
324 pure r
325
326
327 ------------------------------------------------------------------------
328 type TreeAPI = QueryParams "type" NodeType
329 :> Get '[JSON] (Tree NodeTree)
330 :<|> "first-level"
331 :> QueryParams "type" NodeType
332 :> Get '[JSON] (Tree NodeTree)
333
334 treeAPI :: NodeId -> GargServer TreeAPI
335 treeAPI id = tree TreeAdvanced id
336 :<|> tree TreeFirstLevel id
337
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')
342
343 putNode :: forall err a. (HasNodeError err, JSONB a, ToJSON a)
344 => NodeId
345 -> a
346 -> Cmd err Int
347 putNode n h = fromIntegral <$> updateHyperdata n h
348
349 -------------------------------------------------------------
350 type MoveAPI = Summary "Move Node endpoint"
351 :> Capture "parent_id" ParentId
352 :> Put '[JSON] [Int]
353
354 moveNode :: User
355 -> NodeId
356 -> ParentId
357 -> Cmd err [Int]
358 moveNode _u n p = update (Move n p)
359 -------------------------------------------------------------
360
361
362 $(deriveJSON (unPrefix "r_" ) ''RenameNode )
363 instance ToSchema RenameNode
364 instance Arbitrary RenameNode where
365 arbitrary = elements [RenameNode "test"]
366
367
368 -------------------------------------------------------------