Skip to content

Commit

Permalink
Gravatar Image Issues
Browse files Browse the repository at this point in the history
Working on wordpress-mobile#21415

I pushed a fix for the Gravatar URL issue, but the image still doesn't load dynamically when using accountStore.account.email. However, it works fine when I pass my email directly.
  • Loading branch information
Akshaykomar890 committed Feb 7, 2025
1 parent a7737fe commit 9baca46
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.View.OnClickListener
import androidx.appcompat.app.AppCompatActivity
Expand Down Expand Up @@ -483,16 +484,19 @@ class MeFragment : Fragment(R.layout.me_fragment), OnScrollToTopListener {
if (!FluxCUtils.isSignedInWPComOrHasWPOrgSite(accountStore, siteStore)) {
return
}

// we only want to show user details for WordPress.com users
if (accountStore.hasAccessToken()) {
val defaultAccount = accountStore.account
val email = defaultAccount.email
val avatarUrl = meGravatarLoader.constructGravatarUrl(email)
meDisplayName.visibility = View.VISIBLE
meUsername.visibility = View.VISIBLE
cardAvatar.visibility = View.VISIBLE
rowMyProfile.visibility = View.VISIBLE
myProfileDivider.visibility = View.VISIBLE
accountSettingsDivider.visibility = View.VISIBLE
loadAvatar(null)
loadAvatar(avatarUrl)
meUsername.text = getString(R.string.at_username, defaultAccount.userName)
meLoginLogoutTextView.setText(R.string.me_disconnect_from_wordpress_com)
meDisplayName.text = defaultAccount.displayName.ifEmpty { defaultAccount.userName }
Expand All @@ -516,7 +520,7 @@ class MeFragment : Fragment(R.layout.me_fragment), OnScrollToTopListener {

private fun MeFragmentBinding.loadAvatar(injectFilePath: String?, forceRefresh: Boolean = false) {
val newAvatarUploaded = !injectFilePath.isNullOrEmpty()
val avatarUrl = meGravatarLoader.constructGravatarUrl(accountStore.account.avatarUrl)
val avatarUrl = meGravatarLoader.constructGravatarUrl(accountStore.account.email)
val newAvatarSelected = newAvatarUploaded || forceRefresh
meGravatarLoader.load(
newAvatarSelected,
Expand All @@ -526,6 +530,7 @@ class MeFragment : Fragment(R.layout.me_fragment), OnScrollToTopListener {
AVATAR_WITHOUT_BACKGROUND,
object : RequestListener<Drawable> {
override fun onLoadFailed(e: Exception?, model: Any?) {
Log.e("Gravatar", "Failed to load image: ${e?.message}")
val appLogMessage = "onLoadFailed while loading Gravatar image!"
if (e == null) {
AppLog.e(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.wordpress.android.ui.main.utils

import android.graphics.drawable.Drawable
import android.util.Log
import android.widget.ImageView
import org.wordpress.android.R
import org.wordpress.android.WordPress
Expand All @@ -10,6 +11,8 @@ import org.wordpress.android.util.image.ImageManager
import org.wordpress.android.util.image.ImageManager.RequestListener
import org.wordpress.android.util.image.ImageType
import org.wordpress.android.viewmodel.ResourceProvider
import java.math.BigInteger
import java.security.MessageDigest
import javax.inject.Inject
import javax.inject.Singleton

Expand Down Expand Up @@ -57,10 +60,18 @@ class MeGravatarLoader @Inject constructor(
)
}
}

fun constructGravatarUrl(rawAvatarUrl: String, forceRefresh: Boolean = false): String {
fun constructGravatarUrl(email: String, forceRefresh: Boolean = false): String {
val avatarSz = resourseProvider.getDimensionPixelSize(R.dimen.avatar_sz_extra_small)
val cacheBuster = if (forceRefresh) System.currentTimeMillis().toString() else null
return WPAvatarUtils.rewriteAvatarUrl(rawAvatarUrl, avatarSz, cacheBuster)
val trimmedEmail = email.trim().lowercase()
val md5Hash = md5Hash(trimmedEmail)
val cacheBuster = if (forceRefresh) "?d=${System.currentTimeMillis()}" else ""
return "https://www.gravatar.com/avatar/$md5Hash?s=$avatarSz$cacheBuster"
}

fun md5Hash(input: String): String{
val md = MessageDigest.getInstance("MD5")
return BigInteger(1, md.digest(input.toByteArray()))
.toString(16)
.padStart(32, '0')
}
}

0 comments on commit 9baca46

Please sign in to comment.