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
11 -- TODO-ACCESS: CanGetNode
12 -- 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*]}
25 {-# OPTIONS_GHC -fno-warn-orphans #-}
27 {-# LANGUAGE ScopedTypeVariables #-}
28 {-# LANGUAGE TemplateHaskell #-}
29 {-# LANGUAGE TypeOperators #-}
31 module Gargantext.API.Table
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.HashedResponse
44 import Gargantext.API.Ngrams.Types (TabType(..))
45 import Gargantext.API.Prelude (GargServer)
46 import Gargantext.Core.Types (TableResult(..))
47 import Gargantext.Core.Types.Query (Offset, Limit)
48 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
49 import Gargantext.Database.Action.Learn (FavOrTrash(..), moreLike)
50 import Gargantext.Database.Action.Search
51 import Gargantext.Database.Admin.Types.Node
52 import Gargantext.Database.Query.Table.Node.Error (HasNodeError)
53 import Gargantext.Database.Query.Facet (FacetDoc , runViewDocuments, runCountDocuments, OrderBy(..), runViewAuthorsDoc)
54 import Gargantext.Database.Prelude -- (Cmd, CmdM)
55 import Gargantext.Prelude
57 ------------------------------------------------------------------------
59 type TableApi = Summary "Table API"
60 :> QueryParam "tabType" TabType
61 :> QueryParam "limit" Limit
62 :> QueryParam "offset" Offset
63 :> QueryParam "orderBy" OrderBy
64 :> QueryParam "query" Text
65 :> QueryParam "year" Text
66 :> Get '[JSON] (HashedResponse FacetTableResult)
67 :<|> Summary "Table API (POST)"
68 :> ReqBody '[JSON] TableQuery
69 :> Post '[JSON] FacetTableResult
72 :> QueryParam "tabType" TabType
75 data TableQuery = TableQuery
78 , tq_orderBy :: OrderBy
83 type FacetTableResult = TableResult FacetDoc
85 $(deriveJSON (unPrefix "tq_") ''TableQuery)
87 instance ToSchema TableQuery where
88 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "tq_")
90 instance Arbitrary TableQuery where
91 arbitrary = elements [TableQuery { tq_offset = 0
93 , tq_orderBy = DateAsc
95 , tq_query = "electrodes" }]
98 tableApi :: NodeId -> GargServer TableApi
99 tableApi id' = getTableApi id'
100 :<|> postTableApi id'
101 :<|> getTableHashApi id'
104 getTableApi :: HasNodeError err
112 -> Cmd err (HashedResponse FacetTableResult)
113 getTableApi cId tabType mLimit mOffset mOrderBy mQuery mYear = do
114 -- printDebug "[getTableApi] mQuery" mQuery
115 -- printDebug "[getTableApi] mYear" mYear
116 t <- getTable cId tabType mOffset mLimit mOrderBy mQuery mYear
117 pure $ constructHashedResponse t
119 postTableApi :: HasNodeError err
120 => NodeId -> TableQuery -> Cmd err FacetTableResult
121 postTableApi cId (TableQuery o l order ft "") = getTable cId (Just ft) (Just o) (Just l) (Just order) Nothing Nothing
122 postTableApi cId (TableQuery o l order ft q) = case ft of
123 Docs -> searchInCorpus' cId False [q] (Just o) (Just l) (Just order)
124 Trash -> searchInCorpus' cId True [q] (Just o) (Just l) (Just order)
125 x -> panic $ "not implemented in tableApi " <> (cs $ show x)
127 getTableHashApi :: HasNodeError err
128 => NodeId -> Maybe TabType -> Cmd err Text
129 getTableHashApi cId tabType = do
130 HashedResponse { hash = h } <- getTableApi cId tabType Nothing Nothing Nothing Nothing Nothing
133 searchInCorpus' :: CorpusId
139 -> Cmd err FacetTableResult
140 searchInCorpus' cId t q o l order = do
141 docs <- searchInCorpus cId t q o l order
142 countAllDocs <- searchCountInCorpus cId t q
143 pure $ TableResult { tr_docs = docs, tr_count = countAllDocs }
146 getTable :: HasNodeError err
154 -> Cmd err FacetTableResult
155 getTable cId ft o l order query year = do
156 docs <- getTable' cId ft o l order query year
157 docsCount <- runCountDocuments cId (ft == Just Trash) query year
158 pure $ TableResult { tr_docs = docs, tr_count = docsCount }
160 getTable' :: HasNodeError err
168 -> Cmd err [FacetDoc]
169 getTable' cId ft o l order query year =
171 (Just Docs) -> runViewDocuments cId False o l order query year
172 (Just Trash) -> runViewDocuments cId True o l order query year
173 (Just MoreFav) -> moreLike cId o l order IsFav
174 (Just MoreTrash) -> moreLike cId o l order IsTrash
175 x -> panic $ "not implemented in getTable: " <> (cs $ show x)
178 getPair :: ContactId -> Maybe TabType
179 -> Maybe Offset -> Maybe Limit
180 -> Maybe OrderBy -> Cmd err [FacetDoc]
181 getPair cId ft o l order =
183 (Just Docs) -> runViewAuthorsDoc cId False o l order
184 (Just Trash) -> runViewAuthorsDoc cId True o l order
185 _ -> panic $ "not implemented: get Pairing" <> (cs $ show ft)