2 Module : Gargantext.Database.Action.Share
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
13 module Gargantext.Database.Action.Share
16 import Control.Lens (view)
17 import Gargantext.Database
18 import Gargantext.Core.Types.Individu (User(..))
19 import Gargantext.Database.Action.Flow.Utils (getUserId)
20 import Gargantext.Database.Admin.Config (hasNodeType, isInNodeTypes)
21 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataAny(..))
22 import Gargantext.Database.Admin.Types.Node (NodeId)
23 import Gargantext.Database.Admin.Types.Node -- (NodeType(..))
24 import Gargantext.Database.Prelude (Cmd)
25 import Gargantext.Database.Query.Table.Node (getNode, getNodesWith)
26 import Gargantext.Database.Query.Table.Node.Error (HasNodeError, errorWith)
27 import Gargantext.Database.Query.Table.NodeNode (deleteNodeNode)
28 import Gargantext.Database.Query.Tree.Root (getRootId)
29 import Gargantext.Database.Schema.Node
30 import Gargantext.Database.Schema.NodeNode (NodeNodePoly(..))
31 import Gargantext.Prelude
33 -- | TODO move in Config of Gargantext
34 publicNodeTypes :: [NodeType]
35 publicNodeTypes = [NodeDashboard, NodeGraph, NodePhylo]
37 ------------------------------------------------------------------------
39 data ShareNodeWith = ShareNodeWith_User { snwu_nodetype :: NodeType
41 | ShareNodeWith_Node { snwn_nodetype :: NodeType
42 , snwn_node_id :: NodeId
45 ------------------------------------------------------------------------
46 shareNodeWith :: HasNodeError err
50 shareNodeWith (ShareNodeWith_User NodeFolderShared u) n = do
51 nodeToCheck <- getNode n
52 userIdCheck <- getUserId u
53 if not (hasNodeType nodeToCheck NodeTeam)
54 then errorWith "[G.D.A.S.shareNodeWith] Can share node Team only"
56 if (view node_userId nodeToCheck == userIdCheck)
57 then errorWith "[G.D.A.S.shareNodeWith] Can share to others only"
59 folderSharedId <- getFolderId u NodeFolderShared
60 insertDB ([NodeNode folderSharedId n Nothing Nothing]:: [NodeNode])
62 shareNodeWith (ShareNodeWith_Node NodeFolderPublic nId) n = do
63 nodeToCheck <- getNode n
64 if not (isInNodeTypes nodeToCheck publicNodeTypes)
65 then errorWith $ "[G.D.A.S.shareNodeWith] Can share this nodesTypes only: "
66 <> (cs $ show publicNodeTypes)
68 folderToCheck <- getNode nId
69 if hasNodeType folderToCheck NodeFolderPublic
70 then insertDB ([NodeNode nId n Nothing Nothing] :: [NodeNode])
71 else errorWith "[G.D.A.S.shareNodeWith] Can share NodeWith NodeFolderPublic only"
73 shareNodeWith _ _ = errorWith "[G.D.A.S.shareNodeWith] Not implemented for this NodeType"
75 ------------------------------------------------------------------------
76 getFolderId :: HasNodeError err => User -> NodeType -> Cmd err NodeId
79 s <- getNodesWith rootId HyperdataAny (Just nt) Nothing Nothing
81 Nothing -> errorWith "[G.D.A.S.getFolderId] No folder shared found"
82 Just f -> pure (_node_id f)
84 ------------------------------------------------------------------------
87 delFolderTeam :: HasNodeError err => User -> TeamId -> Cmd err Int
88 delFolderTeam u nId = do
89 folderSharedId <- getFolderId u NodeFolderShared
90 deleteNodeNode folderSharedId nId
93 unPublish :: HasNodeError err
96 unPublish p n = deleteNodeNode p n