WLUserManager.m 15.6 KB
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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
/*
 Copyright 2010-2016 Warply Inc. All rights reserved.
 
 Redistribution and use in source and binary forms, without modification,
 are permitted provided that the following conditions are met:
 
 1. Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.
 
 2. Redistributions in binaryform must reproduce the above copyright notice,
 this list of conditions and the following disclaimer in the documentation
 and/or other materials provided with the distribution.
 
 THIS SOFTWARE IS PROVIDED BY THE WARPLY LTD ``AS IS'' AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 EVENT SHALL WARPLY LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#import "WLUserManager.h"
#import "WLGlobals.h"
#import "WLEvent.h"
#import <AdSupport/AdSupport.h>

@implementation WLUserManager

#pragma mark - User Data
///////////////////////////////////////////////////////////////////////////////
+ (void)getNameWithSuccessBlock:(void (^)(NSString *userName))success
                       failureBlock:(void (^)(NSError *error))failure
{
    if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_CONSUMER_DATA_ENABLED)) {
        return;
    }

    [[Warply sharedService] getContextWithPath:@"consumer_data" successBlock:^(NSDictionary *dict){
        if (success)
            success([dict valueForKey:@"user_name"]);
    } failureBlock:^(NSError *error) {
        if (failure)
            failure(error);
    }];
}

///////////////////////////////////////////////////////////////////////////////
+ (void)getEmailWithSuccessBlock:(void (^)(NSString *userEmail))success
                        failureBlock:(void (^)(NSError *error))failure
{
    if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_CONSUMER_DATA_ENABLED)) {
        return;
    }

    [[Warply sharedService] getContextWithPath:@"consumer_data" successBlock:^(NSDictionary *dict){
        if (success)
            success([dict valueForKey:@"user_email"]);
    } failureBlock:^(NSError *error) {
        if (failure)
            failure(error);
    }];
}

///////////////////////////////////////////////////////////////////////////////
+ (void)getMsisdnWithSuccessBlock:(void (^)(NSString *userMsisdn))success
                         failureBlock:(void (^)(NSError *error))failure
{
    if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_CONSUMER_DATA_ENABLED)) {
        return;
    }
    
    [[Warply sharedService] getContextWithPath:@"consumer_data" successBlock:^(NSDictionary *dict){
        if (success)
            success([dict valueForKey:@"msisdn"]);
    } failureBlock:^(NSError *error) {
        if (failure)
            failure(error);
    }];
}

+ (void)getCustomDataWithSuccessBlock:(void (^)(NSDictionary *customData))success
                         failureBlock:(void (^)(NSError *error))failure
{
    if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_CONSUMER_DATA_ENABLED)) {
        return;
    }
    
    [[Warply sharedService] getContextWithPath:@"consumer_data" successBlock:^(NSDictionary *dict){
        if (success)
            success([dict valueForKey:@"custom_data"]);
    } failureBlock:^(NSError *error) {
        if (failure)
            failure(error);
    }];
}

///////////////////////////////////////////////////////////////////////////////
+ (void)sendName:(NSString *)userName
{
    [self sendLoginFieldWithName:@"user_name" andValue:userName];
}

///////////////////////////////////////////////////////////////////////////////
+ (void)sendEmail:(NSString *)userEmail
{
    [self sendLoginFieldWithName:@"user_email" andValue:userEmail];
}

///////////////////////////////////////////////////////////////////////////////
+ (void)sendMsisdn:(NSString *)userMsisdn
{
    [self sendLoginFieldWithName:@"msisdn" andValue:userMsisdn];
}

///////////////////////////////////////////////////////////////////////////////
+ (void)sendLoginFieldWithName:(NSString *)key andValue:(NSString *)value
{
    if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_CONSUMER_DATA_ENABLED)) {
        return;
    }
    
    NSMutableDictionary *login_data = [NSMutableDictionary dictionaryWithCapacity:1];
    if ([value length] > 0)
        [login_data setValue:value forKey:key];
    
    NSDictionary *eventDict = [NSDictionary dictionaryWithObjectsAndKeys:login_data, @"login_data", nil];
    WLEventSimple *event = [WLEventSimple eventWithType:@"login_data" andContext:eventDict];
    [[Warply sharedService] addEvent:event priority:YES];
}

///////////////////////////////////////////////////////////////////////////////
+ (void)sendCustomData:(NSDictionary *)customData
{
    if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_CONSUMER_DATA_ENABLED)) {
        return;
    }
    
    NSMutableDictionary *custom_data = [NSMutableDictionary dictionaryWithCapacity:1];
    if (customData != nil)
        [custom_data setValue:customData forKey:@"custom_data"];
    
    NSDictionary *eventDict = [NSDictionary dictionaryWithObjectsAndKeys:custom_data, @"consumer_data", nil];
    WLEventSimple *event = [WLEventSimple eventWithType:@"consumer_data" andContext:eventDict];
    [[Warply sharedService] addEvent:event priority:YES];
}

#pragma mark - UUIDs
///////////////////////////////////////////////////////////////////////////////
+ (void)sendUUIDS
{
    if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_CONSUMER_DATA_ENABLED)) {
        return;
    }
    
    if (SYSTEM_VERSION_LESS_THAN(@"6.0"))
        return;
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL uuidsAlreadySentNum = [defaults boolForKey:@"uuidsAlreadySent"];
    if (uuidsAlreadySentNum)
        return;
    
    NSMutableDictionary *apple_uuids = [NSMutableDictionary dictionaryWithCapacity:2];
    [apple_uuids setValue:[NSNumber numberWithBool:[ASIdentifierManager sharedManager].advertisingTrackingEnabled] forKey:@"advertising_tracking_enabled"];
    [apple_uuids setValue:[[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString] forKey:@"advertising_identifier"];
    [apple_uuids setValue:[[[UIDevice currentDevice] identifierForVendor] UUIDString] forKey:@"identifier_for_vendor"];
    NSDictionary *login_data = [NSDictionary dictionaryWithObjectsAndKeys:apple_uuids, @"ios_uuids", nil];
    NSDictionary *eventDict = [NSDictionary dictionaryWithObjectsAndKeys:login_data, @"login_data", nil];
    WLEventSimple *event = [WLEventSimple eventWithType:@"login_data" andContext:eventDict];
    [[Warply sharedService] addEvent:event priority:NO];
    [defaults setBool:YES forKey:@"uuidsAlreadySent"];
    [defaults synchronize];
}

#pragma mark - User Tagging
///////////////////////////////////////////////////////////////////////////////
+ (void)addTag:(NSString *)tag
{
    [self addTags:[NSArray arrayWithObject:tag]];
}

///////////////////////////////////////////////////////////////////////////////
+ (void)addTags:(NSArray *)tags
{
    if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_USER_TAGGING_ENABLED)) {
        return;
    }
    //Checking for items that are not NSString type.
#ifdef DEBUG
    for (id tag in tags) {
        NSAssert([tag isKindOfClass:[NSString class]], @"Array with only NSString objects is allowed in addTags:");
    }
#endif
    
    NSDictionary *eventContext = @{@"tags": @{@"action": @"add_tags", @"tags":tags}};
    WLEventSimple *event = [[WLEventSimple alloc] initWithType:@"tags" andContext:eventContext];
    [[Warply sharedService] addEvent:event priority:YES];
    
}

///////////////////////////////////////////////////////////////////////////////
+ (void)removeTag:(NSString *)tag
{
    [self removeTags:[NSArray arrayWithObject:tag]];
}

///////////////////////////////////////////////////////////////////////////////
+ (void)removeTags:(NSArray *)tags
{
    if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_USER_TAGGING_ENABLED)) {
        return;
    }

    NSDictionary *eventContext = @{@"tags": @{@"action": @"remove_tags", @"tags":tags}};
    //Checking for items that are not NSString type.
#ifdef DEBUG
    for (id tag in tags) {
        NSAssert([tag isKindOfClass:[NSString class]], @"Array with only NSString objects is allowed in removeTags:");
    }
#endif
    WLEventSimple *event = [[WLEventSimple alloc] initWithType:@"tags" andContext:eventContext];
    [[Warply sharedService] addEvent:event priority:YES];
    
}

///////////////////////////////////////////////////////////////////////////////
+ (void)removeAllTags
{
    if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_USER_TAGGING_ENABLED)) {
        return;
    }
    NSDictionary *eventContext = @{@"tags": @{@"action": @"remove_all_tags"}};
    //Checking for items that are not NSString type.
    WLEventSimple *event = [[WLEventSimple alloc] initWithType:@"tags" andContext:eventContext];
    [[Warply sharedService] addEvent:event priority:YES];
    
}

///////////////////////////////////////////////////////////////////////////////
+ (void)rewriteTags:(NSArray *)tags
{
    if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_USER_TAGGING_ENABLED)) {
        return;
    }
    //Checking for items that are not NSString type.
#ifdef DEBUG
    for (id tag in tags) {
        NSAssert([tag isKindOfClass:[NSString class]], @"Array with only NSString objects is allowed in addTags:");
    }
#endif
    
    NSDictionary *eventContext = @{@"tags": @{@"action": @"rewrite_tags", @"tags":tags}};
    WLEventSimple *event = [[WLEventSimple alloc] initWithType:@"tags" andContext:eventContext];
    [[Warply sharedService] addEvent:event priority:YES];
    
}

///////////////////////////////////////////////////////////////////////////////
+ (void)getTagsWithSuccessBlock:(void (^)(NSArray *tags))success failureBlock:(void (^)(NSError *error))failure
{
    if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_USER_TAGGING_ENABLED)) {
        return;
    }
    
    [[Warply sharedService] getContextWithPath:@"tags"
                                        successBlock:^(NSArray *tags) {
                                            if (success) {
                                                success(tags);
                                            }
                                        } failureBlock:^(NSError *error) {
                                            if (failure) {
                                                failure(error);
                                            }
                                        }];
}

#pragma mark - User Session
///////////////////////////////////////////////////////////////////////////////
+ (void)sessionLogin:(NSString *)authToken
        successBlock:(void (^)(void))success
        failureBlock:(void (^)(void))failure
{
    if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_USER_SESSION_ENABLED)) {
        return;
    }
    
    if (authToken.length == 0) {
        if (failure) {
            WLLOG(@"Auth token should not be an empty string or nil;");
            failure();
        }
    }
    NSDictionary *userSessionDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSDictionary dictionaryWithObject:authToken forKey:@"auth_token"], @"login", nil];
    NSDictionary *postDictionary = [NSDictionary dictionaryWithObjectsAndKeys:userSessionDict, @"user_session", nil];
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDictionary options:0 error:&error];
    if (error) {
        WLLOG(@"Invalid auth token");
        if (failure) {
            failure();
        }
        return;
    }
    [[Warply sharedService] sendContext:jsonData successBlock:^(NSDictionary *responseDict) {
        if (success)
            success();
    } failureBlock:^(NSError *error) {
        [self sessionRegister:authToken successBlock:success failureBlock:failure];
        ;
    }];
}

///////////////////////////////////////////////////////////////////////////////
+ (void)sessionLogout:(NSString *)authToken
         successBlock:(void (^)(void))success
         failureBlock:(void (^)(void))failure
{
    if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_USER_SESSION_ENABLED)) {
        return;
    }
    
    if (authToken.length == 0) {
        if (failure) {
            WLLOG(@"Auth token cannot be nil or empty string.");
            failure();
        }
        return;
    }
    
    NSDictionary *userSessionDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSDictionary dictionaryWithObject:authToken forKey:@"auth_token"], @"logout", nil];
    NSDictionary *postDictionary = [NSDictionary dictionaryWithObjectsAndKeys:userSessionDict, @"user_session", nil];
    
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDictionary options:0 error:&error];
    if (error != nil) {
        WLLOG(@"Invalid auth token");
        return;
    }
    [[Warply sharedService] sendContext:jsonData successBlock:^(NSDictionary *responseDict){
        if (success)
            success();
    } failureBlock:^(NSError *error){
        if (failure)
            failure();
    }];
}

///////////////////////////////////////////////////////////////////////////////
+ (void)sessionRegister:(NSString *)authToken
           successBlock:(void (^)(void))success
           failureBlock:(void (^)(void))failure
{
    if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_USER_SESSION_ENABLED)) {
        return;
    }
    
    if (authToken.length == 0) {
        if (failure) {
            WLLOG(@"Auth token cannot be nil or empty string.");
            failure();
        }
        return;
    }
    
    NSDictionary *userSessionDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSDictionary dictionaryWithObject:authToken forKey:@"auth_token"], @"register", nil];
    NSDictionary *postDictionary = @{@"user_session": userSessionDict};
    
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDictionary options:0 error:&error];
    
    if (error != nil) {
        WLLOG(@"Invalid auth token");
        if (failure) {
            failure();
        }
        return;
    }

    [[Warply sharedService] sendContext:jsonData successBlock:^(NSDictionary *responseDict){
        if (success)
            success();
    } failureBlock:^(NSError *error) {
        if (failure)
            failure();
    }];
}

#pragma mark - Push Notifications Participation
///////////////////////////////////////////////////////////////////////////////
+ (void)setWarplyNotificationsEnabled:(BOOL)enable successBlock:(void (^)(void))success failureBlock:(void (^)(NSError *error))failure
{
    NSDictionary *postDictionary = @{WL_APPLICATION_DATA:@{WL_WARP_NOTIFICATIONS_ENABLED:[NSNumber numberWithBool:enable]}};
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDictionary options:0 error:NULL];

    [[Warply sharedService] sendContext:jsonData successBlock:^(NSDictionary *contextResponse) {
        if (success) {
            success();
        }
    } failureBlock:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

///////////////////////////////////////////////////////////////////////////////
+ (void)warplyNotificationsEnabledWithsuccessBlock:(void (^)(BOOL isEnabled))success failureBlock:(void (^)(NSError *error))failure;
{
    [[Warply sharedService] getContextWithPath:WL_DEVICE_STATUS successBlock:^(id contextResponse) {
        BOOL isEnabled = [[contextResponse valueForKey:WL_WARP_NOTIFICATIONS_ENABLED] boolValue];
        if (success) {
            success(isEnabled);
        }
    } failureBlock:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    return;
}

@end