diff --git a/src/Hash2Pub/PostService.hs b/src/Hash2Pub/PostService.hs index 92cd772..15901e0 100644 --- a/src/Hash2Pub/PostService.hs +++ b/src/Hash2Pub/PostService.hs @@ -354,6 +354,21 @@ clientDeliverSubscriptions serv fromTag toTag (toHost, toPort) = do haveRead <- tryReadTChan chan maybe (pure acc) (\x -> channelGetAll' chan (x:acc)) haveRead + +-- | Subscribe the client to the given hashtag. On success it returns the given lease time. +clientSubscribeTo :: DHT d => PostService d -> Hashtag -> IO (Either String Integer) +clientSubscribeTo serv tag = do + lookupRes <- lookupKey (baseDHT serv) (Txt.unpack tag) + maybe + (pure . Left $ "No node found") + (\(foundHost, foundPort) -> do + resp <- runClientM (tagSubscribeClient tag (Just . fromString . confServiceHost . serviceConf $ serv)) (mkClientEnv (httpMan serv) (BaseUrl Http foundHost (fromIntegral foundPort) "")) + case resp of + Left err -> pure . Left . show $ err + Right lease -> pure . Right $ lease + ) + lookupRes + -- currently this is unused code getClients :: String -> Int -> HTTP.Manager -> Client IO PostServiceAPI getClients hostname' port' httpMan = hoistClient clientAPI