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.Core.Types.Individu (User(..))
18 import Gargantext.Database.Action.Flow.Utils (getUserId)
19 import Gargantext.Database.Admin.Config (hasNodeType, isInNodeTypes)
20 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataAny(..))
21 import Gargantext.Database.Admin.Types.Node (NodeId)
22 import Gargantext.Database.Admin.Types.Node -- (NodeType(..))
23 import Gargantext.Database.Prelude (Cmd)
24 import Gargantext.Database.Query.Table.Node (getNode, getNodesWith)
25 import Gargantext.Database.Query.Table.Node.Error (HasNodeError, errorWith)
26 import Gargantext.Database.Query.Table.NodeNode (insertNodeNode, deleteNodeNode)
27 import Gargantext.Database.Query.Tree.Root (getRootId)
28 import Gargantext.Database.Schema.Node
29 import Gargantext.Database.Schema.NodeNode (NodeNodePoly(..))
30 import Gargantext.Prelude
32 -- | TODO move in Config of Gargantext
33 publicNodeTypes :: [NodeType]
34 publicNodeTypes = [NodeDashboard, NodeGraph, NodePhylo]
36 ------------------------------------------------------------------------
38 data ShareNodeWith = ShareNodeWith_User { snwu_nodetype :: NodeType
40 | ShareNodeWith_Node { snwn_nodetype :: NodeType
41 , snwn_node_id :: NodeId
44 ------------------------------------------------------------------------
45 shareNodeWith :: HasNodeError err
49 shareNodeWith (ShareNodeWith_User NodeFolderShared u) n = do
50 nodeToCheck <- getNode n
51 userIdCheck <- getUserId u
52 if not (hasNodeType nodeToCheck NodeTeam)
53 then errorWith "[G.D.A.S.shareNodeWith] Can share node Team only"
55 if (view node_userId nodeToCheck == userIdCheck)
56 then errorWith "[G.D.A.S.shareNodeWith] Can share to others only"
58 folderSharedId <- getFolderId u NodeFolderShared
59 insertNodeNode [NodeNode Nothing folderSharedId n Nothing Nothing]
61 shareNodeWith (ShareNodeWith_Node NodeFolderPublic nId) n = do
62 nodeToCheck <- getNode n
63 if not (isInNodeTypes nodeToCheck publicNodeTypes)
64 then errorWith $ "[G.D.A.S.shareNodeWith] Can share this nodesTypes only: "
65 <> (cs $ show publicNodeTypes)
67 folderToCheck <- getNode nId
68 if hasNodeType folderToCheck NodeFolderPublic
69 then insertNodeNode [NodeNode Nothing nId n Nothing Nothing]
70 else errorWith "[G.D.A.S.shareNodeWith] Can share NodeWith NodeFolderPublic only"
72 shareNodeWith _ _ = errorWith "[G.D.A.S.shareNodeWith] Not implemented for this NodeType"
74 ------------------------------------------------------------------------
75 getFolderId :: HasNodeError err => User -> NodeType -> Cmd err NodeId
78 s <- getNodesWith rootId HyperdataAny (Just nt) Nothing Nothing
80 Nothing -> errorWith "[G.D.A.S.getFolderId] No folder shared found"
81 Just f -> pure (_node_id f)
83 ------------------------------------------------------------------------
86 delFolderTeam :: HasNodeError err => User -> TeamId -> Cmd err Int
87 delFolderTeam u nId = do
88 folderSharedId <- getFolderId u NodeFolderShared
89 deleteNodeNode folderSharedId nId
92 unPublish :: HasNodeError err
95 unPublish p n = deleteNodeNode p n