Merge branch 'master' into group-knocking

This commit is contained in:
spaced4ndy
2025-05-02 17:05:40 +04:00
20 changed files with 2950 additions and 2598 deletions
+9 -3
View File
@@ -357,7 +357,7 @@ data ChatCommand
| APICheckToken DeviceToken
| APIDeleteToken DeviceToken
| APIGetNtfConns {nonce :: C.CbNonce, encNtfInfo :: ByteString}
| ApiGetConnNtfMessages {connIds :: NonEmpty AgentConnId}
| APIGetConnNtfMessages (NonEmpty ConnMsgReq)
| APIAddMember GroupId ContactId GroupMemberRole
| APIJoinGroup {groupId :: GroupId, enableNtfs :: MsgFilter}
| APIAcceptMember GroupId GroupMemberId GroupMemberRole
@@ -1139,12 +1139,18 @@ instance FromJSON ChatTagData where
parseJSON invalid = JT.prependFailure "bad ChatTagData, " (JT.typeMismatch "Object" invalid)
data NtfConn = NtfConn
{ user_ :: Maybe User,
connEntity_ :: Maybe ConnectionEntity,
{ user :: User,
agentConnId :: AgentConnId,
agentDbQueueId :: Int64,
connEntity :: ConnectionEntity,
-- Decrypted ntf meta of the expected message (the one notification was sent for).
-- Nothing means it failed to decrypt or to decode, we can still show event for entity
expectedMsg_ :: Maybe NtfMsgInfo
}
deriving (Show)
-- brokerTs is the same msgTs, it is used in ConnMsgReq / APIGetConnNtfMessages
-- to set it as last connection message in case queue is empty
data NtfMsgInfo = NtfMsgInfo {msgId :: Text, msgTs :: UTCTime}
deriving (Show)
+3 -2
View File
@@ -34,7 +34,7 @@ import Text.Read (readMaybe)
import UnliftIO.Async
simplexChatCore :: ChatConfig -> ChatOpts -> (User -> ChatController -> IO ()) -> IO ()
simplexChatCore cfg@ChatConfig {confirmMigrations, testView} opts@ChatOpts {coreOptions = CoreChatOpts {dbOptions, logAgent, yesToUpMigrations}} chat =
simplexChatCore cfg@ChatConfig {confirmMigrations, testView} opts@ChatOpts {coreOptions = CoreChatOpts {dbOptions, logAgent, yesToUpMigrations}, maintenance} chat =
case logAgent of
Just level -> do
setLogLevel level
@@ -48,7 +48,8 @@ simplexChatCore cfg@ChatConfig {confirmMigrations, testView} opts@ChatOpts {core
exitFailure
run db@ChatDatabase {chatStore} = do
u_ <- getSelectActiveUser chatStore
cc <- newChatController db u_ cfg opts False
let backgroundMode = not maintenance
cc <- newChatController db u_ cfg opts backgroundMode
u <- maybe (createActiveUser cc) pure u_
unless testView $ putStrLn $ "Current user: " <> userStr u
runSimplexChat opts u cc chat
+16 -19
View File
@@ -1311,26 +1311,17 @@ processChatCommand' vr = \case
ntfInfos <- withAgent $ \a -> getNotificationConns a nonce encNtfInfo
(errs, ntfMsgs) <- lift $ partitionEithers <$> withStoreBatch' (\db -> map (getMsgConn db) (L.toList ntfInfos))
unless (null errs) $ toView $ CRChatErrors (Just user) errs
pure $ CRNtfConns ntfMsgs
pure $ CRNtfConns $ catMaybes ntfMsgs
where
getMsgConn :: DB.Connection -> NotificationInfo -> IO NtfConn
getMsgConn db NotificationInfo {ntfConnId, ntfMsgMeta = nMsgMeta} = do
getMsgConn :: DB.Connection -> NotificationInfo -> IO (Maybe NtfConn)
getMsgConn db NotificationInfo {ntfConnId, ntfDbQueueId, ntfMsgMeta = nMsgMeta} = do
let agentConnId = AgentConnId ntfConnId
user_ <- getUserByAConnId db agentConnId
connEntity_ <-
pure user_ $>>= \user ->
eitherToMaybe <$> runExceptT (getConnectionEntity db vr user agentConnId)
pure $
NtfConn
{ user_,
connEntity_,
-- Decrypted ntf meta of the expected message (the one notification was sent for)
expectedMsg_ = expectedMsgInfo <$> nMsgMeta
}
ApiGetConnNtfMessages connIds -> withUser $ \_ -> do
let acIds = L.map (\(AgentConnId acId) -> acId) connIds
msgs <- lift $ withAgent' $ \a -> getConnectionMessages a acIds
let ntfMsgs = L.map (\msg -> receivedMsgInfo <$> msg) msgs
mkNtfConn user connEntity = NtfConn {user, agentConnId, agentDbQueueId = ntfDbQueueId, connEntity, expectedMsg_ = expectedMsgInfo <$> nMsgMeta}
getUserByAConnId db agentConnId
$>>= \user -> fmap (mkNtfConn user) . eitherToMaybe <$> runExceptT (getConnectionEntity db vr user agentConnId)
APIGetConnNtfMessages connMsgs -> withUser $ \_ -> do
msgs <- lift $ withAgent' (`getConnectionMessages` connMsgs)
let ntfMsgs = L.map (receivedMsgInfo <$>) msgs
pure $ CRConnNtfMessages ntfMsgs
GetUserProtoServers (AProtocolType p) -> withUser $ \user -> withServerProtocol p $ do
srvs <- withFastStore (`getUserServers` user)
@@ -4080,7 +4071,7 @@ chatCommandP =
"/_ntf check " *> (APICheckToken <$> strP),
"/_ntf delete " *> (APIDeleteToken <$> strP),
"/_ntf conns " *> (APIGetNtfConns <$> strP <* A.space <*> strP),
"/_ntf conn messages " *> (ApiGetConnNtfMessages <$> strP),
"/_ntf conn messages " *> (APIGetConnNtfMessages <$> connMsgsP),
"/_add #" *> (APIAddMember <$> A.decimal <* A.space <*> A.decimal <*> memberRole),
"/_join #" *> (APIJoinGroup <$> A.decimal <*> pure MFAll), -- needs to be changed to support in UI
"/_accept member #" *> (APIAcceptMember <$> A.decimal <* A.space <*> A.decimal <*> memberRole),
@@ -4400,6 +4391,12 @@ chatCommandP =
cfArgs <- optional $ CFArgs <$> (" key=" *> strP <* A.space) <*> (" nonce=" *> strP)
path <- filePath
pure $ CryptoFile path cfArgs
connMsgsP = L.fromList <$> connMsgP `A.sepBy1'` A.char ','
connMsgP = do
AgentConnId msgConnId <- strP <* A.char ':'
msgDbQueueId <- strP <* A.char ':'
ts <- strP
pure ConnMsgReq {msgConnId, msgDbQueueId, msgTs = Just ts}
memberRole =
A.choice
[ " owner" $> GROwner,
@@ -1119,9 +1119,9 @@ Query: UPDATE rcv_messages SET user_ack = ? WHERE conn_id = ? AND internal_id =
Plan:
SEARCH rcv_messages USING COVERING INDEX idx_rcv_messages_conn_id_internal_id (conn_id=? AND internal_id=?)
Query: UPDATE rcv_queues SET last_broker_ts = ? WHERE conn_id = ? AND rcv_queue_id = ?
Query: UPDATE rcv_queues SET last_broker_ts = ? WHERE conn_id = ? AND rcv_queue_id = ? AND last_broker_ts < ?
Plan:
SEARCH rcv_queues USING COVERING INDEX idx_rcv_queue_id (conn_id=? AND rcv_queue_id=?)
SEARCH rcv_queues USING INDEX idx_rcv_queue_id (conn_id=? AND rcv_queue_id=?)
Query: UPDATE rcv_queues SET rcv_primary = ? WHERE conn_id = ?
Plan:
+2 -2
View File
@@ -346,8 +346,8 @@ responseToView hu@(currentRH, user_) cfg@ChatConfig {logLevel, showReactions, sh
CRContactConnectionDeleted u PendingContactConnection {pccConnId} -> ttyUser u ["connection :" <> sShow pccConnId <> " deleted"]
CRNtfTokenStatus status -> ["device token status: " <> plain (smpEncode status)]
CRNtfToken _ status mode srv -> ["device token status: " <> plain (smpEncode status) <> ", notifications mode: " <> plain (strEncode mode) <> ", server: " <> sShow srv]
CRNtfConns {} -> []
CRConnNtfMessages {} -> []
CRNtfConns {ntfConns} -> map (\NtfConn {agentConnId, expectedMsg_} -> plain $ show agentConnId <> " " <> show expectedMsg_) ntfConns
CRConnNtfMessages ntfMsgs -> [sShow ntfMsgs]
CRNtfMessage {} -> []
CRCurrentRemoteHost rhi_ ->
[ maybe