From ccded8995c34bf415fd97be8a4122849cd7ce715 Mon Sep 17 00:00:00 2001 From: manuroe Date: Mon, 7 May 2018 17:58:18 +0200 Subject: [PATCH] Send Stickers: Plug the sticker picker widget with the room datasource to send a sticker #1860 --- Riot/AppDelegate.m | 7 ++-- .../Widgets/WidgetPickerViewController.m | 8 +++- .../Widgets/WidgetViewController.h | 7 ++++ .../Widgets/WidgetViewController.m | 39 ++++++++++++++++--- 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/Riot/AppDelegate.m b/Riot/AppDelegate.m index a3bc7075f..b11d6c6f3 100644 --- a/Riot/AppDelegate.m +++ b/Riot/AppDelegate.m @@ -2200,7 +2200,7 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN sdkOptions.backgroundModeHandler = [[MXUIKitBackgroundModeHandler alloc] init]; // Get modular widget events in rooms histories - [[MXKAppSettings standardAppSettings] addSupportedEventTypes:@[kWidgetEventTypeString]]; + [[MXKAppSettings standardAppSettings] addSupportedEventTypes:@[kWidgetMatrixEventTypeString, kWidgetModularEventTypeString]]; // Disable long press on event in bubble cells [MXKRoomBubbleTableViewCell disableLongPressGestureOnEvent:YES]; @@ -2259,9 +2259,10 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN // Each room member will be considered as a potential contact. [MXKContactManager sharedManager].contactManagerMXRoomSource = MXKContactManagerMXRoomSourceAll; - // Send read receipts for modular widgets events too + // Send read receipts for widgets events too NSMutableArray *acknowledgableEventTypes = [NSMutableArray arrayWithArray:mxSession.acknowledgableEventTypes]; - [acknowledgableEventTypes addObject:kWidgetEventTypeString]; + [acknowledgableEventTypes addObject:kWidgetMatrixEventTypeString]; + [acknowledgableEventTypes addObject:kWidgetModularEventTypeString]; mxSession.acknowledgableEventTypes = acknowledgableEventTypes; } else if (mxSession.state == MXSessionStateStoreDataReady) diff --git a/Riot/ViewController/Widgets/WidgetPickerViewController.m b/Riot/ViewController/Widgets/WidgetPickerViewController.m index 958f263ff..72b4665ee 100644 --- a/Riot/ViewController/Widgets/WidgetPickerViewController.m +++ b/Riot/ViewController/Widgets/WidgetPickerViewController.m @@ -75,8 +75,12 @@ // Display the widget [widget widgetUrl:^(NSString * _Nonnull widgetUrl) { - WidgetViewController *widgetVC = [[WidgetViewController alloc] initWithUrl:widgetUrl forWidget:widget]; - [mxkViewController.navigationController pushViewController:widgetVC animated:YES]; + WidgetViewController *widgetVC = [[WidgetViewController alloc] initWithUrl:widgetUrl forWidget:widget]; + + MXKRoomDataSourceManager *roomDataSourceManager = [MXKRoomDataSourceManager sharedManagerForMatrixSession:mxSession]; + widgetVC.roomDataSource = [roomDataSourceManager roomDataSourceForRoom:roomId create:NO]; + + [mxkViewController.navigationController pushViewController:widgetVC animated:YES]; } failure:^(NSError * _Nonnull error) { diff --git a/Riot/ViewController/Widgets/WidgetViewController.h b/Riot/ViewController/Widgets/WidgetViewController.h index 42b0c67dd..0f9a70685 100644 --- a/Riot/ViewController/Widgets/WidgetViewController.h +++ b/Riot/ViewController/Widgets/WidgetViewController.h @@ -17,6 +17,7 @@ #import "WebViewViewController.h" #import "WidgetManager.h" +#import "MatrixKit/MatrixKit.h" /** `WidgetViewController` displays widget within a webview. @@ -26,6 +27,12 @@ */ @interface WidgetViewController : WebViewViewController +/** + The room data source. + Required if the widget needs to post messages. + */ +@property (nonatomic) MXKRoomDataSource *roomDataSource; + /** Init 'WidgetViewController' instance with a widget. diff --git a/Riot/ViewController/Widgets/WidgetViewController.m b/Riot/ViewController/Widgets/WidgetViewController.m index d4a64b561..aad876a52 100644 --- a/Riot/ViewController/Widgets/WidgetViewController.m +++ b/Riot/ViewController/Widgets/WidgetViewController.m @@ -18,7 +18,7 @@ #import "AppDelegate.h" -NSString *const kJavascriptSendResponseToModular = @"riotIOS.sendResponse('%@', %@);"; +NSString *const kJavascriptSendResponseToPostMessageAPI = @"riotIOS.sendResponse('%@', %@);"; @interface WidgetViewController () { @@ -186,13 +186,42 @@ NSString *const kJavascriptSendResponseToModular = @"riotIOS.sendResponse('%@', - (void)onPostMessageRequest:(NSString*)requestId data:(NSDictionary*)requestData { - // TODO + NSString *action; + MXJSONModelSetString(action, requestData[@"action"]); + + if ([@"m.sticker" isEqualToString:action]) + { + // Extract the sticker event content and send it as is + + // The key should be "data" according to https://docs.google.com/document/d/1uPF7XWY_dXTKVKV7jZQ2KmsI19wn9-kFRgQ1tFQP7wQ/edit?usp=sharing + // TODO: Fix it once spec is finalised + NSDictionary *widgetData; + NSDictionary *stickerContent; + MXJSONModelSetDictionary(widgetData, requestData[@"widgetData"]); + if (widgetData) + { + MXJSONModelSetDictionary(stickerContent, widgetData[@"content"]); + } + + if (stickerContent) + { + // Let the data source manage the sending cycle + [_roomDataSource sendEventOfType:kMXEventTypeStringSticker content:stickerContent success:nil failure:nil]; + } + else + { + NSLog(@"[WidgetVC] onPostMessageRequest: ERROR: Invalid content for m.sticker: %@", requestData); + } + + // Consider we are done with the sticker picker widget + [self withdrawViewControllerAnimated:YES completion:nil]; + } } - (void)sendBoolResponse:(BOOL)response toRequest:(NSString*)requestId { // Convert BOOL to "true" or "false" - NSString *js = [NSString stringWithFormat:kJavascriptSendResponseToModular, + NSString *js = [NSString stringWithFormat:kJavascriptSendResponseToPostMessageAPI, requestId, response ? @"true" : @"false"]; @@ -201,7 +230,7 @@ NSString *const kJavascriptSendResponseToModular = @"riotIOS.sendResponse('%@', - (void)sendIntegerResponse:(NSUInteger)response toRequest:(NSString*)requestId { - NSString *js = [NSString stringWithFormat:kJavascriptSendResponseToModular, + NSString *js = [NSString stringWithFormat:kJavascriptSendResponseToPostMessageAPI, requestId, @(response)]; @@ -227,7 +256,7 @@ NSString *const kJavascriptSendResponseToModular = @"riotIOS.sendResponse('%@', jsString = @"null"; } - NSString *js = [NSString stringWithFormat:kJavascriptSendResponseToModular, + NSString *js = [NSString stringWithFormat:kJavascriptSendResponseToPostMessageAPI, requestId, jsString];