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, msg)
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 msg "Can share node Team only"
55 if (view node_userId nodeToCheck == userIdCheck)
56 then msg "Can share to others only"
58 folderSharedId <- getFolderId u NodeFolderShared
59 insertNodeNode [NodeNode folderSharedId n Nothing Nothing]
61 shareNodeWith (ShareNodeWith_Node NodeFolderPublic nId) n = do
62 nodeToCheck <- getNode n
63 if not (isInNodeTypes nodeToCheck publicNodeTypes)
64 then msg $ "Can share this nodesTypes only: " <> (cs $ show publicNodeTypes)
66 folderToCheck <- getNode nId
67 if hasNodeType folderToCheck NodeFolderPublic
68 then insertNodeNode [NodeNode nId n Nothing Nothing]
69 else msg "Can share NodeWith NodeFolderPublic only"
71 shareNodeWith _ _ = msg "shareNodeWith not implemented for this NodeType"
73 ------------------------------------------------------------------------
74 getFolderId :: HasNodeError err => User -> NodeType -> Cmd err NodeId
77 s <- getNodesWith rootId HyperdataAny (Just nt) Nothing Nothing
79 Nothing -> msg "No folder shared found"
80 Just f -> pure (_node_id f)
82 ------------------------------------------------------------------------
85 delFolderTeam :: HasNodeError err => User -> TeamId -> Cmd err Int
86 delFolderTeam u nId = do
87 folderSharedId <- getFolderId u NodeFolderShared
88 deleteNodeNode folderSharedId nId
91 unPublish :: HasNodeError err
94 unPublish p n = deleteNodeNode p n