Skip to content

Commit

Permalink
Merge 24.8 release finalization (#23187)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdchr committed May 10, 2024
2 parents 65bed38 + e7a325a commit 6a36818
Show file tree
Hide file tree
Showing 50 changed files with 1,210 additions and 183 deletions.
7 changes: 7 additions & 0 deletions MIGRATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
This file documents changes in the data model. Please explain any changes to the
data model as well as any custom migrations.

## WordPress 154

@momozw 2024-05-07

- `AbstractPost`:
- Added `foreignID` (optional, no default, `UUID`)

## WordPress 153

@dvdchr 2023-11-07
Expand Down
3 changes: 3 additions & 0 deletions WordPress/Classes/Models/AbstractPost.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ typedef NS_ENUM(NSUInteger, AbstractPostRemoteStatus) {
@property (nonatomic, copy, nullable) NSDate *autosaveModifiedDate;
@property (nonatomic, copy, nullable) NSNumber *autosaveIdentifier;

/// Used to deduplicate new posts
@property (nonatomic, strong, nullable) NSUUID *foreignID;

/// - warning: deprecated (kahu-offline-mode)
@property (nonatomic, strong, nullable) NSString *confirmedChangesHash;
@property (nonatomic, strong, nullable) NSDate *confirmedChangesTimestamp;
Expand Down
1 change: 1 addition & 0 deletions WordPress/Classes/Models/AbstractPost.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ @implementation AbstractPost
@dynamic autosaveTitle;
@dynamic autosaveModifiedDate;
@dynamic autosaveIdentifier;
@dynamic foreignID;

+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
{
Expand Down
14 changes: 14 additions & 0 deletions WordPress/Classes/Models/Blog+Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ extension Blog {
request.predicate = NSPredicate(format: "blog = %@ AND original = NULL AND postID = %ld", self, postID)
return (try? context.fetch(request))?.first
}

/// Lookup a post in the blog.
///
/// - Parameter foreignID: The foreign ID associated with the post; used to deduplicate new posts.
/// - Returns: The `AbstractPost` associated with the given foreign ID.
@objc(lookupPostWithForeignID:inContext:)
func lookupPost(withForeignID foreignID: UUID, in context: NSManagedObjectContext) -> AbstractPost? {
let request = NSFetchRequest<AbstractPost>(entityName: NSStringFromClass(AbstractPost.self))
request.predicate = NSPredicate(format: "blog = %@ AND original = NULL AND \(#keyPath(AbstractPost.foreignID)) == %@", self, foreignID as NSUUID)
request.fetchLimit = 1
return (try? context.fetch(request))?.first
}
}

// MARK: - Create posts
Expand All @@ -45,6 +57,7 @@ extension Blog {
let post = NSEntityDescription.insertNewObject(forEntityName: NSStringFromClass(Post.self), into: context) as! Post
post.blog = self
post.remoteStatus = .sync
post.foreignID = UUID()

if let categoryID = settings?.defaultCategoryID,
categoryID.intValue != PostCategoryUncategorized,
Expand Down Expand Up @@ -84,6 +97,7 @@ extension Blog {
page.blog = self
page.date_created_gmt = Date()
page.remoteStatus = .sync
page.foreignID = UUID()

if let userID = userID, let author = getAuthorWith(id: userID) {
page.authorID = author.userID
Expand Down
23 changes: 23 additions & 0 deletions WordPress/Classes/RemotePost+Metadata.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Foundation

@objc
extension RemotePost {
var foreignID: UUID? {
guard let metadata else {
return nil
}
let metadataItems = metadata.compactMap { value -> RemotePostMetadataItem? in
guard let dictionary = value as? [String: Any] else {
wpAssertionFailure("Unexpected value", userInfo: [
"value": value
])
return nil
}
return PostHelper.mapDictionaryToMetadataItems(dictionary)
}
guard let value = metadataItems.first(where: { $0.key == PostHelper.foreignIDKey })?.value else {
return nil
}
return UUID(uuidString: value)
}
}
14 changes: 14 additions & 0 deletions WordPress/Classes/Services/PostHelper+Metadata.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

extension PostHelper {
@objc static let foreignIDKey = "wp_jp_foreign_id"

static func mapDictionaryToMetadataItems(_ dictionary: [String: Any]) -> RemotePostMetadataItem? {
let id = dictionary["id"]
return RemotePostMetadataItem(
id: (id as? String) ?? (id as? NSNumber)?.stringValue,
key: dictionary["key"] as? String,
value: dictionary["value"] as? String
)
}
}
23 changes: 20 additions & 3 deletions WordPress/Classes/Services/PostHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ + (void)updatePost:(AbstractPost *)post withRemotePost:(RemotePost *)remotePost
if ([post isKindOfClass:[Page class]]) {
Page *pagePost = (Page *)post;
pagePost.parentID = remotePost.parentID;
pagePost.foreignID = remotePost.foreignID;
} else if ([post isKindOfClass:[Post class]]) {
Post *postPost = (Post *)post;
postPost.commentCount = remotePost.commentCount;
Expand Down Expand Up @@ -92,6 +93,7 @@ + (void)updatePost:(AbstractPost *)post withRemotePost:(RemotePost *)remotePost
publicizeMessage = [publicizeMessageDictionary stringForKey:@"value"];
publicizeMessageID = [publicizeMessageDictionary stringForKey:@"id"];
}
postPost.foreignID = remotePost.foreignID;
postPost.publicID = publicID;
postPost.publicizeMessage = publicizeMessage;
postPost.publicizeMessageID = publicizeMessageID;
Expand Down Expand Up @@ -197,8 +199,17 @@ + (RemotePostCategory *)remoteCategoryWithCategory:(PostCategory *)category
return remoteCategory;
}

+ (NSArray *)remoteMetadataForPost:(Post *)post {
NSMutableArray *metadata = [NSMutableArray arrayWithCapacity:4];
+ (NSArray *)remoteMetadataForPost:(Post *)post
{
NSMutableArray *metadata = [NSMutableArray arrayWithCapacity:5];

/// Send UUID as a foreign ID in metadata so we have a way to deduplicate new posts
if (post.foreignID) {
NSMutableDictionary *uuidDictionary = [NSMutableDictionary dictionaryWithCapacity:3];
uuidDictionary[@"key"] = [self foreignIDKey];
uuidDictionary[@"value"] = [post.foreignID UUIDString];
[metadata addObject:uuidDictionary];
}

if (post.publicID) {
NSMutableDictionary *publicDictionary = [NSMutableDictionary dictionaryWithCapacity:1];
Expand Down Expand Up @@ -238,7 +249,13 @@ + (NSArray *)mergePosts:(NSArray <RemotePost *> *)remotePosts
{
NSMutableArray *posts = [NSMutableArray arrayWithCapacity:remotePosts.count];
for (RemotePost *remotePost in remotePosts) {
AbstractPost *post = [blog lookupPostWithID:remotePost.postID inContext:context];
AbstractPost *post;
NSUUID *foreignID = remotePost.foreignID;
if (foreignID != nil) {
post = [blog lookupPostWithForeignID:foreignID inContext:context];
} else {
post = [blog lookupPostWithID:remotePost.postID inContext:context];
}
if (!post) {
if ([remotePost.type isEqualToString:PostServiceTypePage]) {
// Create a Page entity for posts with a remote type of "page"
Expand Down
12 changes: 4 additions & 8 deletions WordPress/Classes/Services/PostRepository+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,12 @@ extension RemotePostCreateParameters {
}
metadata = Set(PostHelper.remoteMetadata(for: post).compactMap { value -> RemotePostMetadataItem? in
guard let dictionary = value as? [String: Any] else {
assertionFailure("Unexpected value: \(value)")
wpAssertionFailure("Unexpected value", userInfo: [
"value": value
])
return nil
}
let id = dictionary["id"]

return RemotePostMetadataItem(
id: (id as? String) ?? (id as? NSNumber)?.stringValue,
key: dictionary["key"] as? String,
value: dictionary["value"] as? String
)
return PostHelper.mapDictionaryToMetadataItems(dictionary)
})
default:
break
Expand Down
8 changes: 4 additions & 4 deletions WordPress/Classes/ViewRelated/Post/PostListFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ import Foundation
let statuses: [BasePost.Status] = [.publish, .publishPrivate]

let predicate: NSPredicate
if FeatureFlag.syncPublishing.enabled {
if !FeatureFlag.syncPublishing.enabled {
let query =
// existing published/private posts
"(statusAfterSync = status AND status IN (%@))"
Expand Down Expand Up @@ -128,7 +128,7 @@ import Foundation
let statusesForLocalDrafts: [BasePost.Status] = [.draft, .pending, .publish, .publishPrivate, .scheduled]

let predicate: NSPredicate
if FeatureFlag.syncPublishing.enabled {
if !FeatureFlag.syncPublishing.enabled {
let query =
// Existing draft/pending posts
"(statusAfterSync = status AND status IN (%@))"
Expand Down Expand Up @@ -170,7 +170,7 @@ import Foundation
let statuses: [BasePost.Status] = [.scheduled]

let predicate: NSPredicate
if FeatureFlag.syncPublishing.enabled {
if !FeatureFlag.syncPublishing.enabled {
let query =
// existing scheduled posts
"(statusAfterSync = status AND status IN (%@))"
Expand Down Expand Up @@ -213,7 +213,7 @@ import Foundation
let statuses: [BasePost.Status] = [.draft, .pending, .publish, .publishPrivate, .scheduled]

let predicate: NSPredicate
if FeatureFlag.syncPublishing.enabled {
if !FeatureFlag.syncPublishing.enabled {
let query =
// Existing non-trashed posts
"(statusAfterSync = status AND status IN (%@))"
Expand Down
2 changes: 1 addition & 1 deletion WordPress/Classes/WordPress.xcdatamodeld/.xccurrentversion
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<plist version="1.0">
<dict>
<key>_XCCurrentVersionName</key>
<string>WordPress 153.xcdatamodel</string>
<string>WordPress 154.xcdatamodel</string>
</dict>
</plist>
Loading

0 comments on commit 6a36818

Please sign in to comment.