-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathReaderPost.m
424 lines (366 loc) · 13.6 KB
/
ReaderPost.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
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
#import "ReaderPost.h"
#import "AccountService.h"
@import WordPressData;
#import "SourcePostAttribution.h"
#import "WPAccount.h"
#import "WordPress-Swift.h"
@import WordPressShared;
// These keys are used in the getStoredComment method
NSString * const ReaderPostStoredCommentIDKey = @"commentID";
NSString * const ReaderPostStoredCommentTextKey = @"comment";
static NSString * const SourceAttributionSiteTaxonomy = @"site-pick";
static NSString * const SourceAttributionImageTaxonomy = @"image-pick";
static NSString * const SourceAttributionQuoteTaxonomy = @"quote-pick";
static NSString * const SourceAttributionStandardTaxonomy = @"standard-pick";
@implementation ReaderPost
@dynamic authorDisplayName;
@dynamic authorEmail;
@dynamic authorURL;
@dynamic siteIconURL;
@dynamic blogName;
@dynamic blogDescription;
@dynamic blogURL;
@dynamic commentCount;
@dynamic commentsOpen;
@dynamic featuredImage;
@dynamic feedID;
@dynamic feedItemID;
@dynamic isBlogAtomic;
@dynamic isBlogPrivate;
@dynamic isFollowing;
@dynamic isLiked;
@dynamic isReblogged;
@dynamic isWPCom;
@dynamic organizationID;
@dynamic likeCount;
@dynamic score;
@dynamic siteID;
@dynamic sortRank;
@dynamic sortDate;
@dynamic summary;
@dynamic comments;
@dynamic tags;
@dynamic topic;
@dynamic card;
@dynamic globalID;
@dynamic isLikesEnabled;
@dynamic isSharingEnabled;
@dynamic isSiteBlocked;
@dynamic sourceAttribution;
@dynamic isSavedForLater;
@dynamic isSeen;
@dynamic isSeenSupported;
@dynamic isSubscribedComments;
@dynamic canSubscribeComments;
@dynamic receivesCommentNotifications;
@dynamic primaryTag;
@dynamic primaryTagSlug;
@dynamic isExternal;
@dynamic isJetpack;
@dynamic wordCount;
@dynamic readingTime;
@dynamic crossPostMeta;
@dynamic railcar;
@dynamic inUse;
@synthesize rendered;
+ (instancetype)createOrReplaceFromRemotePost:(RemoteReaderPost *)remotePost
forTopic:(ReaderAbstractTopic *)topic
context:(NSManagedObjectContext *) managedObjectContext
{
NSError *error;
ReaderPost *post;
NSString *globalID = remotePost.globalID;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"ReaderPost"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"globalID = %@ AND (topic = %@ OR topic = NULL)", globalID, topic];
NSArray *arr = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
BOOL existing = false;
if (error) {
DDLogError(@"Error fetching an existing reader post. - %@", error);
} else if ([arr count] > 0) {
post = (ReaderPost *)[arr objectAtIndex:0];
existing = YES;
} else {
post = [NSEntityDescription insertNewObjectForEntityForName:@"ReaderPost"
inManagedObjectContext:managedObjectContext];
}
post.authorID = remotePost.authorID;
post.author = remotePost.author;
post.authorAvatarURL = remotePost.authorAvatarURL;
post.authorDisplayName = remotePost.authorDisplayName;
post.authorEmail = remotePost.authorEmail;
post.authorURL = remotePost.authorURL;
post.organizationID = remotePost.organizationID;
post.siteIconURL = remotePost.siteIconURL;
post.blogName = remotePost.blogName;
post.blogDescription = remotePost.blogDescription;
post.blogURL = remotePost.blogURL;
post.commentCount = remotePost.commentCount;
post.commentsOpen = remotePost.commentsOpen;
post.date_created_gmt = [NSDate dateFromServerDate:remotePost.date_created_gmt];
post.featuredImage = remotePost.featuredImage;
post.feedID = remotePost.feedID;
post.feedItemID = remotePost.feedItemID;
post.globalID = remotePost.globalID;
post.isBlogAtomic = remotePost.isBlogAtomic;
post.isBlogPrivate = remotePost.isBlogPrivate;
post.isFollowing = remotePost.isFollowing;
post.isLiked = remotePost.isLiked;
post.isReblogged = remotePost.isReblogged;
post.isWPCom = remotePost.isWPCom;
post.organizationID = remotePost.organizationID;
post.likeCount = remotePost.likeCount;
post.permaLink = remotePost.permalink;
post.postID = remotePost.postID;
post.postTitle = remotePost.postTitle;
post.railcar = remotePost.railcar;
post.score = remotePost.score;
post.siteID = remotePost.siteID;
post.sortDate = remotePost.sortDate;
post.isSeen = remotePost.isSeen;
post.isSeenSupported = remotePost.isSeenSupported;
post.isSubscribedComments = remotePost.isSubscribedComments;
post.canSubscribeComments = remotePost.canSubscribeComments;
post.receivesCommentNotifications = remotePost.receivesCommentNotifications;
if (existing && [topic isKindOfClass:[ReaderSearchTopic class]]) {
// Failsafe. The `read/search` endpoint might return the same post on
// more than one page. If this happens preserve the *original* sortRank
// to avoid content jumping around in the UI.
} else {
post.sortRank = remotePost.sortRank;
}
post.status = remotePost.status;
post.summary = remotePost.summary;
post.tags = remotePost.tags;
post.isSharingEnabled = remotePost.isSharingEnabled;
post.isLikesEnabled = remotePost.isLikesEnabled;
post.isSiteBlocked = NO;
if (remotePost.crossPostMeta) {
if (!post.crossPostMeta) {
ReaderCrossPostMeta *meta = (ReaderCrossPostMeta *)[NSEntityDescription insertNewObjectForEntityForName:[ReaderCrossPostMeta classNameWithoutNamespaces]
inManagedObjectContext:managedObjectContext];
post.crossPostMeta = meta;
}
post.crossPostMeta.siteURL = remotePost.crossPostMeta.siteURL;
post.crossPostMeta.postURL = remotePost.crossPostMeta.postURL;
post.crossPostMeta.commentURL = remotePost.crossPostMeta.commentURL;
post.crossPostMeta.siteID = remotePost.crossPostMeta.siteID;
post.crossPostMeta.postID = remotePost.crossPostMeta.postID;
} else {
post.crossPostMeta = nil;
}
NSString *tag = remotePost.primaryTag;
NSString *slug = remotePost.primaryTagSlug;
if ([topic isKindOfClass:[ReaderTagTopic class]]) {
ReaderTagTopic *tagTopic = (ReaderTagTopic *)topic;
if ([tagTopic.slug isEqualToString:remotePost.primaryTagSlug]) {
tag = remotePost.secondaryTag;
slug = remotePost.secondaryTagSlug;
}
}
post.primaryTag = tag;
post.primaryTagSlug = slug;
post.isExternal = remotePost.isExternal;
post.isJetpack = remotePost.isJetpack;
post.wordCount = remotePost.wordCount;
post.readingTime = remotePost.readingTime;
if (remotePost.sourceAttribution) {
post.sourceAttribution = [self createOrReplaceFromRemoteDiscoverAttribution:remotePost.sourceAttribution forPost:post context:managedObjectContext];
} else {
post.sourceAttribution = nil;
}
post.content = [RichContentFormatter removeInlineStyles:[RichContentFormatter removeForbiddenTags:remotePost.content]];
// assign the topic last.
post.topic = topic;
// auto-suggested image, but NOT an explcitly specified featured image
post.pathForDisplayImage = remotePost.autoSuggestedFeaturedImage;
return post;
}
+ (SourcePostAttribution *)createOrReplaceFromRemoteDiscoverAttribution:(RemoteSourcePostAttribution *)remoteAttribution
forPost:(ReaderPost *)post
context:(NSManagedObjectContext *) managedObjectContext
{
SourcePostAttribution *attribution = post.sourceAttribution;
if (!attribution) {
attribution = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([SourcePostAttribution class])
inManagedObjectContext:managedObjectContext];
}
attribution.authorName = remoteAttribution.authorName;
attribution.authorURL = remoteAttribution.authorURL;
attribution.avatarURL = remoteAttribution.avatarURL;
attribution.blogName = remoteAttribution.blogName;
attribution.blogURL = remoteAttribution.blogURL;
attribution.permalink = remoteAttribution.permalink;
attribution.blogID = remoteAttribution.blogID;
attribution.postID = remoteAttribution.postID;
attribution.commentCount = remoteAttribution.commentCount;
attribution.likeCount = remoteAttribution.likeCount;
attribution.attributionType = [self attributionTypeFromTaxonomies:remoteAttribution.taxonomies];
return attribution;
}
+ (NSString *)attributionTypeFromTaxonomies:(NSArray *)taxonomies
{
if ([taxonomies containsObject:SourceAttributionSiteTaxonomy]) {
return SourcePostAttributionTypeSite;
}
if ([taxonomies containsObject:SourceAttributionImageTaxonomy] ||
[taxonomies containsObject:SourceAttributionQuoteTaxonomy] ||
[taxonomies containsObject:SourceAttributionStandardTaxonomy] ) {
return SourcePostAttributionTypePost;
}
return nil;
}
- (BOOL)isCrossPost
{
return self.crossPostMeta != nil;
}
- (BOOL)isP2Type
{
NSInteger orgID = [self.organizationID intValue];
return orgID == SiteOrganizationTypeP2 || orgID == SiteOrganizationTypeAutomattic;
}
- (NSString *)authorString
{
if ([self.authorDisplayName length] > 0) {
return self.authorDisplayName;
}
return self.author;
}
- (NSURL *)featuredImageURL
{
if ([self.featuredImage length]) {
return [NSURL URLWithString:self.featuredImage];
}
return nil;
}
- (BOOL)contentIncludesFeaturedImage
{
NSURL *featuredImageURL = [self featuredImageURL];
NSString *featuredImage = [featuredImageURL absoluteString];
if (!featuredImage) {
return NO;
}
// Remove any query string params if needed (e.g. resize values)
NSUInteger questionMarkLocation = [featuredImage rangeOfString:@"?" options:NSBackwardsSearch].location;
if (questionMarkLocation != NSNotFound) {
featuredImage = [featuredImage substringToIndex:questionMarkLocation];
}
// One URL might be http and the other https, so don't include the protocol in the check.
NSString *scheme = [featuredImageURL scheme];
if ([scheme length]) {
NSInteger index = [scheme length] + 3; // protocol + ://
featuredImage = [featuredImage substringFromIndex:index];
}
NSString *content = [self contentForDisplay];
return ([content rangeOfString:featuredImage].location != NSNotFound);
}
#pragma mark - PostContentProvider protocol
- (NSString *)blogNameForDisplay
{
if (self.blogName.length > 0) {
return self.blogName;
}
return [[NSURL URLWithString:self.blogURL] host];
}
- (NSString *)titleForDisplay
{
NSString *title = [[self.postTitle trim] stringByDecodingXMLCharacters];
if (!title) {
title = @"";
}
return title;
}
- (NSArray <NSString *> *)tagsForDisplay
{
if (self.tags.length <= 0) {
return @[];
}
NSArray *tags = [self.tags componentsSeparatedByString:@", "];
return [tags sortedArrayUsingSelector:@selector(localizedCompare:)];
}
- (NSString *)authorForDisplay
{
return [self authorString];
}
- (NSDate *)dateForDisplay
{
return [self dateCreated];
}
- (NSString *)contentPreviewForDisplay
{
return self.summary;
}
- (NSURL *)featuredImageURLForDisplay
{
return [self featuredImageURL];
}
- (NSString *)likeCountForDisplay
{
NSString *likeStr = NSLocalizedString(@"Like", @"Text for the 'like' button. Tapping marks a post in the reader as 'liked'.");
NSString *likesStr = NSLocalizedString(@"Likes", @"Text for the 'like' button. Tapping removes the 'liked' status from a post.");
NSInteger count = [self.likeCount integerValue];
NSString *title;
if (count == 0) {
title = likeStr;
} else if (count == 1) {
title = [NSString stringWithFormat:@"%d %@", count, likeStr];
} else {
title = [NSString stringWithFormat:@"%d %@", count, likesStr];
}
return title;
}
- (SourceAttributionStyle)sourceAttributionStyle
{
if ([self.sourceAttribution.attributionType isEqualToString:SourcePostAttributionTypePost]) {
return SourceAttributionStylePost;
} else if ([self.sourceAttribution.attributionType isEqualToString:SourcePostAttributionTypeSite]) {
return SourceAttributionStyleSite;
} else {
return SourceAttributionStyleNone;
}
}
- (NSString *)sourceAuthorNameForDisplay
{
return self.sourceAttribution.authorName;
}
- (NSURL *)sourceAvatarURLForDisplay
{
if (!self.sourceAttribution) {
return nil;
}
return [NSURL URLWithString:self.sourceAttribution.avatarURL];
}
- (NSString *)sourceBlogNameForDisplay
{
return self.sourceAttribution.blogName;
}
- (BOOL)isSourceAttributionWPCom
{
return (self.sourceAttribution.blogID) ? YES : NO;
}
- (NSURL *)avatarURLForDisplay
{
return [NSURL URLWithString:self.authorAvatarURL];
}
- (NSDictionary *)railcarDictionary
{
if (!self.railcar) {
return nil;
}
NSData *jsonData = [self.railcar dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
if ([jsonObj isKindOfClass:[NSDictionary class]]) {
return (NSDictionary *)jsonObj;
}
return nil;
}
- (void)didSave {
[super didSave];
// A ReaderCard can have either a post, or a list of topics, but not both.
// Since this card has a post, we can confidently set `topics` to NULL.
if ([self respondsToSelector:@selector(card)] && self.card.count > 0) {
self.card.allObjects[0].topics = NULL;
[[ContextManager sharedInstance] saveContext:self.managedObjectContext];
}
}
@end