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) {
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(" order by c.COM_CREATEDATE_D asc ");
Query q = em.createNativeQuery(sb.toString());
@ -106,6 +106,7 @@ public class CommentDao {
commentDto.setContent((String) o[i++]);
commentDto.setCreateTimestamp(((Timestamp) o[i++]).getTime());
commentDto.setCreatorName((String) o[i++]);
commentDto.setCreatorEmail((String) o[i++]);
commentDtoList.add(commentDto);
}
return commentDtoList;

View File

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

View File

@ -11,6 +11,8 @@ import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import com.sismics.util.mime.MimeType;
/**
@ -62,4 +64,21 @@ public class ImageUtil {
public static boolean isImage(String mimeType) {
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.rest.exception.ForbiddenClientException;
import com.sismics.rest.util.ValidationUtil;
import com.sismics.util.ImageUtil;
/**
* Comment REST resource.
@ -139,6 +140,7 @@ public class CommentResource extends BaseResource {
.add("id", commentDto.getId())
.add("content", commentDto.getContent())
.add("creator", commentDto.getCreatorName())
.add("creator_gravatar", ImageUtil.computeGravatar(commentDto.getCreatorEmail()))
.add("create_date", commentDto.getCreateTimestamp()));
}

View File

@ -72,15 +72,22 @@
<p ng-show="!comments && commentsError">Error loading comments</p>
</div>
<div ng-repeat="comment in comments" style="overflow: hidden">
<strong>{{ comment.creator }}</strong>
<p>
{{ comment.content }}<br />
<span class="text-muted">{{ comment.create_date | date: 'yyyy-MM-dd' }}</span>
<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>
<p>
{{ comment.content }}<br />
<span class="text-muted">{{ comment.create_date | date: 'yyyy-MM-dd' }}</span>
<span class="text-muted pull-right btn-link"
ng-show="document.writable || userInfo.username == comment.creator"
ng-click="deleteComment(comment)">Delete</span>
</p>
</p>
</div>
</div>
<form ng-submit="addComment()">

View File

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