Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions Modules/Sources/WordPressKit/BloggingPromptsServiceRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,6 @@ open class BloggingPromptsServiceRemote: ServiceRemoteWordPressComREST {
case encodingFailure
}

/// Fetches a number of blogging prompts for the specified site.
/// Note that this method hits wpcom/v2, which means the `WordPressComRestAPI` needs to be initialized with `LocaleKeyV2`.
///
/// - Parameters:
/// - siteID: Used to check which prompts have been answered for the site with given `siteID`.
/// - number: The number of prompts to query. When not specified, this will default to remote implementation.
/// - fromDate: When specified, this will fetch prompts from the given date. When not specified, this will default to remote implementation.
/// - completion: A closure that will be called when the fetch request completes.
open func fetchPrompts(for siteID: NSNumber,
number: Int? = nil,
fromDate: Date? = nil,
completion: @escaping (Result<[RemoteBloggingPrompt], Error>) -> Void) {
let path = path(forEndpoint: "sites/\(siteID)/blogging-prompts", withVersion: ._2_0)
let requestParameter: [String: AnyHashable] = {
var params = [String: AnyHashable]()

if let number, number > 0 {
params["number"] = number
}

if let fromDate {
// convert to yyyy-MM-dd format, excluding the timezone information.
// the date parameter doesn't need to be timezone-accurate since prompts are grouped by date.
params["from"] = Self.dateFormatter.string(from: fromDate)
}

return params
}()

let decoder = JSONDecoder.apiDecoder
// our API decoder assumes that we're converting from snake case.
// revert it to default so the CodingKeys match the actual response keys.
decoder.keyDecodingStrategy = .useDefaultKeys

Task { @MainActor in
await self.wordPressComRestApi
.perform(
.get,
URLString: path,
parameters: requestParameter as [String: AnyObject],
jsonDecoder: decoder,
type: [String: [RemoteBloggingPrompt]].self
)
.map { $0.body.values.first ?? [] }
.mapError { error -> Error in error.asNSError() }
.execute(completion)
}
}

/// Fetches the blogging prompts settings for a given site.
///
/// - Parameters:
Expand Down
42 changes: 4 additions & 38 deletions Sources/WordPressData/Swift/BloggingPrompt+CoreDataClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import CoreData
public class BloggingPrompt: NSManagedObject {

@nonobjc public class func fetchRequest() -> NSFetchRequest<BloggingPrompt> {
return NSFetchRequest<BloggingPrompt>(entityName: Self.classNameWithoutNamespaces())
NSFetchRequest<BloggingPrompt>(entityName: Self.classNameWithoutNamespaces())
}

@nonobjc public class func newObject(in context: NSManagedObjectContext) -> BloggingPrompt? {
return NSEntityDescription.insertNewObject(forEntityName: Self.entityName(), into: context) as? BloggingPrompt
NSEntityDescription.insertNewObject(forEntityName: Self.entityName(), into: context) as? BloggingPrompt
}

public override func awakeFromInsert() {
Expand All @@ -36,14 +36,10 @@ public class BloggingPrompt: NSManagedObject {
self.answerCount = Int32(remotePrompt.answeredUsersCount)
self.displayAvatarURLs = remotePrompt.answeredUserAvatarURLs
self.additionalPostTags = [String]() // reset previously additional tags.

if let brandContext = BrandContext(with: remotePrompt) {
brandContext.configure(self)
}
}

public func textForDisplay() -> String {
return text.stringByDecodingXMLCharacters().trim()
text.stringByDecodingXMLCharacters().trim()
}

/// Convenience method that checks if the given date is within the same day of the prompt's date without considering the timezone information.
Expand All @@ -55,7 +51,7 @@ public class BloggingPrompt: NSManagedObject {
/// - localDate: The date to compare against in local timezone.
/// - Returns: True if the year, month, and day components of the `localDate` matches the prompt's localized date.
public func inSameDay(as dateToCompare: Date) -> Bool {
return DateFormatters.utc.string(from: date) == DateFormatters.local.string(from: dateToCompare)
DateFormatters.utc.string(from: date) == DateFormatters.local.string(from: dateToCompare)
}

/// Used for comparison on upsert – there can't be two `BloggingPrompt` objects with the same date, so we can use it as a unique identifier
Expand All @@ -79,36 +75,6 @@ public extension BloggingPrompt {

private extension BloggingPrompt {

enum Constants {
static let bloganuaryTag = "bloganuary"
}

enum BrandContext {
case bloganuary(String)

init?(with remotePrompt: BloggingPromptRemoteObject) {
// Bloganuary context
if let bloganuaryId = remotePrompt.bloganuaryId,
bloganuaryId.contains(Constants.bloganuaryTag) {
self = .bloganuary(bloganuaryId)
return
}

return nil
}

/// Configures the given prompt with additional data based on the brand context.
///
/// - Parameter prompt: The `BloggingPrompt` instance to configure.
func configure(_ prompt: BloggingPrompt) {
switch self {
case .bloganuary(let id):
prompt.additionalPostTags = [Constants.bloganuaryTag, id]
prompt.attribution = BloggingPromptsAttribution.bloganuary.rawValue
}
}
}

struct DateFormatters {
static let local: DateFormatter = {
let formatter = DateFormatter()
Expand Down
13 changes: 2 additions & 11 deletions Sources/WordPressData/Swift/BloggingPromptRemoteObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public struct BloggingPromptRemoteObject {
let answeredUserAvatarURLs: [URL]
let answeredLink: URL?
let answeredLinkText: String
let bloganuaryId: String?

/// Used for comparison on import
public var dateString: String {
Expand All @@ -32,7 +31,6 @@ extension BloggingPromptRemoteObject: Decodable {
case answeredUserAvatarURLs = "answered_users_sample"
case answeredLink = "answered_link"
case answeredLinkText = "answered_link_text"
case bloganuaryId = "bloganuary_id"
}

/// meta structure to simplify decoding logic for user avatar objects.
Expand Down Expand Up @@ -65,20 +63,13 @@ extension BloggingPromptRemoteObject: Decodable {

self.answeredLink = {
guard let linkURLString = try? container.decode(String.self, forKey: .answeredLink),
let answeredLinkURL = URL(string: linkURLString) else {
let answeredLinkURL = URL(string: linkURLString)
else {
return nil
}
return answeredLinkURL
}()

self.answeredLinkText = try container.decode(String.self, forKey: .answeredLinkText)

self.bloganuaryId = {
guard let remoteBloganuaryId = try? container.decode(String.self, forKey: .bloganuaryId),
!remoteBloganuaryId.isEmpty else {
return nil
}
return remoteBloganuaryId
}()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
public enum BloggingPromptsAttribution: String {
case dayone
case bloganuary
}

This file was deleted.

This file was deleted.

Loading