Skip to content

Commit 74cb1e4

Browse files
authored
Address Gravatar deprecation warnings around user auth flows (#16340)
2 parents a77a19f + 62074b2 commit 74cb1e4

File tree

3 files changed

+22
-78
lines changed

3 files changed

+22
-78
lines changed

Modules/Sources/WordPressUI/Extensions/Gravatar/Gravatar.swift

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,5 @@
11
import Foundation
22

3-
/// Helper Enum that specifies all of the available Gravatar Image Ratings
4-
/// TODO: Convert into a pure Swift String Enum. It's done this way to maintain ObjC Compatibility
5-
///
6-
@available(*, deprecated, message: "Use `Rating` from the Gravatar iOS SDK. See: https://github.com/Automattic/Gravatar-SDK-iOS.")
7-
@objc
8-
public enum GravatarRatings: Int {
9-
case g
10-
case pg
11-
case r
12-
case x
13-
case `default`
14-
15-
func stringValue() -> String {
16-
switch self {
17-
case .default:
18-
fallthrough
19-
case .g:
20-
return "g"
21-
case .pg:
22-
return "pg"
23-
case .r:
24-
return "r"
25-
case .x:
26-
return "x"
27-
}
28-
}
29-
}
30-
313
/// Helper Enum that specifies some of the options for default images
324
/// To see all available options, visit : https://en.gravatar.com/site/implement/images/
335
///
@@ -84,14 +56,14 @@ public struct Gravatar {
8456
public static func gravatarUrl(for email: String,
8557
defaultImage: GravatarDefaultImage? = nil,
8658
size: Int? = nil,
87-
rating: GravatarRatings = .default) -> URL? {
59+
rating: Rating = .general) -> URL? {
8860
let hash = gravatarHash(of: email)
8961
let targetURL = String(format: "%@/%@?d=%@&s=%d&r=%@",
9062
Defaults.baseURL,
9163
hash,
9264
defaultImage?.rawValue ?? GravatarDefaultImage.fileNotFound.rawValue,
9365
size ?? Defaults.imageSize,
94-
rating.stringValue())
66+
rating.rawValue)
9567
return URL(string: targetURL)
9668
}
9769

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Foundation
2+
3+
/// Sources:
4+
/// - https://github.com/Automattic/Gravatar-SDK-iOS/blob/trunk/Sources/Gravatar/Options/Rating.swift
5+
/// Gravatar allows users to self-rate their images so that they can indicate if an image is appropriate for a certain audience. By default, only `general`
6+
/// rated
7+
/// images are displayed unless you indicate that you would like to see higher ratings.
8+
///
9+
/// If the requested email hash does not have an image meeting the requested rating level, then the default avatar is returned (See: ``DefaultAvatarOption``)
10+
public enum Rating: String, Sendable, CaseIterable {
11+
/// Suitable for display on all websites with any audience type.
12+
case general = "g"
13+
/// May contain rude gestures, provocatively dressed individuals, the lesser swear words, or mild violence.
14+
case parentalGuidance = "pg"
15+
/// May contain such things as harsh profanity, intense violence, nudity, or hard drug use.
16+
case restricted = "r"
17+
/// May contain sexual imagery or extremely disturbing violence.
18+
case x
19+
}

Modules/Sources/WordPressUI/Extensions/UIImageView+Gravatar.swift

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,13 @@ private class GravatarNotificationWrapper {
2121
/// UIImageView Helper Methods that allow us to download a Gravatar, given the User's Email
2222
///
2323
extension UIImageView {
24-
25-
/// Downloads and sets the User's Gravatar, given his email.
26-
/// TODO: This is a convenience method. Please, remove once all of the code has been migrated over to Swift.
27-
///
28-
/// - Parameters:
29-
/// - email: the user's email
30-
/// - rating: expected image rating
31-
///
32-
/// This method uses deprecated types. Please check the deprecation warning in `GravatarRatings`. Also check out the UIImageView extension from the Gravatar iOS SDK as an alternative to download images. See: https://github.com/Automattic/Gravatar-SDK-iOS.
33-
@available(*, deprecated, message: "Usage of the deprecated type: GravatarRatings.")
34-
@objc
35-
public func downloadGravatarWithEmail(_ email: String, rating: GravatarRatings) {
36-
downloadGravatarWithEmail(email, rating: rating, placeholderImage: .gravatarPlaceholderImage)
37-
}
38-
3924
/// Downloads and sets the User's Gravatar, given his email.
4025
///
4126
/// - Parameters:
4227
/// - email: the user's email
4328
/// - rating: expected image rating
4429
/// - placeholderImage: Image to be used as Placeholder
45-
/// This method uses deprecated types. Please check the deprecation warning in `GravatarRatings`. Also check out the UIImageView extension from the Gravatar iOS SDK as an alternative to download images. See: https://github.com/Automattic/Gravatar-SDK-iOS.
46-
@available(*, deprecated, message: "Usage of the deprecated type: GravatarRatings.")
47-
@objc
48-
public func downloadGravatarWithEmail(_ email: String, rating: GravatarRatings = .default, placeholderImage: UIImage = .gravatarPlaceholderImage) {
30+
public func downloadGravatarWithEmail(_ email: String, rating: Rating = .general, placeholderImage: UIImage = .gravatarPlaceholderImage) {
4931
let gravatarURL = Gravatar.gravatarUrl(for: email, size: gravatarDefaultSize(), rating: rating)
5032

5133
listenForGravatarChanges(forEmail: email)
@@ -124,35 +106,6 @@ extension UIImageView {
124106
})
125107
}
126108

127-
/// Sets an Image Override in both, AFNetworking's Private Cache + NSURLCache
128-
///
129-
/// - Parameters:
130-
/// - image: new UIImage
131-
/// - rating: rating for the new image.
132-
/// - email: associated email of the new gravatar
133-
/// - Note: You may want to use `updateGravatar(image:, email:)` instead
134-
///
135-
/// *WHY* is this required?. *WHY* life has to be so complicated?, is the universe against us?
136-
/// This has been implemented as a workaround. During Upload, we want any async calls made to the
137-
/// `downloadGravatar` API to return the "Fresh" image.
138-
///
139-
/// Note II:
140-
/// We cannot just clear NSURLCache, since the helper that's supposed to do that, is broken since iOS 8.
141-
/// Ref: Ref: http://blog.airsource.co.uk/2014/10/11/nsurlcache-ios8-broken/
142-
///
143-
/// P.s.:
144-
/// Hope buddah, and the code reviewer, can forgive me for this hack.
145-
///
146-
@available(*, deprecated, message: "Usage of the deprecated type: GravatarRatings.")
147-
@objc public func overrideGravatarImageCache(_ image: UIImage, rating: GravatarRatings, email: String) {
148-
guard let gravatarURL = Gravatar.gravatarUrl(for: email, size: gravatarDefaultSize(), rating: rating) else {
149-
return
150-
}
151-
152-
listenForGravatarChanges(forEmail: email)
153-
overrideImageCache(for: gravatarURL, with: image)
154-
}
155-
156109
/// Updates the gravatar image for the given email, and notifies all gravatar image views
157110
///
158111
/// - Parameters:

0 commit comments

Comments
 (0)