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 DeriveGeneric #-}
13 {-# LANGUAGE FlexibleContexts #-}
14 {-# LANGUAGE FlexibleInstances #-}
15 {-# LANGUAGE FunctionalDependencies #-}
16 {-# LANGUAGE MultiParamTypeClasses #-}
17 {-# LANGUAGE NoImplicitPrelude #-}
18 {-# LANGUAGE OverloadedStrings #-}
19 {-# LANGUAGE RankNTypes #-}
20 {-# LANGUAGE TemplateHaskell #-}
22 module Gargantext.Database.Query.Table.Node.User
25 import Control.Lens (makeLenses)
26 import Data.Aeson.TH (deriveJSON)
27 import Data.Maybe (fromMaybe)
28 import Data.Swagger (ToSchema(..), genericDeclareNamedSchema)
29 import Data.Text (Text)
30 import Database.PostgreSQL.Simple.FromField (FromField, fromField)
31 import GHC.Generics (Generic)
32 import Gargantext.Core (Lang(..))
33 import Gargantext.Core.Types (Name)
34 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
35 import Gargantext.Database.Admin.Types.Node (Node, DocumentId, NodeId(..), UserId, NodeType(..), pgNodeId)
36 import Gargantext.Database.Admin.Types.Hyperdata (Hyperdata)
37 import Gargantext.Database.Prelude -- (fromField', Cmd)
38 import Gargantext.Database.Query.Table.Node
39 import Gargantext.Database.Query.Table.Node.Contact (HyperdataContact, fake_HyperdataContact)
40 import Gargantext.Database.Schema.Node -- (Node(..))
41 import Gargantext.Prelude
42 import Opaleye hiding (FromField)
43 import Test.QuickCheck (elements)
44 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
46 ------------------------------------------------------------------------
48 HyperdataUser { _hu_private :: !(Maybe HyperdataPrivate)
49 , _hu_shared :: !(Maybe HyperdataContact)
50 , _hu_public :: !(Maybe HyperdataPublic)
51 } deriving (Eq, Show, Generic)
53 data HyperdataPrivate =
54 HyperdataPrivate { _hpr_password :: !Text
57 deriving (Eq, Show, Generic)
59 data HyperdataPublic =
60 HyperdataPublic { _hpu_pseudo :: !Text
61 , _hpu_publications :: ![DocumentId]
63 deriving (Eq, Show, Generic)
67 fake_HyperdataUser :: HyperdataUser
68 fake_HyperdataUser = HyperdataUser (Just fake_HyperdataPrivate)
69 (Just fake_HyperdataContact)
70 (Just fake_HyperdataPublic)
72 fake_HyperdataPublic :: HyperdataPublic
73 fake_HyperdataPublic = HyperdataPublic "pseudo" [1..10]
75 fake_HyperdataPrivate :: HyperdataPrivate
76 fake_HyperdataPrivate = HyperdataPrivate "password" EN
78 -- | ToSchema instances
79 instance ToSchema HyperdataUser where
80 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_hu_")
82 instance ToSchema HyperdataPrivate where
83 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_hpr_")
85 instance ToSchema HyperdataPublic where
86 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_hpu_")
89 -- | Arbitrary instances
90 instance Arbitrary HyperdataUser where
91 arbitrary = HyperdataUser <$> arbitrary <*> arbitrary <*> arbitrary
93 instance Arbitrary HyperdataPrivate where
94 arbitrary = elements [HyperdataPrivate "" EN]
96 instance Arbitrary HyperdataPublic where
97 arbitrary = elements [HyperdataPublic "pseudo" [NodeId 2]]
100 -- | Specific Gargantext instance
101 instance Hyperdata HyperdataUser
102 instance Hyperdata HyperdataPrivate
103 instance Hyperdata HyperdataPublic
105 -- | Database (Posgresql-simple instance)
106 instance FromField HyperdataUser where
107 fromField = fromField'
108 instance FromField HyperdataPrivate where
109 fromField = fromField'
110 instance FromField HyperdataPublic where
111 fromField = fromField'
113 -- | Database (Opaleye instance)
114 instance QueryRunnerColumnDefault PGJsonb HyperdataUser where
115 queryRunnerColumnDefault = fieldQueryRunnerColumn
117 instance QueryRunnerColumnDefault PGJsonb HyperdataPrivate where
118 queryRunnerColumnDefault = fieldQueryRunnerColumn
120 instance QueryRunnerColumnDefault PGJsonb HyperdataPublic where
121 queryRunnerColumnDefault = fieldQueryRunnerColumn
124 makeLenses ''HyperdataUser
125 makeLenses ''HyperdataPrivate
126 makeLenses ''HyperdataPublic
128 -- | All Json instances
129 $(deriveJSON (unPrefix "_hu_") ''HyperdataUser)
130 $(deriveJSON (unPrefix "_hpr_") ''HyperdataPrivate)
131 $(deriveJSON (unPrefix "_hpu_") ''HyperdataPublic)
134 -----------------------------------------------------------------
135 getNodeUser :: NodeId -> Cmd err (Node HyperdataUser)
137 fromMaybe (panic $ "Node does not exist: " <> (cs $ show nId)) . headMay
138 <$> runOpaQuery (limit 1 $ selectNode (pgNodeId nId))
141 nodeUserW :: Maybe Name -> Maybe HyperdataUser -> UserId -> NodeWrite
142 nodeUserW maybeName maybeHyperdata = node NodeUser name user Nothing
144 name = maybe "User" identity maybeName
145 user = maybe fake_HyperdataUser identity maybeHyperdata