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 "list" ListId
62 :> QueryParam "limit" Limit
63 :> QueryParam "offset" Offset
64 :> QueryParam "orderBy" OrderBy
65 :> QueryParam "query" Text
66 :> QueryParam "year" Text
67 :> Get '[JSON] (HashedResponse FacetTableResult)
68 :<|> Summary "Table API (POST)"
69 :> ReqBody '[JSON] TableQuery
70 :> Post '[JSON] FacetTableResult
73 :> QueryParam "tabType" TabType
76 data TableQuery = TableQuery
79 , tq_orderBy :: OrderBy
84 type FacetTableResult = TableResult FacetDoc
86 $(deriveJSON (unPrefix "tq_") ''TableQuery)
88 instance ToSchema TableQuery where
89 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "tq_")
91 instance Arbitrary TableQuery where
92 arbitrary = elements [TableQuery { tq_offset = 0
94 , tq_orderBy = DateAsc
96 , tq_query = "electrodes" }]
99 tableApi :: NodeId -> GargServer TableApi
100 tableApi id' = getTableApi id'
101 :<|> postTableApi id'
102 :<|> getTableHashApi id'
105 getTableApi :: HasNodeError err
114 -> Cmd err (HashedResponse FacetTableResult)
115 getTableApi cId tabType _mListId mLimit mOffset mOrderBy mQuery mYear = do
116 -- printDebug "[getTableApi] mQuery" mQuery
117 -- printDebug "[getTableApi] mYear" mYear
118 t <- getTable cId tabType mOffset mLimit mOrderBy mQuery mYear
119 pure $ constructHashedResponse t
121 postTableApi :: HasNodeError err
122 => NodeId -> TableQuery -> Cmd err FacetTableResult
123 postTableApi cId (TableQuery o l order ft "") = getTable cId (Just ft) (Just o) (Just l) (Just order) Nothing Nothing
124 postTableApi cId (TableQuery o l order ft q) = case ft of
125 Docs -> searchInCorpus' cId False [q] (Just o) (Just l) (Just order)
126 Trash -> searchInCorpus' cId True [q] (Just o) (Just l) (Just order)
127 x -> panic $ "not implemented in tableApi " <> (cs $ show x)
129 getTableHashApi :: HasNodeError err
130 => NodeId -> Maybe TabType -> Cmd err Text
131 getTableHashApi cId tabType = do
132 HashedResponse { hash = h } <- getTableApi cId tabType Nothing Nothing Nothing Nothing Nothing Nothing
135 searchInCorpus' :: CorpusId
141 -> Cmd err FacetTableResult
142 searchInCorpus' cId t q o l order = do
143 docs <- searchInCorpus cId t q o l order
144 countAllDocs <- searchCountInCorpus cId t q
145 pure $ TableResult { tr_docs = docs, tr_count = countAllDocs }
148 getTable :: HasNodeError err
156 -> Cmd err FacetTableResult
157 getTable cId ft o l order query year = do
158 docs <- getTable' cId ft o l order query year
159 docsCount <- runCountDocuments cId (ft == Just Trash) query year
160 pure $ TableResult { tr_docs = docs, tr_count = docsCount }
162 getTable' :: HasNodeError err
170 -> Cmd err [FacetDoc]
171 getTable' cId ft o l order query year =
173 (Just Docs) -> runViewDocuments cId False o l order query year
174 (Just Trash) -> runViewDocuments cId True o l order query year
175 (Just MoreFav) -> moreLike cId o l order IsFav
176 (Just MoreTrash) -> moreLike cId o l order IsTrash
177 x -> panic $ "not implemented in getTable: " <> (cs $ show x)
180 getPair :: ContactId -> Maybe TabType
181 -> Maybe Offset -> Maybe Limit
182 -> Maybe OrderBy -> Cmd err [FacetDoc]
183 getPair cId ft o l order =
185 (Just Docs) -> runViewAuthorsDoc cId False o l order
186 (Just Trash) -> runViewAuthorsDoc cId True o l order
187 _ -> panic $ "not implemented: get Pairing" <> (cs $ show ft)