-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathAblyFlutterWriter.m
358 lines (316 loc) · 16.5 KB
/
AblyFlutterWriter.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
@import Ably;
@import UserNotifications;
#import <ably_flutter/ably_flutter-Swift.h>
#import "AblyFlutterWriter.h"
#import "AblyFlutterMessage.h"
#import "AblyFlutterReader.h"
#import "AblyPlatformConstants.h"
#import "AblyFlutterStreamHandler.h"
NS_ASSUME_NONNULL_BEGIN
typedef NSDictionary * (^AblyCodecEncoder)(id toEncode);
NS_ASSUME_NONNULL_END
@implementation AblyFlutterWriter
+ (UInt8) getType:(id)value{
if([value isKindOfClass:[AblyFlutterMessage class]]){
return CodecTypeAblyMessage;
}else if([value isKindOfClass:[ARTErrorInfo class]]){
return CodecTypeErrorInfo;
} else if([value isKindOfClass:[_AblyConnectionStateChange class]]){
return CodecTypeConnectionStateChange;
}else if([value isKindOfClass:[ARTChannelStateChange class]]){
return CodecTypeChannelStateChange;
}else if([value isKindOfClass:[ARTMessage class]]){
return CodecTypeMessage;
}else if([value isKindOfClass:[ARTPresenceMessage class]]){
return CodecTypePresenceMessage;
} else if ([value isKindOfClass:[ARTTokenRequest class]]){
return CodecTypeTokenRequest;
}else if([value isKindOfClass:[ARTTokenParams class]]){
return CodecTypeTokenParams;
}else if([value isKindOfClass:[ARTPaginatedResult class]]){
return CodecTypePaginatedResult;
} else if ([value isKindOfClass:[ARTLocalDevice class]]) {
// Check for ARTLocalDevice before ARTLocalDevice, since ARTLocalDevice extends ARTDeviceDetails.
// If CodecTypeDeviceDetails was used first, the ARTLocalDevice fields won't be serialized.
return CodecTypeLocalDevice;
} else if([value isKindOfClass:[ARTDeviceDetails class]]) {
return CodecTypeDeviceDetails;
} else if ([value isKindOfClass:[ARTPushChannelSubscription class]]) {
return CodecTypePushChannelSubscription;
} else if ([value isKindOfClass:[UNNotificationSettings class]]) {
return CodecTypeUnNotificationSettings;
} else if ([value isKindOfClass:[RemoteMessage class]]) {
return CodecTypeRemoteMessage;
} else if ([value isKindOfClass:[ARTRealtimeChannelOptions class]]) {
return CodecTypeRealtimeChannelOptions;
} else if ([value isKindOfClass:[ARTChannelOptions class]]) {
return CodecTypeRestChannelOptions;
} else if ([value isKindOfClass:[ARTCipherParams class]]) {
return CodecTypeCipherParams;
} else if ([value isKindOfClass:[ARTTokenDetails class]]) {
return CodecTypeTokenDetails;
}
return 0;
}
+ (AblyCodecEncoder) getEncoder:(const NSString*)type {
NSDictionary<NSString *, AblyCodecEncoder>* _handlers = @{
[NSString stringWithFormat:@"%d", CodecTypeAblyMessage]: encodeAblyMessage,
[NSString stringWithFormat:@"%d", CodecTypeErrorInfo]: encodeErrorInfo,
[NSString stringWithFormat:@"%d", CodecTypeConnectionStateChange]: encodeConnectionStateChange,
[NSString stringWithFormat:@"%d", CodecTypeChannelStateChange]: encodeChannelStateChange,
[NSString stringWithFormat:@"%d", CodecTypeMessage]: encodeChannelMessage,
[NSString stringWithFormat:@"%d", CodecTypePresenceMessage]: encodePresenceMessage,
[NSString stringWithFormat:@"%d", CodecTypeTokenParams]: encodeTokenParams,
[NSString stringWithFormat:@"%d", CodecTypePaginatedResult]: encodePaginatedResult,
[NSString stringWithFormat:@"%d", CodecTypeDeviceDetails]: PushNotificationEncoders.encodeDeviceDetails,
[NSString stringWithFormat:@"%d", CodecTypeLocalDevice]: PushNotificationEncoders.encodeLocalDevice,
[NSString stringWithFormat:@"%d", CodecTypePushChannelSubscription]: PushNotificationEncoders.encodePushChannelSubscription,
[NSString stringWithFormat:@"%d", CodecTypeUnNotificationSettings]: PushNotificationEncoders.encodeUNNotificationSettings,
[NSString stringWithFormat:@"%d", CodecTypeRemoteMessage]: PushNotificationEncoders.encodeRemoteMessage,
[NSString stringWithFormat:@"%d", CodecTypeCipherParams]: CryptoCodec.encodeCipherParams,
[NSString stringWithFormat:@"%d", CodecTypeTokenDetails]: encodeTokenDetails,
[NSString stringWithFormat:@"%d", CodecTypeTokenRequest]: encodeTokenRequest,
};
return [_handlers objectForKey:[NSString stringWithFormat:@"%@", type]];
}
- (void)writeValue:(id)value {
UInt8 const type = [AblyFlutterWriter getType:value];
if(type != 0){
[self writeByte: type];
AblyCodecEncoder encoder = [AblyFlutterWriter getEncoder: [NSString stringWithFormat:@"%d", type]];
id encoded = encoder(value);
[self writeValue: encoded];
return;
}
[super writeValue:value];
}
#define WRITE_VALUE(DICTIONARY, JSON_KEY, VALUE) { \
if ( (VALUE) != nil ) { \
[DICTIONARY setObject:VALUE forKey:JSON_KEY]; \
} \
}
static AblyCodecEncoder encodeAblyMessage = ^NSMutableDictionary*(AblyFlutterMessage *const message) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary, TxAblyMessage_registrationHandle, [message handle]);
id value = [message message];
UInt8 type = [AblyFlutterWriter getType:value];
if(type != 0){
AblyCodecEncoder encoder = [AblyFlutterWriter getEncoder: [NSString stringWithFormat:@"%d", type]];
value = encoder(value);
WRITE_VALUE(dictionary, TxAblyMessage_type, [NSNumber numberWithInt:type]);
}
WRITE_VALUE(dictionary, TxAblyMessage_message, value);
return dictionary;
};
static AblyCodecEncoder encodeErrorInfo = ^NSMutableDictionary*(ARTErrorInfo *const e) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary, TxErrorInfo_message, [e message]);
WRITE_VALUE(dictionary, TxErrorInfo_statusCode, [e statusCode]?@([e statusCode]):nil);
// code, href, requestId and cause - not available in ably-cocoa
// track @ https://github.com/ably/ably-flutter/issues/14
return dictionary;
};
+(NSString *) encodeConnectionState: (ARTRealtimeConnectionState) state {
switch (state) {
case ARTRealtimeInitialized:
return TxEnumConstants_initialized;
case ARTRealtimeConnecting:
return TxEnumConstants_connecting;
case ARTRealtimeConnected:
return TxEnumConstants_connected;
case ARTRealtimeDisconnected:
return TxEnumConstants_disconnected;
case ARTRealtimeSuspended:
return TxEnumConstants_suspended;
case ARTRealtimeClosing:
return TxEnumConstants_closing;
case ARTRealtimeClosed:
return TxEnumConstants_closed;
case ARTRealtimeFailed:
return TxEnumConstants_failed;
}
};
+(NSString *) encodeConnectionEvent: (ARTRealtimeConnectionEvent) event {
switch(event) {
case ARTRealtimeConnectionEventInitialized:
return TxEnumConstants_initialized;
case ARTRealtimeConnectionEventConnecting:
return TxEnumConstants_connecting;
case ARTRealtimeConnectionEventConnected:
return TxEnumConstants_connected;
case ARTRealtimeConnectionEventDisconnected:
return TxEnumConstants_disconnected;
case ARTRealtimeConnectionEventSuspended:
return TxEnumConstants_suspended;
case ARTRealtimeConnectionEventClosing:
return TxEnumConstants_closing;
case ARTRealtimeConnectionEventClosed:
return TxEnumConstants_closed;
case ARTRealtimeConnectionEventFailed:
return TxEnumConstants_failed;
case ARTRealtimeConnectionEventUpdate:
return TxEnumConstants_update;
}
}
+(NSString *) encodeChannelState: (ARTRealtimeChannelState) event {
switch(event) {
case ARTRealtimeChannelInitialized:
return TxEnumConstants_initialized;
case ARTRealtimeChannelAttaching:
return TxEnumConstants_attaching;
case ARTRealtimeChannelAttached:
return TxEnumConstants_attached;
case ARTRealtimeChannelDetaching:
return TxEnumConstants_detaching;
case ARTRealtimeChannelDetached:
return TxEnumConstants_detached;
case ARTRealtimeChannelSuspended:
return TxEnumConstants_suspended;
case ARTRealtimeChannelFailed:
return TxEnumConstants_failed;
}
}
+(NSString *) encodeChannelEvent: (ARTChannelEvent) event {
switch(event) {
case ARTChannelEventInitialized:
return TxEnumConstants_initialized;
case ARTChannelEventAttaching:
return TxEnumConstants_attaching;
case ARTChannelEventAttached:
return TxEnumConstants_attached;
case ARTChannelEventDetaching:
return TxEnumConstants_detaching;
case ARTChannelEventDetached:
return TxEnumConstants_detached;
case ARTChannelEventSuspended:
return TxEnumConstants_suspended;
case ARTChannelEventFailed:
return TxEnumConstants_failed;
case ARTChannelEventUpdate:
return TxEnumConstants_update;
}
}
static AblyCodecEncoder encodeConnectionStateChange = ^NSMutableDictionary*(_AblyConnectionStateChange *const stateChange) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary,
TxConnectionStateChange_current,
[AblyFlutterWriter encodeConnectionState: [stateChange.value current]]);
WRITE_VALUE(dictionary,
TxConnectionStateChange_previous,
[AblyFlutterWriter encodeConnectionState: [stateChange.value previous]]);
WRITE_VALUE(dictionary,
TxConnectionStateChange_event,
[AblyFlutterWriter encodeConnectionEvent: [stateChange.value event]]);
WRITE_VALUE(dictionary, TxConnectionStateChange_retryIn, [stateChange.value retryIn]?@((int)([stateChange.value retryIn] * 1000)):nil);
WRITE_VALUE(dictionary, TxConnectionStateChange_reason, encodeErrorInfo([stateChange.value reason]));
WRITE_VALUE(dictionary, TxConnectionStateChange_connectionId, stateChange.connectionId);
WRITE_VALUE(dictionary, TxConnectionStateChange_connectionKey, stateChange.connectionKey);
return dictionary;
};
static AblyCodecEncoder encodeChannelStateChange = ^NSMutableDictionary*(ARTChannelStateChange *const stateChange) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary,
TxChannelStateChange_current,
[AblyFlutterWriter encodeChannelState: [stateChange current]]);
WRITE_VALUE(dictionary,
TxChannelStateChange_previous,
[AblyFlutterWriter encodeChannelState: [stateChange previous]]);
WRITE_VALUE(dictionary,
TxChannelStateChange_event,
[AblyFlutterWriter encodeChannelEvent: [stateChange event]]);
WRITE_VALUE(dictionary, TxChannelStateChange_resumed, [NSNumber numberWithBool: [stateChange resumed]]);
WRITE_VALUE(dictionary, TxChannelStateChange_reason, encodeErrorInfo([stateChange reason]));
return dictionary;
};
static AblyCodecEncoder encodeChannelMessage = ^NSMutableDictionary*(ARTMessage *const message) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary, TxMessage_id, [message id]);
WRITE_VALUE(dictionary, TxMessage_name, [message name]);
WRITE_VALUE(dictionary, TxMessage_clientId, [message clientId]);
WRITE_VALUE(dictionary, TxMessage_connectionId, [message connectionId]);
WRITE_VALUE(dictionary, TxMessage_encoding, [message encoding]);
WRITE_VALUE(dictionary, TxMessage_extras, [message extras]);
WRITE_VALUE(dictionary, TxMessage_data, (NSObject *)[message data]);
WRITE_VALUE(dictionary, TxMessage_timestamp,
[message timestamp]?@((long)([[message timestamp] timeIntervalSince1970]*1000)):nil);
return dictionary;
};
+(NSString *) encodePresenceAction: (ARTPresenceAction) action {
switch(action) {
case ARTPresenceAbsent:
return TxEnumConstants_absent;
case ARTPresencePresent:
return TxEnumConstants_present;
case ARTPresenceEnter:
return TxEnumConstants_enter;
case ARTPresenceLeave:
return TxEnumConstants_leave;
case ARTPresenceUpdate:
return TxEnumConstants_update;
}
}
static AblyCodecEncoder encodePresenceMessage = ^NSMutableDictionary*(ARTPresenceMessage *const message) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary, TxPresenceMessage_id, [message id]);
WRITE_VALUE(dictionary,
TxPresenceMessage_action,
[AblyFlutterWriter encodePresenceAction: [message action]]);
WRITE_VALUE(dictionary, TxPresenceMessage_clientId, [message clientId]);
WRITE_VALUE(dictionary, TxPresenceMessage_data, (NSObject *)[message data]);
WRITE_VALUE(dictionary, TxPresenceMessage_connectionId, [message connectionId]);
WRITE_VALUE(dictionary, TxPresenceMessage_encoding, [message encoding]);
WRITE_VALUE(dictionary, TxPresenceMessage_extras, [message extras]);
WRITE_VALUE(dictionary, TxPresenceMessage_timestamp,
[message timestamp]?@((long)([[message timestamp] timeIntervalSince1970]*1000)):nil);
return dictionary;
};
static AblyCodecEncoder encodeTokenParams = ^NSMutableDictionary*(ARTTokenParams *const params) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary, TxTokenParams_ttl, [params ttl]);
WRITE_VALUE(dictionary, TxTokenParams_nonce, [params nonce]);
WRITE_VALUE(dictionary, TxTokenParams_clientId, [params clientId]);
WRITE_VALUE(dictionary, TxTokenParams_timestamp,
[params timestamp]?@((long)([[params timestamp] timeIntervalSince1970]*1000)):nil);
WRITE_VALUE(dictionary, TxTokenParams_capability, [params capability]);
return dictionary;
};
static AblyCodecEncoder encodeTokenRequest = ^NSMutableDictionary*(ARTTokenRequest *const tokenRequest) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary, TxTokenRequest_ttl, [tokenRequest ttl]);
WRITE_VALUE(dictionary, TxTokenRequest_nonce, [tokenRequest nonce]);
WRITE_VALUE(dictionary, TxTokenRequest_clientId, [tokenRequest clientId]);
WRITE_VALUE(dictionary, TxTokenRequest_timestamp,
[tokenRequest timestamp]?@((long)([[tokenRequest timestamp] timeIntervalSince1970]*1000)):nil);
WRITE_VALUE(dictionary, TxTokenRequest_capability, [tokenRequest capability]);
WRITE_VALUE(dictionary, TxTokenRequest_mac, [tokenRequest mac]);
WRITE_VALUE(dictionary, TxTokenRequest_keyName, [tokenRequest keyName]);
return dictionary;
};
static AblyCodecEncoder encodeTokenDetails = ^NSMutableDictionary*(ARTTokenDetails *const details) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary, TxTokenDetails_token, [details token]);
WRITE_VALUE(dictionary, TxTokenDetails_issued, [details issued]?@((long)([[details issued] timeIntervalSince1970]*1000)):nil);
WRITE_VALUE(dictionary, TxTokenDetails_expires, [details expires]?@((long)([[details expires] timeIntervalSince1970]*1000)):nil);
WRITE_VALUE(dictionary, TxTokenDetails_clientId, [details clientId]);
WRITE_VALUE(dictionary, TxTokenDetails_capability, [details capability]);
return dictionary;
};
static AblyCodecEncoder encodePaginatedResult = ^NSMutableDictionary*(ARTPaginatedResult *const result) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
NSArray* items = [result items];
if([items count] > 0){
UInt8 type = [AblyFlutterWriter getType:items[0]];
if(type != 0){
AblyCodecEncoder encoder = [AblyFlutterWriter getEncoder: [NSString stringWithFormat:@"%d", type]];
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[items count]];
[items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[result addObject: encoder(obj)];
}];
WRITE_VALUE(dictionary, TxPaginatedResult_type, [NSNumber numberWithInt:type]);
WRITE_VALUE(dictionary, TxPaginatedResult_items, result);
}
}
WRITE_VALUE(dictionary, TxPaginatedResult_hasNext, @([result hasNext]));
return dictionary;
};
@end