]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Node/Share.hs
[FIX] test ok. TODO : API info <> demo users should not be able to create accounts
[gargantext.git] / src / Gargantext / API / Node / Share.hs
1 {-|
2 Module : Gargantext.API.Node.Share
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
15 module Gargantext.API.Node.Share
16 where
17
18 import Data.Aeson
19 import Data.Swagger
20 import Data.Text (Text)
21 import GHC.Generics (Generic)
22 import Servant
23 import Test.QuickCheck (elements)
24 import Test.QuickCheck.Arbitrary
25
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
36
37 ------------------------------------------------------------------------
38 data ShareNodeParams = ShareTeamParams { username :: Text }
39 | SharePublicParams { node_id :: NodeId}
40 deriving (Generic)
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)
51 ]
52 ------------------------------------------------------------------------
53 -- TODO permission
54 api :: HasNodeError err
55 => NodeId
56 -> ShareNodeParams
57 -> CmdR err Int
58 api nId (ShareTeamParams user') = do
59 user <- case guessUserName user' of
60 Nothing -> pure user'
61 Just (u,_) -> do
62 isRegistered <- getUserId' (UserName u)
63 case isRegistered of
64 Just _ -> pure u
65 Nothing -> do
66 _ <- newUsers [user']
67 pure u
68
69 fromIntegral <$> DB.shareNodeWith (ShareNodeWith_User NodeFolderShared (UserName user)) nId
70 api nId2 (SharePublicParams nId1) =
71
72 fromIntegral <$> DB.shareNodeWith (ShareNodeWith_Node NodeFolderPublic nId1) nId2
73
74 ------------------------------------------------------------------------
75 type API = Summary " Share Node with username"
76 :> ReqBody '[JSON] ShareNodeParams
77 :> Post '[JSON] Int
78
79 ------------------------------------------------------------------------
80 type Unpublish = Summary " Unpublish Node"
81 :> Capture "node_id" NodeId
82 :> Put '[JSON] Int
83
84 unPublish :: NodeId -> GargServer Unpublish
85 unPublish n = DB.unPublish n