From edbfd0778e9658ee164929eda2e08418701d0fe8 Mon Sep 17 00:00:00 2001 From: Thilo Molitor Date: Sun, 27 Aug 2023 14:37:35 +0200 Subject: [PATCH] Fix mam query bug --- Monal/Classes/DataLayer.h | 1 + Monal/Classes/DataLayer.m | 7 +++++++ Monal/Classes/HelperTools.m | 2 ++ Monal/Classes/MLIQProcessor.m | 9 +++++---- Monal/Classes/MLMessage.h | 2 -- Monal/Classes/MLMucProcessor.m | 9 +++++---- Monal/Classes/XMPPIQ.m | 6 ++++-- Monal/Classes/chatViewController.m | 4 ++-- 8 files changed, 26 insertions(+), 14 deletions(-) diff --git a/Monal/Classes/DataLayer.h b/Monal/Classes/DataLayer.h index 929aa7a558..baf68ad8d8 100644 --- a/Monal/Classes/DataLayer.h +++ b/Monal/Classes/DataLayer.h @@ -171,6 +171,7 @@ extern NSString* const kMessageTypeFiletransfer; -(NSArray*) messagesForHistoryIDs:(NSArray*) historyIDs; -(MLMessage* _Nullable) messageForHistoryID:(NSNumber* _Nullable) historyID; -(NSNumber*) getSmallestHistoryId; +-(NSNumber*) getBiggestHistoryId; /* adds a specified message to the database diff --git a/Monal/Classes/DataLayer.m b/Monal/Classes/DataLayer.m index 7a9c31cdf9..c7910f15dc 100644 --- a/Monal/Classes/DataLayer.m +++ b/Monal/Classes/DataLayer.m @@ -1191,6 +1191,13 @@ -(NSNumber*) getSmallestHistoryId }]; } +-(NSNumber*) getBiggestHistoryId +{ + return [self.db idReadTransaction:^{ + return [self.db executeScalar:@"SELECT MAX(message_history_id) FROM message_history;"]; + }]; +} + -(NSNumber*) addMessageToChatBuddy:(NSString*) buddyName withInboundDir:(BOOL) inbound forAccount:(NSNumber*) accountNo withBody:(NSString*) message actuallyfrom:(NSString*) actualfrom participantJid:(NSString*) participantJid sent:(BOOL) sent unread:(BOOL) unread messageId:(NSString*) messageid serverMessageId:(NSString*) stanzaid messageType:(NSString*) messageType andOverrideDate:(NSDate*) messageDate encrypted:(BOOL) encrypted displayMarkerWanted:(BOOL) displayMarkerWanted usingHistoryId:(NSNumber* _Nullable) historyId checkForDuplicates:(BOOL) checkForDuplicates { if(!buddyName || !message) diff --git a/Monal/Classes/HelperTools.m b/Monal/Classes/HelperTools.m index 0ae7e2f69b..6124dfa154 100644 --- a/Monal/Classes/HelperTools.m +++ b/Monal/Classes/HelperTools.m @@ -293,6 +293,8 @@ +(void) initSystem [self installCrashHandler]; [self installExceptionHandler]; [self activityLog]; + + DDLogError(@"Timestamp for nil: %@", [self generateDateTimeString:nil]); } +(NSDictionary*) getInvalidPushServers diff --git a/Monal/Classes/MLIQProcessor.m b/Monal/Classes/MLIQProcessor.m index 471620fcde..80b3964988 100644 --- a/Monal/Classes/MLIQProcessor.m +++ b/Monal/Classes/MLIQProcessor.m @@ -185,9 +185,10 @@ +(void) processErrorIq:(XMPPIQ*) iqNode forAccount:(xmpp*) account //handle weird XEP-0313 monkey-patching XEP-0059 behaviour (WHY THE HELL??) if(!secondTry && [iqNode check:@"error/{urn:ietf:params:xml:ns:xmpp-stanzas}item-not-found"]) { + MLMessage* latestMessage = [[DataLayer sharedInstance] messageForHistoryID:[[DataLayer sharedInstance] getBiggestHistoryId]]; + DDLogInfo(@"Querying COMPLETE muc mam:2 archive at %@ after timestmap %@ for catchup", account.connectionProperties.identity.jid, [latestMessage timestamp]); XMPPIQ* mamQuery = [[XMPPIQ alloc] initWithType:kiqSetType]; - DDLogInfo(@"Querying COMPLETE muc mam:2 archive for catchup"); - [mamQuery setCompleteMAMQuery]; + [mamQuery setMAMQueryAfterTimestamp:[latestMessage timestamp]]; [account sendIq:mamQuery withHandler:$newHandler(self, handleCatchup, $BOOL(secondTry, YES))]; } else @@ -199,7 +200,7 @@ +(void) processErrorIq:(XMPPIQ*) iqNode forAccount:(xmpp*) account } if(![[iqNode findFirst:@"{urn:xmpp:mam:2}fin@complete|bool"] boolValue] && [iqNode check:@"{urn:xmpp:mam:2}fin/{http://jabber.org/protocol/rsm}set/last#"]) { - DDLogVerbose(@"Paging through mam catchup results with after: %@", [iqNode findFirst:@"{urn:xmpp:mam:2}fin/{http://jabber.org/protocol/rsm}set/last#"]); + DDLogVerbose(@"Paging through mam catchup results at %@ with after: %@", account.connectionProperties.identity.jid, [iqNode findFirst:@"{urn:xmpp:mam:2}fin/{http://jabber.org/protocol/rsm}set/last#"]); //do RSM forward paging XMPPIQ* pageQuery = [[XMPPIQ alloc] initWithType:kiqSetType]; [pageQuery setMAMQueryAfter:[iqNode findFirst:@"{urn:xmpp:mam:2}fin/{http://jabber.org/protocol/rsm}set/last#"]]; @@ -207,7 +208,7 @@ +(void) processErrorIq:(XMPPIQ*) iqNode forAccount:(xmpp*) account } else if([[iqNode findFirst:@"{urn:xmpp:mam:2}fin@complete|bool"] boolValue]) { - DDLogVerbose(@"Mam catchup finished"); + DDLogVerbose(@"Mam catchup finished for %@", account.connectionProperties.identity.jid); [account mamFinishedFor:account.connectionProperties.identity.jid]; } $$ diff --git a/Monal/Classes/MLMessage.h b/Monal/Classes/MLMessage.h index 446e1e3a72..46d30eb6d2 100644 --- a/Monal/Classes/MLMessage.h +++ b/Monal/Classes/MLMessage.h @@ -81,8 +81,6 @@ The of the message in the DB , should be int for message type status. The MUC subeject */ @property (nonatomic, copy) NSString* groupSubject; - -@property (nonatomic, copy) NSDate* delayTimeStamp; @property (nonatomic, copy) NSDate* timestamp; /* diff --git a/Monal/Classes/MLMucProcessor.m b/Monal/Classes/MLMucProcessor.m index 34b54abccd..b624876bec 100644 --- a/Monal/Classes/MLMucProcessor.m +++ b/Monal/Classes/MLMucProcessor.m @@ -938,9 +938,10 @@ -(void) sendJoinPresenceFor:(NSString*) room //handle weird XEP-0313 monkey-patching XEP-0059 behaviour (WHY THE HELL??) if(!secondTry && [iqNode check:@"error/{urn:ietf:params:xml:ns:xmpp-stanzas}item-not-found"]) { + MLMessage* latestMessage = [[DataLayer sharedInstance] lastMessageForContact:iqNode.fromUser]; + DDLogInfo(@"Querying COMPLETE muc mam:2 archive at %@ after timestmap %@ for catchup", iqNode.fromUser, [latestMessage timestamp]); XMPPIQ* mamQuery = [[XMPPIQ alloc] initWithType:kiqSetType to:iqNode.fromUser]; - DDLogInfo(@"Querying COMPLETE muc mam:2 archive for catchup"); - [mamQuery setCompleteMAMQuery]; + [mamQuery setMAMQueryAfterTimestamp:[latestMessage timestamp]]; [_account sendIq:mamQuery withHandler:$newHandler(self, handleCatchup, $BOOL(secondTry, YES))]; } else @@ -952,7 +953,7 @@ -(void) sendJoinPresenceFor:(NSString*) room } if(![[iqNode findFirst:@"{urn:xmpp:mam:2}fin@complete|bool"] boolValue] && [iqNode check:@"{urn:xmpp:mam:2}fin/{http://jabber.org/protocol/rsm}set/last#"]) { - DDLogVerbose(@"Paging through muc mam catchup results with after: %@", [iqNode findFirst:@"{urn:xmpp:mam:2}fin/{http://jabber.org/protocol/rsm}set/last#"]); + DDLogVerbose(@"Paging through muc mam catchup results at %@ with after: %@", iqNode.fromUser, [iqNode findFirst:@"{urn:xmpp:mam:2}fin/{http://jabber.org/protocol/rsm}set/last#"]); //do RSM forward paging XMPPIQ* pageQuery = [[XMPPIQ alloc] initWithType:kiqSetType to:iqNode.fromUser]; [pageQuery setMAMQueryAfter:[iqNode findFirst:@"{urn:xmpp:mam:2}fin/{http://jabber.org/protocol/rsm}set/last#"]]; @@ -960,7 +961,7 @@ -(void) sendJoinPresenceFor:(NSString*) room } else if([[iqNode findFirst:@"{urn:xmpp:mam:2}fin@complete|bool"] boolValue]) { - DDLogVerbose(@"Muc mam catchup finished"); + DDLogVerbose(@"Muc mam catchup of %@ finished", iqNode.fromUser); [_account mamFinishedFor:iqNode.fromUser]; } $$ diff --git a/Monal/Classes/XMPPIQ.m b/Monal/Classes/XMPPIQ.m index ea1e500584..1e47cf968a 100644 --- a/Monal/Classes/XMPPIQ.m +++ b/Monal/Classes/XMPPIQ.m @@ -237,14 +237,16 @@ -(void) setMAMQueryAfter:(NSString*) uid [self addChildNode:queryNode]; } --(void) setCompleteMAMQuery +-(void) setMAMQueryAfterTimestamp:(NSDate*) timestamp { //set iq id to mam query id self.id = [NSString stringWithFormat:@"MLcatchup:%@", [[NSUUID UUID] UUIDString]]; MLXMLNode* queryNode = [[MLXMLNode alloc] initWithElement:@"query" andNamespace:@"urn:xmpp:mam:2" withAttributes:@{ @"queryid": self.id } andChildren:@[ - [[XMPPDataForm alloc] initWithType:@"submit" andFormType:@"urn:xmpp:mam:2"], + [[XMPPDataForm alloc] initWithType:@"submit" formType:@"urn:xmpp:mam:2" andDictionary:@{ + @"start": [HelperTools generateDateTimeString:timestamp] + }], [[MLXMLNode alloc] initWithElement:@"set" andNamespace:@"http://jabber.org/protocol/rsm" withAttributes:@{} andChildren:@[ [[MLXMLNode alloc] initWithElement:@"max" andData:@"50"] ] andData:nil] diff --git a/Monal/Classes/chatViewController.m b/Monal/Classes/chatViewController.m index 7abe28bd5c..c3eae87fdd 100644 --- a/Monal/Classes/chatViewController.m +++ b/Monal/Classes/chatViewController.m @@ -2271,10 +2271,10 @@ -(UITableViewCell*) tableView:(UITableView*) tableView cellForRowAtIndexPath:(NS if(priorRow.inbound != row.inbound) newSender = YES; } - cell.date.text = [self formattedTimeStampWithSource:row.delayTimeStamp ? row.delayTimeStamp : row.timestamp]; + cell.date.text = [self formattedTimeStampWithSource:row.timestamp]; cell.selectionStyle = UITableViewCellSelectionStyleNone; - cell.dividerDate.text = [self formattedDateWithSource:row.delayTimeStamp?row.delayTimeStamp:row.timestamp andPriorDate:priorRow.timestamp]; + cell.dividerDate.text = [self formattedDateWithSource:row.timestamp andPriorDate:priorRow.timestamp]; // Do not hide the lockImage if the message was encrypted cell.lockImage.hidden = !row.encrypted;