2
* Copyright 2012 Facebook
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
#import "FBFrictionlessRequestSettings.h"
20
///////////////////////////////////////////////////////////////////////////////////////////////////
23
@interface FBFrictionlessRequestSettings ()
25
@property (readwrite, retain) NSArray * allowedRecipients;
26
@property (readwrite, retain) FBRequest* activeRequest;
30
///////////////////////////////////////////////////////////////////////////////////////////////////
32
@implementation FBFrictionlessRequestSettings
34
///////////////////////////////////////////////////////////////////////////////////////////////////
37
@synthesize enabled = _enabled;
40
if (self = [super init]) {
41
// start life with an empty frictionless cache
42
self.allowedRecipients = [[[NSArray alloc] init] autorelease];
47
- (void)enableWithFacebook:(Facebook*)facebook {
50
[self reloadRecipientCacheWithFacebook:facebook];
54
- (void)reloadRecipientCacheWithFacebook:(Facebook *)facebook {
55
// request the list of frictionless recipients from the server
56
id request = [facebook requestWithGraphPath:@"me/apprequestformerrecipients"
59
self.activeRequest = request;
63
- (void)updateRecipientCacheWithRecipients:(NSArray*)ids {
64
// if setting recipients directly, no need to complete pending request
65
self.activeRequest = nil;
68
self.allowedRecipients = [[[NSArray alloc] init] autorelease];
70
self.allowedRecipients = [[[NSArray alloc] initWithArray:ids] autorelease];
74
- (BOOL)isFrictionlessEnabledForRecipient:(NSString *)fbid {
75
// trim whitespace from edges
76
fbid = [fbid stringByTrimmingCharactersInSet:
77
[NSCharacterSet whitespaceCharacterSet]];
79
// linear search through cache for a match
80
for (NSString *entry in self.allowedRecipients) {
81
if ([entry isEqualToString:fbid]) {
88
- (BOOL)isFrictionlessEnabledForRecipients:(NSArray*)fbids {
89
// we handle arrays of NSString and NSNumber, and throw on anything else
90
for (id fbid in fbids) {
92
// give us a number, and we convert it to a string
93
if ([fbid isKindOfClass:[NSNumber class]]) {
94
fbidstr = [(NSNumber*)fbid stringValue];
95
} else if ([fbid isKindOfClass:[NSString class]]) {
96
// or give us a string, and we just use it as is
97
fbidstr = (NSString*)fbid;
99
// unexpected type found in the array of fbids
100
@throw [NSException exceptionWithName:NSInvalidArgumentException
101
reason:@"items in fbids must be NSString or NSNumber"
102
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
103
[fbid class], @"invalid class",
107
// if we miss our cache once, we fail the set
108
if (![self isFrictionlessEnabledForRecipient:fbidstr]) {
115
///////////////////////////////////////////////////////////////////////////////////////////////////
118
- (void)request:(FBRequest *)request
121
// a little request bookkeeping
122
self.activeRequest = nil;
124
int items = [[result objectForKey: @"data"] count];
125
NSMutableArray* recipients = [[[NSMutableArray alloc] initWithCapacity: items] autorelease];
127
for (int i = 0; i < items; i++) {
128
[recipients addObject: [[[result objectForKey: @"data"]
130
objectForKey: @"recipient_id"]] ;
133
self.allowedRecipients = recipients;
136
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
137
// if the request to load the frictionless recipients fails, proceed without updating
138
// the recipients cache; the cache may become invalid due to a failed update or other reasons
139
// (e.g. simultaneous use of the same app from multiple devices), in the case of an invalid
140
// cache, a request dialog may either appear a moment later than it usually would, or appear
141
// briefly when it should not appear at all; in either case the correct request behavior
142
// occurs, and the cache is updated
143
self.activeRequest = nil;
146
///////////////////////////////////////////////////////////////////////////////////////////////////
150
self.activeRequest = nil;
151
self.allowedRecipients = nil;
155
///////////////////////////////////////////////////////////////////////////////////////////////////
159
@synthesize allowedRecipients = _allowedRecipients;
160
@synthesize activeRequest = _activeRequest;