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 TypeOperators #-}
13 {-# LANGUAGE ScopedTypeVariables #-}
15 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.API.Prelude
27 import Gargantext.API.Node.File
28 import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..))
29 import Gargantext.Database.Prelude
30 import Gargantext.Database.Admin.Types.Node
31 import Gargantext.Database.Query.Table.NodeNode (selectPublicNodes)
32 import Gargantext.Core.Utils.DateUtils (utc2year)
33 import Gargantext.Database.Admin.Types.Hyperdata
34 import Gargantext.Database.Schema.Node -- (NodePoly(..))
35 import Gargantext.Prelude
37 import Test.QuickCheck (elements)
38 import Test.QuickCheck.Arbitrary
40 import qualified Data.List as List
41 import qualified Data.Map as Map
42 import qualified Data.Set as Set
44 ------------------------------------------------------------------------
48 api :: Text -> GargServer API
49 api baseUrl = (api_home baseUrl)
52 -------------------------------------------------------------------------
53 type API_Home = Summary " Public Home API"
54 :> Get '[JSON] [PublicData]
56 api_home :: Text -> GargServer API_Home
57 api_home baseUrl = catMaybes
58 <$> map (toPublicData baseUrl)
62 -------------------------------------------------------------------------
63 type API_Node = Summary " Public Node API"
64 :> Capture "node" NodeId
67 api_node :: NodeId -> GargServer FileApi
69 pubNodes <- publicNodes
70 -- TODO optimize with SQL
71 case Set.member nId pubNodes of
72 False -> panic "Not allowed" -- TODO throwErr
75 -------------------------------------------------------------------------
78 selectPublic :: HasNodeError err
79 => Cmd err [( Node HyperdataFolder, Maybe Int)]
80 selectPublic = selectPublicNodes
83 -- pure $ replicate 6 defaultPublicData
85 filterPublicDatas :: [(Node HyperdataFolder, Maybe Int)]
86 -> [(Node HyperdataFolder, [NodeId])]
87 filterPublicDatas datas =
89 let mi' = NodeId <$> mi in
90 ( _node_id n, (n, maybe [] (:[]) mi' ))
92 & Map.fromListWith (\(n1,i1) (_n2,i2) -> (n1, i1 <> i2))
93 & Map.filter (not . null . snd)
96 publicNodes :: HasNodeError err
97 => Cmd err (Set NodeId)
99 candidates <- filterPublicDatas <$> selectPublicNodes
102 $ map (\(n, ns) -> (_node_id n) : ns) candidates
105 -- http://localhost:8008/api/v1.0/node/23543/file/download<Paste>
106 -- http://localhost:8000/images/Gargantextuel-212x300.jpg
107 toPublicData :: Text -> (Node HyperdataFolder, [NodeId]) -> Maybe PublicData
108 toPublicData base (n , mn) = PublicData <$> (hd ^? (_Just . hf_data . cf_title))
109 <*> (hd ^? (_Just . hf_data . cf_desc ))
110 <*> (Just $ url' mn) -- "images/Gargantextuel-212x300.jpg"
112 <*> Just (cs $ show $ utc2year (n^.node_date))
113 <*> (hd ^? (_Just . hf_data . cf_query))
114 <*> (hd ^? (_Just . hf_data . cf_authors))
117 $ filter (\(HyperdataField cd _ _) -> cd == JSON)
118 $ n^. (node_hyperdata . hc_fields)
119 url' :: [NodeId] -> Text
122 <> (cs $ show $ (maybe 0 unNodeId $ head mn'))
126 data PublicData = PublicData
134 } | NoData { nodata:: Text}
138 instance FromJSON PublicData where
139 parseJSON = genericParseJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
141 instance ToJSON PublicData where
142 toJSON = genericToJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
144 instance ToSchema PublicData
145 instance Arbitrary PublicData where
147 $ replicate 6 defaultPublicData
149 defaultPublicData :: PublicData
152 (foldl (<>) "" $ replicate 100 "abstract ")
153 "images/Gargantextuel-212x300.jpg"