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