Gravatar – the LotusScript solution

On the XPages Blog, there’s an article by Jeremy Hodge, ‘Gravatar – XPages Managed Bean‘. Gravatars are those little images of people that you see in blog comments that seem to work across most blogs. For those of us who haven’t upgraded our servers to 8.5.x, I thought I’d post the LotusScript solution that we use that can be.

In our case we have a central LotusScript Agent that takes a username as parameter and then checks the user’s record to see if they have uploaded a photograph or if they have entered a gravatar id. If neither, then it displays a holding image.

Gravatar embedded in a discussion

As Jeremy’s article explains, to generate a gravatar image you need to request the user’s image from Gravatar’s server:

http://www.gravatar.com/avatar/HASH?parameters

The hash is explained on the Gravtar developer site. But basically you send a MD5 hash of the email address the user registered with Gravatar.

The difficult bit is generating the MD5 hash of the Gravatar username. Luckily there’s an excellent LotusScript MD5 hash library maintained by Tom O’Neil.

SHA1/MD5 Libraries for Lotuscript

Generating the hash is extremely simple. First reference the ‘MD5’ library in the Agent’s ‘Options’. Then all you do is create a new MD5 object passing it the email address you wish to hash.

Dim strGravatarEmail as string
strGravatarEmail = "usersemail@mysite.com"
Dim myMD5 As New MD5(strGravatarEmail)
Print {[http://www.gravatar.com/avatar/}+myMD5.toHex()+{?s=100&r=pg]}

You can also pass a number of parameters with your image request. The two we user are ‘s’ – size and ‘r’ – rating. In our case we always request ‘pg’ images.

To insert a gravatar image into your form or view you reference your agent as the image’s source:

<img src="generateGravatar?openagent&username=Paul+Hudson" alt="Photo of ..." />

In most cases, you will want to set up the agent to take a username as parameter.

Be aware that every time you request a Gravatar image, you are running an Agent. So make sure your servers are up to it. In our case, we only use the Agent in views and forms. If the page output is generated by an Agent then it generates the gravatars. I would also suggest you set up some website rules so that you can set a HTTP response header that sets anexpiry date on the agent. This should reduce the frequency of requests.

Leave a Reply