Skip to content

Commit

Permalink
#444 Add a notification at comment liked
Browse files Browse the repository at this point in the history
  • Loading branch information
koda-masaru committed Aug 28, 2017
1 parent 11bbb6d commit 1f52962
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.support.project.knowledge.entity.NotifyQueuesEntity;
import org.support.project.knowledge.logic.MailEventLogic;
import org.support.project.knowledge.logic.notification.CommentInsertNotification;
import org.support.project.knowledge.logic.notification.CommentLikedNotification;
import org.support.project.knowledge.logic.notification.KnowledgeUpdateNotification;
import org.support.project.knowledge.logic.notification.LikeInsertNotification;
import org.support.project.knowledge.logic.notification.QueueNotification;
Expand Down Expand Up @@ -59,6 +60,8 @@ private void start() throws Exception {
CommentInsertNotification.get().notify(notifyQueuesEntity);
} else if (notifyQueuesEntity.getType() == QueueNotification.TYPE_KNOWLEDGE_LIKE) {
LikeInsertNotification.get().notify(notifyQueuesEntity);
} else if (notifyQueuesEntity.getType() == QueueNotification.TYPE_COMMENT_LIKE) {
CommentLikedNotification.get().notify(notifyQueuesEntity);
}
// 通知のキューから削除
//notifyQueuesDao.delete(notifyQueuesEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1192,11 +1192,11 @@ public Long addLikeComment(Long commentNo, LoginedUser loginedUser, Locale local
}
LikeCommentsEntity like = new LikeCommentsEntity();
like.setCommentNo(commentNo);
LikeCommentsDao.get().insert(like);
like = LikeCommentsDao.get().insert(like);
Long count = LikeCommentsDao.get().selectOnCommentNo(commentNo);

// 通知 TODO
// NotifyLogic.get().notifyOnKnowledgeLiked(knowledgeId, likesEntity);
// 通知
NotifyLogic.get().notifyOnCommentLiked(like);

