forked from Awful/Awful.app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAwfulPost.m
196 lines (182 loc) · 7.15 KB
/
AwfulPost.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
189
190
191
192
193
194
195
196
//
// AwfulPost.m
// Awful
//
// Created by Nolan Waite on 12-10-26.
// Copyright (c) 2012 Regular Berry Software LLC. All rights reserved.
//
#import "AwfulPost.h"
#import "AwfulDataStack.h"
#import "AwfulForum.h"
#import "AwfulParsing.h"
#import "AwfulSettings.h"
#import "AwfulThread.h"
#import "AwfulUser.h"
#import "GTMNSString+HTML.h"
#import "NSManagedObject+Awful.h"
@implementation AwfulPost
- (BOOL)beenSeen
{
if (!self.thread || self.threadIndexValue == 0) return NO;
return self.threadIndexValue <= self.thread.seenPostsValue;
}
+ (NSSet *)keyPathsForValuesAffectingBeenSeen
{
return [NSSet setWithArray:@[ @"threadIndex", @"thread.seenPosts" ]];
}
- (NSInteger)page
{
return (self.threadIndexValue - 1) / 40 + 1;
}
+ (NSArray *)postsCreatedOrUpdatedFromPageInfo:(PageParsedInfo *)pageInfo
{
if ([pageInfo.forumID length] == 0 || [pageInfo.threadID length] == 0) return nil;
AwfulForum *forum = [AwfulForum firstMatchingPredicate:@"forumID = %@", pageInfo.forumID];
if (!forum) {
forum = [AwfulForum insertNew];
forum.forumID = pageInfo.forumID;
}
forum.name = pageInfo.forumName;
AwfulThread *thread = [AwfulThread firstOrNewThreadWithThreadID:pageInfo.threadID];
thread.forum = forum;
thread.title = pageInfo.threadTitle;
thread.isBookmarkedValue = pageInfo.threadBookmarked;
thread.isClosedValue = pageInfo.threadClosed;
thread.numberOfPagesValue = pageInfo.pagesInThread;
NSArray *allPosts = [thread.posts allObjects];
NSArray *allPostIDs = [allPosts valueForKey:@"postID"];
NSDictionary *existingPosts = [NSDictionary dictionaryWithObjects:allPosts forKeys:allPostIDs];
NSArray *allAuthorNames = [pageInfo.posts valueForKeyPath:@"author.username"];
NSMutableDictionary *existingUsers = [NSMutableDictionary new];
for (AwfulUser *user in [AwfulUser fetchAllMatchingPredicate:@"username IN %@", allAuthorNames]) {
existingUsers[user.username] = user;
}
NSMutableArray *posts = [NSMutableArray new];
for (NSUInteger i = 0; i < [pageInfo.posts count]; i++) {
PostParsedInfo *postInfo = pageInfo.posts[i];
AwfulPost *post = existingPosts[postInfo.postID];
if (!post) {
post = [AwfulPost insertNew];
post.thread = thread;
}
[postInfo applyToObject:post];
if ([postInfo.threadIndex length] > 0) {
post.threadIndexValue = [postInfo.threadIndex integerValue];
} else {
post.threadIndexValue = (pageInfo.pageNumber - 1) * 40 + i + 1;
}
if (!post.author) {
post.author = existingUsers[postInfo.author.username] ?: [AwfulUser insertNew];
}
[postInfo.author applyToObject:post.author];
existingUsers[post.author.username] = post.author;
[posts addObject:post];
if (postInfo.author.originalPoster) {
thread.author = post.author;
}
if (postInfo.beenSeen && thread.seenPostsValue < post.threadIndexValue) {
thread.seenPostsValue = post.threadIndexValue;
}
}
if (pageInfo.pageNumber == thread.numberOfPagesValue) {
thread.lastPostAuthorName = [[posts lastObject] author].username;
thread.lastPostDate = [[posts lastObject] postDate];
}
[[AwfulDataStack sharedDataStack] save];
return posts;
}
+ (NSArray *)postsCreatedOrUpdatedFromJSON:(NSDictionary *)json
{
NSString *forumID = [json[@"forumid"] stringValue];
AwfulForum *forum = [AwfulForum firstMatchingPredicate:@"forumID = %@", forumID];
if (!forum) {
forum = [AwfulForum insertNew];
forum.forumID = forumID;
}
NSString *threadID = [json[@"thread_info"][@"threadid"] stringValue];
AwfulThread *thread = [AwfulThread firstOrNewThreadWithThreadID:threadID];
thread.title = [json[@"thread_info"][@"title"] gtm_stringByUnescapingFromHTML];
thread.archivedValue = [json[@"archived"] boolValue];
if (![json[@"thread_icon"] isEqual:[NSNull null]]) {
thread.threadIconImageURL = [NSURL URLWithString:json[@"thread_icon"][@"iconpath"]];
}
thread.forum = forum;
thread.numberOfPages = json[@"page"][1];
id seenPosts = json[@"seen_posts"];
if ([seenPosts isEqual:[NSNull null]]) {
seenPosts = @0;
}
if (seenPosts) {
thread.seenPosts = seenPosts;
}
NSArray *postIDs = [json[@"posts"] allKeys];
NSMutableDictionary *existingPosts = [NSMutableDictionary new];
for (AwfulPost *post in [AwfulPost fetchAllMatchingPredicate:@"postID IN %@", postIDs]) {
existingPosts[post.postID] = post;
}
for (NSString *postID in json[@"posts"]) {
NSDictionary *info = json[@"posts"][postID];
AwfulPost *post = existingPosts[postID] ?: [AwfulPost insertNew];
post.postID = postID;
if (![info[@"attachmentid"] isEqual:[NSNull null]] && [info[@"attachmentid"] integerValue]) {
post.attachmentID = [info[@"attachmentid"] stringValue];
} else {
post.attachmentID = nil;
}
if (![info[@"editdate"] isEqual:[NSNull null]]) {
post.editDate = [NSDate dateWithTimeIntervalSince1970:[info[@"editdate"] doubleValue]];
} else {
post.editDate = nil;
}
if (![info[@"edituserid"] isEqual:[NSNull null]]) {
NSString *editorUserID = [info[@"edituserid"] stringValue];
AwfulUser *editor = [AwfulUser firstMatchingPredicate:@"userID = %@", editorUserID];
if (!editor) {
editor = [AwfulUser insertNew];
editor.userID = editorUserID;
}
post.editor = editor;
} else {
post.editor = nil;
}
id message = info[@"message"];
if ([message respondsToSelector:@selector(stringValue)]) {
message = [message stringValue];
}
post.innerHTML = message;
post.postDate = [NSDate dateWithTimeIntervalSince1970:[info[@"date"] doubleValue]];
post.thread = thread;
post.threadIndex = info[@"post_index"];
NSString *userID = [info[@"userid"] stringValue];
post.author = [AwfulUser userCreatedOrUpdatedFromJSON:json[@"userids"][userID]];
if ([info[@"op"] boolValue]) {
thread.author = post.author;
}
existingPosts[post.postID] = post;
}
NSArray *posts = [[existingPosts allValues]
sortedArrayUsingComparator:^NSComparisonResult(AwfulPost *a, AwfulPost *b)
{
return [a.threadIndex compare:b.threadIndex];
}];
NSNumber *currentPage = json[@"page"][0];
NSNumber *lastPage = json[@"page"][1];
if ([currentPage isEqual:lastPage]) {
AwfulPost *last;
for (AwfulPost *post in posts) {
if (!last || last.threadIndexValue < post.threadIndexValue) {
last = post;
}
}
thread.lastPostAuthorName = last.author.username;
thread.lastPostDate = last.postDate;
}
[[AwfulDataStack sharedDataStack] save];
return posts;
}
- (BOOL)editableByUserWithID:(NSString *)userID
{
if (!self.thread || self.thread.archivedValue) return NO;
return [self.author.userID isEqual:userID];
}
@end