2 Module : Gargantext.API.Public
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE TemplateHaskell #-}
13 {-# LANGUAGE TypeOperators #-}
14 {-# LANGUAGE ScopedTypeVariables #-}
16 module Gargantext.API.Public
19 import Control.Lens ((^?), (^.), _Just)
20 import Data.Maybe (catMaybes)
21 import Data.Text (Text)
22 import Data.List (replicate, null)
25 import GHC.Generics (Generic)
26 import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..))
27 import Gargantext.Database.Prelude
28 import Gargantext.Database.Admin.Types.Node
29 import Gargantext.Database.Query.Table.NodeNode (selectPublicNodes)
30 import Gargantext.Core.Utils.DateUtils (utc2year)
31 import Gargantext.Database.Admin.Types.Hyperdata
32 import Gargantext.Database.Schema.Node -- (NodePoly(..))
33 import Gargantext.Prelude
35 import Test.QuickCheck (elements)
36 import Test.QuickCheck.Arbitrary
37 import qualified Data.Map as Map
39 ------------------------------------------------------------------------
40 type API = Summary " Public API"
41 :> Get '[JSON] [PublicData]
43 api :: HasNodeError err
44 => Cmd err [PublicData]
45 api = catMaybes <$> map toPublicData <$> filterPublicDatas <$> selectPublic
48 selectPublic :: HasNodeError err
49 => Cmd err [( Node HyperdataFolder, Maybe Int)]
50 selectPublic = selectPublicNodes
53 -- pure $ replicate 6 defaultPublicData
55 filterPublicDatas :: [( Node HyperdataFolder, Maybe Int)] -> [(Node HyperdataFolder, [NodeId])]
56 filterPublicDatas datas = map (\(n,mi) -> let mi' = NodeId <$> mi in
57 ( _node_id n, (n, maybe [] (:[]) mi' ))
59 & Map.fromListWith (\(n1,i1) (_n2,i2) -> (n1, i1 <> i2))
60 & Map.filter (not . null . snd)
64 toPublicData :: (Node HyperdataFolder, [NodeId]) -> Maybe PublicData
65 toPublicData (n , _mn) = PublicData <$> (hd ^? (_Just . hf_data . cf_title))
66 <*> (hd ^? (_Just . hf_data . cf_desc))
67 <*> Just "images/Gargantextuel-212x300.jpg"
69 <*> Just (cs $ show $ utc2year (n^.node_date))
70 <*> (hd ^? (_Just . hf_data . cf_query))
71 <*> (hd ^? (_Just . hf_data . cf_authors))
74 $ filter (\(HyperdataField cd _ _) -> cd == JSON)
75 $ n^. (node_hyperdata . hc_fields)
78 data PublicData = PublicData
86 } | NoData { nodata:: Text}
90 instance FromJSON PublicData where
91 parseJSON = genericParseJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
93 instance ToJSON PublicData where
94 toJSON = genericToJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
96 instance ToSchema PublicData
97 instance Arbitrary PublicData where
99 $ replicate 6 defaultPublicData
101 defaultPublicData :: PublicData
104 (foldl (<>) "" $ replicate 100 "abstract ")
105 "images/Gargantextuel-212x300.jpg"