Gravatar part on Comment type

Topics: General
Jul 20, 2013 at 2:51 AM
Edited Jul 20, 2013 at 3:02 AM
Has anyone tried adding the Gravatar part from the Webmoco.Gravatar module on to a Comment type? I was able to add it to a BlogPost type and it showed up fine when I added a placement entry, so the module is working fine. My problem is that I can't figure how to do placement for it in a Comment type.

Any suggestions on how I would do that?

<Match ContentType="Comment">
<Place Parts_Gravatar="Content"/>
</Match>

doesn't seem to add it to the page at all.

I also tried adding <Place Parts_Gravatar="Content"/> to the <Match ContentType="BlogPost"> section as well.

Thanks for the help =)
Jul 20, 2013 at 3:16 AM
Edited Jul 20, 2013 at 3:23 AM
Doesn't appear to be a placement issue as far as I can tell. It just doesn't seem to want to render on a Comment Type at all.

ACTUALLY:

I'm looking at the Driver, and the module looks like it only renders a gravatar for the current logged in user...it doesn't look at the owner or author field of a Blog or Comment. Alright...ditch that idea =) LOL
Developer
Jul 20, 2013 at 10:02 AM
I have a Gravatar module on my github account called NGravatar. I haven't got it as a part... But should be trivial to add.. Want me to do it??

An example of its use is on my forum site...
Jul 20, 2013 at 4:59 PM
Hey Nick,

I decided to write a shape last night and include it on the Comment shape...just to see what I could come up with
@Display.CommentGravatar(ContentPart: comment)
All the code is in the shape (which is not ideal), but I just wanted a proof of concept
@using Orchard.Comments.Models
@using Orchard.ContentManagement
@using System.Security.Cryptography
@using System.Text
@using Orchard.Users.Models
@{
    CommentPart comment = Model.ContentPart;
    var cm = WorkContext.Resolve<IContentManager>();

    string sUserEmail = string.Empty;

    if (String.IsNullOrWhiteSpace(comment.UserName)) {
        sUserEmail = comment.Email;
    }
    else {
        var user = cm.Query<UserPart, UserPartRecord>().Where(x => x.NormalizedUserName == comment.UserName).List().FirstOrDefault();

        if (user != null) {
            sUserEmail = user.Email;
        }
    }
    
    if (!String.IsNullOrWhiteSpace(sUserEmail)) {
        // Trim the string
        string input = sUserEmail.ToLowerInvariant().Trim();
        
        // step 1, calculate MD5 hash from input
        var md5 = System.Security.Cryptography.MD5.Create();
        var inputBytes = System.Text.Encoding.UTF8.GetBytes(input);
        var hash = md5.ComputeHash(inputBytes);

        // step 2, convert byte array to hex string
        var sb = new StringBuilder();
        for (var i = 0; i < hash.Length; i++) {
            sb.Append(hash[i].ToString("X2"));
        }

        var hash2 = sb.ToString().ToLowerInvariant();

        var sb2 = new StringBuilder();

        // Add the url
        if (WorkContext.HttpContext.Request.IsSecureConnection) {
            sb2.Append("https://secure.gravatar.com/avatar");
        }
        else {
            sb2.Append("http://www.gravatar.com/avatar/");
        }

        // Add the hash & options
        sb2.Append(hash2);
        sb2.Append(".png?s=60&d=mm&r=g");

        <img src="@Html.Raw(sb2)" alt="@T("Gravatar Image")" />
    }
    else {
        <img src="@Href(Html.ThemePath(WorkContext.CurrentTheme, "/Content/avatar.png"))" alt="" />
    }
}

I basically pulled apart the Gravatar module service files and used the pertinent code.

I'll take a look at your NGravatar module for comparison. How trivial do you think it would be to make your module work for Comment types? If it's not too much work and you think it would be worthwhile, I think it would make a great module addition.

Cheers! =)
Jul 20, 2013 at 5:49 PM
Edited Jul 20, 2013 at 5:54 PM
Much better!

I installed your module and my shape now looks like this:
@using Orchard.Comments.Models
@using Orchard.ContentManagement
@using Orchard.Users.Models
@using NGravatar.Html

@{
    CommentPart comment = Model.ContentPart;
    string gravatarEmail = string.Empty;

    if (String.IsNullOrWhiteSpace(comment.UserName)) {
        gravatarEmail = comment.Email;
    }
    else {
        var cm = WorkContext.Resolve<IContentManager>();
        var user = cm.Query<UserPart, UserPartRecord>().Where(x => x.NormalizedUserName == comment.UserName).List().FirstOrDefault();
        
        if (user != null) {
            gravatarEmail = user.Email;
        }
    }
    
    var gravatarUrl = Url.Gravatar(string.IsNullOrWhiteSpace(gravatarEmail) ? "dummy@foobar.com" : gravatarEmail, 60, "mm", NGravatar.Rating.G);
}

<img src="@gravatarUrl" alt="@T("Gravatar Image")" />
So much cleaner. Is there a better way to get to the user email from the Comment.UserName than what I'm doing here? It's the only way I could think of since the CommentPart doesn't look like it actually stores an Owner object (which it really can't because Comments can be anonymous).
May 4, 2014 at 10:33 AM
@psenechal

Thanks a lot. This way solved my issue, too. :)