]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Action/Share.hs
[MERGE] fix warnings
[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
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
29
30 -- | TODO move in Config of Gargantext
31 publicNodeTypes :: [NodeType]
32 publicNodeTypes = [NodeDashboard, NodeGraph, NodePhylo]
33
34 ------------------------------------------------------------------------
35
36 data ShareNodeWith = ShareNodeWith_User { snwu_nodetype :: NodeType
37 , snwu_user :: User }
38 | ShareNodeWith_Node { snwn_nodetype :: NodeType
39 , snwn_node_id :: NodeId
40 }
41
42 ------------------------------------------------------------------------
43 shareNodeWith :: HasNodeError err
44 => ShareNodeWith
45 -> NodeId
46 -> Cmd err Int
47 shareNodeWith (ShareNodeWith_User NodeFolderShared u) n = do
48 nodeToCheck <- getNode n
49 userIdCheck <- getUserId u
50 if not (hasNodeType nodeToCheck NodeTeam)
51 then errorWith "[G.D.A.S.shareNodeWith] Can share node Team only"
52 else
53 if (view node_userId nodeToCheck == userIdCheck)
54 then errorWith "[G.D.A.S.shareNodeWith] Can share to others only"
55 else do
56 folderSharedId <- getFolderId u NodeFolderShared
57 insertDB ([NodeNode folderSharedId n Nothing Nothing]:: [NodeNode])
58
59 shareNodeWith (ShareNodeWith_Node NodeFolderPublic nId) n = do
60 nodeToCheck <- getNode n
61 if not (isInNodeTypes nodeToCheck publicNodeTypes)
62 then errorWith $ "[G.D.A.S.shareNodeWith] Can share this nodesTypes only: "
63 <> (cs $ show publicNodeTypes)
64 else do
65 folderToCheck <- getNode nId
66 if hasNodeType folderToCheck NodeFolderPublic
67 then insertDB ([NodeNode nId n Nothing Nothing] :: [NodeNode])
68 else errorWith "[G.D.A.S.shareNodeWith] Can share NodeWith NodeFolderPublic only"
69
70 shareNodeWith _ _ = errorWith "[G.D.A.S.shareNodeWith] Not implemented for this NodeType"
71
72 ------------------------------------------------------------------------
73 getFolderId :: HasNodeError err => User -> NodeType -> Cmd err NodeId
74 getFolderId u nt = do
75 rootId <- getRootId u
76 s <- getNodesWith rootId HyperdataAny (Just nt) Nothing Nothing
77 case head s of
78 Nothing -> errorWith "[G.D.A.S.getFolderId] No folder shared found"
79 Just f -> pure (_node_id f)
80
81 ------------------------------------------------------------------------
82 type TeamId = NodeId
83
84 delFolderTeam :: HasNodeError err => User -> TeamId -> Cmd err Int
85 delFolderTeam u nId = do
86 folderSharedId <- getFolderId u NodeFolderShared
87 deleteNodeNode folderSharedId nId
88
89
90 unPublish :: HasNodeError err
91 => ParentId -> NodeId
92 -> Cmd err Int
93 unPublish p n = deleteNodeNode p n
94