2 Module : Gargantext.API.Node.Share
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE TemplateHaskell #-}
13 {-# LANGUAGE TypeOperators #-}
15 module Gargantext.API.Node.Share
20 import Data.Text (Text)
21 import GHC.Generics (Generic)
23 import Test.QuickCheck (elements)
24 import Test.QuickCheck.Arbitrary
26 import Gargantext.API.Prelude
27 import Gargantext.Core.Types.Individu (User(..))
28 import Gargantext.Database.Action.User
29 import Gargantext.Database.Action.User.New
30 import Gargantext.Database.Action.Share (ShareNodeWith(..))
31 import Gargantext.Database.Action.Share as DB (shareNodeWith, unPublish)
32 import Gargantext.Database.Admin.Types.Node
33 import Gargantext.Database.Prelude
34 import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..))
35 import Gargantext.Prelude
37 ------------------------------------------------------------------------
38 data ShareNodeParams = ShareTeamParams { username :: Text }
39 | SharePublicParams { node_id :: NodeId}
41 ------------------------------------------------------------------------
42 -- TODO unPrefix "pn_" FromJSON, ToJSON, ToSchema, adapt frontend.
43 instance FromJSON ShareNodeParams where
44 parseJSON = genericParseJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
45 instance ToJSON ShareNodeParams where
46 toJSON = genericToJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
47 instance ToSchema ShareNodeParams
48 instance Arbitrary ShareNodeParams where
49 arbitrary = elements [ ShareTeamParams "user1"
50 , SharePublicParams (NodeId 1)
52 ------------------------------------------------------------------------
54 -- TODO refactor userId which is used twice
55 api :: HasNodeError err
59 api nId (ShareTeamParams user') = do
60 user <- case guessUserName user' of
63 isRegistered <- getUserId' (UserName u)
70 fromIntegral <$> DB.shareNodeWith (ShareNodeWith_User NodeFolderShared (UserName user)) nId
71 api nId2 (SharePublicParams nId1) =
73 fromIntegral <$> DB.shareNodeWith (ShareNodeWith_Node NodeFolderPublic nId1) nId2
75 ------------------------------------------------------------------------
76 type API = Summary " Share Node with username"
77 :> ReqBody '[JSON] ShareNodeParams
80 ------------------------------------------------------------------------
81 type Unpublish = Summary " Unpublish Node"
82 :> Capture "node_id" NodeId
85 unPublish :: NodeId -> GargServer Unpublish
86 unPublish n = DB.unPublish n