Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Commit

Permalink
Bug Fix - Local echoes for typed messages stay (far) longer in grey
Browse files Browse the repository at this point in the history
  • Loading branch information
giomfo committed Feb 2, 2017
1 parent 00391df commit 7eb822a
Showing 1 changed file with 71 additions and 126 deletions.
197 changes: 71 additions & 126 deletions MatrixKit/Models/Room/MXKRoomDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ - (void)destroy

[[NSNotificationCenter defaultCenter] removeObserver:self name:kMXEventDidChangeSentStateNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:kMXEventDidDecryptNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:kMXEventDidChangeIdentifierNotification object:nil];

if (NSCurrentLocaleDidChangeNotificationObserver)
{
Expand Down Expand Up @@ -701,12 +702,12 @@ - (void)setEventsFilterForMessages:(NSArray *)eventsFilterForMessages
if (localEcho.originServerTs != kMXUndefinedTimestamp)
{
// Replace the local echo by the true event sent by the homeserver
[self replaceLocalEcho:localEcho.eventId withEvent:event];
[self replaceEvent:localEcho withEvent:event];
}
else
{
// Remove the local echo, and process independently the true event.
[self replaceLocalEcho:localEcho.eventId withEvent:nil];
[self replaceEvent:localEcho withEvent:nil];
localEcho = nil;
}
}
Expand Down Expand Up @@ -1221,37 +1222,19 @@ - (void)sendTextMessage:(NSString *)text success:(void (^)(NSString *))success f
}

__block MXEvent *localEchoEvent = nil;
__block NSString *localEchoEventId = nil;

void(^onSuccess)(NSString *) = ^(NSString *eventId) {

if (localEchoEventId)
{
[self replaceLocalEcho:localEchoEventId withEvent:localEchoEvent];
}

if (success)
{
success(eventId);
}

};

// Make the request to the homeserver
if (isEmote)
{
[_room sendEmote:text formattedText:html localEcho:&localEchoEvent success:onSuccess failure:failure];
[_room sendEmote:text formattedText:html localEcho:&localEchoEvent success:success failure:failure];
}
else
{
[_room sendTextMessage:text formattedText:html localEcho:&localEchoEvent success:onSuccess failure:failure];
[_room sendTextMessage:text formattedText:html localEcho:&localEchoEvent success:success failure:failure];
}

