Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 2 Channel options bugs #191

Merged
merged 6 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 62 additions & 12 deletions android/src/main/java/io/ably/flutter/plugin/AblyMessageCodec.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.ably.flutter.plugin;

import androidx.annotation.Nullable;

import com.google.firebase.messaging.RemoteMessage;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
Expand Down Expand Up @@ -29,6 +31,7 @@
import io.ably.lib.rest.DeviceDetails;
import io.ably.lib.types.AblyException;
import io.ably.lib.types.AsyncPaginatedResult;
import io.ably.lib.types.ChannelMode;
import io.ably.lib.types.ChannelOptions;
import io.ably.lib.types.ClientOptions;
import io.ably.lib.types.DeltaExtras;
Expand Down Expand Up @@ -355,26 +358,73 @@ private ChannelOptions decodeRestChannelOptions(Map<String, Object> jsonMap) {
if (jsonMap == null) return null;
final Object cipher = jsonMap.get(PlatformConstants.TxRealtimeChannelOptions.cipher);
try {
return ChannelOptions.withCipherKey((String) cipher);
} catch (AblyException ae) {
System.out.println("Exception while decoding RestChannelOptions : " + ae);
return createChannelOptions(cipher);
} catch (AblyException e) {
System.out.println("Exception while decoding RestChannelOptions: " + e);
return null;
}
}

private ChannelOptions decodeRealtimeChannelOptions(Map<String, Object> jsonMap) {
if (jsonMap == null) return null;
final Object cipher = jsonMap.get(PlatformConstants.TxRealtimeChannelOptions.cipher);

ChannelOptions options;
try {
final ChannelOptions o = ChannelOptions.withCipherKey((String) cipher);
readValueFromJson(jsonMap, PlatformConstants.TxRealtimeChannelOptions.params, v -> o.cipherParams = (Map<String, String>) v);
// modes is not supported in ably-java
// Track @ https://github.com/ably/ably-flutter/issues/14
return o;
} catch (AblyException ae) {
System.out.println("Exception while decoding RealtimeChannelOptions: " + ae);
options = createChannelOptions(cipher);
} catch (AblyException e) {
System.out.println("Exception while decoding RealtimeChannelOptions: " + e);
return null;
}
options.params = (Map<String, String>) jsonMap.get(PlatformConstants.TxRealtimeChannelOptions.params);
final ArrayList<String> modes = (ArrayList<String>) jsonMap.get(PlatformConstants.TxRealtimeChannelOptions.modes);
if (modes != null && modes.size() > 0) {
options.modes = createChannelModesArray(modes);
}

return options;
}

private ChannelOptions createChannelOptions(@Nullable Object cipher) throws AblyException {
if (cipher == null) return new ChannelOptions();
if (cipher instanceof String) {
try {
return ChannelOptions.withCipherKey((String) cipher);
} catch (AblyException ae) {
throw AblyException.fromErrorInfo(new ErrorInfo("Exception while decoding RealtimeChannelOptions as String: " + ae, 400, 40000));
}
} else if (cipher instanceof byte[]) {
try {
return ChannelOptions.withCipherKey((byte[]) cipher);
} catch (AblyException ae) {
throw AblyException.fromErrorInfo(new ErrorInfo("Exception while decoding RealtimeChannelOptions as byte array: " + ae, 400, 40000));
}
} else {
throw AblyException.fromErrorInfo(new ErrorInfo("CipherKey must either be a String or a Byte Array.", 400, 40000));
}
}

private ChannelMode[] createChannelModesArray(ArrayList<String> modesString) {
ChannelMode[] modes = new ChannelMode[modesString.size()];
for (int i = 0; i < modesString.size(); i++) {
modes[i] = decodeChannelOptionsMode(modesString.get(i));
}
return modes;
}

private ChannelMode decodeChannelOptionsMode(String mode) {
switch (mode) {
case PlatformConstants.TxEnumConstants.presence:
return ChannelMode.presence;
case PlatformConstants.TxEnumConstants.publish:
return ChannelMode.publish;
case PlatformConstants.TxEnumConstants.subscribe:
return ChannelMode.subscribe;
case PlatformConstants.TxEnumConstants.presenceSubscribe:
return ChannelMode.presence_subscribe;
default:
return null;
}
}

private Param[] decodeRestHistoryParams(Map<String, Object> jsonMap) {
Expand Down Expand Up @@ -733,7 +783,7 @@ private Map<String, Object> encodeLocalDevice(LocalDevice c) {

return jsonMap;
}

private Map<String, Object> encodePushChannelSubscription(PushBase.ChannelSubscription c) {
if (c == null) return null;
final HashMap<String, Object> jsonMap = new HashMap<>();
Expand Down Expand Up @@ -812,7 +862,7 @@ private Map<String, Object> encodeRemoteMessage(RemoteMessage message) {
writeValueToJson(jsonMap, PlatformConstants.TxRemoteMessage.notification, encodeNotification(message.getNotification()));
return jsonMap;
}

private Map<String, Object> encodeNotification(RemoteMessage.Notification notification) {
if (notification == null) return null;
final HashMap<String, Object> jsonMap = new HashMap<>();
Expand Down
11 changes: 4 additions & 7 deletions ios/Classes/AblyFlutterPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -317,21 +317,18 @@ -(void)registerWithCompletionHandler:(FlutterResult)completionHandler;
ARTRealtimeChannelOptions *const channelOptions = (ARTRealtimeChannelOptions*)[realtimePayload objectForKey:TxTransportKeys_options];

ARTRealtimeChannel *const channel = [realtimeWithHandle.channels get:channelName];
const id callback = ^(ARTPaginatedResult<ARTMessage *> * _Nullable paginatedResult, ARTErrorInfo * _Nullable error) {
[channel setOptions:channelOptions callback:^(ARTErrorInfo * _Nullable error) {
if (error) {
result([
FlutterError
errorWithCode:[NSString stringWithFormat: @"%ld", (long)error.code]
message:[NSString stringWithFormat:@"Error getting realtime channel history; err = %@", [error message]]
message:[NSString stringWithFormat:@"Error setting realtime channel options; err = %@", [error message]]
details:error
]);
} else {
NSNumber *const paginatedResultHandle = [ably setPaginatedResult:paginatedResult handle:nil];
result([[AblyFlutterMessage alloc] initWithMessage:paginatedResult handle: paginatedResultHandle]);
result(nil);
}
};
[channel setOptions:channelOptions callback:callback];
result(nil);
}];
};

static const FlutterHandler _getRealtimeHistory = ^void(AblyFlutterPlugin *const plugin, FlutterMethodCall *const call, const FlutterResult result) {
Expand Down