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 (withAccess, PathId(..))
44 import Gargantext.API.Metrics
45 import Gargantext.API.Ngrams (TabType(..), TableNgramsApi, apiNgramsTableCorpus)
46 import Gargantext.API.Node.File
47 import Gargantext.API.Node.New
48 import Gargantext.API.Prelude
49 import Gargantext.API.Table
50 import Gargantext.Core.Types (NodeTableResult)
51 import Gargantext.Core.Types.Individu (User(..))
52 import Gargantext.Core.Types.Main (Tree, NodeTree)
53 import Gargantext.Core.Utils.Prefix (unPrefix)
54 import Gargantext.Database.Action.Flow.Pairing (pairing)
55 import Gargantext.Database.Admin.Types.Hyperdata
56 import Gargantext.Database.Admin.Types.Node
57 import Gargantext.Database.Prelude -- (Cmd, CmdM)
58 import Gargantext.Database.Query.Facet (FacetDoc, OrderBy(..))
59 import Gargantext.Database.Query.Table.Node
60 import Gargantext.Database.Query.Table.Node.Children (getChildren)
61 import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..))
62 import Gargantext.Database.Query.Table.Node.Update (Update(..), update)
63 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
64 import Gargantext.Database.Query.Table.NodeNode
65 import Gargantext.Database.Query.Tree (tree, TreeMode(..))
66 import Gargantext.Prelude
67 import Gargantext.Viz.Phylo.API (PhyloAPI, phyloAPI)
68 import qualified Gargantext.API.Node.Share as Share
69 import qualified Gargantext.API.Node.Update as Update
70 import qualified Gargantext.API.Search as Search
71 import qualified Gargantext.Database.Action.Delete as Action (deleteNode)
72 import qualified Gargantext.Database.Query.Table.Node.Update as U (update, Update(..))
75 import qualified Gargantext.Text.List.Learn as Learn
76 import qualified Data.Vector as Vec
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 :<|> 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 :<|> "search" :> (Search.API Search.SearchResult)
135 :<|> "share" :> Share.API
138 :<|> "pairwith" :> PairWith
139 :<|> "pairs" :> Pairs
140 :<|> "pairing" :> PairingApi
143 :<|> "metrics" :> ScatterAPI
144 :<|> "chart" :> ChartApi
146 :<|> "tree" :> TreeApi
147 :<|> "phylo" :> PhyloAPI
148 -- :<|> "add" :> NodeAddAPI
149 :<|> "move" :> MoveAPI
150 :<|> "unpublish" :> Share.Unpublish
152 :<|> "file" :> FileApi
154 -- TODO-ACCESS: check userId CanRenameNode nodeId
155 -- TODO-EVENTS: NodeRenamed RenameNode or re-use some more general NodeEdited...
156 type RenameApi = Summary " Rename Node"
157 :> ReqBody '[JSON] RenameNode
160 type PostNodeApi = Summary " PostNode Node with ParentId as {id}"
161 :> ReqBody '[JSON] PostNode
162 :> Post '[JSON] [NodeId]
164 type ChildrenApi a = Summary " Summary children"
165 :> QueryParam "type" NodeType
166 :> QueryParam "offset" Int
167 :> QueryParam "limit" Int
168 -- :> Get '[JSON] [Node a]
169 :> Get '[JSON] (NodeTableResult a)
171 ------------------------------------------------------------------------
172 type NodeNodeAPI a = Get '[JSON] (Node a)
174 nodeNodeAPI :: forall proxy a. (JSONB a, ToJSON a)
179 -> GargServer (NodeNodeAPI a)
180 nodeNodeAPI p uId cId nId = withAccess (Proxy :: Proxy (NodeNodeAPI a)) Proxy uId (PathNodeNode cId nId) nodeNodeAPI'
182 nodeNodeAPI' :: GargServer (NodeNodeAPI a)
183 nodeNodeAPI' = getNodeWith nId p
185 ------------------------------------------------------------------------
186 -- TODO: make the NodeId type indexed by `a`, then we no longer need the proxy.
187 nodeAPI :: forall proxy a.
194 -> GargServer (NodeAPI a)
195 nodeAPI p uId id' = withAccess (Proxy :: Proxy (NodeAPI a)) Proxy uId (PathNode id') nodeAPI'
197 nodeAPI' :: GargServer (NodeAPI a)
198 nodeAPI' = getNodeWith id' p
200 :<|> postNode uId id'
201 :<|> postNodeAsyncAPI uId id'
203 :<|> Update.api uId id'
204 :<|> Action.deleteNode (RootId $ NodeId uId) id'
205 :<|> getChildren id' p
209 :<|> apiNgramsTableCorpus id'
223 :<|> phyloAPI id' uId
224 :<|> moveNode (RootId $ NodeId uId) id'
225 -- :<|> nodeAddAPI id'
226 -- :<|> postUpload id'
227 :<|> Share.unPublish id'
232 ------------------------------------------------------------------------
233 data RenameNode = RenameNode { r_name :: Text }
236 ------------------------------------------------------------------------
237 ------------------------------------------------------------------------
238 type CatApi = Summary " To Categorize NodeNodes: 0 for delete, 1/null neutral, 2 favorite"
239 :> ReqBody '[JSON] NodesToCategory
242 data NodesToCategory = NodesToCategory { ntc_nodesId :: [NodeId]
243 , ntc_category :: Int
247 -- TODO unPrefix "ntc_" FromJSON, ToJSON, ToSchema, adapt frontend.
248 instance FromJSON NodesToCategory
249 instance ToJSON NodesToCategory
250 instance ToSchema NodesToCategory
252 catApi :: CorpusId -> GargServer CatApi
255 putCat :: CorpusId -> NodesToCategory -> Cmd err [Int]
256 putCat cId cs' = nodeNodesCategory $ map (\n -> (cId, n, ntc_category cs')) (ntc_nodesId cs')
258 ------------------------------------------------------------------------
259 -- TODO adapt FacetDoc -> ListDoc (and add type of document as column)
260 -- Pairing utilities to move elsewhere
261 type PairingApi = Summary " Pairing API"
262 :> QueryParam "view" TabType
263 -- TODO change TabType -> DocType (CorpusId for pairing)
264 :> QueryParam "offset" Int
265 :> QueryParam "limit" Int
266 :> QueryParam "order" OrderBy
267 :> Get '[JSON] [FacetDoc]
270 type Pairs = Summary "List of Pairs"
271 :> Get '[JSON] [AnnuaireId]
272 pairs :: CorpusId -> GargServer Pairs
274 ns <- getNodeNode cId
275 pure $ map _nn_node2_id ns
277 type PairWith = Summary "Pair a Corpus with an Annuaire"
278 :> "annuaire" :> Capture "annuaire_id" AnnuaireId
279 :> QueryParam "list_id" ListId
282 pairWith :: CorpusId -> GargServer PairWith
283 pairWith cId aId lId = do
284 r <- pairing cId aId lId
285 _ <- insertNodeNode [ NodeNode cId aId Nothing Nothing]
289 ------------------------------------------------------------------------
290 type TreeAPI = QueryParams "type" NodeType :> Get '[JSON] (Tree NodeTree)
292 treeAPI :: NodeId -> GargServer TreeAPI
293 treeAPI = tree Advanced
295 ------------------------------------------------------------------------
296 -- | TODO Check if the name is less than 255 char
297 rename :: NodeId -> RenameNode -> Cmd err [Int]
298 rename nId (RenameNode name') = U.update (U.Rename nId name')
300 putNode :: forall err a. (HasNodeError err, JSONB a, ToJSON a)
304 putNode n h = fromIntegral <$> updateHyperdata n h
306 -------------------------------------------------------------
307 type MoveAPI = Summary "Move Node endpoint"
308 :> Capture "parent_id" ParentId
315 moveNode _u n p = update (Move n p)
316 -------------------------------------------------------------
319 $(deriveJSON (unPrefix "r_" ) ''RenameNode )
320 instance ToSchema RenameNode
321 instance Arbitrary RenameNode where
322 arbitrary = elements [RenameNode "test"]
325 -------------------------------------------------------------