]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Action/Share.hs
[Clean]
[gargantext.git] / src / Gargantext / Database / Action / Share.hs
1 {-|
2 Module : Gargantext.Database.Action.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
13 module Gargantext.Database.Action.Share
14 where
15
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
32
33 -- | TODO move in Config of Gargantext
34 publicNodeTypes :: [NodeType]
35 publicNodeTypes = [NodeDashboard, NodeGraph, NodePhylo]
36
37 ------------------------------------------------------------------------
38
39 data ShareNodeWith = ShareNodeWith_User { snwu_nodetype :: NodeType
40 , snwu_user :: User }
41 | ShareNodeWith_Node { snwn_nodetype :: NodeType
42 , snwn_node_id :: NodeId
43 }
44
45 ------------------------------------------------------------------------
46 shareNodeWith :: HasNodeError err
47 => ShareNodeWith
48 -> NodeId
49 -> Cmd err Int
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"
55 else
56 if (view node_userId nodeToCheck == userIdCheck)
57 then errorWith "[G.D.A.S.shareNodeWith] Can share to others only"
58 else do
59 folderSharedId <- getFolderId u NodeFolderShared
60 insertDB ([NodeNode folderSharedId n Nothing Nothing]:: [NodeNode])
61
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)
67 else do
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"
72
73 shareNodeWith _ _ = errorWith "[G.D.A.S.shareNodeWith] Not implemented for this NodeType"
74
75 ------------------------------------------------------------------------
76 getFolderId :: HasNodeError err => User -> NodeType -> Cmd err NodeId
77 getFolderId u nt = do
78 rootId <- getRootId u
79 s <- getNodesWith rootId HyperdataAny (Just nt) Nothing Nothing
80 case head s of
81 Nothing -> errorWith "[G.D.A.S.getFolderId] No folder shared found"
82 Just f -> pure (_node_id f)
83
84 ------------------------------------------------------------------------
85 type TeamId = NodeId
86
87 delFolderTeam :: HasNodeError err => User -> TeamId -> Cmd err Int
88 delFolderTeam u nId = do
89 folderSharedId <- getFolderId u NodeFolderShared
90 deleteNodeNode folderSharedId nId
91
92
93 unPublish :: HasNodeError err
94 => ParentId -> NodeId
95 -> Cmd err Int
96 unPublish p n = deleteNodeNode p n
97