return count;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class MailLogic {
public static final String NOTIFY_INSERT_KNOWLEDGE = "notify_insert_knowledge";
public static final String NOTIFY_UPDATE_KNOWLEDGE = "notify_update_knowledge";
public static final String NOTIFY_INSERT_LIKE_MYITEM = "notify_insert_like_myitem";
public static final String NOTIFY_INSERT_LIKE_COMMENT_MYITEM = "notify_insert_like_my_comment_item";
public static final String NOTIFY_INSERT_COMMENT = "notify_insert_comment";
public static final String NOTIFY_INSERT_COMMENT_MYITEM = "notify_insert_comment_myitem";
public static final String NOTIFY_ADD_PARTICIPATE = "notify_add_participate";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.support.project.knowledge.logic.notification.AcceptCheckUserNotification;
import org.support.project.knowledge.logic.notification.AddUserNotification;
import org.support.project.knowledge.logic.notification.CommentInsertNotification;
import org.support.project.knowledge.logic.notification.CommentLikedNotification;
import org.support.project.knowledge.logic.notification.EventNotificationByWeek;
import org.support.project.knowledge.logic.notification.KnowledgeUpdateNotification;
import org.support.project.knowledge.logic.notification.LikeInsertNotification;
Expand Down Expand Up @@ -52,6 +53,8 @@ public Notification getNotification(String category) {
return CommentInsertNotification.get();
} else if (MailLogic.NOTIFY_INSERT_LIKE_MYITEM.equals(category)) {
return LikeInsertNotification.get();
} else if (MailLogic.NOTIFY_INSERT_LIKE_COMMENT_MYITEM.equals(category)) {
return CommentLikedNotification.get();
} else if (MailLogic.NOTIFY_EVENT.equals(category)) {
return EventNotificationByWeek.get();
} else if (MailLogic.NOTIFY_REGISTRATION_EVENT.equals(category)) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/support/project/knowledge/logic/NotifyLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import org.support.project.knowledge.config.SystemConfig;
import org.support.project.knowledge.entity.CommentsEntity;
import org.support.project.knowledge.entity.KnowledgesEntity;
import org.support.project.knowledge.entity.LikeCommentsEntity;
import org.support.project.knowledge.entity.LikesEntity;
import org.support.project.knowledge.logic.notification.CommentInsertNotification;
import org.support.project.knowledge.logic.notification.CommentLikedNotification;
import org.support.project.knowledge.logic.notification.DesktopNotification;
import org.support.project.knowledge.logic.notification.KnowledgeUpdateNotification;
import org.support.project.knowledge.logic.notification.LikeInsertNotification;
Expand Down Expand Up @@ -158,6 +160,14 @@ public void notifyOnKnowledgeComment(Long knowledgeId, CommentsEntity commentsEn
notify.insertNotifyQueue();
notifyDeskTop(notify);
}

public void notifyOnCommentLiked(LikeCommentsEntity like) {
CommentLikedNotification notify = CommentLikedNotification.get();
notify.setLike(like);
notify.setType(QueueNotification.TYPE_COMMENT_LIKE);
notify.insertNotifyQueue();
notifyDeskTop(notify);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package org.support.project.knowledge.logic.notification;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.support.project.common.config.INT_FLAG;
import org.support.project.common.config.Resources;
import org.support.project.common.log.Log;
import org.support.project.common.log.LogFactory;
import org.support.project.common.util.RandomUtil;
import org.support.project.common.util.StringUtils;
import org.support.project.di.Container;
import org.support.project.di.DI;
import org.support.project.di.Instance;
import org.support.project.knowledge.dao.CommentsDao;
import org.support.project.knowledge.dao.KnowledgesDao;
import org.support.project.knowledge.dao.LikeCommentsDao;
import org.support.project.knowledge.dao.NotifyConfigsDao;
import org.support.project.knowledge.dao.NotifyQueuesDao;
import org.support.project.knowledge.entity.CommentsEntity;
import org.support.project.knowledge.entity.KnowledgesEntity;
import org.support.project.knowledge.entity.LikeCommentsEntity;
import org.support.project.knowledge.entity.MailLocaleTemplatesEntity;
import org.support.project.knowledge.entity.NotifyConfigsEntity;
import org.support.project.knowledge.entity.NotifyQueuesEntity;
import org.support.project.knowledge.logic.MailLogic;
import org.support.project.knowledge.logic.NotificationLogic;
import org.support.project.knowledge.logic.NotifyLogic;
import org.support.project.knowledge.vo.notification.LikeInsert;
import org.support.project.web.bean.LoginedUser;
import org.support.project.web.bean.MessageResult;
import org.support.project.web.dao.NotificationsDao;
import org.support.project.web.dao.UsersDao;
import org.support.project.web.entity.NotificationsEntity;
import org.support.project.web.entity.UsersEntity;

import net.arnx.jsonic.JSON;

@DI(instance = Instance.Prototype)
public class CommentLikedNotification extends AbstractQueueNotification implements DesktopNotification {
/** ログ */
private static final Log LOG = LogFactory.getLog(CommentLikedNotification.class);
/** インスタンス取得 */
public static CommentLikedNotification get() {
return Container.getComp(CommentLikedNotification.class);
}

private LikeCommentsEntity like;
/**
* @return the like
*/
public LikeCommentsEntity getLike() {
return like;
}
/**
* @param like the like to set
*/
public void setLike(LikeCommentsEntity like) {
this.like = like;
}

/** like id what is sended */
private List<Long> sended = new ArrayList<>();


@Override
public void insertNotifyQueue() {
NotifyQueuesEntity entity = new NotifyQueuesEntity();
entity.setHash(RandomUtil.randamGen(30));
entity.setType(TYPE_COMMENT_LIKE);
entity.setId(like.getNo());

NotifyQueuesDao notifyQueuesDao = NotifyQueuesDao.get();
NotifyQueuesEntity exist = notifyQueuesDao.selectOnTypeAndId(entity.getType(), entity.getId());
if (exist == null) {
notifyQueuesDao.insert(entity);
}
}
@Override
public void notify(NotifyQueuesEntity notifyQueue) throws Exception {
LikeCommentsEntity like = LikeCommentsDao.get().selectOnKey(notifyQueue.getId());
if (null == like) {
LOG.warn("LikeComment record not found. id: " + notifyQueue.getId());
return;
}
CommentsEntity comment = CommentsDao.get().selectOnKey(like.getCommentNo());
if (null == comment) {
LOG.warn("Comment record not found.");
return;
}
KnowledgesEntity knowledge = KnowledgesDao.get().selectOnKey(comment.getKnowledgeId());
if (null == knowledge) {
LOG.warn("Knowledge record not found.");
return;
}

if (sended.contains(comment.getCommentNo())) {
// この通知キューでは、コメント対し何件のイイネがあっても1回送るだけになる
if (LOG.isDebugEnabled()) {
LOG.debug("comment [" + comment.getCommentNo() + "] ");
}
return;
} else {
sended.add(comment.getCommentNo());
}

// イイネを押したユーザ
UsersEntity likeUser = UsersDao.get().selectOnKey(like.getInsertUser());

// 宛先のユーザ(コメントを登録したユーザ)
UsersEntity user = UsersDao.get().selectOnKey(comment.getInsertUser());
if (user != null) {
NotifyConfigsDao notifyConfigsDao = NotifyConfigsDao.get();
NotifyConfigsEntity notifyConfigsEntity = notifyConfigsDao.selectOnKey(user.getUserId());
// 登録者でかつイイネが登録した場合に通知が欲しい
if (notifyConfigsEntity != null && INT_FLAG.flagCheck(notifyConfigsEntity.getMyItemLike())) {
// 通知情報生成
NotificationsEntity notification = new NotificationsEntity();
notification.setTitle(MailLogic.NOTIFY_INSERT_LIKE_COMMENT_MYITEM);

LikeInsert info = new LikeInsert();
info.setKnowledgeId(knowledge.getKnowledgeId());
info.setKnowledgeTitle(knowledge.getTitle());
info.setCommentNo(comment.getCommentNo());

info.setUpdateUser(user.getUserName());
info.setLikeInsertUser(like.getUserName());

notification.setContent(JSON.encode(info));
notification = NotificationsDao.get().insert(notification);

// 通知とユーザの紐付け
NotificationLogic.get().insertUserNotification(notification, user);

// メール送信
Locale locale = user.getLocale();
MailLocaleTemplatesEntity template = MailLogic.get().load(locale, MailLogic.NOTIFY_INSERT_LIKE_MYITEM);
LikeInsertNotification.get().sendLikeMail(knowledge, likeUser, user, template);
}
}
}

@Override
public void convNotification(NotificationsEntity notificationsEntity, LoginedUser loginedUser, TARGET target) {
LikeInsert info = JSON.decode(notificationsEntity.getContent(), LikeInsert.class);
MailLocaleTemplatesEntity template = MailLogic.get().load(loginedUser.getLocale(), MailLogic.NOTIFY_INSERT_LIKE_MYITEM);

String title = template.getTitle();
title = title.replace("{KnowledgeId}", String.valueOf(info.getKnowledgeId()));
title = title.replace("{KnowledgeTitle}", StringUtils.abbreviate(info.getKnowledgeTitle(), 80));
notificationsEntity.setTitle(title);

if (target == TARGET.detail) {
String contents = template.getContent();
contents = contents.replace("{KnowledgeId}", String.valueOf(info.getKnowledgeId()));
contents = contents.replace("{KnowledgeTitle}", info.getKnowledgeTitle());
contents = contents.replace("{User}", info.getUpdateUser());
contents = contents.replace("{URL}", NotifyLogic.get().makeURL(info.getKnowledgeId()));
CommentsEntity entity = CommentsDao.get().selectOnKey(info.getCommentNo());
if (entity != null) {
contents = contents.replace("{Contents}", entity.getComment());
} else {
contents = contents.replace("{Contents}", "");
}
contents = contents.replace("{LikeInsertUser}", info.getLikeInsertUser());
notificationsEntity.setContent(contents);
}
}
@Override
public MessageResult getMessage(LoginedUser loginuser, Locale locale) {
NotifyConfigsDao dao = NotifyConfigsDao.get();
NotifyConfigsEntity entity = dao.selectOnKey(loginuser.getUserId());
if (!NotifyLogic.get().flagCheck(entity.getNotifyDesktop())) {
// デスクトップ通知対象外
return null;
}
CommentsEntity comment = CommentsDao.get().selectOnKey(like.getCommentNo());

if (NotifyLogic.get().flagCheck(entity.getMyItemLike()) && comment.getInsertUser().intValue() == loginuser.getUserId().intValue()) {
// 自分で投稿したナレッジにイイネが押されたので通知
MessageResult messageResult = new MessageResult();
messageResult.setMessage(Resources.getInstance(locale).getResource("knowledge.notify.msg.desktop.myitem.like",
String.valueOf(comment.getKnowledgeId())));
messageResult.setResult(NotifyLogic.get().makeURL(comment.getKnowledgeId())); // Knowledgeへのリンク
return messageResult;
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void notify(NotifyQueuesEntity notifyQueue) throws Exception {
// メール送信
Locale locale = user.getLocale();
MailLocaleTemplatesEntity template = MailLogic.get().load(locale, MailLogic.NOTIFY_INSERT_LIKE_MYITEM);
sendLikeMail(like, knowledge, likeUser, user, template);
sendLikeMail(knowledge, likeUser, user, template);
}
}
}
Expand Down Expand Up @@ -157,7 +157,7 @@ private NotificationsEntity insertNotificationOnLikeMyItem(KnowledgesEntity know
* @param template
* @throws Exception
*/
private void sendLikeMail(LikesEntity like, KnowledgesEntity knowledge, UsersEntity likeUser, UsersEntity user,
public void sendLikeMail(KnowledgesEntity knowledge, UsersEntity likeUser, UsersEntity user,
MailLocaleTemplatesEntity template) throws Exception {
MailConfigsDao mailConfigsDao = MailConfigsDao.get();
MailConfigsEntity mailConfigsEntity = mailConfigsDao.selectOnKey(AppConfig.get().getSystemName());
Expand Down Expand Up @@ -228,8 +228,6 @@ public void convNotification(NotificationsEntity notificationsEntity, LoginedUse
contents = contents.replace("{LikeInsertUser}", info.getLikeInsertUser());
notificationsEntity.setContent(contents);
}


}
@Override
public MessageResult getMessage(LoginedUser loginuser, Locale locale) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface QueueNotification extends Notification {
int TYPE_KNOWLEDGE_COMMENT = 11;
/** notify type: add like */
int TYPE_KNOWLEDGE_LIKE = 21;
/** notify type: add like */
int TYPE_COMMENT_LIKE = 22;

/**
* メール通知のキューを保存
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

public class LikeInsert extends KnowledgeUpdate {
private String likeInsertUser;

/** コメントにイイネが投稿された場合は、コメントのNoを保持 */
private Long commentNo;

/**
* @return the likeInsertUser
Expand All @@ -18,4 +21,18 @@ public void setLikeInsertUser(String likeInsertUser) {
this.likeInsertUser = likeInsertUser;
}

/**
* @return the commentNo
*/
public Long getCommentNo() {
return commentNo;
}

/**
* @param commentNo the commentNo to set
*/
public void setCommentNo(Long commentNo) {
this.commentNo = commentNo;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<mail>
<title>[Knowledge] 評価の通知 {KnowledgeId} - {KnowledgeTitle}</title>
<contents xml:space="preserve">
以下のナレッジに「いいね!」が押されました!
あなたの投稿に、「いいね!」が登録されました。

{URL}

Expand Down
Loading

0 comments on commit 1f52962

Please sign in to comment.