mirror of
https://github.com/sismics/docs.git
synced 2024-11-22 14:07:55 +01:00
Closes #42: Gravatar images in comments
This commit is contained in:
parent
82b39586f0
commit
fc3a8bb4ae
@ -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;
|
||||||
|
@ -19,6 +19,11 @@ public class CommentDto {
|
|||||||
*/
|
*/
|
||||||
private String creatorName;
|
private String creatorName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creator email.
|
||||||
|
*/
|
||||||
|
private String creatorEmail;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Content.
|
* Content.
|
||||||
*/
|
*/
|
||||||
@ -44,6 +49,14 @@ public class CommentDto {
|
|||||||
public void setCreatorName(String creatorName) {
|
public void setCreatorName(String creatorName) {
|
||||||
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;
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
17
docs-core/src/test/java/com/sismics/util/TestImageUtil.java
Normal file
17
docs-core/src/test/java/com/sismics/util/TestImageUtil.java
Normal 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 "));
|
||||||
|
}
|
||||||
|
}
|
@ -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()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,15 +72,22 @@
|
|||||||
<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">
|
||||||
<strong>{{ comment.creator }}</strong>
|
<div class="pull-left">
|
||||||
<p>
|
<a href="#">
|
||||||
{{ comment.content }}<br />
|
<img ng-src="http://www.gravatar.com/avatar/{{ comment.creator_gravatar }}?s=40&d=identicon" class="media-object" />
|
||||||
<span class="text-muted">{{ comment.create_date | date: 'yyyy-MM-dd' }}</span>
|
</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"
|
<span class="text-muted pull-right btn-link"
|
||||||
ng-show="document.writable || userInfo.username == comment.creator"
|
ng-show="document.writable || userInfo.username == comment.creator"
|
||||||
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()">
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user