This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathRemotePost.m
More file actions
95 lines (88 loc) · 3.61 KB
/
RemotePost.m
File metadata and controls
95 lines (88 loc) · 3.61 KB
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
#import "RemotePost.h"
#import <objc/runtime.h>
#import "SHAHasher.h"
NSString * const PostStatusDraft = @"draft";
NSString * const PostStatusPending = @"pending";
NSString * const PostStatusPrivate = @"private";
NSString * const PostStatusPublish = @"publish";
NSString * const PostStatusScheduled = @"future";
NSString * const PostStatusTrash = @"trash";
NSString * const PostStatusDeleted = @"deleted"; // Returned by wpcom REST API when a post is permanently deleted.
@implementation RemotePost
- (id)initWithSiteID:(NSNumber *)siteID status:(NSString *)status title:(NSString *)title content:(NSString *)content
{
self = [super init];
if (self) {
_siteID = siteID;
_status = status;
_title = title;
_content = content;
}
return self;
}
- (NSString *)debugDescription {
NSDictionary *properties = [self debugProperties];
return [NSString stringWithFormat:@"<%@: %p> (%@)", NSStringFromClass([self class]), self, properties];
}
- (NSDictionary *)debugProperties {
unsigned int propertyCount;
objc_property_t *properties = class_copyPropertyList([RemotePost class], &propertyCount);
NSMutableDictionary *debugProperties = [NSMutableDictionary dictionaryWithCapacity:propertyCount];
for (int i = 0; i < propertyCount; i++)
{
// Add property name to array
objc_property_t property = properties[i];
const char *propertyName = property_getName(property);
id value = [self valueForKey:@(propertyName)];
if (value == nil) {
value = [NSNull null];
}
[debugProperties setObject:value forKey:@(propertyName)];
}
free(properties);
return [NSDictionary dictionaryWithDictionary:debugProperties];
}
/// A hash used to determine if the remote content has changed.
///
/// This hash must remain constant regardless of iOS version, app restarts or instances used. `Hasher` or NSObject's `hash` were not used for these reasons.
///
/// `dateModified` is not included within the hash as it is prone to change wihout the content having been changed and is the reason this hash is necessary.
///
/// `autosave` properties are intentionally omitted as remote autosaves are always discarded in favor of local autosaves (aka `revision`s)
///
/// - Note: At the time of writing the backend will occasionally create updates that neither come from autosaves or user initiated saves, these will modify the hash
/// and at present are treated as genuine updates as they are triggered by the user changing their posts content.
- (NSString *)contentHash
{
NSString *hashedContents = [NSString stringWithFormat:@"%@/%@/%@%@/%@/%@%@/%@/%@%@/%@/%@%@/%@/%@%@/%@/%@%@/%@/%@%@/%@/%@%@/%@/%@",
self.postID.stringValue,
self.siteID.stringValue,
self.authorAvatarURL,
self.authorDisplayName,
self.authorEmail,
self.authorURL,
self.authorID.stringValue,
self.date.description,
self.title,
self.URL.absoluteString,
self.shortURL.absoluteString,
self.content,
self.excerpt,
self.slug,
self.suggestedSlug,
self.status,
self.parentID.stringValue,
self.postThumbnailID.stringValue,
self.postThumbnailPath,
self.type,
self.format,
self.commentCount.stringValue,
self.likeCount.stringValue,
[self.tags componentsJoinedByString:@""],
self.pathForDisplayImage,
self.isStickyPost.stringValue,
self.isFeaturedImageChanged ? @"1" : @"2"];
NSData *hashData = [SHAHasher hashForString:hashedContents];
return [SHAHasher sha256StringFromData:hashData];
}
@end