feat: Phase 2e-1 统一通知中心 + 我的 TabBar 聚合未读红点

后端(notify 模块)
- 新增 notify 模块:DAO/Service/Pusher 接口/Controller/Router/CleanupTask
- 数据库 DDL:notify_notifications 表 + 3 索引(user+created/user+is_read/user+category)
- 11 种 type 枚举(好友/群聊 9 种 + meeting_* 2 种预留)+ 4 种 category
- 跨模块集成:contact 3 处 Pusher(friend_request/accepted/rejected)
- 跨模块集成:group 6 处 Pusher(invite/join_request/approved/rejected/kicked/role_changed)
- WS handler 断线补偿:连接建立即推送 notify.unread.total
- 5 REST API(4 用户 + 1 管理员广播)+ 2 WS 事件(notify.new / notify.unread.total)
- 30 天已读通知定时清理(未读永久保留)
- Provider/Wire 依赖注入(NotifyPusher、NotifyConnectHook、UserInfoResolver 接口)

前端
- 新增 notify 模块:API/Pinia Store(5 分类分页缓存 + 未读数 + WS 事件)/NotifyItem/通知中心主页
- profile 入口:铃铛 badge + 菜单项 badge + 数字显示
- App.vue/login 初始化 notifyStore WS 监听;logout 调用 notifyStore.reset() 清缓存
- 清理 contact.js/group.js 中散落 toast 与冗余 notify.friend.request/group.join.request 处理
- CustomTabBar 新增 hasDot() 聚合指示器:我的 Tab 显示纯红点(无数字),
  当前聚合 notifyStore.unreadTotal,未来可扩展「资料待完善/安全提醒/新版本」等

文档
- 新增 Phase 2e 整体路线图 docs/plans/2026-04-20-phase2e-design.md
- 新增 Phase 2e-1 专用设计 docs/plans/2026-04-20-phase2e-1-design.md(§6.4 TabBar 聚合红点)
- 新增 Phase 2e-1 实施计划 docs/plans/2026-04-20-phase2e-1-implementation.plan.md
- 新增 E2E 验证报告 test-report-phase2e-1-notification.md(含 Playwright MCP 2 个现场 Bug 修复记录)
- 更新 docs/progress/CURRENT_STATUS.md、docs/api/README.md、docs/api/frontend/notify.md
- 更新 .cursor/rules/project-context.mdc、docs/plans/2026-02-27-echochat-system-design.md

其他
- .gitignore 排除 .playwright-mcp/ MCP 临时快照

架构决策
- 单端 WS 连接:沿用现有 ws.Hub,多端已读同步推迟到 Phase 2f/二期
- 跨模块依赖:contact/group → notify 严格单向(接口注入模式)
- 降级策略:Pusher 先入库后推送;WS 失败不回滚入库;入库失败仅 Warn 不影响业务

Playwright MCP 回归(4 类场景全通)
- 实时推送(admin 广播 → 1s 内前端自动插入 + 角标 +1)
- Deep-link 跳转(好友申请通知 → contact/request 页)
- 批量清零(全部已读按钮)
- TabBar 聚合红点(有未读亮/全部已读灭)与 notifyStore.unreadTotal 三层同步

Made-with: Cursor
This commit is contained in:
bujinyuan
2026-04-21 10:21:01 +08:00
parent ccfceefdaf
commit f1853f125d
42 changed files with 4493 additions and 97 deletions

View File

