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.hsindex dd18eb3..b548dc5 100644--- a/exe/Daemon.hs+++ b/exe/Daemon.hs@@ -109,7 +109,7 @@ waitForIpfsDaemon = dogo (n - 1)checkIpfsIsOnline :: Daemon Bool- checkIpfsIsOnline = liftIO $ do+ checkIpfsIsOnline = doresult <- tryJust isIpfsExceptionNoDaemon Ipfs.versionpure $ case result ofLeft _ -> Falsediff --git a/src/Radicle/Daemon/Ipfs.hs b/src/Radicle/Daemon/Ipfs.hsindex 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 mnewtype 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 = doIpfs.NameResolveResponse cid <- Ipfs.nameResolve ipnsIdIpfs.DagPutResponse newEntryCid <- Ipfs.dagPut $ MachineEntry values cidIpfs.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 pubsubHandlerwhereTopic topic = machineTopic idpubsubHandler Ipfs.PubsubMessage{..} = messageHandler $ case decodeStrict messageData of@@ -199,7 +199,7 @@ machineInputsFrom=> MachineId -> Maybe MachineEntryIndex -> m (MachineEntryIndex, [Value])machineInputsFrom (MachineId ipnsId) maybeFrom = dolet MachineEntryIndex fromCid = fromMaybe (MachineEntryIndex emptyMachineCid) maybeFrom- Ipfs.NameResolveResponse cid <- liftIO $ Ipfs.nameResolve ipnsId+ Ipfs.NameResolveResponse cid <- Ipfs.nameResolve ipnsIdblocks <- getBlocks cid fromCidpure $ (MachineEntryIndex cid, blocks)where@@ -209,8 +209,8 @@ machineInputsFrom (MachineId ipnsId) maybeFrom = dothen pure []else dolet addr = Ipfs.AddressIpfs cid- entry <- liftIO $ Ipfs.dagGet addr- _ <- liftIO $ Ipfs.pinAdd addr+ entry <- Ipfs.dagGet addr+ _ <- Ipfs.pinAdd addrrest <- getBlocks (entryPrevious entry) fromCidpure $ 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.uuidIpfs.KeyGenResponse ipnsId <- Ipfs.keyGen uuidIpfs.namePublish ipnsId $ Ipfs.AddressIpfs emptyMachineCidpure $ 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 subscriptionsdiff --git a/src/Radicle/Daemon/Monad.hs b/src/Radicle/Daemon/Monad.hsindex d6bd4a6..ba59ca7 100644--- a/src/Radicle/Daemon/Monad.hs+++ b/src/Radicle/Daemon/Monad.hs@@ -24,6 +24,7 @@ import Radicle.Daemon.Errorimport Radicle.Daemon.Ipfs (MonadMachineIpfs)import Radicle.Daemon.Loggingimport Radicle.Daemon.MachineStore+import qualified Radicle.Ipfs as Ipfsnewtype Daemon a = Daemon (ReaderT Env IO a)@@ -41,6 +42,8 @@ instance MonadLog Daemon whereinstance MonadMachineStore Daemon whereaskMachines = asks machines+instance Ipfs.MonadIpfs Daemon+instance MonadMachineIpfs Daemon-- * Environmentdiff --git a/src/Radicle/Ipfs.hs b/src/Radicle/Ipfs.hsindex bb283c8..2245c5b 100644--- a/src/Radicle/Ipfs.hs+++ b/src/Radicle/Ipfs.hs@@ -1,6 +1,7 @@-- | Functions to talk to the IPFS APImodule Radicle.Ipfs- ( IpfsException(..)+ ( MonadIpfs+ , IpfsException(..), ipldLink, parseIpldLink, IpnsId@@ -54,6 +55,8 @@ import qualified Network.HTTP.Conduit as HTTPimport qualified Network.Wreq as Wreqimport 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 amapHttpException path io = catch io (throw . mapHttpExceptionData)wheremapHttpExceptionData :: HttpException -> IpfsException@@ -164,7 +167,7 @@ instance FromJSON VersionResponse wherev <- o .: "Version"pure $ VersionResponse v-version :: IO VersionResponse+version :: (MonadIpfs m) => m VersionResponseversion = 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 $ domgr <- liftIO $ HTTP.newManager HTTP.defaultManagerSettingsurl <- 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" messagenewtype KeyGenResponse = KeyGenResponse IpnsId-keyGen :: Text -> IO KeyGenResponse+keyGen :: (MonadIpfs m) => Text -> m KeyGenResponsekeyGen 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 DagPutResponsedagPut obj = ipfsHttpPost "dag/put" [("pin", "true")] "arg" (Aeson.encode obj)instance FromJSON DagPutResponse where@@ -252,18 +255,18 @@ instance FromJSON PinResponse whereRight cids -> pure $ PinResponse cids-- | Pin objects to local storage.-pinAdd :: Address -> IO PinResponse+pinAdd :: (MonadIpfs m) => Address -> m PinResponsepinAdd addr = ipfsHttpGet "pin/add" [("arg", addressToText addr)]-- | Get a dag node.-dagGet :: FromJSON a => Address -> IO a+dagGet :: (MonadIpfs m) => FromJSON a => Address -> m adagGet addr = doresult <- ipfsHttpGet "dag/get" [("arg", addressToText addr)]case Aeson.fromJSON result ofAeson.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 = donewtype NameResolveResponse= NameResolveResponse CID-nameResolve :: IpnsId -> IO NameResolveResponse+nameResolve :: (MonadIpfs m) => IpnsId -> m NameResolveResponsenameResolve 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 $ dolet opts = Wreq.defaults & Wreq.params .~ paramsurl <- ipfsApiUrl pathres <- Wreq.getWith opts (toS url)getJsonResponseBody path resipfsHttpPost- :: 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 aipfsHttpPost path params payloadArgName payload = mapHttpException path $ dores <- ipfsHttpPost' path params payloadArgName payload- getJsonResponseBody path res+ liftIO $ getJsonResponseBody path resipfsHttpPost'- :: 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 $ dolet opts = Wreq.defaults & Wreq.params .~ paramsurl <- ipfsApiUrl pathWreq.postWith opts (toS url) (Wreq.partLBS payloadArgName payload)