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 DataKinds #-}
28 {-# LANGUAGE DeriveGeneric #-}
29 {-# LANGUAGE FlexibleContexts #-}
30 {-# LANGUAGE FlexibleInstances #-}
31 {-# LANGUAGE NoImplicitPrelude #-}
32 {-# LANGUAGE OverloadedStrings #-}
33 {-# LANGUAGE RankNTypes #-}
34 {-# LANGUAGE ScopedTypeVariables #-}
35 {-# LANGUAGE TemplateHaskell #-}
36 {-# LANGUAGE TypeOperators #-}
38 module Gargantext.API.Table
41 import Data.Aeson.TH (deriveJSON)
44 import Data.Text (Text())
45 import GHC.Generics (Generic)
46 import Gargantext.API.Ngrams (TabType(..))
47 import Gargantext.Core.Types (Offset, Limit, TableResult(..))
48 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
49 import Gargantext.Database.Query.Facet (FacetDoc , runViewDocuments, OrderBy(..), runViewAuthorsDoc)
50 import Gargantext.Database.Action.Learn (FavOrTrash(..), moreLike)
51 import Gargantext.Database.Action.Search
52 import Gargantext.Database.Admin.Types.Node
53 import Gargantext.Database.Prelude -- (Cmd, CmdM)
54 import Gargantext.Prelude
56 import Test.QuickCheck (elements)
57 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
59 ------------------------------------------------------------------------
61 type TableApi = Summary " Table API"
62 :> ReqBody '[JSON] TableQuery
63 :> Post '[JSON] FacetTableResult
65 data TableQuery = TableQuery
68 , tq_orderBy :: OrderBy
73 type FacetTableResult = TableResult FacetDoc
75 $(deriveJSON (unPrefix "tq_") ''TableQuery)
77 instance ToSchema TableQuery where
78 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "tq_")
80 instance Arbitrary TableQuery where
81 arbitrary = elements [TableQuery 0 10 DateAsc Docs "electrodes"]
84 tableApi :: NodeId -> TableQuery -> Cmd err FacetTableResult
85 tableApi cId (TableQuery o l order ft "") = getTable cId (Just ft) (Just o) (Just l) (Just order)
86 tableApi cId (TableQuery o l order ft q) = case ft of
87 Docs -> searchInCorpus' cId False [q] (Just o) (Just l) (Just order)
88 Trash -> searchInCorpus' cId True [q] (Just o) (Just l) (Just order)
89 x -> panic $ "not implemented in tableApi " <> (cs $ show x)
91 searchInCorpus' :: CorpusId
97 -> Cmd err FacetTableResult
98 searchInCorpus' cId t q o l order = do
99 docs <- searchInCorpus cId t q o l order
100 countAllDocs <- searchCountInCorpus cId t q
101 pure $ TableResult { tr_docs = docs, tr_count = countAllDocs }
104 getTable :: NodeId -> Maybe TabType
105 -> Maybe Offset -> Maybe Limit
106 -> Maybe OrderBy -> Cmd err FacetTableResult
107 getTable cId ft o l order = do
108 docs <- getTable' cId ft o l order
109 -- TODO: Rewrite to use runCountOpaQuery and avoid (length allDocs)
110 allDocs <- getTable' cId ft Nothing Nothing Nothing
111 pure $ TableResult { tr_docs = docs, tr_count = length allDocs }
113 getTable' :: NodeId -> Maybe TabType
114 -> Maybe Offset -> Maybe Limit
115 -> Maybe OrderBy -> Cmd err [FacetDoc]
116 getTable' cId ft o l order =
118 (Just Docs) -> runViewDocuments cId False o l order
119 (Just Trash) -> runViewDocuments cId True o l order
120 (Just MoreFav) -> moreLike cId o l order IsFav
121 (Just MoreTrash) -> moreLike cId o l order IsTrash
122 x -> panic $ "not implemented in getTable: " <> (cs $ show x)
125 getPair :: ContactId -> Maybe TabType
126 -> Maybe Offset -> Maybe Limit
127 -> Maybe OrderBy -> Cmd err [FacetDoc]
128 getPair cId ft o l order =
130 (Just Docs) -> runViewAuthorsDoc cId False o l order
131 (Just Trash) -> runViewAuthorsDoc cId True o l order
132 _ -> panic $ "not implemented: get Pairing" <> (cs $ show ft)