Skip to content

Commit

Permalink
Fix mam query bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tmolitor-stud-tu committed Aug 27, 2023
1 parent 43db781 commit eaf1dfb
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions Monal/Classes/DataLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ extern NSString* const kMessageTypeFiletransfer;
-(NSArray<MLMessage*>*) messagesForHistoryIDs:(NSArray<NSNumber*>*) historyIDs;
-(MLMessage* _Nullable) messageForHistoryID:(NSNumber* _Nullable) historyID;
-(NSNumber*) getSmallestHistoryId;
-(NSNumber*) getBiggestHistoryId;

/*
adds a specified message to the database
Expand Down
7 changes: 7 additions & 0 deletions Monal/Classes/DataLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Monal/Classes/HelperTools.m
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ +(void) initSystem
[self installCrashHandler];
[self installExceptionHandler];
[self activityLog];

DDLogError(@"Timestamp for nil: %@", [self generateDateTimeString:nil]);
}

+(NSDictionary<NSString*, NSString*>*) getInvalidPushServers
Expand Down
9 changes: 5 additions & 4 deletions Monal/Classes/MLIQProcessor.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -199,15 +200,15 @@ +(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#"]];
[account sendIq:pageQuery withHandler:$newHandler(self, handleCatchup, $BOOL(secondTry, NO))];
}
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];
}
$$
Expand Down
2 changes: 0 additions & 2 deletions Monal/Classes/MLMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*
Expand Down
9 changes: 5 additions & 4 deletions Monal/Classes/MLMucProcessor.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -952,15 +953,15 @@ -(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#"]];
[_account sendIq:pageQuery withHandler:$newHandler(self, handleCatchup, $BOOL(secondTry, NO))];
}
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];
}
$$
Expand Down
2 changes: 1 addition & 1 deletion Monal/Classes/XMPPIQ.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ FOUNDATION_EXPORT NSString* const kiqErrorType;

-(void) setMAMQueryLatestMessagesForJid:(NSString* _Nullable) jid before:(NSString* _Nullable) uid;
-(void) setMAMQueryAfter:(NSString*) uid;
-(void) setCompleteMAMQuery;
setMAMQueryAfterTimestamp:(NSDate*) timestamp;
-(void) setMAMQueryForLatestId;

-(void) setMucListQueryFor:(NSString*) listType;
Expand Down
6 changes: 4 additions & 2 deletions Monal/Classes/XMPPIQ.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions Monal/Classes/chatViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit eaf1dfb

Please sign in to comment.