forked from Awful/Awful.app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAwfulUser.m
111 lines (100 loc) · 4.12 KB
/
AwfulUser.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
//
// AwfulUser.m
// Awful
//
// Created by Nolan Waite on 12-12-28.
// Copyright (c) 2012 Regular Berry Software LLC. All rights reserved.
//
#import "AwfulUser.h"
#import "AwfulDataStack.h"
#import "AwfulParsing.h"
#import "NSManagedObject+Awful.h"
#import "TFHpple.h"
@implementation AwfulUser
- (NSURL *)avatarURL
{
if ([self.customTitle length] == 0) return nil;
NSData *data = [self.customTitle dataUsingEncoding:NSUTF8StringEncoding];
TFHpple *html = [[TFHpple alloc] initWithHTMLData:data];
// The avatar is an image that's the first child of its parent, which is either a <div>, an
// <a>, or the implied <body>.
TFHppleElement *avatar = [html searchForSingle:@"//img[count(preceding-sibling::*) = 0 and (parent::div or parent::body or parent::a)]"];
NSString *src = [avatar objectForKey:@"src"];
if ([src length] == 0) return nil;
return [NSURL URLWithString:src];
}
+ (NSSet *)keyPathsForValuesAffectingAvatarURL
{
return [NSSet setWithObject:@"customTitle"];
}
+ (instancetype)userCreatedOrUpdatedFromProfileInfo:(ProfileParsedInfo *)profileInfo
{
AwfulUser *user = [self firstMatchingPredicate:@"userID = %@", profileInfo.userID];
if (!user) user = [AwfulUser insertNew];
[profileInfo applyToObject:user];
user.homepageURL = [profileInfo.homepage absoluteString];
user.profilePictureURL = [profileInfo.profilePicture absoluteString];
[[AwfulDataStack sharedDataStack] save];
return user;
}
+ (instancetype)userCreatedOrUpdatedFromJSON:(NSDictionary *)json
{
NSString *userID = [json[@"userid"] stringValue];
if (!userID) return nil;
AwfulUser *user = [self firstMatchingPredicate:@"userID = %@", userID];
if (!user) {
user = [self insertNew];
user.userID = userID;
}
user.username = Stringify(json[@"username"]);
// Everything else is optional.
if (json[@"aim"]) user.aimName = StringOrNilIfEmpty(json[@"aim"]);
if (json[@"biography"]) user.aboutMe = StringOrNilIfEmpty(json[@"biography"]);
if (json[@"gender"]) {
if ([json[@"gender"] isEqual:@"F"]) user.gender = @"female";
else if ([json[@"gender"] isEqual:@"M"]) user.gender = @"male";
else user.gender = @"porpoise";
}
if (json[@"homepage"]) user.homepageURL = StringOrNilIfEmpty(json[@"homepage"]);
if (json[@"icq"]) user.icqName = StringOrNilIfEmpty(json[@"icq"]);
if (json[@"interests"]) user.interests = StringOrNilIfEmpty(json[@"interests"]);
if (json[@"joindate"]) {
user.regdate = [NSDate dateWithTimeIntervalSince1970:[json[@"joindate"] doubleValue]];
}
if (json[@"lastpost"]) {
user.lastPost = [NSDate dateWithTimeIntervalSince1970:[json[@"lastpost"] doubleValue]];
}
if (json[@"location"]) user.location = StringOrNilIfEmpty(json[@"location"]);
if (json[@"occupation"]) user.occupation = StringOrNilIfEmpty(json[@"occupation"]);
if (json[@"picture"]) {
NSString *url = StringOrNilIfEmpty(json[@"picture"]);
if (![[NSURL URLWithString:url] host]) {
url = [NSString stringWithFormat:@"http://forums.somethingawful.com%@", url];
}
user.profilePictureURL = url;
}
if (json[@"posts"]) user.postCount = json[@"posts"];
if (json[@"postsperday"]) user.postRate = [json[@"postsperday"] stringValue];
if (json[@"role"]) {
user.administratorValue = [json[@"role"] isEqual:@"A"];
user.moderatorValue = [json[@"role"] isEqual:@"M"];
}
if (json[@"usertitle"]) user.customTitle = json[@"usertitle"];
if (json[@"yahoo"]) user.yahooName = StringOrNilIfEmpty(json[@"yahoo"]);
[[AwfulDataStack sharedDataStack] save];
return user;
}
static id StringOrNilIfEmpty(const id obj)
{
if (!obj || [obj isEqual:[NSNull null]]) return nil;
if ([obj respondsToSelector:@selector(length)] && [obj length] == 0) return nil;
return Stringify(obj);
}
static NSString * Stringify(const id obj)
{
if (!obj || [obj isEqual:[NSNull null]]) return obj;
if ([obj isKindOfClass:[NSString class]]) return obj;
if ([obj respondsToSelector:@selector(stringValue)]) return [obj stringValue];
return [obj description];
}
@end