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 (maybe, catMaybes)
21 import Data.Tuple (snd)
22 import Data.Text (Text)
23 import Data.List (replicate, null)
26 import GHC.Generics (Generic)
27 import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..))
28 import Gargantext.Database.Prelude
29 import Gargantext.Database.Admin.Types.Node
30 import Gargantext.Database.Query.Table.NodeNode (selectPublicNodes)
31 import Gargantext.Core.Utils.DateUtils (utc2year)
32 import Gargantext.Database.Admin.Types.Hyperdata
33 import Gargantext.Database.Schema.Node -- (NodePoly(..))
34 import Gargantext.Prelude
36 import Test.QuickCheck (elements)
37 import Test.QuickCheck.Arbitrary
38 import qualified Data.Map as Map
40 ------------------------------------------------------------------------
41 type API = Summary " Public API"
42 :> Get '[JSON] [PublicData]
44 api :: HasNodeError err
45 => Cmd err [PublicData]
46 api = catMaybes <$> map toPublicData <$> filterPublicDatas <$> selectPublic
49 selectPublic :: HasNodeError err
50 => Cmd err [( Node HyperdataFolder, Maybe Int)]
51 selectPublic = selectPublicNodes
54 -- pure $ replicate 6 defaultPublicData
56 filterPublicDatas :: [( Node HyperdataFolder, Maybe Int)] -> [(Node HyperdataFolder, [NodeId])]
57 filterPublicDatas datas = map (\(n,mi) -> let mi' = NodeId <$> mi in
58 ( _node_id n, (n, maybe [] (:[]) mi' ))
60 & Map.fromListWith (\(n1,i1) (_n2,i2) -> (n1, i1 <> i2))
61 & Map.filter (not . null . snd)
65 toPublicData :: (Node HyperdataFolder, [NodeId]) -> Maybe PublicData
66 toPublicData (n , _mn) = PublicData <$> (hd ^? (_Just . hf_data . cf_title))
67 <*> (hd ^? (_Just . hf_data . cf_desc))
68 <*> Just "images/Gargantextuel-212x300.jpg"
70 <*> Just (cs $ show $ utc2year (n^.node_date))
71 <*> (hd ^? (_Just . hf_data . cf_query))
72 <*> (hd ^? (_Just . hf_data . cf_authors))
75 $ filter (\(HyperdataField cd _ _) -> cd == JSON)
76 $ n^. (node_hyperdata . hc_fields)
79 data PublicData = PublicData
87 } | NoData { nodata:: Text}
91 instance FromJSON PublicData where
92 parseJSON = genericParseJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
94 instance ToJSON PublicData where
95 toJSON = genericToJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
97 instance ToSchema PublicData
98 instance Arbitrary PublicData where
100 $ replicate 6 defaultPublicData
102 defaultPublicData :: PublicData
105 (foldl (<>) "" $ replicate 100 "abstract ")
106 "images/Gargantextuel-212x300.jpg"