Skip to content

Commit 73c3f0c

Browse files
authored
Refactor foreignID (#24951)
1 parent 585a552 commit 73c3f0c

5 files changed

Lines changed: 16 additions & 30 deletions

File tree

Sources/WordPressData/Objective-C/PostHelper.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ + (void)updatePost:(AbstractPost *)post withRemotePost:(RemotePost *)remotePost
5858
}
5959

6060
post.rawMetadata = [PostHelper makeRawMetadataFrom:remotePost];
61+
post.foreignID = [PostHelper getForeignIDFor:remotePost];
6162

6263
post.autosaveTitle = remotePost.autosave.title;
6364
post.autosaveExcerpt = remotePost.autosave.excerpt;
@@ -68,7 +69,6 @@ + (void)updatePost:(AbstractPost *)post withRemotePost:(RemotePost *)remotePost
6869
if ([post isKindOfClass:[Page class]]) {
6970
Page *pagePost = (Page *)post;
7071
pagePost.parentID = remotePost.parentID;
71-
pagePost.foreignID = remotePost.foreignID;
7272
} else if ([post isKindOfClass:[Post class]]) {
7373
Post *postPost = (Post *)post;
7474
postPost.commentsStatus = remotePost.commentsStatus;
@@ -100,7 +100,6 @@ + (void)updatePost:(AbstractPost *)post withRemotePost:(RemotePost *)remotePost
100100
publicizeMessage = [publicizeMessageDictionary stringForKey:@"value"];
101101
publicizeMessageID = [publicizeMessageDictionary stringForKey:@"id"];
102102
}
103-
postPost.foreignID = remotePost.foreignID;
104103
postPost.publicID = publicID;
105104
postPost.publicizeMessage = publicizeMessage;
106105
postPost.publicizeMessageID = publicizeMessageID;
@@ -185,7 +184,7 @@ + (NSArray *)mergePosts:(NSArray <RemotePost *> *)remotePosts
185184
for (RemotePost *remotePost in remotePosts) {
186185
AbstractPost *post = [blog lookupPostWithID:remotePost.postID inContext:context];
187186
if (post == nil) {
188-
NSUUID *foreignID = remotePost.foreignID;
187+
NSUUID *foreignID = [PostHelper getForeignIDFor:remotePost];
189188
if (foreignID != nil) {
190189
post = [blog lookupLocalPostWithForeignID:foreignID inContext:context];
191190
}

Sources/WordPressData/Swift/PostHelper+Metadata.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@ import WordPressShared
33
import WordPressKit
44

55
extension PostHelper {
6-
@objc public static let foreignIDKey = "wp_jp_foreign_id"
6+
@objc public static let foreignIDKey = PostMetadataContainer.Key.foreignID.rawValue
7+
8+
@objc public static func getForeignID(for post: RemotePost) -> UUID? {
9+
guard let metadata = post.metadata as? [[String: Any]] else {
10+
return nil
11+
}
12+
let container = PostMetadataContainer(metadata: metadata)
13+
guard let value = container.getString(for: .foreignID) else {
14+
return nil
15+
}
16+
return UUID(uuidString: value)
17+
}
718

819
@objc public static func makeRawMetadata(from post: RemotePost) -> Data? {
920
guard let metadata = post.metadata else {

Sources/WordPressData/Swift/PostMetadata.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,5 @@ private extension PostMetadataContainer {
7474
extension PostMetadataContainer.Key {
7575
static let jetpackNewsletterAccess: PostMetadataContainer.Key = "_jetpack_newsletter_access"
7676
static let jetpackNewsletterEmailDisabled: PostMetadataContainer.Key = "_jetpack_dont_email_post_to_subs"
77+
static let foreignID: PostMetadataContainer.Key = "wp_jp_foreign_id"
7778
}

Sources/WordPressData/Swift/RemotePost+Metadata.swift

Lines changed: 0 additions & 25 deletions
This file was deleted.

Tests/KeystoneTests/Tests/Services/PostRepositoryTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class PostRepositoryTests: CoreDataTestCase {
227227
if $0 <= 3 {
228228
post.metadata = [[
229229
"id": 1234,
230-
"key": PostHelper.foreignIDKey,
230+
"key": "wp_jp_foreign_id",
231231
"value": "892D484C-9972-47DE-8103-03A7FDE4EFCC"
232232
]]
233233
}

0 commit comments

Comments
 (0)