Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
prolic committed Oct 30, 2024
1 parent 7d18037 commit 7f51c2e
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 154 deletions.
6 changes: 1 addition & 5 deletions cabal.project
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
packages:
./
../HsQML
tests: True

source-repository-package
type: git
location: https://github.com/prolic/secp256k1-schnorr
tag: 10555f6d6cdba34e60799d915ac3e60a01c76d46

source-repository-package
type: git
location: https://github.com/prolic/HsQML/
tag: e7fe83f9cc535c66c423d5402d3351c774a5bc2c
6 changes: 3 additions & 3 deletions src/Futr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

module Futr where

import Control.Monad (forM, forM_, void, unless, when)
import Control.Monad (forM, forM_, void, unless)
import Data.Aeson (ToJSON, pairs, toEncoding, (.=))
import Data.Maybe (catMaybes, listToMaybe)
import Data.Map.Strict qualified as Map
import Data.Proxy (Proxy(..))
import Data.Text (Text, isPrefixOf, pack)
import Data.Text (Text, isPrefixOf)
import Data.Typeable (Typeable)
import Effectful
import Effectful.Concurrent
Expand Down Expand Up @@ -258,7 +258,7 @@ parseNprofileOrNpub input =
loginWithAccount :: FutrEff es => ObjRef () -> PKeyMgmt.Account -> Eff es Bool
loginWithAccount obj a = do
modify @AppState $ \s -> s { keyPair = Just (secKeyToKeyPair $ PKeyMgmt.nsec a) }
let (rs, t) = PKeyMgmt.relays a
let (rs, _) = PKeyMgmt.relays a
-- add general relays
--modify @AppState $ \st -> st { generalRelays = (rs, t) }
-- add all relays to the relay pool
Expand Down
12 changes: 5 additions & 7 deletions src/Nostr/OutboxModel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

module Nostr.OutboxModel where

import Control.Monad (forM, forM_, void)
import Data.List (find)
import Control.Monad (forM, void)
import Data.Map.Strict qualified as Map
import Data.Maybe (catMaybes)
import Data.Text (Text)
Expand Down Expand Up @@ -102,19 +101,19 @@ runOutboxModel = interpret $ \_ -> \case
modify $ \st -> st { publishStatus = Publishing event : publishStatus st }
void $ async $ do
relays <- gets $ \st -> Map.findWithDefault ([], 0) pk (outboxes st)
publishToRelays event pk High relays
publishToRelays event High relays

SendToInboxes event pk -> do
modify $ \st -> st { publishStatus = Publishing event : publishStatus st }
void $ async $ do
relays <- gets $ \st -> Map.findWithDefault ([], 0) pk (inboxes st)
publishToRelays event pk Critical relays
publishToRelays event Critical relays

SendToDMRelays event pk -> do
modify $ \st -> st { publishStatus = Publishing event : publishStatus st }
void $ async $ do
relays <- gets $ \st -> Map.findWithDefault ([], 0) pk (dmRelays st)
publishToRelays event pk Critical relays
publishToRelays event Critical relays


-- Helper functions
Expand Down Expand Up @@ -158,11 +157,10 @@ collectResponseWithTimeout event queue endTime = do
-- Helper function for common publish logic
publishToRelays :: OutboxModelEff es
=> Event
-> PubKeyXO
-> RelayPriority
-> ([RelayURI], Int) -- relay list and timestamp
-> Eff es ()
publishToRelays event pk priority (relays, _) = do
publishToRelays event priority (relays, _) = do
subs <- forM relays \relay -> do
requestConnection relay priority
RP.sendEvent event [relay]
Expand Down
38 changes: 18 additions & 20 deletions src/Nostr/PublishManager.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

module Nostr.PublishManager where

import Control.Monad (forever, forM_, void, when)
import Control.Monad (forM_)
import Data.List (find)
import Data.Map.Strict qualified as Map
import Data.Text (Text, pack)
import Data.Text (Text)
import Data.Either (isRight)
import Data.Aeson
import Effectful
import Effectful.Concurrent (Concurrent, threadDelay)
import Effectful.Concurrent.Async (async)
import Effectful.Dispatch.Dynamic
import Effectful.State.Static.Shared
import Effectful.TH
import GHC.Generics (Generic)

import Logging
import Nostr.Keys (PubKeyXO, byteStringToHex)
import Nostr.Keys (byteStringToHex)
import Nostr.Types (Event(..), EventId(..), RelayURI)
import Nostr.Util
import Types (PublishStatus(..))
Expand Down Expand Up @@ -60,7 +58,7 @@ type instance DispatchOf PublishManager = Dynamic

makeEffect ''PublishManager

type PublishManagerEff es = (State PublishManagerState :> es, Logging :> es, Util :> es, Concurrent :> es)
type PublishManagerEff es = (State PublishManagerState :> es, Logging :> es, Util :> es)

-- | State for publish manager
data PublishManagerState = PublishManagerState
Expand Down Expand Up @@ -149,34 +147,34 @@ runPublishManager = interpret $ \_ -> \case
GetMetrics relay ->
gets $ \st -> Map.findWithDefault initialMetrics relay (metrics st)

TrackSubscription subId relay -> do
TrackSubscription subId' relay -> do
now <- getCurrentTime
modify $ \st -> st
{ activeSubscriptions = Map.insert subId
(SubscriptionInfo subId now relay)
{ activeSubscriptions = Map.insert subId'
(SubscriptionInfo subId' now relay)
(activeSubscriptions st)
}

UpdateSubscriptionActivity subId -> do
UpdateSubscriptionActivity subId' -> do
now <- getCurrentTime
modify $ \st -> st
{ activeSubscriptions = Map.adjust
(\info -> info { lastActivity = now })
subId
{ activeSubscriptions = Map.adjust
(\info -> info { lastActivity = now })
subId'
(activeSubscriptions st)
}

CleanupStaleSubscriptions timeout -> do
now <- getCurrentTime
st <- get
let cutoff = now - timeout
let (stale, active) = Map.partition
(\info -> lastActivity info < cutoff)
let (stale, active) = Map.partition
(\info -> lastActivity info < cutoff)
(activeSubscriptions st)

-- Log stale subscriptions being cleaned up
forM_ (Map.toList stale) $ \(subId, info) ->
logInfo $ "Cleaning up stale subscription " <> subId
forM_ (Map.toList stale) $ \(subId', info) ->
logInfo $ "Cleaning up stale subscription " <> subId'
<> " for relay " <> relayUri info

modify $ \st' -> st' { activeSubscriptions = active }
Expand Down Expand Up @@ -235,8 +233,8 @@ notMatchingEventId eid status = case status of

-- Add helper
isStaleStatus :: Int -> PublishStatus -> Bool
isStaleStatus cutoff = \case
isStaleStatus cutoff status = case status of
Publishing _ -> True -- Always clean up stuck "publishing" status
PublishSuccess _ -> False -- Keep successful publishes
PublishFailed _ _ -> True -- Clean up failed publishes
PublishPartial _ _ -> True -- Clean up partial publishes
PublishFailed e _ -> createdAt e < cutoff -- Clean up old failed publishes
PublishPartial e _ -> createdAt e < cutoff -- Clean up old partial publishes
Loading

0 comments on commit 7f51c2e

Please sign in to comment.