forked from Awful/Awful.app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAwfulThread.m
188 lines (170 loc) · 7.01 KB
/
AwfulThread.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
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
//
// AwfulThread.m
// Awful
//
// Created by Nolan Waite on 12-05-17.
// Copyright (c) 2012 Regular Berry Software LLC. All rights reserved.
//
#import "AwfulThread.h"
#import "AwfulDataStack.h"
#import "AwfulParsing.h"
#import "AwfulUser.h"
#import "GTMNSString+HTML.h"
#import "NSManagedObject+Awful.h"
@implementation AwfulThread
- (NSString *)firstIconName
{
NSString *basename = [[self.threadIconImageURL lastPathComponent]
stringByDeletingPathExtension];
return [basename stringByAppendingPathExtension:@"png"];
}
- (NSString *)secondIconName
{
NSString *basename = [[self.threadIconImageURL2 lastPathComponent]
stringByDeletingPathExtension];
return [basename stringByAppendingPathExtension:@"png"];
}
- (BOOL)beenSeen
{
return self.seenPostsValue > 0;
}
+ (NSSet *)keyPathsForValuesAffectingBeenSeen
{
return [NSSet setWithObject:@"seenPosts"];
}
+ (NSArray *)threadsCreatedOrUpdatedWithParsedInfo:(NSArray *)threadInfos
{
NSMutableDictionary *existingThreads = [NSMutableDictionary new];
NSArray *threadIDs = [threadInfos valueForKey:@"threadID"];
for (AwfulThread *thread in [self fetchAllMatchingPredicate:@"threadID IN %@", threadIDs]) {
existingThreads[thread.threadID] = thread;
}
NSMutableDictionary *existingUsers = [NSMutableDictionary new];
NSArray *usernames = [threadInfos valueForKeyPath:@"author.username"];
for (AwfulUser *user in [AwfulUser fetchAllMatchingPredicate:@"username IN %@", usernames]) {
existingUsers[user.username] = user;
}
for (ThreadParsedInfo *info in threadInfos) {
if ([info.threadID length] == 0) {
NSLog(@"ignoring ID-less thread (announcement?)");
continue;
}
AwfulThread *thread = existingThreads[info.threadID] ?: [AwfulThread insertNew];
[info applyToObject:thread];
if (!thread.author) thread.author = [AwfulUser insertNew];
[info.author applyToObject:thread.author];
existingUsers[thread.author.username] = thread.author;
existingThreads[thread.threadID] = thread;
}
[[AwfulDataStack sharedDataStack] save];
return [existingThreads allValues];
}
+ (NSArray *)threadsCreatedOrUpdatedWithJSON:(NSDictionary *)json
{
NSMutableDictionary *existingThreads = [NSMutableDictionary new];
NSArray *threadIDs = [json valueForKeyPath:@"threads.threadid.stringValue"];
for (AwfulThread *thread in [self fetchAllMatchingPredicate:@"threadID in %@", threadIDs]) {
existingThreads[thread.threadID] = thread;
}
NSMutableArray *parsedThreads = [NSMutableArray new];
for (NSDictionary *info in json[@"threads"]) {
NSString *threadID = [info[@"threadid"] stringValue];
AwfulThread *thread = existingThreads[threadID] ?: [AwfulThread insertNew];
thread.threadID = threadID;
// TODO fix for numeric-looking usernames coming in as JSON numbers. Remove when fixed
// server-side.
if ([info[@"lastposter"] respondsToSelector:@selector(stringValue)]) {
thread.lastPostAuthorName = [info[@"lastposter"] stringValue];
} else {
thread.lastPostAuthorName = info[@"lastposter"];
}
thread.lastPostDate = [NSDate dateWithTimeIntervalSince1970:[info[@"lastpost"] doubleValue]];
NSDictionary *icon = json[@"icons"][[info[@"iconid"] stringValue]];
thread.threadIconImageURL = [NSURL URLWithString:icon[@"iconpath"]];
if (info[@"type"]) {
thread.threadIconImageURL2 = SecondaryIconURLForType(info[@"type"]);
} else {
thread.threadIconImageURL2 = nil;
}
NSNumber *starCategory = info[@"bookmark_category"];
if ([starCategory isEqual:[NSNull null]]) {
thread.isBookmarkedValue = NO;
thread.starCategoryValue = AwfulStarCategoryNone;
} else {
thread.isBookmarkedValue = YES;
thread.starCategoryValue = (AwfulStarCategory)[starCategory integerValue];
}
thread.isClosedValue = ![info[@"open"] boolValue];
thread.isStickyValue = [info[@"sticky"] boolValue];
thread.threadID = [info[@"threadid"] stringValue];
thread.threadVotes = info[@"vote_count"];
if (![info[@"vote_score_sum"] isEqual:[NSNull null]]) {
NSDecimal rating = [info[@"vote_score_average"] decimalValue];
thread.threadRating = [NSDecimalNumber decimalNumberWithDecimal:rating];
} else {
thread.threadRating = nil;
}
thread.title = [info[@"title"] gtm_stringByUnescapingFromHTML];
thread.totalReplies = info[@"replycount"];
id seenPosts = info[@"seen_posts"];
if (!seenPosts || [[NSNull null] isEqual:seenPosts]) {
seenPosts = @0;
}
thread.seenPosts = seenPosts;
NSDictionary *authorJSON = @{
@"userid": info[@"postuserid"],
@"username": info[@"postusername"]
};
thread.author = [AwfulUser userCreatedOrUpdatedFromJSON:authorJSON];
existingThreads[thread.threadID] = thread;
[parsedThreads addObject:thread];
}
[[AwfulDataStack sharedDataStack] save];
return parsedThreads;
}
static NSURL * SecondaryIconURLForType(NSString *type)
{
static NSDictionary *types;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
types = @{
@"ASK": @"http://fi.somethingawful.com/ama.gif",
@"TELL": @"http://fi.somethingawful.com/tma.gif",
@"BUY": @"http://fi.somethingawful.com/forums/posticons/icon-38-buying.gif",
@"TRADE": @"http://fi.somethingawful.com/forums/posticons/icon-46-trading.gif",
@"AUCTION": @"http://fi.somethingawful.com/forums/posticons/icon-52-trading.gif",
@"SELL": @"http://fi.somethingawful.com/forums/posticons/icon-37-selling.gif"
};
});
return [NSURL URLWithString:types[type]];
}
+ (instancetype)firstOrNewThreadWithThreadID:(NSString *)threadID
{
AwfulThread *thread = [self firstMatchingPredicate:@"threadID = %@", threadID];
if (thread) return thread;
thread = [self insertNew];
thread.threadID = threadID;
[[AwfulDataStack sharedDataStack] save];
return thread;
}
#pragma mark - _AwfulThread
- (void)setTotalReplies:(NSNumber *)totalReplies
{
[self willChangeValueForKey:AwfulThreadAttributes.totalReplies];
self.primitiveTotalReplies = totalReplies;
[self didChangeValueForKey:AwfulThreadAttributes.totalReplies];
NSInteger minimumNumberOfPages = 1 + [totalReplies integerValue] / 40;
if (minimumNumberOfPages > self.numberOfPagesValue) {
self.numberOfPagesValue = minimumNumberOfPages;
}
}
- (void)setSeenPosts:(NSNumber *)seenPosts
{
[self willChangeValueForKey:AwfulThreadAttributes.seenPosts];
self.primitiveSeenPosts = seenPosts;
[self didChangeValueForKey:AwfulThreadAttributes.seenPosts];
if (self.seenPostsValue > self.totalRepliesValue + 1) {
self.totalRepliesValue = self.seenPostsValue - 1;
}
}
@end