]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Public.hs
[FIX] warnings
[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 (maybe, catMaybes)
21 import Data.Tuple (snd)
22 import Data.Text (Text)
23 import Data.List (replicate, null)
24 import Data.Aeson
25 import Data.Swagger
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
35 import Servant
36 import Test.QuickCheck (elements)
37 import Test.QuickCheck.Arbitrary
38 import qualified Data.Map as Map
39
40 ------------------------------------------------------------------------
41 type API = Summary " Public API"
42 :> Get '[JSON] [PublicData]
43
44 api :: HasNodeError err
45 => Cmd err [PublicData]
46 api = catMaybes <$> map toPublicData <$> filterPublicDatas <$> selectPublic
47
48
49 selectPublic :: HasNodeError err
50 => Cmd err [( Node HyperdataFolder, Maybe Int)]
51 selectPublic = selectPublicNodes
52
53 -- | For tests only
54 -- pure $ replicate 6 defaultPublicData
55
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' ))
59 ) datas
60 & Map.fromListWith (\(n1,i1) (_n2,i2) -> (n1, i1 <> i2))
61 & Map.filter (not . null . snd)
62 & Map.elems
63
64
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"
69 <*> Just "https://.."
70 <*> Just (cs $ show $ utc2year (n^.node_date))
71 <*> (hd ^? (_Just . hf_data . cf_query))
72 <*> (hd ^? (_Just . hf_data . cf_authors))
73 where
74 hd = head
75 $ filter (\(HyperdataField cd _ _) -> cd == JSON)
76 $ n^. (node_hyperdata . hc_fields)
77
78
79 data PublicData = PublicData
80 { title :: Text
81 , abstract :: Text
82 , img :: Text
83 , url :: Text
84 , date :: Text
85 , database :: Text
86 , author :: Text
87 } | NoData { nodata:: Text}
88 deriving (Generic)
89
90
91 instance FromJSON PublicData where
92 parseJSON = genericParseJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
93
94 instance ToJSON PublicData where
95 toJSON = genericToJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
96
97 instance ToSchema PublicData
98 instance Arbitrary PublicData where
99 arbitrary = elements
100 $ replicate 6 defaultPublicData
101
102 defaultPublicData :: PublicData
103 defaultPublicData =
104 PublicData "Title"
105 (foldl (<>) "" $ replicate 100 "abstract ")
106 "images/Gargantextuel-212x300.jpg"
107 "https://.."
108 "YY/MM/DD"
109 "database"
110 "Author"
111
112
113
114