Skip to content

A peer-to-peer stack for code collaboration

Generalize action monad in 'Radicle.Ipfs'

Thomas Scholtesd441bb9abe55
diff --git a/exe/Daemon.hs b/exe/Daemon.hs
index dd18eb3..b548dc5 100644
--- a/exe/Daemon.hs
+++ b/exe/Daemon.hs
@@ -109,7 +109,7 @@ waitForIpfsDaemon = do
go (n - 1)
checkIpfsIsOnline :: Daemon Bool
- checkIpfsIsOnline = liftIO $ do
+ checkIpfsIsOnline = do
result <- tryJust isIpfsExceptionNoDaemon Ipfs.version
pure $ case result of
Left _ -> False
diff --git a/src/Radicle/Daemon/Ipfs.hs b/src/Radicle/Daemon/Ipfs.hs
index 3d6f396..fa87ba9 100644
--- a/src/Radicle/Daemon/Ipfs.hs
+++ b/src/Radicle/Daemon/Ipfs.hs
@@ -51,7 +51,7 @@ valueToJson = Aeson.String . renderCompactPretty
-- * Types
-class (MonadIO m, MonadThrow m, MonadUnliftIO m) => MonadMachineIpfs m
+class (MonadIO m, MonadThrow m, MonadUnliftIO m, Ipfs.MonadIpfs m) => MonadMachineIpfs m
newtype MachineId = MachineId { getMachineId :: Text }
deriving (Show, Eq, Ord, Generic)
@@ -162,7 +162,7 @@ instance Aeson.FromJSON MachineEntryIndex where
-- TODO: we might want to make this safer by taking the expected head
-- MachineEntryIndex as an argument.
writeIpfs :: (MonadMachineIpfs m) => MachineId -> [Value] -> m MachineEntryIndex
-writeIpfs (MachineId ipnsId) values = liftIO $ do
+writeIpfs (MachineId ipnsId) values = do
Ipfs.NameResolveResponse cid <- Ipfs.nameResolve ipnsId
Ipfs.DagPutResponse newEntryCid <- Ipfs.dagPut $ MachineEntry values cid
Ipfs.namePublish ipnsId $ Ipfs.AddressIpfs newEntryCid
@@ -176,11 +176,11 @@ machineTopic (MachineId id) = Topic ("radicle:machine:" <> id)
-- | Publish a 'Message' on a machine's IPFS pubsub topic.
publish :: (MonadMachineIpfs m) => MachineId -> Message -> m ()
-publish id msg = liftIO $ Ipfs.publish (getTopic (machineTopic id)) (Aeson.encode msg)
+publish id msg = Ipfs.publish (getTopic (machineTopic id)) (Aeson.encode msg)
-- | Subscribe to messages on a machine's IPFS pubsub topic.
subscribeForever :: (MonadMachineIpfs m) => MachineId -> (Either Text Message -> IO ()) -> m ()
-subscribeForever id messageHandler = liftIO $ Ipfs.subscribe topic pubsubHandler
+subscribeForever id messageHandler = Ipfs.subscribe topic pubsubHandler
where
Topic topic = machineTopic id
pubsubHandler Ipfs.PubsubMessage{..} = messageHandler $ case decodeStrict messageData of
@@ -199,7 +199,7 @@ machineInputsFrom
=> MachineId -> Maybe MachineEntryIndex -> m (MachineEntryIndex, [Value])
machineInputsFrom (MachineId ipnsId) maybeFrom = do
let MachineEntryIndex fromCid = fromMaybe (MachineEntryIndex emptyMachineCid) maybeFrom
- Ipfs.NameResolveResponse cid <- liftIO $ Ipfs.nameResolve ipnsId
+ Ipfs.NameResolveResponse cid <- Ipfs.nameResolve ipnsId
blocks <- getBlocks cid fromCid
pure $ (MachineEntryIndex cid, blocks)
where
@@ -209,8 +209,8 @@ machineInputsFrom (MachineId ipnsId) maybeFrom = do
then pure []
else do
let addr = Ipfs.AddressIpfs cid
- entry <- liftIO $ Ipfs.dagGet addr
- _ <- liftIO $ Ipfs.pinAdd addr
+ entry <- Ipfs.dagGet addr
+ _ <- Ipfs.pinAdd addr
rest <- getBlocks (entryPrevious entry) fromCid
pure $ rest <> entryExpressions entry
@@ -231,8 +231,8 @@ emptyMachineCid =
-- Generates a new IPNS key and sets the IPNS record to the empty
-- machine DAG node.
createMachine :: (MonadMachineIpfs m) => m MachineId
-createMachine = liftIO $ do
- uuid <- UUID.uuid
+createMachine = do
+ uuid <- liftIO UUID.uuid
Ipfs.KeyGenResponse ipnsId <- Ipfs.keyGen uuid
Ipfs.namePublish ipnsId $ Ipfs.AddressIpfs emptyMachineCid
pure $ MachineId ipnsId
@@ -240,7 +240,7 @@ createMachine = liftIO $ do
-- | Publish to IPNS a machine entry index for a machine. Should only be issued
-- by the writer.
ipnsPublish :: (MonadMachineIpfs m) => MachineId -> MachineEntryIndex -> m ()
-ipnsPublish id (MachineEntryIndex cid) = liftIO $ Ipfs.namePublish (getMachineId id) $ Ipfs.AddressIpfs cid
+ipnsPublish id (MachineEntryIndex cid) = Ipfs.namePublish (getMachineId id) $ Ipfs.AddressIpfs cid
-- * Topic subscriptions
diff --git a/src/Radicle/Daemon/Monad.hs b/src/Radicle/Daemon/Monad.hs
index d6bd4a6..ba59ca7 100644
--- a/src/Radicle/Daemon/Monad.hs
+++ b/src/Radicle/Daemon/Monad.hs
@@ -24,6 +24,7 @@ import Radicle.Daemon.Error
import Radicle.Daemon.Ipfs (MonadMachineIpfs)
import Radicle.Daemon.Logging
import Radicle.Daemon.MachineStore
+import qualified Radicle.Ipfs as Ipfs
newtype Daemon a = Daemon (ReaderT Env IO a)
@@ -41,6 +42,8 @@ instance MonadLog Daemon where
instance MonadMachineStore Daemon where
askMachines = asks machines
+instance Ipfs.MonadIpfs Daemon
+
instance MonadMachineIpfs Daemon
-- * Environment
diff --git a/src/Radicle/Ipfs.hs b/src/Radicle/Ipfs.hs
index bb283c8..2245c5b 100644
--- a/src/Radicle/Ipfs.hs
+++ b/src/Radicle/Ipfs.hs
@@ -1,6 +1,7 @@
-- | Functions to talk to the IPFS API
module Radicle.Ipfs
- ( IpfsException(..)
+ ( MonadIpfs
+ , IpfsException(..)
, ipldLink
, parseIpldLink
, IpnsId
@@ -54,6 +55,8 @@ import qualified Network.HTTP.Conduit as HTTP
import qualified Network.Wreq as Wreq
import System.Environment (lookupEnv)
+class (MonadIO m, MonadCatch m) => MonadIpfs m
+
data IpfsException
= IpfsException Text
| IpfsExceptionErrResp Text
@@ -89,7 +92,7 @@ instance Exception IpfsException where
-- | Catches 'HttpException's and re-throws them as 'IpfsException's.
--
-- @path@ is the IPFS API path that is added to some errors.
-mapHttpException :: Text -> IO a -> IO a
+mapHttpException :: (MonadCatch m) => Text -> m a -> m a
mapHttpException path io = catch io (throw . mapHttpExceptionData)
where
mapHttpExceptionData :: HttpException -> IpfsException
@@ -164,7 +167,7 @@ instance FromJSON VersionResponse where
v <- o .: "Version"
pure $ VersionResponse v
-version :: IO VersionResponse
+version :: (MonadIpfs m) => m VersionResponse
version = ipfsHttpGet "version" []
data PubsubMessage = PubsubMessage
@@ -185,8 +188,8 @@ instance FromJSON PubsubMessage where
-- | Subscribe to a topic and call @messageHandler@ on every message.
-- The IO action blocks while we are subscribed. To stop subscription
-- you need to kill the thread the subscription is running in.
-subscribe :: Text -> (PubsubMessage -> IO ()) -> IO ()
-subscribe topic messageHandler = runResourceT $ do
+subscribe :: (MonadIpfs m) => Text -> (PubsubMessage -> IO ()) -> m ()
+subscribe topic messageHandler = liftIO $ runResourceT $ do
mgr <- liftIO $ HTTP.newManager HTTP.defaultManagerSettings
url <- liftIO $ ipfsApiUrl "pubsub/sub"
req <- HTTP.parseRequest (toS url) <&>
@@ -213,13 +216,13 @@ subscribe topic messageHandler = runResourceT $ do
-- | Publish a message to a topic.
-publish :: Text -> LByteString -> IO ()
+publish :: (MonadIpfs m) => Text -> LByteString -> m ()
publish topic message =
void $ ipfsHttpPost' "pubsub/pub" [("arg", topic)] "data" message
newtype KeyGenResponse = KeyGenResponse IpnsId
-keyGen :: Text -> IO KeyGenResponse
+keyGen :: (MonadIpfs m) => Text -> m KeyGenResponse
keyGen name = ipfsHttpGet "key/gen" [("arg", name), ("type", "ed25519")]
instance FromJSON KeyGenResponse where
@@ -231,7 +234,7 @@ newtype DagPutResponse
= DagPutResponse CID
-- | Put and pin a dag node.
-dagPut :: ToJSON a => a -> IO DagPutResponse
+dagPut :: (MonadIpfs m, ToJSON a) => a -> m DagPutResponse
dagPut obj = ipfsHttpPost "dag/put" [("pin", "true")] "arg" (Aeson.encode obj)
instance FromJSON DagPutResponse where
@@ -252,18 +255,18 @@ instance FromJSON PinResponse where
Right cids -> pure $ PinResponse cids
-- | Pin objects to local storage.
-pinAdd :: Address -> IO PinResponse
+pinAdd :: (MonadIpfs m) => Address -> m PinResponse
pinAdd addr = ipfsHttpGet "pin/add" [("arg", addressToText addr)]
-- | Get a dag node.
-dagGet :: FromJSON a => Address -> IO a
+dagGet :: (MonadIpfs m) => FromJSON a => Address -> m a
dagGet addr = do
result <- ipfsHttpGet "dag/get" [("arg", addressToText addr)]
case Aeson.fromJSON result of
Aeson.Error err -> throw $ IpfsExceptionIpldParse addr (toS err)
Aeson.Success a -> pure a
-namePublish :: IpnsId -> Address -> IO ()
+namePublish :: (MonadIpfs m) => IpnsId -> Address -> m ()
namePublish ipnsId addr = do
_ :: Aeson.Value <- ipfsHttpGet "name/publish" [("arg", addressToText addr), ("key", ipnsId)]
pure ()
@@ -272,7 +275,7 @@ namePublish ipnsId addr = do
newtype NameResolveResponse
= NameResolveResponse CID
-nameResolve :: IpnsId -> IO NameResolveResponse
+nameResolve :: (MonadIpfs m) => IpnsId -> m NameResolveResponse
nameResolve ipnsId = ipfsHttpGet "name/resolve" [("arg", ipnsId), ("recursive", "true")]
instance FromJSON NameResolveResponse where
@@ -289,34 +292,35 @@ instance FromJSON NameResolveResponse where
--------------------------------------------------------------------------
ipfsHttpGet
- :: FromJSON a
+ :: (FromJSON a, MonadIpfs m)
=> Text -- ^ Path of the endpoint under "/api/v0/"
-> [(Text, Text)] -- ^ URL query parameters
- -> IO a
-ipfsHttpGet path params = mapHttpException path $ do
+ -> m a
+ipfsHttpGet path params = liftIO $ mapHttpException path $ do
let opts = Wreq.defaults & Wreq.params .~ params
url <- ipfsApiUrl path
res <- Wreq.getWith opts (toS url)
getJsonResponseBody path res
ipfsHttpPost
- :: FromJSON a
+ :: (MonadIpfs m, FromJSON a)
=> Text -- ^ Path of the endpoint under "/api/v0/"
-> [(Text, Text)] -- ^ URL query parameters
-> Text -- ^ Name of the argument for payload
-> LByteString -- ^ Payload argument
- -> IO a
+ -> m a
ipfsHttpPost path params payloadArgName payload = mapHttpException path $ do
res <- ipfsHttpPost' path params payloadArgName payload
- getJsonResponseBody path res
+ liftIO $ getJsonResponseBody path res
ipfsHttpPost'
- :: Text -- ^ Path of the endpoint under "/api/v0/"
+ :: (MonadIpfs m)
+ => Text -- ^ Path of the endpoint under "/api/v0/"
-> [(Text, Text)] -- ^ URL query parameters
-> Text -- ^ Name of the argument for payload
-> LByteString -- ^ Payload argument
- -> IO (Wreq.Response LByteString)
-ipfsHttpPost' path params payloadArgName payload = mapHttpException path $ do
+ -> m (Wreq.Response LByteString)
+ipfsHttpPost' path params payloadArgName payload = liftIO $ mapHttpException path $ do
let opts = Wreq.defaults & Wreq.params .~ params
url <- ipfsApiUrl path
Wreq.postWith opts (toS url) (Wreq.partLBS payloadArgName payload)