diff options
author | James Moger <james.moger@gitblit.com> | 2011-11-21 09:24:05 -0500 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2011-11-21 09:24:05 -0500 |
commit | 797322eb90a1f5d21166fd691479b050ad7b754b (patch) | |
tree | 4f646539429037515e38ade9fe469efaf6900b4e /src/com/gitblit/utils | |
parent | 712210f4c56cc9b74455c9b994f9102a178288c9 (diff) | |
download | gitblit-797322eb90a1f5d21166fd691479b050ad7b754b.tar.gz gitblit-797322eb90a1f5d21166fd691479b050ad7b754b.zip |
Tighter Gravatar integration. New window/tab on most activity links.
Each Gravatar thumbnail is now a link to the Gitblit Gravatar profile
page. This page displays some of the profile information and contains a
link to the full Gravatar profile.
Activity page links now launch a new window/tab instead of targeting
"self" since it can be expensive to requery the activity information.
Diffstat (limited to 'src/com/gitblit/utils')
-rw-r--r-- | src/com/gitblit/utils/ActivityUtils.java | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/com/gitblit/utils/ActivityUtils.java b/src/com/gitblit/utils/ActivityUtils.java index 8c8a7ec2..204fe3c3 100644 --- a/src/com/gitblit/utils/ActivityUtils.java +++ b/src/com/gitblit/utils/ActivityUtils.java @@ -15,7 +15,11 @@ */
package com.gitblit.utils;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.lang.reflect.Type;
import java.text.DateFormat;
+import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
@@ -33,8 +37,10 @@ import org.eclipse.jgit.revwalk.RevCommit; import com.gitblit.GitBlit;
import com.gitblit.models.Activity;
import com.gitblit.models.Activity.RepositoryCommit;
+import com.gitblit.models.GravatarProfile;
import com.gitblit.models.RefModel;
import com.gitblit.models.RepositoryModel;
+import com.google.gson.reflect.TypeToken;
/**
* Utility class for building activity information from repositories.
@@ -127,4 +133,64 @@ public class ActivityUtils { }
return recentActivity;
}
+
+ /**
+ * Returns the Gravatar profile, if available, for the specified email
+ * address.
+ *
+ * @param emailaddress
+ * @return a Gravatar Profile
+ * @throws IOException
+ */
+ public static GravatarProfile getGravatarProfileFromAddress(String emailaddress)
+ throws IOException {
+ return getGravatarProfile(StringUtils.getMD5(emailaddress.toLowerCase()));
+ }
+
+ /**
+ * Creates a Gravatar thumbnail url from the specified email address.
+ *
+ * @param email
+ * address to query Gravatar
+ * @param width
+ * size of thumbnail. if width <= 0, the defalt of 60 is used.
+ * @return
+ */
+ public static String getGravatarThumbnailUrl(String email, int width) {
+ if (width <= 0) {
+ width = 60;
+ }
+ String emailHash = StringUtils.getMD5(email);
+ String url = MessageFormat.format(
+ "http://www.gravatar.com/avatar/{0}?s={1,number,0}&d=identicon", emailHash, width);
+ return url;
+ }
+
+ /**
+ * Returns the Gravatar profile, if available, for the specified hashcode.
+ * address.
+ *
+ * @param hash
+ * the hash of the email address
+ * @return a Gravatar Profile
+ * @throws IOException
+ */
+ public static GravatarProfile getGravatarProfile(String hash) throws IOException {
+ String url = MessageFormat.format("http://www.gravatar.com/{0}.json", hash);
+ // Gravatar has a complex json structure
+ Type profileType = new TypeToken<Map<String, List<GravatarProfile>>>() {
+ }.getType();
+ Map<String, List<GravatarProfile>> profiles = null;
+ try {
+ profiles = JsonUtils.retrieveJson(url, profileType);
+ } catch (FileNotFoundException e) {
+ }
+ if (profiles == null || profiles.size() == 0) {
+ return null;
+ }
+ // due to the complex json structure we need to pull out the profile
+ // from a list 2 levels deep
+ GravatarProfile profile = profiles.values().iterator().next().get(0);
+ return profile;
+ }
}
|