@@ -39,6 +39,8 @@ module Simplex.Messaging.Agent.Store.AgentStore
3939 updateNewConnRcv ,
4040 updateNewConnSnd ,
4141 createSndConn ,
42+ getSubscriptionServers ,
43+ getUserServerRcvQueueSubs ,
4244 getConn ,
4345 getDeletedConn ,
4446 getConns ,
@@ -110,6 +112,7 @@ module Simplex.Messaging.Agent.Store.AgentStore
110112 updateSndMsgRcpt ,
111113 getPendingQueueMsg ,
112114 getConnectionsForDelivery ,
115+ getAllSndQueuesForDelivery ,
113116 updatePendingMsgRIState ,
114117 deletePendingMsgs ,
115118 getExpiredSndMessages ,
@@ -137,6 +140,7 @@ module Simplex.Messaging.Agent.Store.AgentStore
137140 -- Async commands
138141 createCommand ,
139142 getPendingCommandServers ,
143+ getAllPendingCommandConns ,
140144 getPendingServerCommand ,
141145 updateCommandServer ,
142146 deleteCommand ,
@@ -884,8 +888,8 @@ createSndMsg db connId sndMsgData@SndMsgData {internalSndId, internalHash} = do
884888 insertSndMsgDetails_ db connId sndMsgData
885889 updateSndMsgHash db connId internalSndId internalHash
886890
887- createSndMsgDelivery :: DB. Connection -> ConnId -> SndQueue -> InternalId -> IO ()
888- createSndMsgDelivery db connId SndQueue {dbQueueId} msgId =
891+ createSndMsgDelivery :: DB. Connection -> SndQueue -> InternalId -> IO ()
892+ createSndMsgDelivery db SndQueue {connId, dbQueueId} msgId =
889893 DB. execute db " INSERT INTO snd_message_deliveries (conn_id, snd_queue_id, internal_id) VALUES (?, ?, ?)" (connId, dbQueueId, msgId)
890894
891895getSndMsgViaRcpt :: DB. Connection -> ConnId -> InternalSndId -> IO (Either StoreError SndMsg )
@@ -917,6 +921,15 @@ getConnectionsForDelivery :: DB.Connection -> IO [ConnId]
917921getConnectionsForDelivery db =
918922 map fromOnly <$> DB. query_ db " SELECT DISTINCT conn_id FROM snd_message_deliveries WHERE failed = 0"
919923
924+ getAllSndQueuesForDelivery :: DB. Connection -> IO [SndQueue ]
925+ getAllSndQueuesForDelivery db = map toSndQueue <$> DB. query_ db (sndQueueQuery <> " " <> delivery)
926+ where
927+ delivery = [sql |
928+ JOIN (SELECT DISTINCT conn_id, snd_queue_id FROM snd_message_deliveries WHERE failed = 0) d
929+ ON d.conn_id = q.conn_id AND d.snd_queue_id = q.snd_queue_id
930+ WHERE c.deleted = 0
931+ |]
932+
920933getPendingQueueMsg :: DB. Connection -> ConnId -> SndQueue -> IO (Either StoreError (Maybe (Maybe RcvQueue , PendingMsgData )))
921934getPendingQueueMsg db connId SndQueue {dbQueueId} =
922935 getWorkItem " message" getMsgId getMsgData markMsgFailed
@@ -1319,6 +1332,21 @@ getPendingCommandServers db connIds =
13191332 smpServer (host, port, keyHash) = SMPServer <$> host <*> port <*> keyHash
13201333 conns = S. fromList connIds
13211334
1335+ getAllPendingCommandConns :: DB. Connection -> IO [(ConnId , Maybe SMPServer )]
1336+ getAllPendingCommandConns db =
1337+ map toResult
1338+ <$> DB. query_
1339+ db
1340+ [sql |
1341+ SELECT DISTINCT c.conn_id, c.host, c.port, COALESCE(c.server_key_hash, s.key_hash)
1342+ FROM commands c
1343+ JOIN connections cs ON c.conn_id = cs.conn_id
1344+ LEFT JOIN servers s ON s.host = c.host AND s.port = c.port
1345+ WHERE cs.deleted = 0
1346+ |]
1347+ where
1348+ toResult (connId, host, port, keyHash) = (connId, SMPServer <$> host <*> port <*> keyHash)
1349+
13221350getPendingServerCommand :: DB. Connection -> ConnId -> Maybe SMPServer -> IO (Either StoreError (Maybe PendingCommand ))
13231351getPendingServerCommand db connId srv_ = getWorkItem " command" getCmdId getCommand markCommandFailed
13241352 where
@@ -2023,6 +2051,30 @@ newQueueId_ :: [Only Int64] -> DBEntityId
20232051newQueueId_ [] = DBEntityId 1
20242052newQueueId_ (Only maxId : _) = DBEntityId (maxId + 1 )
20252053
2054+ -- * subscribe all connections
2055+
2056+ getSubscriptionServers :: DB. Connection -> IO [(UserId , SMPServer )]
2057+ getSubscriptionServers db =
2058+ map toUserServer
2059+ <$> DB. query_
2060+ db
2061+ [sql |
2062+ SELECT DISTINCT c.user_id, q.host, q.port, COALESCE(q.server_key_hash, s.key_hash)
2063+ FROM rcv_queues q
2064+ JOIN servers s ON q.host = s.host AND q.port = s.port
2065+ JOIN connections c ON q.conn_id = c.conn_id
2066+ WHERE c.deleted = 0 AND q.deleted = 0
2067+ |]
2068+ where
2069+ toUserServer :: (UserId , NonEmpty TransportHost , ServiceName , C. KeyHash ) -> (UserId , SMPServer )
2070+ toUserServer (userId, host, port, keyHash) = (userId, SMPServer host port keyHash)
2071+
2072+ getUserServerRcvQueueSubs :: DB. Connection -> UserId -> SMPServer -> IO [RcvQueueSub ]
2073+ getUserServerRcvQueueSubs db userId srv =
2074+ map toRcvQueueSub <$> DB. query db (rcvQueueSubQuery <> condition) (userId, host srv, port srv)
2075+ where
2076+ condition = " WHERE c.deleted = 0 AND q.deleted = 0 AND c.user_id = ? AND q.host = ? AND q.port = ?"
2077+
20262078-- * getConn helpers
20272079
20282080getConn :: DB. Connection -> ConnId -> IO (Either StoreError SomeConn )
@@ -2229,7 +2281,7 @@ rcvQueueQuery :: Query
22292281rcvQueueQuery =
22302282 [sql |
22312283 SELECT c.user_id, COALESCE(q.server_key_hash, s.key_hash), q.conn_id, q.host, q.port, q.rcv_id, q.rcv_private_key, q.rcv_dh_secret,
2232- q.e2e_priv_key, q.e2e_dh_secret, q.snd_id, q.queue_mode, q.status,
2284+ q.e2e_priv_key, q.e2e_dh_secret, q.snd_id, q.queue_mode, q.status, c.enable_ntfs,
22332285 q.rcv_queue_id, q.rcv_primary, q.replace_rcv_queue_id, q.switch_status, q.smp_client_version, q.delete_errors,
22342286 q.ntf_public_key, q.ntf_private_key, q.ntf_id, q.rcv_ntf_dh_secret,
22352287 q.link_id, q.link_key, q.link_priv_sig_key, q.link_enc_fixed_data
@@ -2240,13 +2292,13 @@ rcvQueueQuery =
22402292
22412293toRcvQueue ::
22422294 (UserId , C. KeyHash , ConnId , NonEmpty TransportHost , ServiceName , SMP. RecipientId , SMP. RcvPrivateAuthKey , SMP. RcvDhSecret , C. PrivateKeyX25519 , Maybe C. DhSecretX25519 , SMP. SenderId , Maybe QueueMode )
2243- :. (QueueStatus , DBEntityId , BoolInt , Maybe Int64 , Maybe RcvSwitchStatus , Maybe VersionSMPC , Int )
2295+ :. (QueueStatus , Maybe BoolInt , DBEntityId , BoolInt , Maybe Int64 , Maybe RcvSwitchStatus , Maybe VersionSMPC , Int )
22442296 :. (Maybe SMP. NtfPublicAuthKey , Maybe SMP. NtfPrivateAuthKey , Maybe SMP. NotifierId , Maybe RcvNtfDhSecret )
22452297 :. (Maybe SMP. LinkId , Maybe LinkKey , Maybe C. PrivateKeyEd25519 , Maybe EncDataBytes ) ->
22462298 RcvQueue
22472299toRcvQueue
22482300 ( (userId, keyHash, connId, host, port, rcvId, rcvPrivateKey, rcvDhSecret, e2ePrivKey, e2eDhSecret, sndId, queueMode)
2249- :. (status, dbQueueId, BI primary, dbReplaceQueueId, rcvSwchStatus, smpClientVersion_, deleteErrors)
2301+ :. (status, enableNtfs_, dbQueueId, BI primary, dbReplaceQueueId, rcvSwchStatus, smpClientVersion_, deleteErrors)
22502302 :. (ntfPublicKey_, ntfPrivateKey_, notifierId_, rcvNtfDhSecret_)
22512303 :. (shortLinkId_, shortLinkKey_, linkPrivSigKey_, linkEncFixedData_)
22522304 ) =
@@ -2258,8 +2310,9 @@ toRcvQueue
22582310 shortLink = case (shortLinkId_, shortLinkKey_, linkPrivSigKey_, linkEncFixedData_) of
22592311 (Just shortLinkId, Just shortLinkKey, Just linkPrivSigKey, Just linkEncFixedData) -> Just ShortLinkCreds {shortLinkId, shortLinkKey, linkPrivSigKey, linkEncFixedData}
22602312 _ -> Nothing
2313+ enableNtfs = maybe True unBI enableNtfs_
22612314 -- TODO [certs rcv] read client service
2262- in RcvQueue {userId, connId, server, rcvId, rcvPrivateKey, rcvDhSecret, e2ePrivKey, e2eDhSecret, sndId, queueMode, shortLink, clientService = Nothing , status, dbQueueId, primary, dbReplaceQueueId, rcvSwchStatus, smpClientVersion, clientNtfCreds, deleteErrors}
2315+ in RcvQueue {userId, connId, server, rcvId, rcvPrivateKey, rcvDhSecret, e2ePrivKey, e2eDhSecret, sndId, queueMode, shortLink, clientService = Nothing , status, enableNtfs, dbQueueId, primary, dbReplaceQueueId, rcvSwchStatus, smpClientVersion, clientNtfCreds, deleteErrors}
22632316
22642317-- | returns all connection queue credentials, the first queue is the primary one
22652318getRcvQueueSubsByConnId_ :: DB. Connection -> ConnId -> IO (Maybe (NonEmpty RcvQueueSub ))
@@ -2270,16 +2323,17 @@ getRcvQueueSubsByConnId_ db connId =
22702323rcvQueueSubQuery :: Query
22712324rcvQueueSubQuery =
22722325 [sql |
2273- SELECT c.user_id, q.conn_id, q.host, q.port, COALESCE(q.server_key_hash, s.key_hash), q.rcv_id, q.rcv_private_key, q.status,
2326+ SELECT c.user_id, q.conn_id, q.host, q.port, COALESCE(q.server_key_hash, s.key_hash), q.rcv_id, q.rcv_private_key, q.status, c.enable_ntfs,
22742327 q.rcv_queue_id, q.rcv_primary, q.replace_rcv_queue_id
22752328 FROM rcv_queues q
22762329 JOIN servers s ON q.host = s.host AND q.port = s.port
22772330 JOIN connections c ON q.conn_id = c.conn_id
22782331 |]
22792332
2280- toRcvQueueSub :: (UserId , ConnId , NonEmpty TransportHost , ServiceName , C. KeyHash , SMP. RecipientId , SMP. RcvPrivateAuthKey , QueueStatus , Int64 , BoolInt , Maybe Int64 ) -> RcvQueueSub
2281- toRcvQueueSub (userId, connId, host, port, keyHash, rcvId, rcvPrivateKey, status, dbQueueId, BI primary, dbReplaceQueueId) =
2282- RcvQueueSub {userId, connId, server = SMPServer host port keyHash, rcvId, dbQueueId, primary, dbReplaceQueueId, rcvPrivateKey, status}
2333+ toRcvQueueSub :: (UserId , ConnId , NonEmpty TransportHost , ServiceName , C. KeyHash , SMP. RecipientId , SMP. RcvPrivateAuthKey , QueueStatus , Maybe BoolInt , Int64 , BoolInt , Maybe Int64 ) -> RcvQueueSub
2334+ toRcvQueueSub (userId, connId, host, port, keyHash, rcvId, rcvPrivateKey, status, enableNtfs_, dbQueueId, BI primary, dbReplaceQueueId) =
2335+ let enableNtfs = maybe True unBI enableNtfs_
2336+ in RcvQueueSub {userId, connId, server = SMPServer host port keyHash, rcvId, rcvPrivateKey, status, enableNtfs, dbQueueId, primary, dbReplaceQueueId}
22832337
22842338getRcvQueueById :: DB. Connection -> ConnId -> Int64 -> IO (Either StoreError RcvQueue )
22852339getRcvQueueById db connId dbRcvId =
0 commit comments