Closes #42: Gravatar images in comments

This commit is contained in:
jendib 2015-11-19 00:05:04 +01:00
parent 82b39586f0
commit fc3a8bb4ae
7 changed files with 67 additions and 7 deletions

View File

@ -90,7 +90,7 @@ public class CommentDao {
*/ */
public List<CommentDto> getByDocumentId(String documentId) { public List<CommentDto> getByDocumentId(String documentId) {
EntityManager em = ThreadLocalContext.get().getEntityManager(); EntityManager em = ThreadLocalContext.get().getEntityManager();
StringBuilder sb = new StringBuilder("select c.COM_ID_C, c.COM_CONTENT_C, c.COM_CREATEDATE_D, u.USE_USERNAME_C from T_COMMENT c, T_USER u"); StringBuilder sb = new StringBuilder("select c.COM_ID_C, c.COM_CONTENT_C, c.COM_CREATEDATE_D, u.USE_USERNAME_C, u.USE_EMAIL_C from T_COMMENT c, T_USER u");
sb.append(" where c.COM_IDDOC_C = :documentId and c.COM_IDUSER_C = u.USE_ID_C and c.COM_DELETEDATE_D is null "); sb.append(" where c.COM_IDDOC_C = :documentId and c.COM_IDUSER_C = u.USE_ID_C and c.COM_DELETEDATE_D is null ");
sb.append(" order by c.COM_CREATEDATE_D asc "); sb.append(" order by c.COM_CREATEDATE_D asc ");
Query q = em.createNativeQuery(sb.toString()); Query q = em.createNativeQuery(sb.toString());
@ -106,6 +106,7 @@ public class CommentDao {
commentDto.setContent((String) o[i++]); commentDto.setContent((String) o[i++]);
commentDto.setCreateTimestamp(((Timestamp) o[i++]).getTime()); commentDto.setCreateTimestamp(((Timestamp) o[i++]).getTime());
commentDto.setCreatorName((String) o[i++]); commentDto.setCreatorName((String) o[i++]);
commentDto.setCreatorEmail((String) o[i++]);
commentDtoList.add(commentDto); commentDtoList.add(commentDto);
} }
return commentDtoList; return commentDtoList;

View File

@ -19,6 +19,11 @@ public class CommentDto {
*/ */
private String creatorName; private String creatorName;
/**
* Creator email.
*/
private String creatorEmail;
/** /**
* Content. * Content.
*/ */
@ -45,6 +50,14 @@ public class CommentDto {
this.creatorName = creatorName; this.creatorName = creatorName;
} }
public String getCreatorEmail() {
return creatorEmail;
}
public void setCreatorEmail(String creatorEmail) {
this.creatorEmail = creatorEmail;
}
public String getContent() { public String getContent() {
return content; return content;
} }

View File

@ -11,6 +11,8 @@ import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter; import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream; import javax.imageio.stream.ImageOutputStream;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import com.sismics.util.mime.MimeType; import com.sismics.util.mime.MimeType;
/** /**
@ -62,4 +64,21 @@ public class ImageUtil {
public static boolean isImage(String mimeType) { public static boolean isImage(String mimeType) {
return mimeType.equals(MimeType.IMAGE_GIF) || mimeType.equals(MimeType.IMAGE_PNG) || mimeType.equals(MimeType.IMAGE_JPEG); return mimeType.equals(MimeType.IMAGE_GIF) || mimeType.equals(MimeType.IMAGE_PNG) || mimeType.equals(MimeType.IMAGE_JPEG);
} }
/**
* Compute Gravatar hash.
* See https://en.gravatar.com/site/implement/hash/.
*
* @param email
* @return Gravatar hash
*/
public static String computeGravatar(String email) {
if (email == null) {
return null;
}
return Hashing.md5().hashString(
email.trim().toLowerCase(), Charsets.UTF_8)
.toString();
}
} }

View File

@ -0,0 +1,17 @@
package com.sismics.util;
import junit.framework.Assert;
import org.junit.Test;
/**
* Test of the image utilities.
*
* @author bgamard
*/
public class TestImageUtil {
@Test
public void computeGravatarTest() throws Exception {
Assert.assertEquals("0bc83cb571cd1c50ba6f3e8a78ef1346", ImageUtil.computeGravatar("MyEmailAddress@example.com "));
}
}

View File

@ -21,6 +21,7 @@ import com.sismics.docs.core.dao.jpa.dto.CommentDto;
import com.sismics.docs.core.model.jpa.Comment; import com.sismics.docs.core.model.jpa.Comment;
import com.sismics.rest.exception.ForbiddenClientException; import com.sismics.rest.exception.ForbiddenClientException;
import com.sismics.rest.util.ValidationUtil; import com.sismics.rest.util.ValidationUtil;
import com.sismics.util.ImageUtil;
/** /**
* Comment REST resource. * Comment REST resource.
@ -139,6 +140,7 @@ public class CommentResource extends BaseResource {
.add("id", commentDto.getId()) .add("id", commentDto.getId())
.add("content", commentDto.getContent()) .add("content", commentDto.getContent())
.add("creator", commentDto.getCreatorName()) .add("creator", commentDto.getCreatorName())
.add("creator_gravatar", ImageUtil.computeGravatar(commentDto.getCreatorEmail()))
.add("create_date", commentDto.getCreateTimestamp())); .add("create_date", commentDto.getCreateTimestamp()));
} }

View File

@ -72,7 +72,13 @@
<p ng-show="!comments && commentsError">Error loading comments</p> <p ng-show="!comments && commentsError">Error loading comments</p>
</div> </div>
<div ng-repeat="comment in comments" style="overflow: hidden"> <div ng-repeat="comment in comments" class="media" style="overflow: hidden">
<div class="pull-left">
<a href="#">
<img ng-src="http://www.gravatar.com/avatar/{{ comment.creator_gravatar }}?s=40&d=identicon" class="media-object" />
</a>
</div>
<div class="media-body">
<strong>{{ comment.creator }}</strong> <strong>{{ comment.creator }}</strong>
<p> <p>
{{ comment.content }}<br /> {{ comment.content }}<br />
@ -82,6 +88,7 @@
ng-click="deleteComment(comment)">Delete</span> ng-click="deleteComment(comment)">Delete</span>
</p> </p>
</div> </div>
</div>
<form ng-submit="addComment()"> <form ng-submit="addComment()">
<div class="form-group"> <div class="form-group">

View File

@ -127,6 +127,7 @@ public class TestCommentResource extends BaseJerseyTest {
Assert.assertEquals(comment2Id, comment.getString("id")); Assert.assertEquals(comment2Id, comment.getString("id"));
Assert.assertEquals("Comment by comment2", comment.getString("content")); Assert.assertEquals("Comment by comment2", comment.getString("content"));
Assert.assertEquals("comment2", comment.getString("creator")); Assert.assertEquals("comment2", comment.getString("creator"));
Assert.assertEquals("d6e56c42f61983bba80d370138763420", comment.getString("creator_gravatar"));
Assert.assertNotNull(comment.getJsonNumber("create_date")); Assert.assertNotNull(comment.getJsonNumber("create_date"));
// Delete a comment with comment2 // Delete a comment with comment2