summaryrefslogtreecommitdiff
path: root/Juick/Model/Message.m
blob: a7934ac60ce7aeda34bfe2c80abc67cc8c560af9 (plain) (blame)
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
//
//  Message.m
//  Juick
//
//  Created by Vitaly Takmazov on 29.10.13.
//  Copyright (c) 2013 com.juick. All rights reserved.
//

#import "Message.h"
#import "PDKeychainBindings.h"
#import "AppDelegate.h"

@implementation Message

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"text" : @"body",
             @"mid" : @"mid",
             @"rid" : @"rid",
             @"attach" : @"photo.small",
             @"repliesCount": @"replies",
             @"repliesBy": @"repliesby"};
}


+(void) pullNextFromPath:(NSString *)path  params:(NSDictionary *) params callback:(void(^)(NSArray *))callback {
    AFHTTPSessionManager *manager = [APIClient sharedClient].manager;
    if ([[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] != nil) {
        [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]];
    }
    [manager GET:path parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSMutableArray *messages = [NSMutableArray new];
        [((NSArray *)responseObject) enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [messages addObject:[Message yy_modelWithJSON:obj]];
        }];
        callback(messages);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"REST Error: %@", error);
        NSInteger statusCode = ((NSHTTPURLResponse *)task.response).statusCode;
        if (statusCode == 401) {
            AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [appDelegate pushLoginForm];
        }
    }];
}

+(NSString *) messagesUrl {
    return @"https://api.juick.com/messages";
}

+(NSString *) threadUrl {
    return @"https://api.juick.com/thread";
}

+(NSString *) feedUrl {
    return @"https://api.juick.com/home";
}

@end