-
Notifications
You must be signed in to change notification settings - Fork 25
/
ARTPushChannel.m
324 lines (277 loc) · 11.5 KB
/
ARTPushChannel.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
#import "ARTPushChannel+Private.h"
#import "ARTHttp.h"
#import "ARTLog.h"
#import "ARTJsonLikeEncoder.h"
#import "ARTRest+Private.h"
#import "ARTClientOptions.h"
#import "ARTPaginatedResult+Private.h"
#import "ARTPushChannelSubscription.h"
#import "ARTChannel+Private.h"
#import "ARTLocalDevice+Private.h"
#import "ARTNSMutableRequest+ARTPush.h"
@implementation ARTPushChannel {
ARTQueuedDealloc *_dealloc;
}
- (instancetype)initWithInternal:(ARTPushChannelInternal *)internal queuedDealloc:(ARTQueuedDealloc *)dealloc {
self = [super init];
if (self) {
_internal = internal;
_dealloc = dealloc;
}
return self;
}
- (void)subscribeDevice {
[_internal subscribeDevice];
}
- (void)subscribeDevice:(ARTCallback)callback {
[_internal subscribeDevice:callback];
}
- (void)subscribeClient {
[_internal subscribeClient];
}
- (void)subscribeClient:(ARTCallback)callback {
[_internal subscribeClient:callback];
}
- (void)unsubscribeDevice {
[_internal unsubscribeDevice];
}
- (void)unsubscribeDevice:(ARTCallback)callback {
[_internal unsubscribeDevice:callback];
}
- (void)unsubscribeClient {
[_internal unsubscribeClient];
}
- (void)unsubscribeClient:(ARTCallback)callback {
[_internal unsubscribeClient:callback];
}
- (BOOL)listSubscriptions:(NSStringDictionary *)params
callback:(ARTPaginatedPushChannelCallback)callback
error:(NSError *_Nullable *_Nullable)errorPtr {
return [_internal listSubscriptions:params callback:callback error:errorPtr];
}
@end
const NSUInteger ARTDefaultLimit = 100;
@implementation ARTPushChannelInternal {
@private
dispatch_queue_t _queue;
dispatch_queue_t _userQueue;
@public
__weak ARTRestInternal *_rest; // weak because rest may own self and always outlives it
ARTLog *_logger;
__weak ARTChannel *_channel; // weak because channel owns self
}
- (instancetype)init:(ARTRestInternal *)rest withChannel:(ARTChannel *)channel {
if (self == [super self]) {
_rest = rest;
_queue = rest.queue;
_userQueue = rest.userQueue;
_logger = [rest logger];
_channel = channel;
_logger = channel.logger;
}
return self;
}
- (void)subscribeDevice {
[self subscribeDevice:nil];
}
- (void)unsubscribeDevice {
[self unsubscribeDevice:nil];
}
- (void)subscribeClient {
[self subscribeClient:nil];
}
- (void)unsubscribeClient {
[self unsubscribeClient:nil];
}
- (void)subscribeDevice:(ARTCallback)callback {
if (callback) {
ARTCallback userCallback = callback;
callback = ^(ARTErrorInfo *_Nullable error) {
dispatch_async(self->_userQueue, ^{
userCallback(error);
});
};
}
dispatch_async(_queue, ^{
ARTLocalDevice *device = [self getDevice:callback];
if (![device isRegistered]) {
return;
}
NSString *deviceId = device.id;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"/push/channelSubscriptions"]];
request.HTTPMethod = @"POST";
request.HTTPBody = [[self->_rest defaultEncoder] encode:@{
@"deviceId": deviceId,
@"channel": self->_channel.name,
} error:nil];
[request setValue:[[self->_rest defaultEncoder] mimeType] forHTTPHeaderField:@"Content-Type"];
[request setDeviceAuthentication:deviceId localDevice:device];
[self->_logger debug:__FILE__ line:__LINE__ message:@"subscribe notifications for device %@ in channel %@", deviceId, self->_channel.name];
[self->_rest executeRequest:request withAuthOption:ARTAuthenticationOn completion:^(NSHTTPURLResponse *response, NSData *data, NSError *error) {
if (error) {
[self->_logger error:@"%@: subscribe notifications for device %@ in channel %@ failed (%@)", NSStringFromClass(self.class), deviceId, self->_channel.name, error.localizedDescription];
}
if (callback) callback(error ? [ARTErrorInfo createFromNSError:error] : nil);
}];
});
}
- (void)subscribeClient:(ARTCallback)callback {
if (callback) {
ARTCallback userCallback = callback;
callback = ^(ARTErrorInfo *_Nullable error) {
dispatch_async(self->_userQueue, ^{
userCallback(error);
});
};
}
dispatch_async(_queue, ^{
NSString *clientId = [self getClientId:callback];
if (!clientId) {
return;
}
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"/push/channelSubscriptions"]];
request.HTTPMethod = @"POST";
request.HTTPBody = [[self->_rest defaultEncoder] encode:@{
@"clientId": clientId,
@"channel": self->_channel.name,
} error:nil];
[request setValue:[[self->_rest defaultEncoder] mimeType] forHTTPHeaderField:@"Content-Type"];
[self->_logger debug:__FILE__ line:__LINE__ message:@"subscribe notifications for clientId %@ in channel %@", clientId, self->_channel.name];
[self->_rest executeRequest:request withAuthOption:ARTAuthenticationOn completion:^(NSHTTPURLResponse *response, NSData *data, NSError *error) {
if (error) {
[self->_logger error:@"%@: subscribe notifications for clientId %@ in channel %@ failed (%@)", NSStringFromClass(self.class), clientId, self->_channel.name, error.localizedDescription];
}
if (callback) callback(error ? [ARTErrorInfo createFromNSError:error] : nil);
}];
});
}
- (void)unsubscribeDevice:(ARTCallback)callback {
if (callback) {
ARTCallback userCallback = callback;
callback = ^(ARTErrorInfo *_Nullable error) {
dispatch_async(self->_userQueue, ^{
userCallback(error);
});
};
}
dispatch_async(_queue, ^{
ARTLocalDevice *device = [self getDevice:callback];
if (![device isRegistered]) {
return;
}
NSString *deviceId = device.id;
NSURLComponents *components = [[NSURLComponents alloc] initWithURL:[NSURL URLWithString:@"/push/channelSubscriptions"] resolvingAgainstBaseURL:NO];
components.queryItems = @[
[NSURLQueryItem queryItemWithName:@"deviceId" value:deviceId],
[NSURLQueryItem queryItemWithName:@"channel" value:self->_channel.name],
];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[components URL]];
request.HTTPMethod = @"DELETE";
[request setDeviceAuthentication:deviceId localDevice:device];
[self->_logger debug:__FILE__ line:__LINE__ message:@"unsubscribe notifications for device %@ in channel %@", deviceId, self->_channel.name];
[self->_rest executeRequest:request withAuthOption:ARTAuthenticationOn completion:^(NSHTTPURLResponse *response, NSData *data, NSError *error) {
if (error) {
[self->_logger error:@"%@: unsubscribe notifications for device %@ in channel %@ failed (%@)", NSStringFromClass(self.class), deviceId, self->_channel.name, error.localizedDescription];
}
if (callback) callback(error ? [ARTErrorInfo createFromNSError:error] : nil);
}];
});
}
- (void)unsubscribeClient:(ARTCallback)callback {
if (callback) {
ARTCallback userCallback = callback;
callback = ^(ARTErrorInfo *_Nullable error) {
dispatch_async(self->_userQueue, ^{
userCallback(error);
});
};
}
dispatch_async(_queue, ^{
NSString *clientId = [self getClientId:callback];
if (!clientId) {
return;
}
NSURLComponents *components = [[NSURLComponents alloc] initWithURL:[NSURL URLWithString:@"/push/channelSubscriptions"] resolvingAgainstBaseURL:NO];
components.queryItems = @[
[NSURLQueryItem queryItemWithName:@"clientId" value:clientId],
[NSURLQueryItem queryItemWithName:@"channel" value:self->_channel.name],
];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[components URL]];
request.HTTPMethod = @"DELETE";
[self->_logger debug:__FILE__ line:__LINE__ message:@"unsubscribe notifications for clientId %@ in channel %@", clientId, self->_channel.name];
[self->_rest executeRequest:request withAuthOption:ARTAuthenticationOn completion:^(NSHTTPURLResponse *response, NSData *data, NSError *error) {
if (error) {
[self->_logger error:@"%@: unsubscribe notifications for clientId %@ in channel %@ failed (%@)", NSStringFromClass(self.class), clientId, self->_channel.name, error.localizedDescription];
}
if (callback) callback(error ? [ARTErrorInfo createFromNSError:error] : nil);
}];
});
}
- (BOOL)listSubscriptions:(NSStringDictionary *)params
callback:(ARTPaginatedPushChannelCallback)callback
error:(NSError * __autoreleasing *)errorPtr {
if (callback) {
ARTPaginatedPushChannelCallback userCallback = callback;
callback = ^(ARTPaginatedResult<ARTPushChannelSubscription *> *result, ARTErrorInfo *error) {
dispatch_async(self->_userQueue, ^{
userCallback(result, error);
});
};
}
__block BOOL ret;
dispatch_sync(_queue, ^{
NSMutableDictionary<NSString *, NSString *> *mutableParams = params ? [NSMutableDictionary dictionaryWithDictionary:params] : [[NSMutableDictionary alloc] init];
if (!mutableParams[@"deviceId"] && !mutableParams[@"clientId"]) {
if (errorPtr) {
*errorPtr = [NSError errorWithDomain:ARTAblyErrorDomain
code:ARTDataQueryErrorMissingRequiredFields
userInfo:@{NSLocalizedDescriptionKey:@"cannot list subscriptions with null device ID or null client ID"}];
}
ret = NO;
return;
}
if (mutableParams[@"deviceId"] && mutableParams[@"clientId"]) {
if (errorPtr) {
*errorPtr = [NSError errorWithDomain:ARTAblyErrorDomain
code:ARTDataQueryErrorInvalidParameters
userInfo:@{NSLocalizedDescriptionKey:@"cannot list subscriptions with device ID and client ID"}];
}
ret = NO;
return;
}
mutableParams[@"concatFilters"] = @"true";
NSURLComponents *components = [[NSURLComponents alloc] initWithURL:[NSURL URLWithString:@"/push/channelSubscriptions"] resolvingAgainstBaseURL:NO];
components.queryItems = [mutableParams art_asURLQueryItems];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[components URL]];
request.HTTPMethod = @"GET";
ARTPaginatedResultResponseProcessor responseProcessor = ^(NSHTTPURLResponse *response, NSData *data, NSError **error) {
return [self->_rest.encoders[response.MIMEType] decodePushChannelSubscriptions:data error:error];
};
[ARTPaginatedResult executePaginated:self->_rest withRequest:request andResponseProcessor:responseProcessor callback:callback];
ret = YES;
});
return ret;
}
- (ARTLocalDevice *)getDevice:(ARTCallback)callback {
#if TARGET_OS_IOS
ARTLocalDevice *device = [_rest device_nosync];
#else
ARTLocalDevice *device = nil;
#endif
if (![device isRegistered]) {
if (callback) callback([ARTErrorInfo createWithCode:0 message:@"cannot use device before device activation has finished"]);
}
return device;
}
- (NSString *)getClientId:(ARTCallback)callback {
ARTLocalDevice *device = [self getDevice:callback];
if (![device isRegistered]) {
return nil;
}
if (!device.clientId) {
if (callback) callback([ARTErrorInfo createWithCode:0 message:@"cannot subscribe/unsubscribe with null client ID"]);
return nil;
}
return device.clientId;
}
@end