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 (Offset, Limit, TableResult(..))
47 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
48 import Gargantext.Database.Query.Facet (FacetDoc , runViewDocuments, runCountDocuments, OrderBy(..), runViewAuthorsDoc)
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.Prelude -- (Cmd, CmdM)
53 import Gargantext.Prelude
55 ------------------------------------------------------------------------
57 type TableApi = Summary "Table API"
58 :> QueryParam "tabType" TabType
59 :> QueryParam "list" ListId
60 :> QueryParam "limit" Int
61 :> QueryParam "offset" Int
62 :> QueryParam "orderBy" OrderBy
63 :> QueryParam "query" Text
64 :> Get '[JSON] (HashedResponse FacetTableResult)
65 :<|> Summary "Table API (POST)"
66 :> ReqBody '[JSON] TableQuery
67 :> Post '[JSON] FacetTableResult
70 :> QueryParam "tabType" TabType
73 data TableQuery = TableQuery
76 , tq_orderBy :: OrderBy
81 type FacetTableResult = TableResult FacetDoc
83 $(deriveJSON (unPrefix "tq_") ''TableQuery)
85 instance ToSchema TableQuery where
86 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "tq_")
88 instance Arbitrary TableQuery where
89 arbitrary = elements [TableQuery 0 10 DateAsc Docs "electrodes"]
92 tableApi :: NodeId -> GargServer TableApi
93 tableApi id' = getTableApi id'
95 :<|> getTableHashApi id'
105 -> Cmd err (HashedResponse FacetTableResult)
106 getTableApi cId tabType _mListId mLimit mOffset mOrderBy mQuery = do
107 printDebug "[getTableApi] mQuery" mQuery
108 t <- getTable cId tabType mOffset mLimit mOrderBy mQuery
109 pure $ constructHashedResponse t
111 postTableApi :: NodeId -> TableQuery -> Cmd err FacetTableResult
112 postTableApi cId (TableQuery o l order ft "") = getTable cId (Just ft) (Just o) (Just l) (Just order) Nothing
113 postTableApi cId (TableQuery o l order ft q) = case ft of
114 Docs -> searchInCorpus' cId False [q] (Just o) (Just l) (Just order)
115 Trash -> searchInCorpus' cId True [q] (Just o) (Just l) (Just order)
116 x -> panic $ "not implemented in tableApi " <> (cs $ show x)
118 getTableHashApi :: NodeId -> Maybe TabType -> Cmd err Text
119 getTableHashApi cId tabType = do
120 HashedResponse { hash = h } <- getTableApi cId tabType Nothing Nothing Nothing Nothing Nothing
123 searchInCorpus' :: CorpusId
129 -> Cmd err FacetTableResult
130 searchInCorpus' cId t q o l order = do
131 docs <- searchInCorpus cId t q o l order
132 countAllDocs <- searchCountInCorpus cId t q
133 pure $ TableResult { tr_docs = docs, tr_count = countAllDocs }
142 -> Cmd err FacetTableResult
143 getTable cId ft o l order query = do
144 docs <- getTable' cId ft o l order query
145 docsCount <- runCountDocuments cId (if ft == Just Trash then True else False) query
146 pure $ TableResult { tr_docs = docs, tr_count = docsCount }
154 -> Cmd err [FacetDoc]
155 getTable' cId ft o l order query =
157 (Just Docs) -> runViewDocuments cId False o l order query
158 (Just Trash) -> runViewDocuments cId True o l order query
159 (Just MoreFav) -> moreLike cId o l order IsFav
160 (Just MoreTrash) -> moreLike cId o l order IsTrash
161 x -> panic $ "not implemented in getTable: " <> (cs $ show x)
164 getPair :: ContactId -> Maybe TabType
165 -> Maybe Offset -> Maybe Limit
166 -> Maybe OrderBy -> Cmd err [FacetDoc]
167 getPair cId ft o l order =
169 (Just Docs) -> runViewAuthorsDoc cId False o l order
170 (Just Trash) -> runViewAuthorsDoc cId True o l order
171 _ -> panic $ "not implemented: get Pairing" <> (cs $ show ft)