Skip to content

Commit

Permalink
Sync publicize sharing limit on launch and blog switch
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdchr committed Jul 12, 2023
1 parent 9be6a75 commit 76078f7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
11 changes: 11 additions & 0 deletions WordPress/Classes/Services/BlogService.m
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ - (void)syncBlogAndAllMetadata:(Blog *)blog completionHandler:(void (^)(void))co
dispatch_group_leave(syncGroup);
}];

if ([Feature enabled:FeatureFlagJetpackSocial] && blog.dotComID != nil) {
JetpackSocialService *jetpackSocialService = [[JetpackSocialService alloc] initWithContextManager:ContextManager.sharedInstance];
dispatch_group_enter(syncGroup);
[jetpackSocialService syncSharingLimitWithDotComID:blog.dotComID success:^{
dispatch_group_leave(syncGroup);
} failure:^(NSError * _Nullable error) {
DDLogError(@"Failed syncing publicize sharing limit for blog %@: %@", blog.url, error);
dispatch_group_leave(syncGroup);
}];
}

dispatch_group_enter(syncGroup);
[remote getAllAuthorsWithSuccess:^(NSArray<RemoteUser *> *users) {
[self updateMultiAuthor:users forBlog:blogObjectID completionHandler:^{
Expand Down
37 changes: 36 additions & 1 deletion WordPress/Classes/Services/JetpackSocialService.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import WordPressKit
import CoreData

class JetpackSocialService {
@objc class JetpackSocialService: NSObject {

// MARK: Properties

Expand All @@ -16,6 +16,13 @@ class JetpackSocialService {

// MARK: Methods


/// Init method for Objective-C.
///
@objc init(contextManager: ContextManager) {
self.coreDataStack = contextManager
}

init(coreDataStack: CoreDataStackSwift = ContextManager.shared) {
self.coreDataStack = coreDataStack
}
Expand Down Expand Up @@ -62,15 +69,43 @@ class JetpackSocialService {
}
}

/// Sync method for Objective-C.
/// Fetches the latest state of the blog's Publicize auto-sharing limits and stores them locally.
///
/// - Parameters:
/// - dotComID: The WP.com ID of the blog.
/// - success: Closure called when the sync process succeeds.
/// - failure: Closure called when the sync process fails.
@objc func syncSharingLimit(dotComID: NSNumber?,
success: (() -> Void)?,
failure: ((NSError?) -> Void)?) {
guard let blogID = dotComID?.intValue else {
failure?(ServiceError.nilBlogID as NSError)
return
}

syncSharingLimit(for: blogID, completion: { [success, failure] result in
switch result {
case .success:
success?()
case .failure(let error):
failure?(error as NSError)
}
})
}

// MARK: Errors

enum ServiceError: LocalizedError {
case blogNotFound(id: Int)
case nilBlogID

var errorDescription: String? {
switch self {
case .blogNotFound(let id):
return "Blog with id: \(id) was unexpectedly not found."
case .nilBlogID:
return "Blog ID is unexpectedly nil."
}
}
}
Expand Down

0 comments on commit 76078f7

Please sign in to comment.