Skip to content

Commit

Permalink
feat #25:注册时添加系统消息
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyueyi committed Oct 28, 2022
1 parent 12d2849 commit f9a9b20
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ public enum NotifyTypeEnum {
COLLECT(4, "收藏"),
FOLLOW(5, "关注消息"),
SYSTEM(6, "系统消息"),
DELETE_COMMENT(11, "删除评论"),
DELETE_REPLY(12, "删除回复"),
CANCEL_PRAISE(13, "取消点赞"),
CANCEL_COLLECT(14, "取消收藏"),
CANCEL_FOLLOW(15, "取消关注"),
DELETE_COMMENT(1, "删除评论"),
DELETE_REPLY(2, "删除回复"),
CANCEL_PRAISE(3, "取消点赞"),
CANCEL_COLLECT(4, "取消收藏"),
CANCEL_FOLLOW(5, "取消关注"),

// 注册、登录添加系统相关提示消息
REGISTER(6, "用户注册"),
LOGIN(6, "用户登录"),
;


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.liuyueyi.forum.service.notify.service.impl;

import com.github.liueyueyi.forum.api.model.enums.NotifyStatEnum;
import com.github.liueyueyi.forum.api.model.enums.NotifyTypeEnum;
import com.github.liueyueyi.forum.api.model.vo.notify.NotifyMsgEvent;
import com.github.liuyueyi.forum.service.article.repository.entity.ArticleDO;
import com.github.liuyueyi.forum.service.article.service.ArticleReadService;
Expand All @@ -23,6 +24,7 @@
@Async
@Service
public class NotifyMsgListener<T> implements ApplicationListener<NotifyMsgEvent<T>> {
private static final Long ADMIN_ID = 1L;
private final ArticleReadService articleReadService;

private final CommentReadService commentReadService;
Expand Down Expand Up @@ -59,6 +61,13 @@ public void onApplicationEvent(NotifyMsgEvent<T> msgEvent) {
case CANCEL_FOLLOW:
// todo 取消操作,若之前的消息是未读状态,则移除对应的记录
log.info("取消操作: {}", msgEvent);
case LOGIN:
// todo 用户登录,判断是否需要插入新的通知消息,暂时先不做
break;
case REGISTER:
// 首次注册,插入一个欢迎的消息
saveRegisterSystemNotify((Long) msgEvent.getContent());
break;
default:
// todo 系统消息
}
Expand Down Expand Up @@ -140,4 +149,18 @@ private void saveFollowNotify(NotifyMsgEvent<UserRelationDO> event) {
}
}

private void saveRegisterSystemNotify(Long userId) {
NotifyMsgDO msg = new NotifyMsgDO().setRelatedId(0L)
.setNotifyUserId(userId)
.setOperateUserId(ADMIN_ID)
.setType(NotifyTypeEnum.REGISTER.getType())
.setState(NotifyStatEnum.UNREAD.getStat())
.setMsg("欢迎注册使用论坛,更多的使用姿势请参考:<a href=\"/\">使用教程<a/>");
NotifyMsgDO record = notifyMsgDao.getByUserIdRelatedIdAndType(msg);
if (record == null) {
// 若之前已经有对应的通知,则不重复记录;因为用户的关注是一对一的,可以重复的关注、取消,但是最终我们只通知一次
notifyMsgDao.save(msg);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.github.liuyueyi.forum.service.user.service.user;

import com.github.liueyueyi.forum.api.model.context.ReqInfoContext;
import com.github.liueyueyi.forum.api.model.enums.NotifyTypeEnum;
import com.github.liueyueyi.forum.api.model.exception.ExceptionUtil;
import com.github.liueyueyi.forum.api.model.vo.article.dto.YearArticleDTO;
import com.github.liueyueyi.forum.api.model.vo.constants.StatusEnum;
import com.github.liueyueyi.forum.api.model.vo.notify.NotifyMsgEvent;
import com.github.liueyueyi.forum.api.model.vo.user.UserInfoSaveReq;
import com.github.liueyueyi.forum.api.model.vo.user.UserSaveReq;
import com.github.liueyueyi.forum.api.model.vo.user.dto.ArticleFootCountDTO;
import com.github.liueyueyi.forum.api.model.vo.user.dto.BaseUserInfoDTO;
import com.github.liueyueyi.forum.api.model.vo.user.dto.UserStatisticInfoDTO;
import com.github.liuyueyi.forum.core.util.SpringUtil;
import com.github.liuyueyi.forum.service.article.repository.dao.ArticleDao;
import com.github.liuyueyi.forum.service.article.service.ArticleReadService;
import com.github.liuyueyi.forum.service.user.converter.UserConverter;
Expand Down Expand Up @@ -65,6 +68,9 @@ public void registerOrGetUserInfo(UserSaveReq req) {
if (record != null) {
// 用户存在,不需要注册
req.setUserId(record.getId());

// 用户登录事件
SpringUtil.publishEvent(new NotifyMsgEvent<>(this, NotifyTypeEnum.LOGIN, record.getId()));
return;
}

Expand All @@ -79,6 +85,9 @@ record = UserConverter.toDO(req);
userInfo.setUserName(UserRandomGenHelper.genNickName());
userInfo.setPhoto(UserRandomGenHelper.genAvatar());
userDao.save(userInfo);

// 用户注册事件
SpringUtil.publishEvent(new NotifyMsgEvent<>(this, NotifyTypeEnum.REGISTER, userInfo.getUserId()));
}

@Override
Expand Down
20 changes: 13 additions & 7 deletions forum-ui/src/main/resources/templates/biz/notice/msg.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,18 @@
</div>
<div class="notice-content">
<div id="itemList">
<div th:replace="biz/notice/notify-comment"></div>
<div th:replace="biz/notice/notify-reply"></div>
<div th:replace="biz/notice/notify-praise"></div>
<div th:replace="biz/notice/notify-collect"></div>
<div th:replace="biz/notice/notify-follow"></div>
<div th:replace="biz/notice/notify-system"></div>
<div th:if="${#lists.isEmpty(vo.list.list)}">
<div class="notification" th:text="暂无通知消息">
</div>
</div>
<div th:if="${!#lists.isEmpty(vo.list.list)}">
<div th:replace="biz/notice/notify-comment"></div>
<div th:replace="biz/notice/notify-reply"></div>
<div th:replace="biz/notice/notify-praise"></div>
<div th:replace="biz/notice/notify-collect"></div>
<div th:replace="biz/notice/notify-follow"></div>
<div th:replace="biz/notice/notify-system"></div>
</div>
</div>
<button
th:if="${vo.list.hasMore}"
Expand All @@ -81,7 +87,7 @@
</div>

<!-- 底部信息 -->
<!-- <div th:replace="layout/footer :: footer"></div> -->
<div th:replace="layout/footer :: footer"></div>
<script th:inline="javascript">
const selectType = [[${vo.selectType}]].toLowerCase()
const params = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="right">
<div class="top">
<div class="content">
<div class="comment" th:text="${msg.msg}">m</div>
<div class="comment" th:utext="${msg.msg}">m</div>
</div>
</div>
<div class="bottom">
Expand Down

0 comments on commit f9a9b20

Please sign in to comment.