@@ -12,6 +12,7 @@ import (
"github.com/echochat/backend/app/group/dao"
"github.com/echochat/backend/app/group/model"
imModel "github.com/echochat/backend/app/im/model"
notifyService "github.com/echochat/backend/app/notify/service"
"github.com/echochat/backend/pkg/logs"
"github.com/echochat/backend/pkg/ws"
"go.uber.org/zap"
@@ -46,6 +47,12 @@ type MessageWriter interface {
Create(ctx context.Context, msg *imModel.Message) error
}
// NotifyPusher 通知推送接口
// 由 notify.service.NotifyService 隐式实现group → notify 单向依赖
type NotifyPusher interface {
Push(ctx context.Context, payload *notifyService.PushPayload)
}
// GroupService 群聊业务服务
type GroupService struct {
groupDAO *dao.GroupDAO
@@ -53,6 +60,7 @@ type GroupService struct {
userInfo UserInfoProvider
pubsub *ws.PubSub
msgWriter MessageWriter
notifyPusher NotifyPusher
}
// NewGroupService 创建 GroupService 实例
@@ -62,6 +70,7 @@ func NewGroupService(
userInfo UserInfoProvider,
pubsub *ws.PubSub,
msgWriter MessageWriter,
notifyPusher NotifyPusher,
) *GroupService {
return &GroupService{
groupDAO: groupDAO,
@@ -69,6 +78,7 @@ func NewGroupService(
userInfo: userInfo,
pubsub: pubsub,
msgWriter: msgWriter,
notifyPusher: notifyPusher,
}
}
@@ -308,6 +318,17 @@ func (s *GroupService) InviteMembers(ctx context.Context, userID, groupID int64,
"user_ids": addedIDs,
"operator_id": userID,
})
// 向被邀请的每个用户推送入群通知,由通知中心持久化 + WS 实时弹出
s.pushGroupNotify(ctx, addedIDs, userID, constants.NotifyTypeGroupInvite,
fmt.Sprintf("%s 邀请你加入「%s」", inviterName, group.Name),
groupID, map[string]interface{}{
"group_id": groupID,
"group_name": group.Name,
"conversation_id": group.ConversationID,
"inviter_id": userID,
"inviter_name": inviterName,
})
}
return nil
@@ -357,6 +378,16 @@ func (s *GroupService) KickMember(ctx context.Context, userID, groupID, targetID
"operator_id": userID,
})
// 通知被踢用户:持久化到通知中心
operatorName := s.getUserNickname(ctx, userID)
s.pushGroupNotify(ctx, []int64{targetID}, userID, constants.NotifyTypeGroupKicked,
fmt.Sprintf("%s 将你移出了群聊「%s」", operatorName, group.Name),
groupID, map[string]interface{}{
"group_id": groupID,
"group_name": group.Name,
"operator_id": userID,
})
return nil
}
@@ -402,6 +433,22 @@ func (s *GroupService) SetMemberRole(ctx context.Context, userID, groupID, targe
"operator_id": userID,
})
// 通知被设置角色的用户:持久化到通知中心
var content string
if role == constants.GroupRoleAdmin {
content = fmt.Sprintf("你已被设为群聊「%s」的管理员", group.Name)
} else {
content = fmt.Sprintf("你的群聊「%s」管理员身份已被取消", group.Name)
}
s.pushGroupNotify(ctx, []int64{targetID}, userID, constants.NotifyTypeGroupRoleChanged,
content,
groupID, map[string]interface{}{
"group_id": groupID,
"group_name": group.Name,
"role": role,
"operator_id": userID,
})
return nil
}
@@ -585,6 +632,18 @@ func (s *GroupService) SubmitJoinRequest(ctx context.Context, userID, groupID in
"message": message,
})
// 向群管理员持久化通知:需要审批的入群申请
s.pushGroupNotify(ctx, adminIDs, userID, constants.NotifyTypeGroupJoinRequest,
fmt.Sprintf("%s 申请加入群聊「%s」", userName, group.Name),
groupID, map[string]interface{}{
"group_id": groupID,
"group_name": group.Name,
"request_id": req.ID,
"applicant_id": userID,
"applicant_name": userName,
"message": message,
})
return nil
}
@@ -692,10 +751,33 @@ func (s *GroupService) ReviewJoinRequest(ctx context.Context, userID, groupID, r
"user_ids": []int64{req.UserID},
})
// 通知申请人:入群申请已通过
s.pushGroupNotify(ctx, []int64{req.UserID}, userID, constants.NotifyTypeGroupJoinApproved,
fmt.Sprintf("你加入群聊「%s」的申请已通过", group.Name),
groupID, map[string]interface{}{
"group_id": groupID,
"group_name": group.Name,
"conversation_id": group.ConversationID,
"request_id": requestID,
})
return nil
}
return s.joinRequestDAO.Reject(ctx, requestID, userID)
if err := s.joinRequestDAO.Reject(ctx, requestID, userID); err != nil {
return err
}
// 通知申请人:入群申请被拒绝
s.pushGroupNotify(ctx, []int64{req.UserID}, userID, constants.NotifyTypeGroupJoinRejected,
fmt.Sprintf("你加入群聊「%s」的申请已被拒绝", group.Name),
groupID, map[string]interface{}{
"group_id": groupID,
"group_name": group.Name,
"request_id": requestID,
})
return nil
}
// SearchGroups 搜索公开群
@@ -858,6 +940,52 @@ func (s *GroupService) pushToGroupMembers(ctx context.Context, conversationID in
s.pushToMembers(ctx, memberIDs, excludeUID, event, data)
}
// pushGroupNotify 向多个用户批量投递群聊相关通知(持久化 + WS
// notifyPusher 内部自动写入通知中心并推送 notify.new 事件
func (s *GroupService) pushGroupNotify(
ctx context.Context,
userIDs []int64,
actorID int64,
notifyType string,
content string,
groupID int64,
extra map[string]interface{},
) {
if s.notifyPusher == nil || len(userIDs) == 0 {
return
}
gid := groupID
actor := actorID
payloads := make([]*notifyService.PushPayload, 0, len(userIDs))
for _, uid := range userIDs {
if uid <= 0 || uid == actorID {
continue
}
payloads = append(payloads, &notifyService.PushPayload{
UserID: uid,
Type: notifyType,
Content: content,
ActorID: &actor,
TargetType: constants.NotifyTargetGroup,
TargetID: &gid,
Extra: extra,
})
}
if len(payloads) == 0 {
return
}
// 使用批量接口,减少数据库往返
if batch, ok := s.notifyPusher.(interface {
PushBatch(ctx context.Context, payloads []*notifyService.PushPayload)
}); ok {
batch.PushBatch(ctx, payloads)
return
}
for _, p := range payloads {
s.notifyPusher.Push(ctx, p)
}
}
// writeSystemMessage 写入系统消息到群会话
func (s *GroupService) writeSystemMessage(ctx context.Context, conversationID int64, content string) {
if s.msgWriter == nil {