Commit 487ddd4c authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Introduce the NodeNodeCategory

Now a `NodeNodePoly` stores a typed `NodeNodeCategory`, we can use
to enforce invariants and making lookups more efficient.
parent 0336eec7
......@@ -122,7 +122,7 @@ insertNodeNode ns = mkCmd $ \conn -> fromIntegral <$> (runInsert_ conn
-> NodeNode (pgNodeId n1)
(pgNodeId n2)
(sqlDouble <$> x)
(sqlInt4 <$> y)
(sqlInt4 . toDBid <$> y)
) ns
......@@ -260,6 +260,8 @@ selectPublicNodes = runOpaQuery (queryWithType NodeFolderPublic)
-- | A 'Node' is read-only if there exist a match in the node_nodes directory
-- where the source is a public folder. Certain category of nodes (like private/shared folders, etc)
-- are automatically read-only.
-- NOTE(adn) This query could probably be simplified to just account for the category, which should
-- be enough to understand if a node is read-only or not.
isNodeReadOnly :: HasDBid NodeType => NodeId -> DBCmd err Bool
isNodeReadOnly nodeId = do
( result :: [NodeId] ) <- runOpaQuery $ do
......@@ -269,6 +271,7 @@ isNodeReadOnly nodeId = do
let isLinked = (nn ^. nn_node1_id .== pgNodeId nodeId) .|| (nn ^. nn_node2_id .== pgNodeId nodeId)
where_ isLinked
where_ ((nn ^. nn_category) .== sqlInt4 (toDBid NNC_read_only_publish))
pure $ nn ^. nn_node1_id
return $ Prelude.not (L.null result) -- Return True if any rows are found
......@@ -324,8 +327,10 @@ shareNode (SourceId sourceId) (TargetId targetId) =
-- 'getUserRootPublicNode' to acquire the 'TargetId'.
publishNode :: SourceId -> TargetId -> DBCmd err ()
publishNode (SourceId sourceId) (TargetId targetId) =
void $ insertNodeNode [ NodeNode sourceId targetId Nothing Nothing ]
void $ insertNodeNode [ NodeNode sourceId targetId Nothing (Just NNC_read_only_publish) ]
-- | FIXME(adn) This needs to delete all the children relationships,
-- recursively.
unpublishNode :: SourceId -> TargetId -> DBCmd err ()
unpublishNode (SourceId sourceId) (TargetId targetId) =
void $ deleteNodeNode sourceId targetId
......
......@@ -13,11 +13,30 @@ commentary with @some markup@.
{-# LANGUAGE Arrows #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
module Gargantext.Database.Schema.NodeNode where
module Gargantext.Database.Schema.NodeNode (
-- * Opaque type synonims
NodeNodeRead
, NodeNodeWrite
, NodeNode
-- * Types
, NodeNodePoly(..)
, NodeNodeCategory(..)
-- * Lenses
, nn_node1_id
, nn_node2_id
, nn_score
, nn_category
, nodeNodeTable
) where
import Gargantext.Core (HasDBid(..))
import Gargantext.Core.Types
import Gargantext.Database.Schema.Prelude
import Gargantext.Prelude
......@@ -40,7 +59,22 @@ type NodeNodeRead = NodeNodePoly (Field SqlInt4)
(Field SqlFloat8)
(Field SqlInt4)
type NodeNode = NodeNodePoly NodeId NodeId (Maybe Double) (Maybe Int)
data NodeNodeCategory
= -- | Read-only publishing relationship between nodes.
NNC_read_only_publish
deriving (Show, Eq, Ord)
instance HasDBid NodeNodeCategory where
toDBid = \case
NNC_read_only_publish -> 0
lookupDBid x = case x of
0 -> Just NNC_read_only_publish
_ -> Nothing
instance DefaultFromField SqlInt4 (Maybe NodeNodeCategory) where
defaultFromField = lookupDBid <$> fromPGSFromField
type NodeNode = NodeNodePoly NodeId NodeId (Maybe Double) (Maybe NodeNodeCategory)
$(makeAdaptorAndInstance "pNodeNode" ''NodeNodePoly)
makeLenses ''NodeNodePoly
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment