]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Action/Share.hs
[REFACT] lightning the code
[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, 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
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 msg "Can share node Team only"
54 else
55 if (view node_userId nodeToCheck == userIdCheck)
56 then msg "Can share to others only"
57 else do
58 folderSharedId <- getFolderId u NodeFolderShared
59 insertNodeNode [NodeNode 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 msg $ "Can share this nodesTypes only: " <> (cs $ show publicNodeTypes)
65 else do
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"
70
71 shareNodeWith _ _ = msg "shareNodeWith not implemented for this NodeType"
72
73 ------------------------------------------------------------------------
74 getFolderId :: HasNodeError err => User -> NodeType -> Cmd err NodeId
75 getFolderId u nt = do
76 rootId <- getRootId u
77 s <- getNodesWith rootId HyperdataAny (Just nt) Nothing Nothing
78 case head s of
79 Nothing -> msg "No folder shared found"
80 Just f -> pure (_node_id f)
81
82 ------------------------------------------------------------------------
83 type TeamId = NodeId
84
85 delFolderTeam :: HasNodeError err => User -> TeamId -> Cmd err Int
86 delFolderTeam u nId = do
87 folderSharedId <- getFolderId u NodeFolderShared
88 deleteNodeNode folderSharedId nId
89
90
91 unPublish :: HasNodeError err
92 => ParentId -> NodeId
93 -> Cmd err Int
94 unPublish p n = deleteNodeNode p n
95