@@ -48,7 +48,15 @@ federationEnabled = federationSecret != ""
49-- ============================================
49-- ============================================
51+type PollEventType = PollMessage {}
53+impl ToJSON PollEventType where
54+ toJson _ = toJson "message"
56+impl FromJSON PollEventType where
57+ parseJson _ = PollMessage {}
51−type PollEvent = {id: MessageIdHex, eventType: Text, payload: {encryptedBlob: BlobHex, fromKey: PubkeyHex, timestamp: Timestamp, toKey: PubkeyHex}}
59+type PollEvent = {id: MessageIdHex, eventType: PollEventType, payload: {encryptedBlob: BlobHex, fromKey: PubkeyHex, timestamp: Timestamp, toKey: PubkeyHex}}
53type GossipEventType = GossipOnline {} | GossipOffline {}
61type GossipEventType = GossipOnline {} | GossipOffline {}
@@ -63,7 +71,6 @@ impl FromJSON GossipEventType where
63 if s == "online" then GossipOnline {} else GossipOffline {}
71 if s == "online" then GossipOnline {} else GossipOffline {}
65isGossipOnline = \et -> case et of GossipOnline {} -> True {}; _ -> False {}
73isGossipOnline = \et -> case et of GossipOnline {} -> True {}; _ -> False {}
66−isGossipOffline = \et -> case et of GossipOffline {} -> True {}; _ -> False {}
68type GossipEvent = {eventType: GossipEventType, pubkey: PubkeyHex, server: ServerName, timestamp: Timestamp, ttl: Int<S>}
75type GossipEvent = {eventType: GossipEventType, pubkey: PubkeyHex, server: ServerName, timestamp: Timestamp, ttl: Int<S>}
@@ -715,7 +722,7 @@ pollOnce = \token cursor -> do
715 let limitedMsgs = boundedSmallest matchingMsgs maxPollEvents (\m -> m.receivedAt)
722 let limitedMsgs = boundedSmallest matchingMsgs maxPollEvents (\m -> m.receivedAt)
716 let evts = do
723 let evts = do
717 m <- limitedMsgs
724 m <- limitedMsgs
718− yield {id: m.messageId, eventType: "message",
725+ yield {id: m.messageId, eventType: PollMessage {},
719 payload: {toKey: m.toKey, fromKey: m.fromKey,
726 payload: {toKey: m.toKey, fromKey: m.fromKey,
720 encryptedBlob: m.encryptedBlob,
727 encryptedBlob: m.encryptedBlob,
721 timestamp: m.timestamp}}
728 timestamp: m.timestamp}}
@@ -906,11 +913,12 @@ serve = \req -> case req of
906 p <- presence
913 p <- presence
907 where p.pubkey == pubkey && p.expiresAt > t
914 where p.pubkey == pubkey && p.expiresAt > t
909− let foundServer = (fold (\acc p ->
916+ let localPresent = any valid (\p -> p.server == serverHostN)
910− if acc.server == serverHostN then acc
917+ let bestRemote = (fold (\acc p ->
911− else if p.server == serverHostN then {server: serverHostN, best: p.expiresAt}
918+ if p.server != serverHostN && p.expiresAt > acc.best
912− else if p.expiresAt > acc.best then {server: p.server, best: p.expiresAt}
919+ then {server: p.server, best: p.expiresAt}
913− else acc) {server: "", best: 0} valid).server
920+ else acc) {server: "", best: 0} valid).server
921+ let foundServer = if localPresent then serverHostN else bestRemote
914 yield {pr: pr, authorized: True {}, foundServer: foundServer}
922 yield {pr: pr, authorized: True {}, foundServer: foundServer}
915 forkBroadcastOffline t result.pr
923 forkBroadcastOffline t result.pr
916 yield (respond {online: result.foundServer != "", server: result.foundServer, authorized: result.authorized})
924 yield (respond {online: result.foundServer != "", server: result.foundServer, authorized: result.authorized})
@@ -926,7 +934,7 @@ serve = \req -> case req of
927 let allValidEntries = validGossipEvents events t
935 let allValidEntries = validGossipEvents events t
928 let onlineEntries = do e <- allValidEntries; where isGossipOnline e.eventType; yield e
936 let onlineEntries = do e <- allValidEntries; where isGossipOnline e.eventType; yield e
929− let offlineEntries = do e <- allValidEntries; where isGossipOffline e.eventType; yield e
937+ let offlineEntries = do e <- allValidEntries; where not (isGossipOnline e.eventType); yield e
930 let filteredOnline = do
938 let filteredOnline = do
931 e <- onlineEntries
939 e <- onlineEntries
932 where none offlineEntries (\o -> o.server == fromServer && samePubkeyServer o e && o.timestamp > e.timestamp)
940 where none offlineEntries (\o -> o.server == fromServer && samePubkeyServer o e && o.timestamp > e.timestamp)
@@ -936,8 +944,8 @@ serve = \req -> case req of
936 where none onlineEntries (\o -> samePubkeyServer o e && o.timestamp > e.timestamp)
944 where none onlineEntries (\o -> samePubkeyServer o e && o.timestamp > e.timestamp)
938 let trustedOffline = do
946 let trustedOffline = do
939− e <- offlineEntries
947+ e <- allValidEntries
940− where e.server == fromServer
948+ where not (isGossipOnline e.eventType) && e.server == fromServer
942 let deduped = mergeByPubkeyServer [] filteredOnline
950 let deduped = mergeByPubkeyServer [] filteredOnline
943 gossipResult <- atomic do
951 gossipResult <- atomic do