if (localEchoEvent)
{
// From here the local echo will be handled thanks to its local event id (temporary id).
localEchoEventId = localEchoEvent.eventId;

// Make the data source digest this fake local echo message
[self queueEventForProcessing:localEchoEvent withRoomState:_room.state direction:MXTimelineDirectionForwards];
[self processQueuedEvents:nil];
Expand Down Expand Up @@ -1305,29 +1288,11 @@ - (void)sendImage:(NSURL *)imageLocalURL mimeType:(NSString *)mimetype success:(
- (void)sendImageData:(NSData*)imageData withImageSize:(CGSize)imageSize mimeType:(NSString*)mimetype andThumbnail:(UIImage*)thumbnail success:(void (^)(NSString *eventId))success failure:(void (^)(NSError *error))failure
{
__block MXEvent *localEchoEvent = nil;
__block NSString *localEchoEventId = nil;

void(^onSuccess)(NSString *) = ^(NSString *eventId) {

if (localEchoEventId)
{
[self replaceLocalEcho:localEchoEventId withEvent:localEchoEvent];
}

if (success)
{
success(eventId);
}

};

[_room sendImage:imageData withImageSize:imageSize mimeType:mimetype andThumbnail:thumbnail localEcho:&localEchoEvent success:onSuccess failure:failure];
[_room sendImage:imageData withImageSize:imageSize mimeType:mimetype andThumbnail:thumbnail localEcho:&localEchoEvent success:success failure:failure];

if (localEchoEvent)
{
// From here the local echo will be handled thanks to its local event id (temporary id).
localEchoEventId = localEchoEvent.eventId;

// Make the data source digest this fake local echo message
[self queueEventForProcessing:localEchoEvent withRoomState:_room.state direction:MXTimelineDirectionForwards];
[self processQueuedEvents:nil];
Expand All @@ -1337,29 +1302,11 @@ - (void)sendImageData:(NSData*)imageData withImageSize:(CGSize)imageSize mimeTyp
- (void)sendVideo:(NSURL *)videoLocalURL withThumbnail:(UIImage *)videoThumbnail success:(void (^)(NSString *))success failure:(void (^)(NSError *))failure
{
__block MXEvent *localEchoEvent = nil;
__block NSString *localEchoEventId = nil;

void(^onSuccess)(NSString *) = ^(NSString *eventId) {

if (localEchoEventId)
{
[self replaceLocalEcho:localEchoEventId withEvent:localEchoEvent];
}

if (success)
{
success(eventId);
}

};

[_room sendVideo:videoLocalURL withThumbnail:videoThumbnail localEcho:&localEchoEvent success:onSuccess failure:failure];
[_room sendVideo:videoLocalURL withThumbnail:videoThumbnail localEcho:&localEchoEvent success:success failure:failure];

if (localEchoEvent)
{
// From here the local echo will be handled thanks to its local event id (temporary id).
localEchoEventId = localEchoEvent.eventId;

// Make the data source digest this fake local echo message
[self queueEventForProcessing:localEchoEvent withRoomState:_room.state direction:MXTimelineDirectionForwards];
[self processQueuedEvents:nil];
Expand All @@ -1369,29 +1316,11 @@ - (void)sendVideo:(NSURL *)videoLocalURL withThumbnail:(UIImage *)videoThumbnail
- (void)sendFile:(NSURL *)fileLocalURL mimeType:(NSString*)mimeType success:(void (^)(NSString *))success failure:(void (^)(NSError *))failure
{
__block MXEvent *localEchoEvent = nil;
__block NSString *localEchoEventId = nil;

void(^onSuccess)(NSString *) = ^(NSString *eventId) {

if (localEchoEventId)
{
[self replaceLocalEcho:localEchoEventId withEvent:localEchoEvent];
}

if (success)
{
success(eventId);
}

};

[_room sendFile:fileLocalURL mimeType:mimeType localEcho:&localEchoEvent success:onSuccess failure:failure];
[_room sendFile:fileLocalURL mimeType:mimeType localEcho:&localEchoEvent success:success failure:failure];

if (localEchoEvent)
{
// From here the local echo will be handled thanks to its local event id (temporary id).
localEchoEventId = localEchoEvent.eventId;

// Make the data source digest this fake local echo message
[self queueEventForProcessing:localEchoEvent withRoomState:_room.state direction:MXTimelineDirectionForwards];
[self processQueuedEvents:nil];
Expand All @@ -1401,30 +1330,12 @@ - (void)sendFile:(NSURL *)fileLocalURL mimeType:(NSString*)mimeType success:(voi
- (void)sendMessageWithContent:(NSDictionary *)msgContent success:(void (^)(NSString *))success failure:(void (^)(NSError *))failure
{
__block MXEvent *localEchoEvent = nil;
__block NSString *localEchoEventId = nil;

void(^onSuccess)(NSString *) = ^(NSString *eventId) {

if (localEchoEventId)
{
[self replaceLocalEcho:localEchoEventId withEvent:localEchoEvent];
}

if (success)
{
success(eventId);
}

};

// Make the request to the homeserver
[_room sendMessageWithContent:msgContent localEcho:&localEchoEvent success:onSuccess failure:failure];
[_room sendMessageWithContent:msgContent localEcho:&localEchoEvent success:success failure:failure];

if (localEchoEvent)
{
// From here the local echo will be handled thanks to its local event id (temporary id)
localEchoEventId = localEchoEvent.eventId;

// Make the data source digest this fake local echo message
[self queueEventForProcessing:localEchoEvent withRoomState:_room.state direction:MXTimelineDirectionForwards];
[self processQueuedEvents:nil];
Expand All @@ -1443,28 +1354,12 @@ - (void)resendEventWithEventId:(NSString *)eventId success:(void (^)(NSString *)

NSLog(@"[MXKRoomDataSource] resendEventWithEventId. Event: %@", event);

NSString *localEchoEventId = event.eventId;

void(^onSuccess)(NSString *) = ^(NSString *eventId) {

if (localEchoEventId)
{
[self replaceLocalEcho:localEchoEventId withEvent:event];
}

if (success)
{
success(eventId);
}

};

// Check first whether the event is encrypted
if ([event.wireType isEqualToString:kMXEventTypeStringRoomEncrypted])
{
// We try here to resent an encrypted event
// Note: we keep the existing local echo.
[_room sendEventOfType:kMXEventTypeStringRoomEncrypted content:event.wireContent localEcho:&event success:onSuccess failure:failure];
[_room sendEventOfType:kMXEventTypeStringRoomEncrypted content:event.wireContent localEcho:&event success:success failure:failure];
}
else if ([event.type isEqualToString:kMXEventTypeStringRoomMessage])
{
Expand All @@ -1473,7 +1368,7 @@ - (void)resendEventWithEventId:(NSString *)eventId success:(void (^)(NSString *)
if ([msgType isEqualToString:kMXMessageTypeText] || [msgType isEqualToString:kMXMessageTypeEmote])
{
// Resend the Matrix event by reusing the existing echo
[_room sendMessageWithContent:event.content localEcho:&event success:onSuccess failure:failure];
[_room sendMessageWithContent:event.content localEcho:&event success:success failure:failure];
}
else if ([msgType isEqualToString:kMXMessageTypeImage])
{
Expand Down Expand Up @@ -1514,7 +1409,7 @@ - (void)resendEventWithEventId:(NSString *)eventId success:(void (^)(NSString *)
else
{
// Resend the Matrix event by reusing the existing echo
[_room sendMessageWithContent:event.content localEcho:&event success:onSuccess failure:failure];
[_room sendMessageWithContent:event.content localEcho:&event success:success failure:failure];
}
}
else if ([msgType isEqualToString:kMXMessageTypeVideo])
Expand All @@ -1530,7 +1425,7 @@ - (void)resendEventWithEventId:(NSString *)eventId success:(void (^)(NSString *)
else
{
// Resend the Matrix event by reusing the existing echo
[_room sendMessageWithContent:event.content localEcho:&event success:onSuccess failure:failure];
[_room sendMessageWithContent:event.content localEcho:&event success:success failure:failure];
}
}
else if ([msgType isEqualToString:kMXMessageTypeFile])
Expand Down Expand Up @@ -1565,7 +1460,7 @@ - (void)resendEventWithEventId:(NSString *)eventId success:(void (^)(NSString *)
else
{
// Resend the Matrix event by reusing the existing echo
[_room sendMessageWithContent:event.content localEcho:&event success:onSuccess failure:failure];
[_room sendMessageWithContent:event.content localEcho:&event success:success failure:failure];
}
}
else
Expand Down Expand Up @@ -1794,10 +1689,16 @@ - (void)lastMessageWithEventFormatter:(MXKEventFormatter*)eventFormatter onCompl

#pragma mark - Private methods

- (void)replaceLocalEcho:(NSString*)localEchoEventId withEvent:(MXEvent*)event
- (void)replaceEvent:(MXEvent*)eventToReplace withEvent:(MXEvent*)event
{
// Retrieve the cell data hosting the local echo
id<MXKRoomBubbleCellDataStoring> bubbleData = [self cellDataOfEventWithEventId:localEchoEventId];
if (eventToReplace.isLocalEvent)
{
// Stop listening to the identifier change for the replaced event.
[[NSNotificationCenter defaultCenter] removeObserver:self name:kMXEventDidChangeIdentifierNotification object:eventToReplace];
}

// Retrieve the cell data hosting the replaced event
id<MXKRoomBubbleCellDataStoring> bubbleData = [self cellDataOfEventWithEventId:eventToReplace.eventId];
if (!bubbleData)
{
return;
Expand All @@ -1809,23 +1710,29 @@ - (void)replaceLocalEcho:(NSString*)localEchoEventId withEvent:(MXEvent*)event
// Check whether the local echo is replaced or removed
if (event)
{
remainingEvents = [bubbleData updateEvent:localEchoEventId withEvent:event];
remainingEvents = [bubbleData updateEvent:eventToReplace.eventId withEvent:event];
}
else
{
remainingEvents = [bubbleData removeEvent:localEchoEventId];
remainingEvents = [bubbleData removeEvent:eventToReplace.eventId];
}
}

// Update bubbles mapping
@synchronized (eventIdToBubbleMap)
{
// Remove the broken link from the map
[eventIdToBubbleMap removeObjectForKey:localEchoEventId];
[eventIdToBubbleMap removeObjectForKey:eventToReplace.eventId];

if (event && remainingEvents)
{
eventIdToBubbleMap[event.eventId] = bubbleData;

if (event.isLocalEvent)
{
// Listen to the identifier change for the local events.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(localEventDidChangeIdentifier:) name:kMXEventDidChangeIdentifierNotification object:event];
}
}
}

Expand All @@ -1836,7 +1743,7 @@ - (void)replaceLocalEcho:(NSString*)localEchoEventId withEvent:(MXEvent*)event
}

// Update lastMessage if it has been replaced
if ([lastMessage.eventId isEqualToString:localEchoEventId])
if ([lastMessage.eventId isEqualToString:eventToReplace.eventId])
{
// The new event should have the same characteristics as localEcho: it should
// match [self lastMessageWithEventFormatter:] criteria and can replace it as
Expand Down Expand Up @@ -1864,6 +1771,12 @@ - (void)replaceLocalEcho:(NSString*)localEchoEventId withEvent:(MXEvent*)event
for (MXEvent *event in cellData.events)
{
[eventIdToBubbleMap removeObjectForKey:event.eventId];

if (event.isLocalEvent)
{
// Stop listening to the identifier change for this event.
[[NSNotificationCenter defaultCenter] removeObserver:self name:kMXEventDidChangeIdentifierNotification object:event];
}
}
}

Expand Down Expand Up @@ -1987,6 +1900,32 @@ - (void)eventDidChangeSentState:(NSNotification *)notif
}
}

- (void)localEventDidChangeIdentifier:(NSNotification *)notif
{
MXEvent *event = notif.object;
NSString *previousId = notif.userInfo[kMXEventIdentifierKey];

if (event && previousId)
{
// Update bubbles mapping
@synchronized (eventIdToBubbleMap)
{
id<MXKRoomBubbleCellDataStoring> bubbleData = eventIdToBubbleMap[previousId];
if (bubbleData && event.eventId)
{
eventIdToBubbleMap[event.eventId] = bubbleData;
[eventIdToBubbleMap removeObjectForKey:previousId];
}
}

if (!event.isLocalEvent)
{
// Stop listening to the identifier change when the event becomes an actual event.
[[NSNotificationCenter defaultCenter] removeObserver:self name:kMXEventDidChangeIdentifierNotification object:event];
}
}
}

- (void)eventDidDecrypt:(NSNotification *)notif
{
MXEvent *event = notif.object;
Expand Down Expand Up @@ -2434,6 +2373,12 @@ - (void)processQueuedEvents:(void (^)(NSUInteger addedHistoryCellNb, NSUInteger
{
eventIdToBubbleMap[queuedEvent.event.eventId] = bubbleData;
}

if (queuedEvent.event.isLocalEvent)
{
// Listen to the identifier change for the local events.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(localEventDidChangeIdentifier:) name:kMXEventDidChangeIdentifierNotification object:queuedEvent.event];
}
}

if (queuedEvent.direction == MXTimelineDirectionForwards)
Expand Down

0 comments on commit 7eb822a

Please sign in to comment.