Skip to content

Commit

Permalink
Merge branch 'develop' into fix/stories-9-16-export
Browse files Browse the repository at this point in the history
# Conflicts:
#	Podfile
#	Podfile.lock
  • Loading branch information
bjtitus committed May 11, 2021
2 parents 385fc8a + 074db16 commit 0d8a86d
Show file tree
Hide file tree
Showing 199 changed files with 56,420 additions and 3,697 deletions.
6 changes: 6 additions & 0 deletions MIGRATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
This file documents changes in the data model. Please explain any changes to the
data model as well as any custom migrations.

## WordPress 124

@scoutharris 2021-05-07

- `LikeUser`: added `dateFetched` attribute.

## WordPress 123

@scoutharris 2021-04-28
Expand Down
6 changes: 3 additions & 3 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def wordpress_kit
end

def kanvas
#pod 'Kanvas', '~> 1.2.6'
#pod 'Kanvas', '~> 1.2.7'
#pod 'Kanvas', :git => 'https://github.com/tumblr/Kanvas-iOS.git', :tag => ''
pod 'Kanvas', :git => 'https://github.com/tumblr/Kanvas-iOS.git', :commit => '704a5e'
pod 'Kanvas', :git => 'https://github.com/tumblr/Kanvas-iOS.git', :commit => '53b0a2'
#pod 'Kanvas', :path => '../Kanvas-iOS'
end

Expand Down Expand Up @@ -161,7 +161,7 @@ abstract_target 'Apps' do
## Gutenberg (React Native)
## =====================
##
gutenberg :tag => 'v1.52.0'
gutenberg :tag => 'v1.52.1'


## Third party libraries
Expand Down
184 changes: 92 additions & 92 deletions Podfile.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
17.4
-----
* [**] A new author can be chosen for Posts and Pages on multi-author sites. [#16281]


17.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ extension LikeUser {
@NSManaged public var likedPostID: Int64
@NSManaged public var likedCommentID: Int64
@NSManaged public var preferredBlog: LikeUserPreferredBlog?

@NSManaged public var dateFetched: Date
}
33 changes: 20 additions & 13 deletions WordPress/Classes/Services/CommentService+Likes.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extension CommentService {

/**
Fetches a list of users that liked the comment with the given ID.
Fetches a list of users from remote that liked the comment with the given IDs.
@param commentID The ID of the comment to fetch likes for
@param siteID The ID of the site that contains the post
Expand All @@ -23,13 +23,32 @@ extension CommentService {
self.createNewUsers(from: remoteLikeUsers, commentID: commentID, siteID: siteID) {
let users = self.likeUsersFor(commentID: commentID, siteID: siteID)
success(users)
LikeUserHelper.purgeStaleLikes()
}
} failure: { error in
DDLogError(String(describing: error))
failure(error)
}
}

/**
Fetches a list of users from Core Data that liked the comment with the given IDs.
@param commentID The ID of the comment to fetch likes for
@param siteID The ID of the site that contains the post
*/
func likeUsersFor(commentID: NSNumber, siteID: NSNumber) -> [LikeUser] {
let request = LikeUser.fetchRequest() as NSFetchRequest<LikeUser>
request.predicate = NSPredicate(format: "likedSiteID = %@ AND likedCommentID = %@", siteID, commentID)
request.sortDescriptors = [NSSortDescriptor(key: "dateLiked", ascending: false)]

if let users = try? managedObjectContext.fetch(request) {
return users
}

return [LikeUser]()
}

}

private extension CommentService {
Expand Down Expand Up @@ -75,16 +94,4 @@ private extension CommentService {
}
}

func likeUsersFor(commentID: NSNumber, siteID: NSNumber) -> [LikeUser] {
let request = LikeUser.fetchRequest() as NSFetchRequest<LikeUser>
request.predicate = NSPredicate(format: "likedSiteID = %@ AND likedCommentID = %@", siteID, commentID)
request.sortDescriptors = [NSSortDescriptor(key: "dateLiked", ascending: false)]

if let users = try? managedObjectContext.fetch(request) {
return users
}

return [LikeUser]()
}

}
13 changes: 0 additions & 13 deletions WordPress/Classes/Services/CommentService.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,6 @@ extern NSUInteger const WPTopLevelHierarchicalCommentsPerPage;
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure;

/**
Fetches a list of users that liked the comment with the given ID.
@param commentID The ID of the comment to fetch likes for
@param siteID The ID of the site that contains the post
@param success A success block
@param failure A failure block
*/
- (void)getLikesForCommentID:(NSNumber *)commentID
siteID:(NSNumber *)siteID
success:(void (^)(NSArray<RemoteUser *> *))success
failure:(void (^)(NSError * _Nullable))failure;

