From b9b0843d1f3924ae8517245818613132dbae85cd Mon Sep 17 00:00:00 2001 From: giomfo Date: Tue, 3 Apr 2018 15:09:28 +0200 Subject: [PATCH] Render sticker events in the timeline - useMXJSONModelSetXxx helpers to avoid crash because of malformed content. vector-im/riot-ios#1819 --- MatrixKit/Models/Room/MXKAttachment.m | 21 ++++++++----------- MatrixKit/Utils/MXKEventFormatter.m | 15 ++++++------- .../MXKRoomBubbleTableViewCell.h | 2 +- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/MatrixKit/Models/Room/MXKAttachment.m b/MatrixKit/Models/Room/MXKAttachment.m index fa86de99d..86d6b5a3c 100644 --- a/MatrixKit/Models/Room/MXKAttachment.m +++ b/MatrixKit/Models/Room/MXKAttachment.m @@ -84,7 +84,7 @@ - (instancetype)initWithEvent:(MXEvent *)mxEvent andMatrixSession:(MXSession*)mx if (mxEvent.eventType == MXEventTypeSticker) { _type = MXKAttachmentTypeSticker; - _thumbnailInfo = eventContent[@"info"][@"thumbnail_info"]; + MXJSONModelSetDictionary(_thumbnailInfo, eventContent[@"info"][@"thumbnail_info"]); } else { @@ -101,7 +101,7 @@ - (instancetype)initWithEvent:(MXEvent *)mxEvent andMatrixSession:(MXSession*)mx else if ([msgtype isEqualToString:kMXMessageTypeVideo]) { _type = MXKAttachmentTypeVideo; - _thumbnailInfo = eventContent[@"info"][@"thumbnail_info"]; + MXJSONModelSetDictionary(_thumbnailInfo, eventContent[@"info"][@"thumbnail_info"]); } else if ([msgtype isEqualToString:kMXMessageTypeLocation]) { @@ -119,24 +119,21 @@ - (instancetype)initWithEvent:(MXEvent *)mxEvent andMatrixSession:(MXSession*)mx } } - _originalFileName = [eventContent[@"body"] isKindOfClass:[NSString class]] ? eventContent[@"body"] : nil; - - _contentInfo = eventContent[@"info"]; - - thumbnailFile = _contentInfo[@"thumbnail_file"]; - - contentFile = eventContent[@"file"]; + MXJSONModelSetString(_originalFileName, eventContent[@"body"]); + MXJSONModelSetDictionary(_contentInfo, eventContent[@"info"]); + MXJSONModelSetDictionary(thumbnailFile, _contentInfo[@"thumbnail_file"]); + MXJSONModelSetDictionary(contentFile, eventContent[@"file"]); // Retrieve the content url by taking into account the potential encryption. if (contentFile) { _isEncrypted = YES; - _contentURL = contentFile[@"url"]; + MXJSONModelSetString(_contentURL, contentFile[@"url"]); } else { _isEncrypted = NO; - _contentURL = eventContent[@"url"]; + MXJSONModelSetString(_contentURL, eventContent[@"url"]); } // Note: When the attachment uploading is in progress, the upload id is stored in the content url (nasty trick). @@ -155,7 +152,7 @@ - (instancetype)initWithEvent:(MXEvent *)mxEvent andMatrixSession:(MXSession*)mx NSString *mimetype = nil; if (_contentInfo) { - mimetype = _contentInfo[@"mimetype"]; + MXJSONModelSetString(mimetype, _contentInfo[@"mimetype"]); } _cacheFilePath = [MXMediaManager cachePathForMediaWithURL:_actualURL andType:mimetype inFolder:_eventRoomId]; diff --git a/MatrixKit/Utils/MXKEventFormatter.m b/MatrixKit/Utils/MXKEventFormatter.m index b78b8e7ac..855da50ca 100644 --- a/MatrixKit/Utils/MXKEventFormatter.m +++ b/MatrixKit/Utils/MXKEventFormatter.m @@ -850,15 +850,14 @@ - (NSAttributedString *)attributedStringFromEvent:(MXEvent *)event withRoomState BOOL isHTML = NO; // Use the HTML formatted string if provided - if ([event.content[@"format"] isEqualToString:kMXRoomMessageFormatHTML] - && [event.content[@"formatted_body"] isKindOfClass:[NSString class]]) + if ([event.content[@"format"] isEqualToString:kMXRoomMessageFormatHTML]) { isHTML =YES; - body = event.content[@"formatted_body"]; + MXJSONModelSetString(body, event.content[@"formatted_body"]); } - else if ([event.content[@"body"] isKindOfClass:[NSString class]]) + else { - body = event.content[@"body"]; + MXJSONModelSetString(body, event.content[@"body"]); } if (body) @@ -1024,10 +1023,8 @@ - (NSAttributedString *)attributedStringFromEvent:(MXEvent *)event withRoomState case MXEventTypeSticker: { NSString *body; - if ([event.content[@"body"] isKindOfClass:[NSString class]]) - { - body = event.content[@"body"]; - } + MXJSONModelSetString(body, event.content[@"body"]); + // Check sticker validity if (![self isSupportedAttachment:event]) { diff --git a/MatrixKit/Views/RoomBubbleList/MXKRoomBubbleTableViewCell.h b/MatrixKit/Views/RoomBubbleList/MXKRoomBubbleTableViewCell.h index 032a0e4e3..0782354d6 100644 --- a/MatrixKit/Views/RoomBubbleList/MXKRoomBubbleTableViewCell.h +++ b/MatrixKit/Views/RoomBubbleList/MXKRoomBubbleTableViewCell.h @@ -247,7 +247,7 @@ extern NSString *const kMXKRoomBubbleCellUrl; /** Method used during [MXKCellRendering render:] to check the provided `cellData` and prepare the protected `bubbleData`. - Do not be overridden by a class child. + Do not override it. @param cellData the data object to render. */