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.User (getUserId)
20 import Gargantext.Database.Admin.Config (hasNodeType, isInNodeTypes)
21 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataAny(..))
22 import Gargantext.Database.Admin.Types.Node
23 import Gargantext.Database.Query.Table.Node (getNode, getNodesWith)
24 import Gargantext.Database.Query.Table.Node.Error (HasNodeError, errorWith)
25 import Gargantext.Database.Query.Table.NodeNode (deleteNodeNode)
26 import Gargantext.Database.Query.Tree.Root (getRootId)
27 import Gargantext.Database.Schema.Node
28 import Gargantext.Prelude
30 -- | TODO move in Config of Gargantext
31 publicNodeTypes :: [NodeType]
32 publicNodeTypes = [NodeDashboard, NodeGraph, NodePhylo, NodeFile]
34 ------------------------------------------------------------------------
35 data ShareNodeWith = ShareNodeWith_User { snwu_nodetype :: NodeType
38 | ShareNodeWith_Node { snwn_nodetype :: NodeType
39 , snwn_node_id :: NodeId
41 ------------------------------------------------------------------------
42 shareNodeWith :: HasNodeError err
46 shareNodeWith (ShareNodeWith_User NodeFolderShared u) n = do
47 nodeToCheck <- getNode n
48 userIdCheck <- getUserId u
49 if not (hasNodeType nodeToCheck NodeTeam)
50 then errorWith "[G.D.A.S.shareNodeWith] Can share node Team only"
52 if (view node_userId nodeToCheck == userIdCheck)
53 then errorWith "[G.D.A.S.shareNodeWith] Can share to others only"
55 folderSharedId <- getFolderId u NodeFolderShared
56 insertDB ([NodeNode folderSharedId n Nothing Nothing]:: [NodeNode])
58 shareNodeWith (ShareNodeWith_Node NodeFolderPublic nId) n = do
59 nodeToCheck <- getNode n
60 if not (isInNodeTypes nodeToCheck publicNodeTypes)
61 then errorWith $ "[G.D.A.S.shareNodeWith] Can share this nodesTypes only: "
62 <> (cs $ show publicNodeTypes)
64 folderToCheck <- getNode nId
65 if hasNodeType folderToCheck NodeFolderPublic
66 then insertDB ([NodeNode nId n Nothing Nothing] :: [NodeNode])
67 else errorWith "[G.D.A.S.shareNodeWith] Can share NodeWith NodeFolderPublic only"
69 shareNodeWith _ _ = errorWith "[G.D.A.S.shareNodeWith] Not implemented for this NodeType"
71 ------------------------------------------------------------------------
72 getFolderId :: HasNodeError err => User -> NodeType -> Cmd err NodeId
75 s <- getNodesWith rootId HyperdataAny (Just nt) Nothing Nothing
77 Nothing -> errorWith "[G.D.A.S.getFolderId] No folder shared found"
78 Just f -> pure (_node_id f)
80 ------------------------------------------------------------------------
83 delFolderTeam :: HasNodeError err => User -> TeamId -> Cmd err Int
84 delFolderTeam u nId = do
85 folderSharedId <- getFolderId u NodeFolderShared
86 deleteNodeNode folderSharedId nId
88 unPublish :: HasNodeError err
91 unPublish p n = deleteNodeNode p n