/**
Get a CommentServiceRemoteREST for the given site.
This is public so it can be accessed from Swift extensions.
Expand Down
13 changes: 0 additions & 13 deletions WordPress/Classes/Services/CommentService.m
Original file line number Diff line number Diff line change
Expand Up @@ -778,19 +778,6 @@ - (void)toggleLikeStatusForComment:(Comment *)comment
}
}

// TODO: remove when CommentServiceTests is updated to use LikeUsers method.
- (void)getLikesForCommentID:(NSNumber *)commentID
siteID:(NSNumber *)siteID
success:(void (^)(NSArray<RemoteUser *> *))success
failure:(void (^)(NSError * _Nullable))failure
{
NSParameterAssert(commentID);
NSParameterAssert(siteID);

CommentServiceRemoteREST *remote = [self restRemoteForSite:siteID];
[remote getLikesForCommentID:commentID success:success failure:failure];
}


#pragma mark - Private methods

Expand Down
28 changes: 28 additions & 0 deletions WordPress/Classes/Services/LikeUserHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Foundation
likeUser.likedPostID = remoteUser.likedPostID?.int64Value ?? 0
likeUser.likedCommentID = remoteUser.likedCommentID?.int64Value ?? 0
likeUser.preferredBlog = createPreferredBlogFrom(remotePreferredBlog: remoteUser.preferredBlog, forUser: likeUser, context: context)
likeUser.dateFetched = Date()
}

private class func createPreferredBlogFrom(remotePreferredBlog: RemoteLikeUserPreferredBlog?,
Expand All @@ -43,4 +44,31 @@ import Foundation
return preferredBlog
}

class func purgeStaleLikes() {
let derivedContext = ContextManager.shared.newDerivedContext()

derivedContext.perform {
purgeStaleLikes(fromContext: derivedContext)
ContextManager.shared.save(derivedContext)
}
}

// Delete all LikeUsers that were last fetched at least 7 days ago.
private class func purgeStaleLikes(fromContext context: NSManagedObjectContext) {
guard let staleDate = Calendar.current.date(byAdding: .day, value: -7, to: Date()) else {
DDLogError("Error creating date to purge stale Likes.")
return
}

let request = LikeUser.fetchRequest() as NSFetchRequest<LikeUser>
request.predicate = NSPredicate(format: "dateFetched <= %@", staleDate as CVarArg)

do {
let users = try context.fetch(request)
users.forEach { context.delete($0) }
} catch {
DDLogError("Error fetching Like Users: \(error)")
}
}

}
27 changes: 6 additions & 21 deletions WordPress/Classes/Services/PostCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ class PostCoordinator: NSObject {
trackObserver(receipt: uuid, for: post)

return
} else {
// Ensure that all media references are up to date
post.media.forEach { media in
self.updateReferences(to: media, in: post)
}
}

completion(.success(post))
Expand Down Expand Up @@ -241,30 +246,10 @@ class PostCoordinator: NSObject {
})
}

func upload(assets: [ExportableAsset], to post: AbstractPost, completion: @escaping (Result<AbstractPost, SavingError>) -> Void) -> [Media?] {
guard mediaCoordinator.uploadMedia(for: post) else {
change(post: post, status: .failed) { savedPost in
completion(.failure(SavingError.mediaFailure(savedPost)))
}
return []
}

change(post: post, status: .pushing)

change(post: post, status: .pushingMedia)

// Only observe if we're not already
guard !isObserving(post: post) else {
return []
}

let uuid = observeMedia(for: post, completion: completion)
trackObserver(receipt: uuid, for: post)

func add(assets: [ExportableAsset], to post: AbstractPost) -> [Media?] {
let media = assets.map { asset in
return mediaCoordinator.addMedia(from: asset, to: post)
}

return media
}

Expand Down
35 changes: 21 additions & 14 deletions WordPress/Classes/Services/PostService+Likes.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extension PostService {

/**
Fetches a list of users that liked the post with the given ID.
Fetches a list of users from remote that liked the post with the given IDs.
@param postID The ID of the post to fetch likes for
@param siteID The ID of the site that contains the post
Expand All @@ -13,7 +13,7 @@ extension PostService {
success: @escaping (([LikeUser]) -> Void),
failure: @escaping ((Error?) -> Void)) {

guard let remote = PostServiceRemoteFactory().restRemoteFor(siteID: siteID, context: managedObjectContext) else {
guard let remote = postServiceRemoteFactory.restRemoteFor(siteID: siteID, context: managedObjectContext) else {
DDLogError("Unable to create a REST remote for posts.")
failure(nil)
return
Expand All @@ -23,13 +23,32 @@ extension PostService {
self.createNewUsers(from: remoteLikeUsers, postID: postID, siteID: siteID) {
let users = self.likeUsersFor(postID: postID, siteID: siteID)
success(users)
LikeUserHelper.purgeStaleLikes()
}
} failure: { error in
DDLogError(String(describing: error))
failure(error)
}
}

/**
Fetches a list of users from Core Data that liked the post with the given IDs.
@param postID The ID of the post to fetch likes for
@param siteID The ID of the site that contains the post
*/
func likeUsersFor(postID: NSNumber, siteID: NSNumber) -> [LikeUser] {
let request = LikeUser.fetchRequest() as NSFetchRequest<LikeUser>
request.predicate = NSPredicate(format: "likedSiteID = %@ AND likedPostID = %@", siteID, postID)
request.sortDescriptors = [NSSortDescriptor(key: "dateLiked", ascending: false)]

if let users = try? managedObjectContext.fetch(request) {
return users
}

return [LikeUser]()
}

}

