2 Module : Gargantext.Database.Action.Query.Node.User
3 Description : User Node in Gargantext
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE FunctionalDependencies #-}
13 {-# LANGUAGE TemplateHaskell #-}
15 module Gargantext.Database.Query.Table.Node.User
18 import Control.Lens (makeLenses)
19 import Data.Aeson.TH (deriveJSON)
20 import Data.Maybe (fromMaybe)
21 import Data.Swagger (ToSchema(..), genericDeclareNamedSchema)
22 import Data.Text (Text)
23 import Database.PostgreSQL.Simple.FromField (FromField, fromField)
24 import GHC.Generics (Generic)
25 import Gargantext.Core (Lang(..))
26 import Gargantext.Core.Types (Name)
27 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
28 import Gargantext.Database.Admin.Types.Node (Node, DocumentId, NodeId(..), UserId, NodeType(..), pgNodeId)
29 import Gargantext.Database.Admin.Types.Hyperdata (Hyperdata)
30 import Gargantext.Database.Prelude -- (fromField', Cmd)
31 import Gargantext.Database.Query.Table.Node
32 import Gargantext.Database.Query.Table.Node.Contact (HyperdataContact, fake_HyperdataContact)
33 import Gargantext.Database.Schema.Node -- (Node(..))
34 import Gargantext.Prelude
35 import Opaleye hiding (FromField)
36 import Test.QuickCheck (elements)
37 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
39 ------------------------------------------------------------------------
41 HyperdataUser { _hu_private :: !(Maybe HyperdataPrivate)
42 , _hu_shared :: !(Maybe HyperdataContact)
43 , _hu_public :: !(Maybe HyperdataPublic)
44 } deriving (Eq, Show, Generic)
46 data HyperdataPrivate =
47 HyperdataPrivate { _hpr_password :: !Text
50 deriving (Eq, Show, Generic)
52 data HyperdataPublic =
53 HyperdataPublic { _hpu_pseudo :: !Text
54 , _hpu_publications :: ![DocumentId]
56 deriving (Eq, Show, Generic)
60 fake_HyperdataUser :: HyperdataUser
61 fake_HyperdataUser = HyperdataUser (Just fake_HyperdataPrivate)
62 (Just fake_HyperdataContact)
63 (Just fake_HyperdataPublic)
65 fake_HyperdataPublic :: HyperdataPublic
66 fake_HyperdataPublic = HyperdataPublic "pseudo" [1..10]
68 fake_HyperdataPrivate :: HyperdataPrivate
69 fake_HyperdataPrivate = HyperdataPrivate "password" EN
71 -- | ToSchema instances
72 instance ToSchema HyperdataUser where
73 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_hu_")
75 instance ToSchema HyperdataPrivate where
76 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_hpr_")
78 instance ToSchema HyperdataPublic where
79 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_hpu_")
82 -- | Arbitrary instances
83 instance Arbitrary HyperdataUser where
84 arbitrary = HyperdataUser <$> arbitrary <*> arbitrary <*> arbitrary
86 instance Arbitrary HyperdataPrivate where
87 arbitrary = elements [HyperdataPrivate "" EN]
89 instance Arbitrary HyperdataPublic where
90 arbitrary = elements [HyperdataPublic "pseudo" [NodeId 2]]
93 -- | Specific Gargantext instance
94 instance Hyperdata HyperdataUser
95 instance Hyperdata HyperdataPrivate
96 instance Hyperdata HyperdataPublic
98 -- | Database (Posgresql-simple instance)
99 instance FromField HyperdataUser where
100 fromField = fromField'
101 instance FromField HyperdataPrivate where
102 fromField = fromField'
103 instance FromField HyperdataPublic where
104 fromField = fromField'
106 -- | Database (Opaleye instance)
107 instance QueryRunnerColumnDefault PGJsonb HyperdataUser where
108 queryRunnerColumnDefault = fieldQueryRunnerColumn
110 instance QueryRunnerColumnDefault PGJsonb HyperdataPrivate where
111 queryRunnerColumnDefault = fieldQueryRunnerColumn
113 instance QueryRunnerColumnDefault PGJsonb HyperdataPublic where
114 queryRunnerColumnDefault = fieldQueryRunnerColumn
117 makeLenses ''HyperdataUser
118 makeLenses ''HyperdataPrivate
119 makeLenses ''HyperdataPublic
121 -- | All Json instances
122 $(deriveJSON (unPrefix "_hu_") ''HyperdataUser)
123 $(deriveJSON (unPrefix "_hpr_") ''HyperdataPrivate)
124 $(deriveJSON (unPrefix "_hpu_") ''HyperdataPublic)
127 -----------------------------------------------------------------
128 getNodeUser :: NodeId -> Cmd err (Node HyperdataUser)
130 fromMaybe (panic $ "Node does not exist: " <> (cs $ show nId)) . headMay
131 <$> runOpaQuery (limit 1 $ selectNode (pgNodeId nId))
134 nodeUserW :: Maybe Name -> Maybe HyperdataUser -> UserId -> NodeWrite
135 nodeUserW maybeName maybeHyperdata = node NodeUser name user Nothing
137 name = maybe "User" identity maybeName
138 user = maybe fake_HyperdataUser identity maybeHyperdata