~jose-exposito89/ubuntuone-ios-files/social-networking

« back to all changes in this revision

Viewing changes to Dependencies/ShareKit/Submodules/facebook-ios-sdk/src/FBFrictionlessRequestSettings.m

  • Committer: José Expósito
  • Date: 2012-10-23 15:22:36 UTC
  • Revision ID: jose.exposito89@gmail.com-20121023152236-f2rfafq000ex0vgz
Added support to share links in social networks

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2012 Facebook
 
3
 *
 
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
 
7
 *
 
8
 *    http://www.apache.org/licenses/LICENSE-2.0
 
9
 
 
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.
 
15
 */
 
16
 
 
17
#import "Facebook.h"
 
18
#import "FBFrictionlessRequestSettings.h"
 
19
 
 
20
///////////////////////////////////////////////////////////////////////////////////////////////////
 
21
// private interface
 
22
//
 
23
@interface FBFrictionlessRequestSettings ()
 
24
 
 
25
@property (readwrite, retain) NSArray *     allowedRecipients;
 
26
@property (readwrite, retain) FBRequest*    activeRequest;
 
27
 
 
28
@end
 
29
 
 
30
///////////////////////////////////////////////////////////////////////////////////////////////////
 
31
 
 
32
@implementation FBFrictionlessRequestSettings 
 
33
 
 
34
///////////////////////////////////////////////////////////////////////////////////////////////////
 
35
// public
 
36
 
 
37
@synthesize enabled = _enabled;
 
38
 
 
39
- (id)init {
 
40
    if (self = [super init]) {
 
41
        // start life with an empty frictionless cache
 
42
        self.allowedRecipients = [[[NSArray alloc] init] autorelease];
 
43
    }
 
44
    return self;
 
45
}
 
46
 
 
47
- (void)enableWithFacebook:(Facebook*)facebook {
 
48
    if (!_enabled) {
 
49
        _enabled = true;
 
50
        [self reloadRecipientCacheWithFacebook:facebook];
 
51
    }
 
52
}
 
53
 
 
54
- (void)reloadRecipientCacheWithFacebook:(Facebook *)facebook {
 
55
    // request the list of frictionless recipients from the server
 
56
    id request = [facebook requestWithGraphPath:@"me/apprequestformerrecipients"
 
57
                                    andDelegate:self];
 
58
    if (request) {
 
59
        self.activeRequest = request;
 
60
    }    
 
61
}
 
62
 
 
63
- (void)updateRecipientCacheWithRecipients:(NSArray*)ids {
 
64
    // if setting recipients directly, no need to complete pending request
 
65
    self.activeRequest = nil;
 
66
    
 
67
    if (ids == nil) {
 
68
        self.allowedRecipients = [[[NSArray alloc] init] autorelease];
 
69
    } else {
 
70
        self.allowedRecipients = [[[NSArray alloc] initWithArray:ids] autorelease];
 
71
    }
 
72
}
 
73
 
 
74
- (BOOL)isFrictionlessEnabledForRecipient:(NSString *)fbid {
 
75
    // trim whitespace from edges
 
76
    fbid = [fbid stringByTrimmingCharactersInSet:
 
77
                             [NSCharacterSet whitespaceCharacterSet]];
 
78
 
 
79
    // linear search through cache for a match
 
80
    for (NSString *entry in self.allowedRecipients) {
 
81
        if ([entry isEqualToString:fbid]) {
 
82
            return YES;
 
83
        }
 
84
    }
 
85
    return NO;
 
86
}
 
87
 
 
88
- (BOOL)isFrictionlessEnabledForRecipients:(NSArray*)fbids {
 
89
    // we handle arrays of NSString and NSNumber, and throw on anything else
 
90
    for (id fbid in fbids) {
 
91
        NSString* fbidstr;
 
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;
 
98
        } else {
 
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", 
 
104
                                                   nil]];
 
105
        }
 
106
        
 
107
        // if we miss our cache once, we fail the set
 
108
        if (![self isFrictionlessEnabledForRecipient:fbidstr]) {
 
109
            return NO;
 
110
        }
 
111
    }
 
112
    return YES;
 
113
}
 
114
 
 
115
///////////////////////////////////////////////////////////////////////////////////////////////////
 
116
// FBRequestDelegate
 
117
 
 
118
- (void)request:(FBRequest *)request
 
119
        didLoad:(id)result {
 
120
 
 
121
    // a little request bookkeeping
 
122
    self.activeRequest = nil;
 
123
 
 
124
    int items = [[result objectForKey: @"data"] count];
 
125
    NSMutableArray* recipients = [[[NSMutableArray alloc] initWithCapacity: items] autorelease];
 
126
        
 
127
    for (int i = 0; i < items; i++) {
 
128
        [recipients addObject: [[[result objectForKey: @"data"] 
 
129
                                 objectAtIndex: i] 
 
130
                                objectForKey: @"recipient_id"]] ;
 
131
    }
 
132
        
 
133
    self.allowedRecipients = recipients;        
 
134
}
 
135
 
 
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;
 
144
}
 
145
 
 
146
///////////////////////////////////////////////////////////////////////////////////////////////////
 
147
// NSObject
 
148
 
 
149
- (void)dealloc {    
 
150
    self.activeRequest = nil;
 
151
    self.allowedRecipients = nil;
 
152
    [super dealloc];
 
153
}
 
154
 
 
155
///////////////////////////////////////////////////////////////////////////////////////////////////
 
156
// private helpers
 
157
// 
 
158
 
 
159
@synthesize allowedRecipients = _allowedRecipients;
 
160
@synthesize activeRequest = _activeRequest;
 
161
 
 
162
@end