private extension PostService {
Expand Down Expand Up @@ -75,16 +94,4 @@ private extension PostService {
}
}

func likeUsersFor(postID: NSNumber, siteID: NSNumber) -> [LikeUser] {
let request = LikeUser.fetchRequest() as NSFetchRequest<LikeUser>
request.predicate = NSPredicate(format: "likedSiteID = %@ AND likedPostID = %@", siteID, postID)
request.sortDescriptors = [NSSortDescriptor(key: "dateLiked", ascending: false)]

if let users = try? managedObjectContext.fetch(request) {
return users
}

return [LikeUser]()
}

}
16 changes: 3 additions & 13 deletions WordPress/Classes/Services/PostService.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ extern const NSUInteger PostServiceDefaultNumberToSync;

@interface PostService : LocalCoreDataService

// This is public so it can be accessed from Swift extensions.
@property (nonnull, strong, nonatomic) PostServiceRemoteFactory *postServiceRemoteFactory;

- (instancetype)initWithManagedObjectContext:(NSManagedObjectContext *)context
postServiceRemoteFactory:(PostServiceRemoteFactory *)postServiceRemoteFactory NS_DESIGNATED_INITIALIZER;

Expand Down Expand Up @@ -171,19 +174,6 @@ forceDraftIfCreating:(BOOL)forceDraftIfCreating
success:(nullable void (^)(void))success
failure:(void (^)(NSError * _Nullable error))failure;

/**
Fetches a list of users that liked the post with the given ID.
@param postID The ID of the post to fetch likes for
@param siteID The ID of the site that contains the post
@param success A success block
@param failure A failure block
*/
- (void)getLikesForPostID:(NSNumber *)postID
siteID:(NSNumber *)siteID
success:(void (^)(NSArray<RemoteUser *> *users))success
failure:(void (^)(NSError * _Nullable error))failure;

@end

NS_ASSUME_NONNULL_END
25 changes: 0 additions & 25 deletions WordPress/Classes/Services/PostService.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

@interface PostService ()

@property (nonnull, strong, nonatomic) PostServiceRemoteFactory *postServiceRemoteFactory;

@end

@implementation PostService
Expand Down Expand Up @@ -684,29 +682,6 @@ - (void)restoreRemotePostWithPost:(AbstractPost*)post
[remote restorePost:remotePost success:successBlock failure:failureBlock];
}

// TODO: remove when PostServiceWPComTests is updated to use LikeUsers method.
- (void)getLikesForPostID:(NSNumber *)postID
siteID:(NSNumber *)siteID
success:(void (^)(NSArray<RemoteUser *> *))success
failure:(void (^)(NSError * _Nullable))failure
{
NSParameterAssert(postID);
NSParameterAssert(siteID);

PostServiceRemoteREST *remote = [self.postServiceRemoteFactory restRemoteForSiteID:siteID
context:self.managedObjectContext];
if (remote) {
[remote getLikesForPostID:postID
success:success
failure:failure];
} else {
NSError *error = [NSError errorWithDomain:PostServiceErrorDomain
code:0
userInfo:@{ NSLocalizedDescriptionKey : @"Unable to create a REST remote for posts." }];
failure(error);
}
}

#pragma mark - Helpers

- (void)initializeDraft:(AbstractPost *)post {
Expand Down
Loading

0 comments on commit 0d8a86d

Please sign in to comment.