]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Public.hs
[TextFlow] Type rename
[gargantext.git] / src / Gargantext / API / Public.hs
1 {-|
2 Module : Gargantext.API.Public
3 Description :
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 -}
11
12 {-# LANGUAGE TemplateHaskell #-}
13 {-# LANGUAGE TypeOperators #-}
14 {-# LANGUAGE ScopedTypeVariables #-}
15
16 module Gargantext.API.Public
17 where
18
19 import Control.Lens ((^?), (^.), _Just)
20 import Data.Maybe (catMaybes)
21 import Data.Text (Text)
22 import Data.List (replicate, null)
23 import Data.Aeson
24 import Data.Swagger
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
34 import Servant
35 import Test.QuickCheck (elements)
36 import Test.QuickCheck.Arbitrary
37 import qualified Data.Map as Map
38
39 ------------------------------------------------------------------------
40 type API = Summary " Public API"
41 :> Get '[JSON] [PublicData]
42
43 api :: HasNodeError err
44 => Cmd err [PublicData]
45 api = catMaybes <$> map toPublicData <$> filterPublicDatas <$> selectPublic
46
47
48 selectPublic :: HasNodeError err
49 => Cmd err [( Node HyperdataFolder, Maybe Int)]
50 selectPublic = selectPublicNodes
51
52 -- For tests only
53 -- pure $ replicate 6 defaultPublicData
54
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' ))
58 ) datas
59 & Map.fromListWith (\(n1,i1) (_n2,i2) -> (n1, i1 <> i2))
60 & Map.filter (not . null . snd)
61 & Map.elems
62
63
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"
68 <*> Just "https://.."
69 <*> Just (cs $ show $ utc2year (n^.node_date))
70 <*> (hd ^? (_Just . hf_data . cf_query))
71 <*> (hd ^? (_Just . hf_data . cf_authors))
72 where
73 hd = head
74 $ filter (\(HyperdataField cd _ _) -> cd == JSON)
75 $ n^. (node_hyperdata . hc_fields)
76
77
78 data PublicData = PublicData
79 { title :: Text
80 , abstract :: Text
81 , img :: Text
82 , url :: Text
83 , date :: Text
84 , database :: Text
85 , author :: Text
86 } | NoData { nodata:: Text}
87 deriving (Generic)
88
89
90 instance FromJSON PublicData where
91 parseJSON = genericParseJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
92
93 instance ToJSON PublicData where
94 toJSON = genericToJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
95
96 instance ToSchema PublicData
97 instance Arbitrary PublicData where
98 arbitrary = elements
99 $ replicate 6 defaultPublicData
100
101 defaultPublicData :: PublicData
102 defaultPublicData =
103 PublicData "Title"
104 (foldl (<>) "" $ replicate 100 "abstract ")
105 "images/Gargantextuel-212x300.jpg"
106 "https://.."
107 "YY/MM/DD"
108 "database"
109 "Author"
110
111
112
113