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
20 import Control.Lens ((^?), (^.), _Just)
21 import Data.Maybe (catMaybes)
22 import Data.Text (Text)
23 import Data.List (replicate, null)
26 import GHC.Generics (Generic)
27 import Gargantext.API.Prelude
28 import Gargantext.API.Node.File
29 import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..))
30 import Gargantext.Database.Prelude
31 import Gargantext.Database.Admin.Types.Node
32 import Gargantext.Database.Query.Table.NodeNode (selectPublicNodes)
33 import Gargantext.Core.Utils.DateUtils (utc2year)
34 import Gargantext.Database.Admin.Types.Hyperdata
35 import Gargantext.Database.Schema.Node -- (NodePoly(..))
36 import Gargantext.Prelude
38 import Test.QuickCheck (elements)
39 import Test.QuickCheck.Arbitrary
41 import qualified Data.List as List
42 import qualified Data.Map as Map
43 import qualified Data.Set as Set
45 ------------------------------------------------------------------------
49 api :: Text -> GargServer API
50 api baseUrl = (api_home baseUrl)
53 -------------------------------------------------------------------------
54 type API_Home = Summary " Public Home API"
55 :> Get '[JSON] [PublicData]
57 api_home :: Text -> GargServer API_Home
58 api_home baseUrl = catMaybes
59 <$> map (toPublicData baseUrl)
63 -------------------------------------------------------------------------
64 type API_Node = Summary " Public Node API"
65 :> Capture "node" NodeId
68 api_node :: NodeId -> GargServer FileApi
70 pubNodes <- publicNodes
71 -- TODO optimize with SQL
72 case Set.member nId pubNodes of
73 False -> panic "Not allowed" -- TODO throwErr
76 -------------------------------------------------------------------------
79 selectPublic :: HasNodeError err
80 => Cmd err [( Node HyperdataFolder, Maybe Int)]
81 selectPublic = selectPublicNodes
84 -- pure $ replicate 6 defaultPublicData
86 filterPublicDatas :: [(Node HyperdataFolder, Maybe Int)]
87 -> [(Node HyperdataFolder, [NodeId])]
88 filterPublicDatas datas =
90 let mi' = NodeId <$> mi in
91 ( _node_id n, (n, maybe [] (:[]) mi' ))
93 & Map.fromListWith (\(n1,i1) (_n2,i2) -> (n1, i1 <> i2))
94 & Map.filter (not . null . snd)
97 publicNodes :: HasNodeError err
98 => Cmd err (Set NodeId)
100 candidates <- filterPublicDatas <$> selectPublicNodes
103 $ map (\(n, ns) -> (_node_id n) : ns) candidates
106 -- http://localhost:8008/api/v1.0/node/23543/file/download<Paste>
107 -- http://localhost:8000/images/Gargantextuel-212x300.jpg
108 toPublicData :: Text -> (Node HyperdataFolder, [NodeId]) -> Maybe PublicData
109 toPublicData base (n , mn) = PublicData <$> (hd ^? (_Just . hf_data . cf_title))
110 <*> (hd ^? (_Just . hf_data . cf_desc ))
111 <*> (Just $ url' mn) -- "images/Gargantextuel-212x300.jpg"
113 <*> Just (cs $ show $ utc2year (n^.node_date))
114 <*> (hd ^? (_Just . hf_data . cf_query))
115 <*> (hd ^? (_Just . hf_data . cf_authors))
118 $ filter (\(HyperdataField cd _ _) -> cd == JSON)
119 $ n^. (node_hyperdata . hc_fields)
120 url' :: [NodeId] -> Text
123 <> (cs $ show $ (maybe 0 unNodeId $ head mn'))
127 data PublicData = PublicData
135 } | NoData { nodata:: Text}
139 instance FromJSON PublicData where
140 parseJSON = genericParseJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
142 instance ToJSON PublicData where
143 toJSON = genericToJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
145 instance ToSchema PublicData
146 instance Arbitrary PublicData where
148 $ replicate 6 defaultPublicData
150 defaultPublicData :: PublicData
153 (foldl (<>) "" $ replicate 100 "abstract ")
154 "images/Gargantextuel-212x300.jpg"