feat(phase2c): 群聊与已读回执功能完整实现
Phase 2c 全部 14 个 Task 完成,包含: 后端(Go): - MinIO 文件存储服务集成(Docker + Go SDK + 通用上传 API) - Group 模块完整实现(DAO + Service + Controller + Router + Wire) - 18 个群管理 REST API + 11 个 WS 群事件推送 - 群创建/解散/邀请/踢人/退出/转让群主/设管理员/禁言/全体禁言/群公告/群昵称/免打扰/搜索 - IM Service 扩展(群消息发送/撤回 + @提醒 + 管理员无时限撤回) - 已读回执后端(单聊会话级 last_read_msg_id + 群聊消息级 im_message_reads) - 管理端群组管理(列表/详情/解散) - 数据库迁移(3 张新表 + 2 张表字段扩展) 前端(uni-app): - 群聊 Store + API 封装 + 11 个 WS 事件监听 - 已读回执 UI(单聊已读/未读标记 + 群聊 X人已读 + 已读详情页) - 7 个群聊页面(对话/创建/设置/成员/邀请/审批/搜索) - 会话列表改造(全部/单聊/群聊 Tab + @标记 + 免打扰标识) 管理端(Vue 3 + Element Plus): - 群组列表页(搜索/分页/详情弹窗/解散群聊) - 侧边栏群组管理入口 文档同步:进度/架构/设计/API/规范文档全部更新 Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
// Package controller 提供 admin 模块的 HTTP 接口处理
|
||||
package controller
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/echochat/backend/app/admin/service"
|
||||
"github.com/echochat/backend/pkg/logs"
|
||||
"github.com/echochat/backend/pkg/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// GroupManageController 管理端群组管理控制器
|
||||
type GroupManageController struct {
|
||||
groupManageService *service.GroupManageService
|
||||
}
|
||||
|
||||
// NewGroupManageController 创建群组管理控制器实例
|
||||
func NewGroupManageController(svc *service.GroupManageService) *GroupManageController {
|
||||
return &GroupManageController{groupManageService: svc}
|
||||
}
|
||||
|
||||
// GetGroupList 获取群组列表
|
||||
// GET /api/v1/admin/groups?page=1&page_size=20&keyword=xxx
|
||||
func (ctl *GroupManageController) GetGroupList(c *gin.Context) {
|
||||
funcName := "admin.group_manage_controller.GetGroupList"
|
||||
ctx := c.Request.Context()
|
||||
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
keyword := c.Query("keyword")
|
||||
|
||||
list, total, err := ctl.groupManageService.ListGroups(ctx, page, pageSize, keyword)
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "获取群组列表失败", zap.Error(err))
|
||||
utils.ResponseError(c, "获取群组列表失败")
|
||||
return
|
||||
}
|
||||
|
||||
logs.Info(ctx, funcName, "获取群组列表成功",
|
||||
zap.Int64("total", total), zap.Int("page", page))
|
||||
utils.ResponseOK(c, gin.H{
|
||||
"list": list,
|
||||
"total": total,
|
||||
})
|
||||
}
|
||||
|
||||
// GetGroupDetail 获取群组详情
|
||||
// GET /api/v1/admin/groups/:id
|
||||
func (ctl *GroupManageController) GetGroupDetail(c *gin.Context) {
|
||||
funcName := "admin.group_manage_controller.GetGroupDetail"
|
||||
ctx := c.Request.Context()
|
||||
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
detail, err := ctl.groupManageService.GetGroupDetail(ctx, id)
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "获取群组详情失败",
|
||||
zap.Int64("group_id", id), zap.Error(err))
|
||||
utils.ResponseError(c, "获取群组详情失败")
|
||||
return
|
||||
}
|
||||
|
||||
logs.Info(ctx, funcName, "获取群组详情成功", zap.Int64("group_id", id))
|
||||
utils.ResponseOK(c, detail)
|
||||
}
|
||||
|
||||
// DissolveGroup 解散群聊
|
||||
// DELETE /api/v1/admin/groups/:id
|
||||
func (ctl *GroupManageController) DissolveGroup(c *gin.Context) {
|
||||
funcName := "admin.group_manage_controller.DissolveGroup"
|
||||
ctx := c.Request.Context()
|
||||
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctl.groupManageService.DissolveGroup(ctx, id); err != nil {
|
||||
logs.Error(ctx, funcName, "解散群聊失败",
|
||||
zap.Int64("group_id", id), zap.Error(err))
|
||||
utils.ResponseError(c, "解散群聊失败")
|
||||
return
|
||||
}
|
||||
|
||||
logs.Info(ctx, funcName, "解散群聊成功", zap.Int64("group_id", id))
|
||||
utils.ResponseOK(c, nil)
|
||||
}
|
||||
@@ -18,4 +18,6 @@ var AdminSet = wire.NewSet(
|
||||
controller.NewOnlineController,
|
||||
service.NewContactManageService,
|
||||
controller.NewContactManageController,
|
||||
service.NewGroupManageService,
|
||||
controller.NewGroupManageController,
|
||||
)
|
||||
|
||||
@@ -15,6 +15,7 @@ func RegisterRoutes(
|
||||
userCtrl *controller.UserManageController,
|
||||
onlineCtrl *controller.OnlineController,
|
||||
contactManageCtrl *controller.ContactManageController,
|
||||
groupManageCtrl *controller.GroupManageController,
|
||||
jwtAuth gin.HandlerFunc,
|
||||
) {
|
||||
// 管理端路由组:JWT 认证 + admin/super_admin 角色检查
|
||||
@@ -38,5 +39,10 @@ func RegisterRoutes(
|
||||
// 好友关系管理
|
||||
adminGroup.GET("/contacts", contactManageCtrl.GetAllContacts)
|
||||
adminGroup.DELETE("/contacts/:id", contactManageCtrl.DeleteContact)
|
||||
|
||||
// 群组管理
|
||||
adminGroup.GET("/groups", groupManageCtrl.GetGroupList)
|
||||
adminGroup.GET("/groups/:id", groupManageCtrl.GetGroupDetail)
|
||||
adminGroup.DELETE("/groups/:id", groupManageCtrl.DissolveGroup)
|
||||
}
|
||||
}
|
||||
|
||||
247
backend/go-service/app/admin/service/group_manage_service.go
Normal file
247
backend/go-service/app/admin/service/group_manage_service.go
Normal file
@@ -0,0 +1,247 @@
|
||||
// Package service 提供 admin 模块的核心业务逻辑
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
groupDAO "github.com/echochat/backend/app/group/dao"
|
||||
groupModel "github.com/echochat/backend/app/group/model"
|
||||
"github.com/echochat/backend/pkg/logs"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GroupManageService 管理端群组管理服务
|
||||
type GroupManageService struct {
|
||||
db *gorm.DB
|
||||
groupDAO *groupDAO.GroupDAO
|
||||
}
|
||||
|
||||
// NewGroupManageService 创建群组管理服务实例
|
||||
func NewGroupManageService(db *gorm.DB, groupDAO *groupDAO.GroupDAO) *GroupManageService {
|
||||
return &GroupManageService{db: db, groupDAO: groupDAO}
|
||||
}
|
||||
|
||||
// GroupListItem 管理端群组列表项
|
||||
type GroupListItem struct {
|
||||
ID int64 `json:"id"`
|
||||
ConversationID int64 `json:"conversation_id"`
|
||||
Name string `json:"name"`
|
||||
Avatar string `json:"avatar"`
|
||||
OwnerID int64 `json:"owner_id"`
|
||||
OwnerName string `json:"owner_name"`
|
||||
MemberCount int64 `json:"member_count"`
|
||||
MaxMembers int `json:"max_members"`
|
||||
Status int `json:"status"`
|
||||
IsAllMuted bool `json:"is_all_muted"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
// GroupDetailInfo 管理端群组详情
|
||||
type GroupDetailInfo struct {
|
||||
GroupListItem
|
||||
Notice string `json:"notice"`
|
||||
IsSearchable bool `json:"is_searchable"`
|
||||
Members []GroupMemberInfo `json:"members"`
|
||||
}
|
||||
|
||||
// GroupMemberInfo 群成员信息
|
||||
type GroupMemberInfo struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
Username string `json:"username"`
|
||||
Nickname string `json:"nickname"`
|
||||
Role int `json:"role"`
|
||||
IsMuted bool `json:"is_muted"`
|
||||
JoinedAt string `json:"joined_at"`
|
||||
}
|
||||
|
||||
// ListGroups 获取群组列表(分页 + 搜索)
|
||||
func (s *GroupManageService) ListGroups(ctx context.Context, page, pageSize int, keyword string) ([]GroupListItem, int64, error) {
|
||||
funcName := "service.group_manage_service.ListGroups"
|
||||
logs.Info(ctx, funcName, "获取群组列表",
|
||||
zap.Int("page", page), zap.Int("page_size", pageSize), zap.String("keyword", keyword))
|
||||
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize < 1 || pageSize > 100 {
|
||||
pageSize = 20
|
||||
}
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
var groups []groupModel.Group
|
||||
var total int64
|
||||
|
||||
query := s.db.WithContext(ctx).Model(&groupModel.Group{})
|
||||
if keyword != "" {
|
||||
query = query.Where("name ILIKE ?", "%"+keyword+"%")
|
||||
}
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if err := query.Order("created_at DESC").Offset(offset).Limit(pageSize).Find(&groups).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 批量查询群主名称,避免 N+1
|
||||
ownerIDs := make([]int64, 0, len(groups))
|
||||
convIDs := make([]int64, 0, len(groups))
|
||||
for _, g := range groups {
|
||||
ownerIDs = append(ownerIDs, g.OwnerID)
|
||||
convIDs = append(convIDs, g.ConversationID)
|
||||
}
|
||||
ownerNameMap := s.batchGetUsernames(ctx, ownerIDs)
|
||||
memberCountMap := s.batchGetMemberCounts(ctx, convIDs)
|
||||
|
||||
list := make([]GroupListItem, 0, len(groups))
|
||||
for _, g := range groups {
|
||||
list = append(list, GroupListItem{
|
||||
ID: g.ID,
|
||||
ConversationID: g.ConversationID,
|
||||
Name: g.Name,
|
||||
Avatar: g.Avatar,
|
||||
OwnerID: g.OwnerID,
|
||||
OwnerName: ownerNameMap[g.OwnerID],
|
||||
MemberCount: memberCountMap[g.ConversationID],
|
||||
MaxMembers: g.MaxMembers,
|
||||
Status: g.Status,
|
||||
IsAllMuted: g.IsAllMuted,
|
||||
CreatedAt: g.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
totalPages := int(math.Ceil(float64(total) / float64(pageSize)))
|
||||
logs.Info(ctx, funcName, "获取群组列表成功",
|
||||
zap.Int64("total", total), zap.Int("total_pages", totalPages))
|
||||
|
||||
return list, total, nil
|
||||
}
|
||||
|
||||
// GetGroupDetail 获取群组详情(含成员列表)
|
||||
func (s *GroupManageService) GetGroupDetail(ctx context.Context, groupID int64) (*GroupDetailInfo, error) {
|
||||
funcName := "service.group_manage_service.GetGroupDetail"
|
||||
logs.Info(ctx, funcName, "获取群组详情", zap.Int64("group_id", groupID))
|
||||
|
||||
group, err := s.groupDAO.GetByID(ctx, groupID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
memberCount, _ := s.groupDAO.GetMemberCount(ctx, group.ConversationID)
|
||||
ownerName := s.getUsername(ctx, group.OwnerID)
|
||||
|
||||
members, err := s.groupDAO.GetMembers(ctx, group.ConversationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
memberInfos := make([]GroupMemberInfo, 0, len(members))
|
||||
for _, m := range members {
|
||||
username := s.getUsername(ctx, m.UserID)
|
||||
memberInfos = append(memberInfos, GroupMemberInfo{
|
||||
UserID: m.UserID,
|
||||
Username: username,
|
||||
Nickname: m.Nickname,
|
||||
Role: m.Role,
|
||||
IsMuted: m.IsMuted,
|
||||
JoinedAt: m.JoinedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
detail := &GroupDetailInfo{
|
||||
GroupListItem: GroupListItem{
|
||||
ID: group.ID,
|
||||
ConversationID: group.ConversationID,
|
||||
Name: group.Name,
|
||||
Avatar: group.Avatar,
|
||||
OwnerID: group.OwnerID,
|
||||
OwnerName: ownerName,
|
||||
MemberCount: memberCount,
|
||||
MaxMembers: group.MaxMembers,
|
||||
Status: group.Status,
|
||||
IsAllMuted: group.IsAllMuted,
|
||||
CreatedAt: group.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
},
|
||||
Notice: group.Notice,
|
||||
IsSearchable: group.IsSearchable,
|
||||
Members: memberInfos,
|
||||
}
|
||||
|
||||
return detail, nil
|
||||
}
|
||||
|
||||
// DissolveGroup 管理端解散群聊
|
||||
func (s *GroupManageService) DissolveGroup(ctx context.Context, groupID int64) error {
|
||||
funcName := "service.group_manage_service.DissolveGroup"
|
||||
logs.Info(ctx, funcName, "管理端解散群聊", zap.Int64("group_id", groupID))
|
||||
return s.groupDAO.DissolveGroup(ctx, groupID)
|
||||
}
|
||||
|
||||
// getUsername 通过用户 ID 获取用户名(优先昵称,其次用户名)
|
||||
func (s *GroupManageService) getUsername(ctx context.Context, userID int64) string {
|
||||
var username string
|
||||
s.db.WithContext(ctx).Table("auth_users").
|
||||
Where("id = ?", userID).
|
||||
Pluck("COALESCE(NULLIF(nickname, ''), username)", &username)
|
||||
if username == "" {
|
||||
return strconv.FormatInt(userID, 10)
|
||||
}
|
||||
return username
|
||||
}
|
||||
|
||||
// batchGetUsernames 批量查询用户名,返回 userID → 显示名 映射
|
||||
func (s *GroupManageService) batchGetUsernames(ctx context.Context, userIDs []int64) map[int64]string {
|
||||
result := make(map[int64]string, len(userIDs))
|
||||
if len(userIDs) == 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
type row struct {
|
||||
ID int64 `gorm:"column:id"`
|
||||
Username string `gorm:"column:display_name"`
|
||||
}
|
||||
var rows []row
|
||||
s.db.WithContext(ctx).Table("auth_users").
|
||||
Select("id, COALESCE(NULLIF(nickname, ''), username) AS display_name").
|
||||
Where("id IN ?", userIDs).
|
||||
Find(&rows)
|
||||
|
||||
for _, r := range rows {
|
||||
result[r.ID] = r.Username
|
||||
}
|
||||
for _, uid := range userIDs {
|
||||
if _, ok := result[uid]; !ok {
|
||||
result[uid] = strconv.FormatInt(uid, 10)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// batchGetMemberCounts 批量查询会话的成员数,返回 conversationID → count 映射
|
||||
func (s *GroupManageService) batchGetMemberCounts(ctx context.Context, conversationIDs []int64) map[int64]int64 {
|
||||
result := make(map[int64]int64, len(conversationIDs))
|
||||
if len(conversationIDs) == 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
type row struct {
|
||||
ConversationID int64 `gorm:"column:conversation_id"`
|
||||
Count int64 `gorm:"column:cnt"`
|
||||
}
|
||||
var rows []row
|
||||
s.db.WithContext(ctx).Table("im_conversation_members").
|
||||
Select("conversation_id, COUNT(*) AS cnt").
|
||||
Where("conversation_id IN ?", conversationIDs).
|
||||
Group("conversation_id").
|
||||
Find(&rows)
|
||||
|
||||
for _, r := range rows {
|
||||
result[r.ConversationID] = r.Count
|
||||
}
|
||||
return result
|
||||
}
|
||||
46
backend/go-service/app/constants/group.go
Normal file
46
backend/go-service/app/constants/group.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package constants
|
||||
|
||||
// 群聊状态
|
||||
const (
|
||||
GroupStatusNormal = 1 // 正常
|
||||
GroupStatusDissolved = 2 // 已解散
|
||||
)
|
||||
|
||||
// GroupStatusMap 群聊状态中文映射
|
||||
var GroupStatusMap = map[int]string{
|
||||
GroupStatusNormal: "正常",
|
||||
GroupStatusDissolved: "已解散",
|
||||
}
|
||||
|
||||
// 群成员角色(im_conversation_members.role)
|
||||
const (
|
||||
GroupRoleNormal = 0 // 普通成员
|
||||
GroupRoleAdmin = 1 // 管理员
|
||||
GroupRoleOwner = 2 // 群主
|
||||
)
|
||||
|
||||
// GroupRoleMap 群成员角色中文映射
|
||||
var GroupRoleMap = map[int]string{
|
||||
GroupRoleNormal: "普通成员",
|
||||
GroupRoleAdmin: "管理员",
|
||||
GroupRoleOwner: "群主",
|
||||
}
|
||||
|
||||
// 入群申请状态(im_group_join_requests.status)
|
||||
const (
|
||||
JoinRequestStatusPending = 0 // 待审批
|
||||
JoinRequestStatusApproved = 1 // 通过
|
||||
JoinRequestStatusRejected = 2 // 拒绝
|
||||
)
|
||||
|
||||
// JoinRequestStatusMap 入群申请状态中文映射
|
||||
var JoinRequestStatusMap = map[int]string{
|
||||
JoinRequestStatusPending: "待审批",
|
||||
JoinRequestStatusApproved: "通过",
|
||||
JoinRequestStatusRejected: "拒绝",
|
||||
}
|
||||
|
||||
// 群聊默认配置
|
||||
const (
|
||||
GroupDefaultMaxMembers = 200 // 默认最大成员数
|
||||
)
|
||||
@@ -14,20 +14,22 @@ var ConversationTypeMap = map[int]string{
|
||||
|
||||
// 消息类型
|
||||
const (
|
||||
MessageTypeText = 1 // 文本消息
|
||||
MessageTypeImage = 2 // 图片消息(预留)
|
||||
MessageTypeVoice = 3 // 语音消息(预留)
|
||||
MessageTypeVideo = 4 // 视频消息(预留)
|
||||
MessageTypeFile = 5 // 文件消息(预留)
|
||||
MessageTypeText = 1 // 文本消息
|
||||
MessageTypeImage = 2 // 图片消息(预留)
|
||||
MessageTypeVoice = 3 // 语音消息(预留)
|
||||
MessageTypeVideo = 4 // 视频消息(预留)
|
||||
MessageTypeFile = 5 // 文件消息(预留)
|
||||
MessageTypeSystem = 10 // 系统消息(群聊操作通知)
|
||||
)
|
||||
|
||||
// MessageTypeMap 消息类型中文映射
|
||||
var MessageTypeMap = map[int]string{
|
||||
MessageTypeText: "文本",
|
||||
MessageTypeImage: "图片",
|
||||
MessageTypeVoice: "语音",
|
||||
MessageTypeVideo: "视频",
|
||||
MessageTypeFile: "文件",
|
||||
MessageTypeText: "文本",
|
||||
MessageTypeImage: "图片",
|
||||
MessageTypeVoice: "语音",
|
||||
MessageTypeVideo: "视频",
|
||||
MessageTypeFile: "文件",
|
||||
MessageTypeSystem: "系统消息",
|
||||
}
|
||||
|
||||
// 消息状态
|
||||
|
||||
177
backend/go-service/app/dto/group_dto.go
Normal file
177
backend/go-service/app/dto/group_dto.go
Normal file
@@ -0,0 +1,177 @@
|
||||
package dto
|
||||
|
||||
// ====== 群聊管理 DTO ======
|
||||
|
||||
// CreateGroupRequest 创建群聊请求
|
||||
// POST /api/v1/groups
|
||||
type CreateGroupRequest struct {
|
||||
Name string `json:"name" binding:"required,max=100"` // 群名称
|
||||
MemberIDs []int64 `json:"member_ids" binding:"required"` // 初始成员用户 ID 列表(不含创建者自身)
|
||||
Avatar string `json:"avatar"` // 群头像 URL(可选)
|
||||
}
|
||||
|
||||
// UpdateGroupRequest 更新群信息请求
|
||||
// PUT /api/v1/groups/:id
|
||||
type UpdateGroupRequest struct {
|
||||
Name *string `json:"name"` // 群名称
|
||||
Avatar *string `json:"avatar"` // 群头像 URL
|
||||
Notice *string `json:"notice"` // 群公告
|
||||
IsSearchable *bool `json:"is_searchable"` // 是否可被搜索
|
||||
}
|
||||
|
||||
// GroupDTO 群聊信息传输对象(返回给前端)
|
||||
type GroupDTO struct {
|
||||
ID int64 `json:"id"` // 群 ID
|
||||
ConversationID int64 `json:"conversation_id"` // 关联会话 ID
|
||||
Name string `json:"name"` // 群名称
|
||||
Avatar string `json:"avatar"` // 群头像
|
||||
OwnerID int64 `json:"owner_id"` // 群主用户 ID
|
||||
Notice string `json:"notice"` // 群公告
|
||||
MaxMembers int `json:"max_members"` // 最大成员数
|
||||
MemberCount int `json:"member_count"` // 当前成员数
|
||||
IsSearchable bool `json:"is_searchable"` // 是否可被搜索
|
||||
IsAllMuted bool `json:"is_all_muted"` // 是否全体禁言
|
||||
Status int `json:"status"` // 群状态:1=正常,2=已解散
|
||||
CreatedAt string `json:"created_at"` // 创建时间
|
||||
}
|
||||
|
||||
// ====== 群成员 DTO ======
|
||||
|
||||
// GroupMemberDTO 群成员信息传输对象
|
||||
type GroupMemberDTO struct {
|
||||
UserID int64 `json:"user_id"` // 用户 ID
|
||||
Nickname string `json:"nickname"` // 群内昵称
|
||||
UserNickname string `json:"user_nickname"` // 用户原始昵称
|
||||
Avatar string `json:"avatar"` // 用户头像
|
||||
Role int `json:"role"` // 角色:0=普通成员,1=管理员,2=群主
|
||||
IsMuted bool `json:"is_muted"` // 是否被禁言
|
||||
JoinedAt string `json:"joined_at"` // 加入时间
|
||||
}
|
||||
|
||||
// InviteMembersRequest 邀请入群请求
|
||||
// POST /api/v1/groups/:id/members
|
||||
type InviteMembersRequest struct {
|
||||
UserIDs []int64 `json:"user_ids" binding:"required"` // 被邀请的用户 ID 列表
|
||||
}
|
||||
|
||||
// SetRoleRequest 设置/取消管理员请求
|
||||
// PUT /api/v1/groups/:id/members/:uid/role
|
||||
type SetRoleRequest struct {
|
||||
Role int `json:"role" binding:"oneof=0 1"` // 目标角色:0=普通成员,1=管理员
|
||||
}
|
||||
|
||||
// MuteRequest 禁言/解除禁言请求
|
||||
// PUT /api/v1/groups/:id/members/:uid/mute
|
||||
type MuteRequest struct {
|
||||
IsMuted bool `json:"is_muted"` // true=禁言, false=解除
|
||||
}
|
||||
|
||||
// TransferOwnerRequest 转让群主请求
|
||||
// PUT /api/v1/groups/:id/transfer
|
||||
type TransferOwnerRequest struct {
|
||||
NewOwnerID int64 `json:"new_owner_id" binding:"required"` // 新群主用户 ID
|
||||
}
|
||||
|
||||
// UpdateNicknameRequest 修改群昵称请求
|
||||
// PUT /api/v1/groups/:id/members/me/nickname
|
||||
type UpdateNicknameRequest struct {
|
||||
Nickname string `json:"nickname" binding:"max=50"` // 群内昵称
|
||||
}
|
||||
|
||||
// ====== 入群申请 DTO ======
|
||||
|
||||
// JoinGroupRequest 申请入群请求
|
||||
// POST /api/v1/groups/:id/join-requests
|
||||
type JoinGroupRequest struct {
|
||||
Message string `json:"message"` // 申请附言
|
||||
}
|
||||
|
||||
// ReviewJoinRequest 审批入群申请请求
|
||||
// PUT /api/v1/groups/:id/join-requests/:rid
|
||||
type ReviewJoinRequest struct {
|
||||
Action string `json:"action" binding:"required,oneof=approve reject"` // 操作:approve=通过, reject=拒绝
|
||||
}
|
||||
|
||||
// JoinRequestDTO 入群申请传输对象
|
||||
type JoinRequestDTO struct {
|
||||
ID int64 `json:"id"` // 申请 ID
|
||||
GroupID int64 `json:"group_id"` // 群 ID
|
||||
UserID int64 `json:"user_id"` // 申请人用户 ID
|
||||
UserNickname string `json:"user_nickname"` // 申请人昵称
|
||||
UserAvatar string `json:"user_avatar"` // 申请人头像
|
||||
Message string `json:"message"` // 申请附言
|
||||
Status int `json:"status"` // 状态:0=待审批,1=通过,2=拒绝
|
||||
CreatedAt string `json:"created_at"` // 申请时间
|
||||
}
|
||||
|
||||
// ====== 群搜索 DTO ======
|
||||
|
||||
// SearchGroupRequest 搜索群聊请求
|
||||
// GET /api/v1/groups/search?keyword=xx&page=1&page_size=20
|
||||
type SearchGroupRequest struct {
|
||||
Keyword string `form:"keyword" binding:"required"` // 搜索关键词
|
||||
Page int `form:"page"` // 页码(默认 1)
|
||||
PageSize int `form:"page_size"` // 每页数量(默认 20)
|
||||
}
|
||||
|
||||
// SearchGroupResponse 搜索群聊响应
|
||||
type SearchGroupResponse struct {
|
||||
List []GroupDTO `json:"list"` // 搜索结果
|
||||
Total int64 `json:"total"` // 总数
|
||||
}
|
||||
|
||||
// ====== 已读回执 DTO ======
|
||||
|
||||
// MarkGroupReadRequest 群聊标记已读请求(WS 事件 im.message.read 的 data 字段)
|
||||
type MarkGroupReadRequest struct {
|
||||
ConversationID int64 `json:"conversation_id"` // 会话 ID
|
||||
MessageIDs []int64 `json:"message_ids"` // 已读消息 ID 列表
|
||||
}
|
||||
|
||||
// MessageReadCountDTO 消息已读计数传输对象
|
||||
type MessageReadCountDTO struct {
|
||||
MessageID int64 `json:"message_id"` // 消息 ID
|
||||
ReadCount int `json:"read_count"` // 已读人数
|
||||
}
|
||||
|
||||
// MessageReadDetailDTO 消息已读详情传输对象
|
||||
type MessageReadDetailDTO struct {
|
||||
UserID int64 `json:"user_id"` // 已读用户 ID
|
||||
UserNickname string `json:"user_nickname"` // 用户昵称
|
||||
UserAvatar string `json:"user_avatar"` // 用户头像
|
||||
ReadAt string `json:"read_at"` // 已读时间
|
||||
}
|
||||
|
||||
// GetReadDetailRequest 获取已读详情请求
|
||||
// GET /api/v1/im/messages/:id/reads?page=1&page_size=20
|
||||
type GetReadDetailRequest struct {
|
||||
Page int `form:"page"` // 页码(默认 1)
|
||||
PageSize int `form:"page_size"` // 每页数量(默认 20)
|
||||
}
|
||||
|
||||
// GetReadDetailResponse 获取已读详情响应
|
||||
type GetReadDetailResponse struct {
|
||||
ReadList []MessageReadDetailDTO `json:"read_list"` // 已读用户列表
|
||||
UnreadList []MessageReadDetailDTO `json:"unread_list"` // 未读用户列表
|
||||
ReadCount int `json:"read_count"` // 已读人数
|
||||
TotalCount int `json:"total_count"` // 群成员总数(不含发送者)
|
||||
}
|
||||
|
||||
// ====== 免打扰 DTO ======
|
||||
|
||||
// SetDoNotDisturbRequest 设置/取消免打扰请求
|
||||
// PUT /api/v1/im/conversations/:id/dnd
|
||||
type SetDoNotDisturbRequest struct {
|
||||
IsDoNotDisturb bool `json:"is_do_not_disturb"` // true=开启, false=关闭
|
||||
}
|
||||
|
||||
// ====== 跨模块接口数据传输 ======
|
||||
|
||||
// GroupBrief 群简要信息(IM 模块通过 GroupInfoGetter 接口获取)
|
||||
type GroupBrief struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Avatar string `json:"avatar"`
|
||||
IsAllMuted bool `json:"is_all_muted"`
|
||||
Status int `json:"status"`
|
||||
}
|
||||
@@ -4,23 +4,25 @@ package dto
|
||||
|
||||
// SendMessageRequest 发送消息请求(WS 事件 im.message.send 的 data 字段)
|
||||
type SendMessageRequest struct {
|
||||
ConversationID int64 `json:"conversation_id"` // 会话 ID(与 TargetUserID 二选一)
|
||||
TargetUserID int64 `json:"target_user_id"` // 对方用户 ID(首次发消息时使用,自动创建会话)
|
||||
Type int `json:"type"` // 消息类型:1=文本
|
||||
Content string `json:"content"` // 消息内容
|
||||
ClientMsgID string `json:"client_msg_id"` // 客户端消息唯一 ID,用于幂等去重
|
||||
ConversationID int64 `json:"conversation_id"` // 会话 ID(与 TargetUserID 二选一)
|
||||
TargetUserID int64 `json:"target_user_id"` // 对方用户 ID(首次发消息时使用,自动创建会话)
|
||||
Type int `json:"type"` // 消息类型:1=文本
|
||||
Content string `json:"content"` // 消息内容
|
||||
ClientMsgID string `json:"client_msg_id"` // 客户端消息唯一 ID,用于幂等去重
|
||||
AtUserIDs []int64 `json:"at_user_ids"` // @提醒用户 ID 列表(含 0 表示 @所有人)
|
||||
}
|
||||
|
||||
// MessageDTO 消息传输对象(返回给前端)
|
||||
type MessageDTO struct {
|
||||
ID int64 `json:"id"` // 消息 ID
|
||||
ConversationID int64 `json:"conversation_id"` // 所属会话 ID
|
||||
SenderID int64 `json:"sender_id"` // 发送者用户 ID
|
||||
Type int `json:"type"` // 消息类型
|
||||
Content string `json:"content"` // 消息内容
|
||||
Status int `json:"status"` // 消息状态:1=正常,2=已撤回
|
||||
ClientMsgID string `json:"client_msg_id"` // 客户端消息 ID
|
||||
CreatedAt string `json:"created_at"` // 发送时间
|
||||
ID int64 `json:"id"` // 消息 ID
|
||||
ConversationID int64 `json:"conversation_id"` // 所属会话 ID
|
||||
SenderID int64 `json:"sender_id"` // 发送者用户 ID
|
||||
Type int `json:"type"` // 消息类型
|
||||
Content string `json:"content"` // 消息内容
|
||||
Status int `json:"status"` // 消息状态:1=正常,2=已撤回
|
||||
ClientMsgID string `json:"client_msg_id"` // 客户端消息 ID
|
||||
AtUserIDs []int64 `json:"at_user_ids"` // @提醒用户 ID 列表
|
||||
CreatedAt string `json:"created_at"` // 发送时间
|
||||
}
|
||||
|
||||
// RecallMessageRequest 撤回消息请求(WS 事件 im.message.recall 的 data 字段)
|
||||
@@ -32,16 +34,19 @@ type RecallMessageRequest struct {
|
||||
|
||||
// ConversationDTO 会话列表条目(返回给前端)
|
||||
type ConversationDTO struct {
|
||||
ID int64 `json:"id"` // 会话 ID
|
||||
Type int `json:"type"` // 会话类型:1=单聊
|
||||
PeerUserID int64 `json:"peer_user_id"` // 单聊对方用户 ID
|
||||
PeerNickname string `json:"peer_nickname"` // 对方昵称
|
||||
PeerAvatar string `json:"peer_avatar"` // 对方头像
|
||||
LastMsgContent string `json:"last_msg_content"` // 最后消息预览
|
||||
LastMsgTime string `json:"last_msg_time"` // 最后消息时间
|
||||
ID int64 `json:"id"` // 会话 ID
|
||||
Type int `json:"type"` // 会话类型:1=单聊,2=群聊
|
||||
PeerUserID int64 `json:"peer_user_id"` // 单聊对方用户 ID(群聊为 0)
|
||||
PeerNickname string `json:"peer_nickname"` // 对方昵称(群聊时为群名称)
|
||||
PeerAvatar string `json:"peer_avatar"` // 对方头像(群聊时为群头像)
|
||||
LastMsgContent string `json:"last_msg_content"` // 最后消息预览
|
||||
LastMsgTime string `json:"last_msg_time"` // 最后消息时间
|
||||
LastMsgSenderID *int64 `json:"last_msg_sender_id"` // 最后消息发送者 ID
|
||||
IsPinned bool `json:"is_pinned"` // 是否置顶
|
||||
UnreadCount int `json:"unread_count"` // 未读消息数
|
||||
IsPinned bool `json:"is_pinned"` // 是否置顶
|
||||
UnreadCount int `json:"unread_count"` // 未读消息数
|
||||
GroupID int64 `json:"group_id,omitempty"` // 群聊 ID(仅 type=2 有值)
|
||||
IsDoNotDisturb bool `json:"is_do_not_disturb"` // 是否免打扰
|
||||
AtMeCount int `json:"at_me_count"` // 被@提醒未读计数
|
||||
}
|
||||
|
||||
// ConversationListResponse 会话列表响应
|
||||
|
||||
70
backend/go-service/app/file/controller/file_controller.go
Normal file
70
backend/go-service/app/file/controller/file_controller.go
Normal file
@@ -0,0 +1,70 @@
|
||||
// Package controller 提供文件上传模块的 HTTP 接口处理
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/echochat/backend/app/file/service"
|
||||
"github.com/echochat/backend/pkg/middleware"
|
||||
"github.com/echochat/backend/pkg/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const maxUploadSize = 10 << 20 // 10 MB
|
||||
|
||||
// FileController 文件上传控制器
|
||||
type FileController struct {
|
||||
fileService *service.FileService
|
||||
}
|
||||
|
||||
// NewFileController 创建文件上传控制器
|
||||
func NewFileController(fileService *service.FileService) *FileController {
|
||||
return &FileController{fileService: fileService}
|
||||
}
|
||||
|
||||
// Upload 处理文件上传请求
|
||||
// POST /api/v1/upload
|
||||
// 支持 multipart/form-data,字段名为 "file"
|
||||
func (ctl *FileController) Upload(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
_, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
fileHeader, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "请选择要上传的文件")
|
||||
return
|
||||
}
|
||||
|
||||
if fileHeader.Size > maxUploadSize {
|
||||
utils.ResponseBadRequest(c, "文件大小不能超过 10MB")
|
||||
return
|
||||
}
|
||||
|
||||
result, err := ctl.fileService.Upload(ctx, fileHeader)
|
||||
if err != nil {
|
||||
ctl.handleError(c, err, "文件上传失败")
|
||||
return
|
||||
}
|
||||
|
||||
utils.ResponseOK(c, result)
|
||||
}
|
||||
|
||||
// handleError 统一业务错误映射
|
||||
// 已知业务错误 → 返回 Service 层定义的具体提示
|
||||
// 未知错误 → 返回 fallbackMsg(未传则默认"服务器内部错误")
|
||||
func (ctl *FileController) handleError(c *gin.Context, err error, fallbackMsg ...string) {
|
||||
switch err {
|
||||
case service.ErrFileOpen:
|
||||
utils.ResponseBadRequest(c, err.Error())
|
||||
case service.ErrUploadFailed:
|
||||
utils.ResponseError(c, err.Error())
|
||||
default:
|
||||
msg := "服务器内部错误"
|
||||
if len(fallbackMsg) > 0 && fallbackMsg[0] != "" {
|
||||
msg = fallbackMsg[0]
|
||||
}
|
||||
utils.ResponseError(c, msg)
|
||||
}
|
||||
}
|
||||
14
backend/go-service/app/file/provider.go
Normal file
14
backend/go-service/app/file/provider.go
Normal file
@@ -0,0 +1,14 @@
|
||||
// Package file 文件上传模块 Wire 依赖注入配置
|
||||
package file
|
||||
|
||||
import (
|
||||
"github.com/echochat/backend/app/file/controller"
|
||||
"github.com/echochat/backend/app/file/service"
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
||||
// FileSet 文件上传模块 Wire Provider Set
|
||||
var FileSet = wire.NewSet(
|
||||
service.NewFileService,
|
||||
controller.NewFileController,
|
||||
)
|
||||
16
backend/go-service/app/file/router.go
Normal file
16
backend/go-service/app/file/router.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Package file 文件上传模块路由注册
|
||||
package file
|
||||
|
||||
import (
|
||||
"github.com/echochat/backend/app/file/controller"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// RegisterRoutes 注册文件上传模块的所有路由(需要 JWT 中间件)
|
||||
func RegisterRoutes(r *gin.Engine, ctrl *controller.FileController, jwtAuth gin.HandlerFunc) {
|
||||
authed := r.Group("/api/v1")
|
||||
authed.Use(jwtAuth)
|
||||
{
|
||||
authed.POST("/upload", ctrl.Upload)
|
||||
}
|
||||
}
|
||||
90
backend/go-service/app/file/service/file_service.go
Normal file
90
backend/go-service/app/file/service/file_service.go
Normal file
@@ -0,0 +1,90 @@
|
||||
// Package service 提供文件上传模块的业务逻辑
|
||||
// 封装 MinIO 对象存储的上传操作,返回可访问的文件 URL
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/echochat/backend/config"
|
||||
"github.com/echochat/backend/pkg/logs"
|
||||
"github.com/google/uuid"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrFileOpen = errors.New("打开上传文件失败")
|
||||
ErrUploadFailed = errors.New("文件上传失败")
|
||||
)
|
||||
|
||||
// FileService 文件上传服务
|
||||
type FileService struct {
|
||||
minioClient *minio.Client
|
||||
minioCfg *config.MinioConfig
|
||||
}
|
||||
|
||||
// NewFileService 创建文件上传服务实例
|
||||
func NewFileService(client *minio.Client, cfg *config.MinioConfig) *FileService {
|
||||
return &FileService{
|
||||
minioClient: client,
|
||||
minioCfg: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
// UploadResult 文件上传结果
|
||||
type UploadResult struct {
|
||||
URL string `json:"url"`
|
||||
FileName string `json:"file_name"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
// Upload 上传文件到 MinIO,返回文件访问 URL
|
||||
// 文件按日期目录组织:uploads/2026/03/04/{uuid}.{ext}
|
||||
func (s *FileService) Upload(ctx context.Context, fileHeader *multipart.FileHeader) (*UploadResult, error) {
|
||||
funcName := "service.file_service.Upload"
|
||||
logs.Info(ctx, funcName, "上传文件",
|
||||
zap.String("file_name", fileHeader.Filename),
|
||||
zap.Int64("size", fileHeader.Size))
|
||||
|
||||
file, err := fileHeader.Open()
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "打开上传文件失败", zap.Error(err))
|
||||
return nil, ErrFileOpen
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
ext := strings.ToLower(filepath.Ext(fileHeader.Filename))
|
||||
now := time.Now()
|
||||
objectName := fmt.Sprintf("uploads/%s/%s%s", now.Format("2006/01/02"), uuid.New().String(), ext)
|
||||
|
||||
contentType := fileHeader.Header.Get("Content-Type")
|
||||
if contentType == "" {
|
||||
contentType = "application/octet-stream"
|
||||
}
|
||||
|
||||
_, err = s.minioClient.PutObject(ctx, s.minioCfg.Bucket, objectName, file, fileHeader.Size, minio.PutObjectOptions{
|
||||
ContentType: contentType,
|
||||
})
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "上传文件到 MinIO 失败", zap.Error(err))
|
||||
return nil, ErrUploadFailed
|
||||
}
|
||||
|
||||
scheme := "http"
|
||||
if s.minioCfg.UseSSL {
|
||||
scheme = "https"
|
||||
}
|
||||
url := fmt.Sprintf("%s://%s/%s/%s", scheme, s.minioCfg.Endpoint, s.minioCfg.Bucket, objectName)
|
||||
|
||||
return &UploadResult{
|
||||
URL: url,
|
||||
FileName: fileHeader.Filename,
|
||||
Size: fileHeader.Size,
|
||||
}, nil
|
||||
}
|
||||
542
backend/go-service/app/group/controller/group_controller.go
Normal file
542
backend/go-service/app/group/controller/group_controller.go
Normal file
@@ -0,0 +1,542 @@
|
||||
// Package controller 提供 group 模块的 HTTP 接口处理
|
||||
package controller
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/echochat/backend/app/dto"
|
||||
"github.com/echochat/backend/app/group/service"
|
||||
"github.com/echochat/backend/pkg/middleware"
|
||||
"github.com/echochat/backend/pkg/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GroupController 群聊管理控制器(REST API)
|
||||
type GroupController struct {
|
||||
groupService *service.GroupService
|
||||
}
|
||||
|
||||
// NewGroupController 创建 GroupController 实例
|
||||
func NewGroupController(groupService *service.GroupService) *GroupController {
|
||||
return &GroupController{groupService: groupService}
|
||||
}
|
||||
|
||||
// CreateGroup 创建群聊
|
||||
// POST /api/v1/groups
|
||||
func (ctl *GroupController) CreateGroup(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.CreateGroupRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.ResponseBadRequest(c, "参数校验失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
result, err := ctl.groupService.CreateGroup(ctx, userID, &req)
|
||||
if err != nil {
|
||||
ctl.handleError(c, err, "创建群聊失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, result)
|
||||
}
|
||||
|
||||
// GetGroupDetail 获取群详情
|
||||
// GET /api/v1/groups/:id
|
||||
func (ctl *GroupController) GetGroupDetail(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "群 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
result, err := ctl.groupService.GetGroupDetail(ctx, userID, groupID)
|
||||
if err != nil {
|
||||
ctl.handleError(c, err, "获取群详情失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, result)
|
||||
}
|
||||
|
||||
// UpdateGroup 更新群信息
|
||||
// PUT /api/v1/groups/:id
|
||||
func (ctl *GroupController) UpdateGroup(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "群 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.UpdateGroupRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.ResponseBadRequest(c, "参数校验失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctl.groupService.UpdateGroup(ctx, userID, groupID, &req); err != nil {
|
||||
ctl.handleError(c, err, "更新群信息失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, nil)
|
||||
}
|
||||
|
||||
// DissolveGroup 解散群聊
|
||||
// DELETE /api/v1/groups/:id
|
||||
func (ctl *GroupController) DissolveGroup(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "群 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctl.groupService.DissolveGroup(ctx, userID, groupID); err != nil {
|
||||
ctl.handleError(c, err, "解散群聊失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, nil)
|
||||
}
|
||||
|
||||
// GetMembers 获取群成员列表
|
||||
// GET /api/v1/groups/:id/members
|
||||
func (ctl *GroupController) GetMembers(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "群 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
result, err := ctl.groupService.GetMembers(ctx, userID, groupID)
|
||||
if err != nil {
|
||||
ctl.handleError(c, err, "获取成员列表失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, result)
|
||||
}
|
||||
|
||||
// InviteMembers 邀请入群
|
||||
// POST /api/v1/groups/:id/members
|
||||
func (ctl *GroupController) InviteMembers(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "群 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.InviteMembersRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.ResponseBadRequest(c, "参数校验失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctl.groupService.InviteMembers(ctx, userID, groupID, req.UserIDs); err != nil {
|
||||
ctl.handleError(c, err, "邀请入群失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, nil)
|
||||
}
|
||||
|
||||
// KickMember 踢出成员
|
||||
// DELETE /api/v1/groups/:id/members/:uid
|
||||
func (ctl *GroupController) KickMember(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "群 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
targetID, err := strconv.ParseInt(c.Param("uid"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "用户 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctl.groupService.KickMember(ctx, userID, groupID, targetID); err != nil {
|
||||
ctl.handleError(c, err, "踢出成员失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, nil)
|
||||
}
|
||||
|
||||
// SetMemberRole 设置/取消管理员
|
||||
// PUT /api/v1/groups/:id/members/:uid/role
|
||||
func (ctl *GroupController) SetMemberRole(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "群 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
targetID, err := strconv.ParseInt(c.Param("uid"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "用户 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.SetRoleRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.ResponseBadRequest(c, "参数校验失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctl.groupService.SetMemberRole(ctx, userID, groupID, targetID, req.Role); err != nil {
|
||||
ctl.handleError(c, err, "设置角色失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, nil)
|
||||
}
|
||||
|
||||
// MuteMember 禁言/解除禁言
|
||||
// PUT /api/v1/groups/:id/members/:uid/mute
|
||||
func (ctl *GroupController) MuteMember(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "群 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
targetID, err := strconv.ParseInt(c.Param("uid"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "用户 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.MuteRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.ResponseBadRequest(c, "参数校验失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctl.groupService.MuteMember(ctx, userID, groupID, targetID, req.IsMuted); err != nil {
|
||||
ctl.handleError(c, err, "操作失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, nil)
|
||||
}
|
||||
|
||||
// TransferOwner 转让群主
|
||||
// PUT /api/v1/groups/:id/transfer
|
||||
func (ctl *GroupController) TransferOwner(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "群 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.TransferOwnerRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.ResponseBadRequest(c, "参数校验失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctl.groupService.TransferOwner(ctx, userID, groupID, req.NewOwnerID); err != nil {
|
||||
ctl.handleError(c, err, "转让群主失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, nil)
|
||||
}
|
||||
|
||||
// LeaveGroup 退出群聊
|
||||
// DELETE /api/v1/groups/:id/members/me
|
||||
func (ctl *GroupController) LeaveGroup(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "群 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctl.groupService.LeaveGroup(ctx, userID, groupID); err != nil {
|
||||
ctl.handleError(c, err, "退出群聊失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, nil)
|
||||
}
|
||||
|
||||
// UpdateNickname 修改群内昵称
|
||||
// PUT /api/v1/groups/:id/members/me/nickname
|
||||
func (ctl *GroupController) UpdateNickname(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "群 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.UpdateNicknameRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.ResponseBadRequest(c, "参数校验失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctl.groupService.UpdateNickname(ctx, userID, groupID, req.Nickname); err != nil {
|
||||
ctl.handleError(c, err, "修改昵称失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, nil)
|
||||
}
|
||||
|
||||
// SubmitJoinRequest 申请入群
|
||||
// POST /api/v1/groups/:id/join-requests
|
||||
func (ctl *GroupController) SubmitJoinRequest(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "群 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.JoinGroupRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.ResponseBadRequest(c, "参数校验失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctl.groupService.SubmitJoinRequest(ctx, userID, groupID, req.Message); err != nil {
|
||||
ctl.handleError(c, err, "申请入群失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, nil)
|
||||
}
|
||||
|
||||
// GetJoinRequests 获取入群申请列表
|
||||
// GET /api/v1/groups/:id/join-requests
|
||||
func (ctl *GroupController) GetJoinRequests(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "群 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
result, err := ctl.groupService.GetJoinRequests(ctx, userID, groupID)
|
||||
if err != nil {
|
||||
ctl.handleError(c, err, "获取入群申请失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, result)
|
||||
}
|
||||
|
||||
// ReviewJoinRequest 审批入群申请
|
||||
// PUT /api/v1/groups/:id/join-requests/:rid
|
||||
func (ctl *GroupController) ReviewJoinRequest(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "群 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
requestID, err := strconv.ParseInt(c.Param("rid"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "申请 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.ReviewJoinRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.ResponseBadRequest(c, "参数校验失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctl.groupService.ReviewJoinRequest(ctx, userID, groupID, requestID, req.Action); err != nil {
|
||||
ctl.handleError(c, err, "审批失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, nil)
|
||||
}
|
||||
|
||||
// SearchGroups 搜索群聊
|
||||
// GET /api/v1/groups/search?keyword=xx&page=1&page_size=20
|
||||
func (ctl *GroupController) SearchGroups(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
_, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.SearchGroupRequest
|
||||
if err := c.ShouldBindQuery(&req); err != nil {
|
||||
utils.ResponseBadRequest(c, "参数校验失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
result, err := ctl.groupService.SearchGroups(ctx, &req)
|
||||
if err != nil {
|
||||
ctl.handleError(c, err, "搜索群聊失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, result)
|
||||
}
|
||||
|
||||
// SetAllMuted 设置全体禁言
|
||||
// PUT /api/v1/groups/:id/all-mute
|
||||
func (ctl *GroupController) SetAllMuted(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
groupID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "群 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
var body struct {
|
||||
IsAllMuted bool `json:"is_all_muted"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
utils.ResponseBadRequest(c, "参数校验失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctl.groupService.SetAllMuted(ctx, userID, groupID, body.IsAllMuted); err != nil {
|
||||
ctl.handleError(c, err, "设置全体禁言失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, nil)
|
||||
}
|
||||
|
||||
// handleError 统一业务错误映射
|
||||
// 已知业务错误 → 返回 Service 层定义的具体提示
|
||||
// 未知错误 → 返回 fallbackMsg(未传则默认"服务器内部错误")
|
||||
func (ctl *GroupController) handleError(c *gin.Context, err error, fallbackMsg ...string) {
|
||||
switch err {
|
||||
case service.ErrGroupNotFound:
|
||||
utils.ResponseNotFound(c, err.Error())
|
||||
case service.ErrGroupDissolved:
|
||||
utils.ResponseBadRequest(c, err.Error())
|
||||
case service.ErrNotGroupMember:
|
||||
utils.ResponseForbidden(c, err.Error())
|
||||
case service.ErrNotGroupOwner:
|
||||
utils.ResponseForbidden(c, err.Error())
|
||||
case service.ErrNotGroupAdmin:
|
||||
utils.ResponseForbidden(c, err.Error())
|
||||
case service.ErrGroupFull:
|
||||
utils.ResponseBadRequest(c, err.Error())
|
||||
case service.ErrAlreadyMember:
|
||||
utils.ResponseBadRequest(c, err.Error())
|
||||
case service.ErrCannotKickHigherRole:
|
||||
utils.ResponseForbidden(c, err.Error())
|
||||
case service.ErrOwnerCannotLeave:
|
||||
utils.ResponseBadRequest(c, err.Error())
|
||||
case service.ErrCannotMuteSelf:
|
||||
utils.ResponseBadRequest(c, err.Error())
|
||||
case service.ErrAlreadyMuted:
|
||||
utils.ResponseBadRequest(c, err.Error())
|
||||
case service.ErrUserMuted:
|
||||
utils.ResponseForbidden(c, err.Error())
|
||||
case service.ErrGroupAllMuted:
|
||||
utils.ResponseForbidden(c, err.Error())
|
||||
case service.ErrPendingRequestExists:
|
||||
utils.ResponseBadRequest(c, err.Error())
|
||||
case service.ErrJoinRequestNotFound:
|
||||
utils.ResponseNotFound(c, err.Error())
|
||||
default:
|
||||
msg := "服务器内部错误"
|
||||
if len(fallbackMsg) > 0 && fallbackMsg[0] != "" {
|
||||
msg = fallbackMsg[0]
|
||||
}
|
||||
utils.ResponseError(c, msg)
|
||||
}
|
||||
}
|
||||
346
backend/go-service/app/group/dao/group_dao.go
Normal file
346
backend/go-service/app/group/dao/group_dao.go
Normal file
@@ -0,0 +1,346 @@
|
||||
// Package dao 提供 group 模块的数据库访问操作
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/echochat/backend/app/constants"
|
||||
"github.com/echochat/backend/app/dto"
|
||||
"github.com/echochat/backend/app/group/model"
|
||||
imModel "github.com/echochat/backend/app/im/model"
|
||||
"github.com/echochat/backend/pkg/logs"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GroupDAO 群聊数据访问对象
|
||||
type GroupDAO struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewGroupDAO 创建 GroupDAO 实例
|
||||
func NewGroupDAO(db *gorm.DB) *GroupDAO {
|
||||
return &GroupDAO{db: db}
|
||||
}
|
||||
|
||||
// CreateGroupWithMembers 在事务中创建群聊(会话 + 群信息 + 群成员)
|
||||
// 创建者自动成为群主(role=2)
|
||||
func (d *GroupDAO) CreateGroupWithMembers(ctx context.Context, ownerID int64, name, avatar string, memberIDs []int64) (*model.Group, error) {
|
||||
funcName := "dao.group_dao.CreateGroupWithMembers"
|
||||
logs.Info(ctx, funcName, "创建群聊",
|
||||
zap.Int64("owner_id", ownerID), zap.String("name", name), zap.Int("member_count", len(memberIDs)+1))
|
||||
|
||||
var group model.Group
|
||||
err := d.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
conv := &imModel.Conversation{
|
||||
Type: constants.ConversationTypeGroup,
|
||||
CreatorID: ownerID,
|
||||
}
|
||||
if err := tx.Create(conv).Error; err != nil {
|
||||
logs.Error(ctx, funcName, "创建会话失败", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
group = model.Group{
|
||||
ConversationID: conv.ID,
|
||||
Name: name,
|
||||
Avatar: avatar,
|
||||
OwnerID: ownerID,
|
||||
MaxMembers: constants.GroupDefaultMaxMembers,
|
||||
Status: constants.GroupStatusNormal,
|
||||
}
|
||||
if err := tx.Create(&group).Error; err != nil {
|
||||
logs.Error(ctx, funcName, "创建群信息失败", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
ownerMember := &imModel.ConversationMember{
|
||||
ConversationID: conv.ID,
|
||||
UserID: ownerID,
|
||||
Role: constants.GroupRoleOwner,
|
||||
JoinedAt: &now,
|
||||
}
|
||||
if err := tx.Create(ownerMember).Error; err != nil {
|
||||
logs.Error(ctx, funcName, "创建群主成员记录失败", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
for _, uid := range memberIDs {
|
||||
member := &imModel.ConversationMember{
|
||||
ConversationID: conv.ID,
|
||||
UserID: uid,
|
||||
Role: constants.GroupRoleNormal,
|
||||
JoinedAt: &now,
|
||||
}
|
||||
if err := tx.Create(member).Error; err != nil {
|
||||
logs.Error(ctx, funcName, "创建群成员记录失败",
|
||||
zap.Int64("user_id", uid), zap.Error(err))
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "创建群聊失败", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
return &group, nil
|
||||
}
|
||||
|
||||
// GetByID 根据群 ID 获取群信息
|
||||
func (d *GroupDAO) GetByID(ctx context.Context, groupID int64) (*model.Group, error) {
|
||||
funcName := "dao.group_dao.GetByID"
|
||||
logs.Debug(ctx, funcName, "获取群信息", zap.Int64("group_id", groupID))
|
||||
|
||||
var group model.Group
|
||||
err := d.db.WithContext(ctx).First(&group, groupID).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &group, nil
|
||||
}
|
||||
|
||||
// GetByConversationID 根据会话 ID 获取群信息
|
||||
func (d *GroupDAO) GetByConversationID(ctx context.Context, conversationID int64) (*model.Group, error) {
|
||||
funcName := "dao.group_dao.GetByConversationID"
|
||||
logs.Debug(ctx, funcName, "按会话 ID 获取群信息", zap.Int64("conversation_id", conversationID))
|
||||
|
||||
var group model.Group
|
||||
err := d.db.WithContext(ctx).Where("conversation_id = ?", conversationID).First(&group).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &group, nil
|
||||
}
|
||||
|
||||
// UpdateGroupInfo 更新群基本信息(名称/头像/公告/可搜索性)
|
||||
func (d *GroupDAO) UpdateGroupInfo(ctx context.Context, groupID int64, updates map[string]interface{}) error {
|
||||
funcName := "dao.group_dao.UpdateGroupInfo"
|
||||
logs.Info(ctx, funcName, "更新群信息", zap.Int64("group_id", groupID))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&model.Group{}).
|
||||
Where("id = ?", groupID).
|
||||
Updates(updates).Error
|
||||
}
|
||||
|
||||
// DissolveGroup 解散群聊(标记状态为已解散)
|
||||
func (d *GroupDAO) DissolveGroup(ctx context.Context, groupID int64) error {
|
||||
funcName := "dao.group_dao.DissolveGroup"
|
||||
logs.Info(ctx, funcName, "解散群聊", zap.Int64("group_id", groupID))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&model.Group{}).
|
||||
Where("id = ?", groupID).
|
||||
Update("status", constants.GroupStatusDissolved).Error
|
||||
}
|
||||
|
||||
// TransferOwner 转让群主
|
||||
func (d *GroupDAO) TransferOwner(ctx context.Context, groupID, oldOwnerID, newOwnerID int64, conversationID int64) error {
|
||||
funcName := "dao.group_dao.TransferOwner"
|
||||
logs.Info(ctx, funcName, "转让群主",
|
||||
zap.Int64("group_id", groupID), zap.Int64("old_owner", oldOwnerID), zap.Int64("new_owner", newOwnerID))
|
||||
|
||||
return d.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Model(&model.Group{}).
|
||||
Where("id = ?", groupID).
|
||||
Update("owner_id", newOwnerID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, oldOwnerID).
|
||||
Update("role", constants.GroupRoleNormal).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, newOwnerID).
|
||||
Update("role", constants.GroupRoleOwner).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// SetAllMuted 设置/取消全体禁言
|
||||
func (d *GroupDAO) SetAllMuted(ctx context.Context, groupID int64, isMuted bool) error {
|
||||
funcName := "dao.group_dao.SetAllMuted"
|
||||
logs.Info(ctx, funcName, "设置全体禁言", zap.Int64("group_id", groupID), zap.Bool("is_muted", isMuted))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&model.Group{}).
|
||||
Where("id = ?", groupID).
|
||||
Update("is_all_muted", isMuted).Error
|
||||
}
|
||||
|
||||
// GetGroupBrief 获取群简要信息(满足 im/service.GroupInfoGetter 接口)
|
||||
func (d *GroupDAO) GetGroupBrief(ctx context.Context, conversationID int64) (*dto.GroupBrief, error) {
|
||||
funcName := "dao.group_dao.GetGroupBrief"
|
||||
logs.Debug(ctx, funcName, "获取群简要信息", zap.Int64("conversation_id", conversationID))
|
||||
|
||||
var group model.Group
|
||||
err := d.db.WithContext(ctx).Where("conversation_id = ?", conversationID).First(&group).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto.GroupBrief{
|
||||
ID: group.ID,
|
||||
Name: group.Name,
|
||||
Avatar: group.Avatar,
|
||||
IsAllMuted: group.IsAllMuted,
|
||||
Status: group.Status,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetMemberCount 获取群成员数量
|
||||
func (d *GroupDAO) GetMemberCount(ctx context.Context, conversationID int64) (int64, error) {
|
||||
var count int64
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ?", conversationID).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetMember 获取指定群成员记录
|
||||
func (d *GroupDAO) GetMember(ctx context.Context, conversationID, userID int64) (*imModel.ConversationMember, error) {
|
||||
funcName := "dao.group_dao.GetMember"
|
||||
logs.Debug(ctx, funcName, "查询群成员",
|
||||
zap.Int64("conversation_id", conversationID), zap.Int64("user_id", userID))
|
||||
|
||||
var member imModel.ConversationMember
|
||||
err := d.db.WithContext(ctx).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, userID).
|
||||
First(&member).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &member, nil
|
||||
}
|
||||
|
||||
// GetMembers 获取群所有成员列表
|
||||
func (d *GroupDAO) GetMembers(ctx context.Context, conversationID int64) ([]imModel.ConversationMember, error) {
|
||||
funcName := "dao.group_dao.GetMembers"
|
||||
logs.Debug(ctx, funcName, "获取群成员列表", zap.Int64("conversation_id", conversationID))
|
||||
|
||||
var members []imModel.ConversationMember
|
||||
err := d.db.WithContext(ctx).
|
||||
Where("conversation_id = ?", conversationID).
|
||||
Order("role DESC, joined_at ASC").
|
||||
Find(&members).Error
|
||||
return members, err
|
||||
}
|
||||
|
||||
// GetMemberIDs 获取群所有成员 ID
|
||||
func (d *GroupDAO) GetMemberIDs(ctx context.Context, conversationID int64) ([]int64, error) {
|
||||
var ids []int64
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ?", conversationID).
|
||||
Pluck("user_id", &ids).Error
|
||||
return ids, err
|
||||
}
|
||||
|
||||
// GetAdminIDs 获取群主和管理员 ID 列表(用于推送入群申请通知)
|
||||
func (d *GroupDAO) GetAdminIDs(ctx context.Context, conversationID int64) ([]int64, error) {
|
||||
var ids []int64
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ? AND role >= ?", conversationID, constants.GroupRoleAdmin).
|
||||
Pluck("user_id", &ids).Error
|
||||
return ids, err
|
||||
}
|
||||
|
||||
// AddMember 添加群成员
|
||||
func (d *GroupDAO) AddMember(ctx context.Context, conversationID, userID int64, role int) error {
|
||||
funcName := "dao.group_dao.AddMember"
|
||||
logs.Info(ctx, funcName, "添加群成员",
|
||||
zap.Int64("conversation_id", conversationID), zap.Int64("user_id", userID))
|
||||
|
||||
now := time.Now()
|
||||
member := &imModel.ConversationMember{
|
||||
ConversationID: conversationID,
|
||||
UserID: userID,
|
||||
Role: role,
|
||||
JoinedAt: &now,
|
||||
}
|
||||
return d.db.WithContext(ctx).Create(member).Error
|
||||
}
|
||||
|
||||
// RemoveMember 移除群成员
|
||||
func (d *GroupDAO) RemoveMember(ctx context.Context, conversationID, userID int64) error {
|
||||
funcName := "dao.group_dao.RemoveMember"
|
||||
logs.Info(ctx, funcName, "移除群成员",
|
||||
zap.Int64("conversation_id", conversationID), zap.Int64("user_id", userID))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, userID).
|
||||
Delete(&imModel.ConversationMember{}).Error
|
||||
}
|
||||
|
||||
// UpdateMemberRole 更新成员角色
|
||||
func (d *GroupDAO) UpdateMemberRole(ctx context.Context, conversationID, userID int64, role int) error {
|
||||
funcName := "dao.group_dao.UpdateMemberRole"
|
||||
logs.Info(ctx, funcName, "更新成员角色",
|
||||
zap.Int64("conversation_id", conversationID), zap.Int64("user_id", userID), zap.Int("role", role))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, userID).
|
||||
Update("role", role).Error
|
||||
}
|
||||
|
||||
// UpdateMemberMuted 更新成员禁言状态
|
||||
func (d *GroupDAO) UpdateMemberMuted(ctx context.Context, conversationID, userID int64, isMuted bool) error {
|
||||
funcName := "dao.group_dao.UpdateMemberMuted"
|
||||
logs.Info(ctx, funcName, "更新成员禁言状态",
|
||||
zap.Int64("conversation_id", conversationID), zap.Int64("user_id", userID), zap.Bool("is_muted", isMuted))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, userID).
|
||||
Update("is_muted", isMuted).Error
|
||||
}
|
||||
|
||||
// UpdateMemberNickname 更新成员群内昵称
|
||||
func (d *GroupDAO) UpdateMemberNickname(ctx context.Context, conversationID, userID int64, nickname string) error {
|
||||
funcName := "dao.group_dao.UpdateMemberNickname"
|
||||
logs.Info(ctx, funcName, "更新群昵称",
|
||||
zap.Int64("conversation_id", conversationID), zap.Int64("user_id", userID))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, userID).
|
||||
Update("nickname", nickname).Error
|
||||
}
|
||||
|
||||
// SearchGroups 搜索可发现的群聊(按群名称全文搜索)
|
||||
func (d *GroupDAO) SearchGroups(ctx context.Context, keyword string, offset, limit int) ([]model.Group, int64, error) {
|
||||
funcName := "dao.group_dao.SearchGroups"
|
||||
logs.Debug(ctx, funcName, "搜索群聊", zap.String("keyword", keyword))
|
||||
|
||||
var total int64
|
||||
query := d.db.WithContext(ctx).
|
||||
Model(&model.Group{}).
|
||||
Where("status = ? AND is_searchable = true AND to_tsvector('simple', name) @@ plainto_tsquery('simple', ?)",
|
||||
constants.GroupStatusNormal, keyword)
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
logs.Error(ctx, funcName, "搜索计数失败", zap.Error(err))
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
var groups []model.Group
|
||||
err := query.Offset(offset).Limit(limit).Order("created_at DESC").Find(&groups).Error
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "搜索查询失败", zap.Error(err))
|
||||
return nil, 0, err
|
||||
}
|
||||
return groups, total, nil
|
||||
}
|
||||
112
backend/go-service/app/group/dao/join_request_dao.go
Normal file
112
backend/go-service/app/group/dao/join_request_dao.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/echochat/backend/app/constants"
|
||||
"github.com/echochat/backend/app/group/model"
|
||||
"github.com/echochat/backend/pkg/logs"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// JoinRequestDAO 入群申请数据访问对象
|
||||
type JoinRequestDAO struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewJoinRequestDAO 创建 JoinRequestDAO 实例
|
||||
func NewJoinRequestDAO(db *gorm.DB) *JoinRequestDAO {
|
||||
return &JoinRequestDAO{db: db}
|
||||
}
|
||||
|
||||
// Create 创建入群申请
|
||||
func (d *JoinRequestDAO) Create(ctx context.Context, groupID, userID int64, message string) (*model.GroupJoinRequest, error) {
|
||||
funcName := "dao.join_request_dao.Create"
|
||||
logs.Info(ctx, funcName, "创建入群申请",
|
||||
zap.Int64("group_id", groupID), zap.Int64("user_id", userID))
|
||||
|
||||
req := &model.GroupJoinRequest{
|
||||
GroupID: groupID,
|
||||
UserID: userID,
|
||||
Message: message,
|
||||
Status: constants.JoinRequestStatusPending,
|
||||
}
|
||||
err := d.db.WithContext(ctx).Create(req).Error
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "创建入群申请失败", zap.Error(err))
|
||||
}
|
||||
return req, err
|
||||
}
|
||||
|
||||
// GetByID 根据 ID 获取入群申请
|
||||
func (d *JoinRequestDAO) GetByID(ctx context.Context, id int64) (*model.GroupJoinRequest, error) {
|
||||
var req model.GroupJoinRequest
|
||||
err := d.db.WithContext(ctx).First(&req, id).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
// GetPendingByGroupAndUser 查找用户对某群的待处理申请
|
||||
func (d *JoinRequestDAO) GetPendingByGroupAndUser(ctx context.Context, groupID, userID int64) (*model.GroupJoinRequest, error) {
|
||||
funcName := "dao.join_request_dao.GetPendingByGroupAndUser"
|
||||
logs.Debug(ctx, funcName, "查找待处理申请",
|
||||
zap.Int64("group_id", groupID), zap.Int64("user_id", userID))
|
||||
|
||||
var req model.GroupJoinRequest
|
||||
err := d.db.WithContext(ctx).
|
||||
Where("group_id = ? AND user_id = ? AND status = ?", groupID, userID, constants.JoinRequestStatusPending).
|
||||
First(&req).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
// GetListByGroup 获取群的入群申请列表
|
||||
func (d *JoinRequestDAO) GetListByGroup(ctx context.Context, groupID int64) ([]model.GroupJoinRequest, error) {
|
||||
funcName := "dao.join_request_dao.GetListByGroup"
|
||||
logs.Debug(ctx, funcName, "获取入群申请列表", zap.Int64("group_id", groupID))
|
||||
|
||||
var requests []model.GroupJoinRequest
|
||||
err := d.db.WithContext(ctx).
|
||||
Where("group_id = ?", groupID).
|
||||
Order("created_at DESC").
|
||||
Find(&requests).Error
|
||||
return requests, err
|
||||
}
|
||||
|
||||
// Approve 通过入群申请
|
||||
func (d *JoinRequestDAO) Approve(ctx context.Context, id, reviewerID int64) error {
|
||||
funcName := "dao.join_request_dao.Approve"
|
||||
logs.Info(ctx, funcName, "通过入群申请",
|
||||
zap.Int64("request_id", id), zap.Int64("reviewer_id", reviewerID))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&model.GroupJoinRequest{}).
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]interface{}{
|
||||
"status": constants.JoinRequestStatusApproved,
|
||||
"reviewer_id": reviewerID,
|
||||
}).Error
|
||||
}
|
||||
|
||||
// Reject 拒绝入群申请
|
||||
func (d *JoinRequestDAO) Reject(ctx context.Context, id, reviewerID int64) error {
|
||||
funcName := "dao.join_request_dao.Reject"
|
||||
logs.Info(ctx, funcName, "拒绝入群申请",
|
||||
zap.Int64("request_id", id), zap.Int64("reviewer_id", reviewerID))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&model.GroupJoinRequest{}).
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]interface{}{
|
||||
"status": constants.JoinRequestStatusRejected,
|
||||
"reviewer_id": reviewerID,
|
||||
}).Error
|
||||
}
|
||||
129
backend/go-service/app/group/dao/message_read_dao.go
Normal file
129
backend/go-service/app/group/dao/message_read_dao.go
Normal file
@@ -0,0 +1,129 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/echochat/backend/app/group/model"
|
||||
"github.com/echochat/backend/pkg/logs"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// MessageReadDAO 消息已读记录数据访问对象
|
||||
type MessageReadDAO struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewMessageReadDAO 创建 MessageReadDAO 实例
|
||||
func NewMessageReadDAO(db *gorm.DB) *MessageReadDAO {
|
||||
return &MessageReadDAO{db: db}
|
||||
}
|
||||
|
||||
// BatchCreate 批量创建已读记录(忽略重复冲突)
|
||||
func (d *MessageReadDAO) BatchCreate(ctx context.Context, reads []model.MessageRead) error {
|
||||
funcName := "dao.message_read_dao.BatchCreate"
|
||||
logs.Info(ctx, funcName, "批量创建已读记录", zap.Int("count", len(reads)))
|
||||
|
||||
if len(reads) == 0 {
|
||||
return nil
|
||||
}
|
||||
return d.db.WithContext(ctx).
|
||||
Clauses(clause.OnConflict{DoNothing: true}).
|
||||
Create(&reads).Error
|
||||
}
|
||||
|
||||
// GetReadCount 获取消息的已读人数
|
||||
func (d *MessageReadDAO) GetReadCount(ctx context.Context, messageID int64) (int64, error) {
|
||||
var count int64
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&model.MessageRead{}).
|
||||
Where("message_id = ?", messageID).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetReadCountBatch 批量获取多条消息的已读人数
|
||||
func (d *MessageReadDAO) GetReadCountBatch(ctx context.Context, messageIDs []int64) (map[int64]int, error) {
|
||||
funcName := "dao.message_read_dao.GetReadCountBatch"
|
||||
logs.Debug(ctx, funcName, "批量获取已读计数", zap.Int("count", len(messageIDs)))
|
||||
|
||||
type result struct {
|
||||
MessageID int64 `gorm:"column:message_id"`
|
||||
ReadCount int `gorm:"column:read_count"`
|
||||
}
|
||||
|
||||
var results []result
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&model.MessageRead{}).
|
||||
Select("message_id, COUNT(*) as read_count").
|
||||
Where("message_id IN ?", messageIDs).
|
||||
Group("message_id").
|
||||
Scan(&results).Error
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m := make(map[int64]int, len(results))
|
||||
for _, r := range results {
|
||||
m[r.MessageID] = r.ReadCount
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetReadUsers 获取消息的已读用户列表
|
||||
func (d *MessageReadDAO) GetReadUsers(ctx context.Context, messageID int64) ([]model.MessageRead, error) {
|
||||
funcName := "dao.message_read_dao.GetReadUsers"
|
||||
logs.Debug(ctx, funcName, "获取已读用户列表", zap.Int64("message_id", messageID))
|
||||
|
||||
var reads []model.MessageRead
|
||||
err := d.db.WithContext(ctx).
|
||||
Where("message_id = ?", messageID).
|
||||
Order("read_at ASC").
|
||||
Find(&reads).Error
|
||||
return reads, err
|
||||
}
|
||||
|
||||
// BatchCreateReads 批量标记消息已读(满足 im/service.MessageReadRecorder 接口)
|
||||
func (d *MessageReadDAO) BatchCreateReads(ctx context.Context, messageIDs []int64, userID int64) error {
|
||||
funcName := "dao.message_read_dao.BatchCreateReads"
|
||||
logs.Info(ctx, funcName, "批量标记已读",
|
||||
zap.Int64("user_id", userID), zap.Int("count", len(messageIDs)))
|
||||
|
||||
if len(messageIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
reads := make([]model.MessageRead, 0, len(messageIDs))
|
||||
for _, msgID := range messageIDs {
|
||||
reads = append(reads, model.MessageRead{
|
||||
MessageID: msgID,
|
||||
UserID: userID,
|
||||
})
|
||||
}
|
||||
return d.BatchCreate(ctx, reads)
|
||||
}
|
||||
|
||||
// GetReadUserIDs 获取消息的已读用户 ID 列表(满足 im/service.MessageReadRecorder 接口)
|
||||
func (d *MessageReadDAO) GetReadUserIDs(ctx context.Context, messageID int64) ([]int64, error) {
|
||||
funcName := "dao.message_read_dao.GetReadUserIDs"
|
||||
logs.Debug(ctx, funcName, "获取已读用户 ID 列表", zap.Int64("message_id", messageID))
|
||||
|
||||
var ids []int64
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&model.MessageRead{}).
|
||||
Where("message_id = ?", messageID).
|
||||
Pluck("user_id", &ids).Error
|
||||
return ids, err
|
||||
}
|
||||
|
||||
// HasRead 检查用户是否已读某消息
|
||||
func (d *MessageReadDAO) HasRead(ctx context.Context, messageID, userID int64) (bool, error) {
|
||||
var count int64
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&model.MessageRead{}).
|
||||
Where("message_id = ? AND user_id = ?", messageID, userID).
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
27
backend/go-service/app/group/model/group.go
Normal file
27
backend/go-service/app/group/model/group.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// Package model 定义 group 模块的数据库模型
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// Group 群聊信息模型,对应 im_groups 表
|
||||
// 与 im_conversations (type=2) 一对一关联
|
||||
// Status: 1=正常,2=已解散
|
||||
type Group struct {
|
||||
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"` // 群唯一标识
|
||||
ConversationID int64 `json:"conversation_id" gorm:"not null;uniqueIndex"` // 关联 im_conversations.id
|
||||
Name string `json:"name" gorm:"size:100;not null;default:''"` // 群名称
|
||||
Avatar string `json:"avatar" gorm:"size:500;default:''"` // 群头像 URL(MinIO)
|
||||
OwnerID int64 `json:"owner_id" gorm:"not null;index:idx_im_groups_owner"` // 群主用户 ID
|
||||
Notice string `json:"notice" gorm:"type:text;default:''"` // 群公告内容
|
||||
MaxMembers int `json:"max_members" gorm:"not null;default:200"` // 最大成员数
|
||||
IsSearchable bool `json:"is_searchable" gorm:"not null;default:true"` // 是否可被搜索发现
|
||||
IsAllMuted bool `json:"is_all_muted" gorm:"not null;default:false"` // 是否全体禁言
|
||||
Status int `json:"status" gorm:"not null;default:1"` // 群状态:1=正常,2=已解散
|
||||
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime;type:timestamp(0)"` // 创建时间
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"not null;autoUpdateTime;type:timestamp(0)"` // 更新时间
|
||||
}
|
||||
|
||||
// TableName 指定数据库表名
|
||||
func (Group) TableName() string {
|
||||
return "im_groups"
|
||||
}
|
||||
21
backend/go-service/app/group/model/join_request.go
Normal file
21
backend/go-service/app/group/model/join_request.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// GroupJoinRequest 入群申请模型,对应 im_group_join_requests 表
|
||||
// Status: 0=待审批,1=通过,2=拒绝
|
||||
type GroupJoinRequest struct {
|
||||
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"` // 申请唯一标识
|
||||
GroupID int64 `json:"group_id" gorm:"not null;index:idx_group_join_req_group"` // 目标群 ID
|
||||
UserID int64 `json:"user_id" gorm:"not null;index:idx_group_join_req_user"` // 申请人用户 ID
|
||||
Message string `json:"message" gorm:"type:text;default:''"` // 申请附言
|
||||
ReviewerID *int64 `json:"reviewer_id"` // 审批人用户 ID
|
||||
Status int `json:"status" gorm:"not null;default:0"` // 状态:0=待审批,1=通过,2=拒绝
|
||||
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime;type:timestamp(0)"` // 申请时间
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"not null;autoUpdateTime;type:timestamp(0)"` // 更新时间
|
||||
}
|
||||
|
||||
// TableName 指定数据库表名
|
||||
func (GroupJoinRequest) TableName() string {
|
||||
return "im_group_join_requests"
|
||||
}
|
||||
16
backend/go-service/app/group/model/message_read.go
Normal file
16
backend/go-service/app/group/model/message_read.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// MessageRead 群聊消息已读记录模型,对应 im_message_reads 表
|
||||
// 复合主键 (message_id, user_id),记录每条群消息的已读用户
|
||||
type MessageRead struct {
|
||||
MessageID int64 `json:"message_id" gorm:"primaryKey"` // 消息 ID
|
||||
UserID int64 `json:"user_id" gorm:"primaryKey"` // 已读用户 ID
|
||||
ReadAt time.Time `json:"read_at" gorm:"not null;autoCreateTime;type:timestamp(0)"` // 已读时间
|
||||
}
|
||||
|
||||
// TableName 指定数据库表名
|
||||
func (MessageRead) TableName() string {
|
||||
return "im_message_reads"
|
||||
}
|
||||
18
backend/go-service/app/group/provider.go
Normal file
18
backend/go-service/app/group/provider.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// Package group 群聊管理模块
|
||||
package group
|
||||
|
||||
import (
|
||||
"github.com/echochat/backend/app/group/controller"
|
||||
"github.com/echochat/backend/app/group/dao"
|
||||
"github.com/echochat/backend/app/group/service"
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
||||
// GroupSet 群聊模块 Wire Provider Set
|
||||
var GroupSet = wire.NewSet(
|
||||
dao.NewGroupDAO,
|
||||
dao.NewJoinRequestDAO,
|
||||
dao.NewMessageReadDAO,
|
||||
service.NewGroupService,
|
||||
controller.NewGroupController,
|
||||
)
|
||||
38
backend/go-service/app/group/router.go
Normal file
38
backend/go-service/app/group/router.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package group
|
||||
|
||||
import (
|
||||
"github.com/echochat/backend/app/group/controller"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// RegisterRoutes 注册 group 模块的所有路由(需要 JWT 中间件)
|
||||
func RegisterRoutes(r *gin.Engine, ctrl *controller.GroupController, jwtAuth gin.HandlerFunc) {
|
||||
authed := r.Group("/api/v1")
|
||||
authed.Use(jwtAuth)
|
||||
{
|
||||
// 群聊管理
|
||||
authed.POST("/groups", ctrl.CreateGroup)
|
||||
authed.GET("/groups/search", ctrl.SearchGroups)
|
||||
authed.GET("/groups/:id", ctrl.GetGroupDetail)
|
||||
authed.PUT("/groups/:id", ctrl.UpdateGroup)
|
||||
authed.DELETE("/groups/:id", ctrl.DissolveGroup)
|
||||
|
||||
// 群成员管理
|
||||
authed.GET("/groups/:id/members", ctrl.GetMembers)
|
||||
authed.POST("/groups/:id/members", ctrl.InviteMembers)
|
||||
authed.DELETE("/groups/:id/members/me", ctrl.LeaveGroup)
|
||||
authed.DELETE("/groups/:id/members/:uid", ctrl.KickMember)
|
||||
authed.PUT("/groups/:id/members/me/nickname", ctrl.UpdateNickname)
|
||||
authed.PUT("/groups/:id/members/:uid/role", ctrl.SetMemberRole)
|
||||
authed.PUT("/groups/:id/members/:uid/mute", ctrl.MuteMember)
|
||||
|
||||
// 群主转让 + 全体禁言
|
||||
authed.PUT("/groups/:id/transfer", ctrl.TransferOwner)
|
||||
authed.PUT("/groups/:id/all-mute", ctrl.SetAllMuted)
|
||||
|
||||
// 入群申请
|
||||
authed.POST("/groups/:id/join-requests", ctrl.SubmitJoinRequest)
|
||||
authed.GET("/groups/:id/join-requests", ctrl.GetJoinRequests)
|
||||
authed.PUT("/groups/:id/join-requests/:rid", ctrl.ReviewJoinRequest)
|
||||
}
|
||||
}
|
||||
940
backend/go-service/app/group/service/group_service.go
Normal file
940
backend/go-service/app/group/service/group_service.go
Normal file
@@ -0,0 +1,940 @@
|
||||
// Package service 提供 group 模块的业务逻辑
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
authModel "github.com/echochat/backend/app/auth/model"
|
||||
"github.com/echochat/backend/app/constants"
|
||||
"github.com/echochat/backend/app/dto"
|
||||
"github.com/echochat/backend/app/group/dao"
|
||||
"github.com/echochat/backend/app/group/model"
|
||||
imModel "github.com/echochat/backend/app/im/model"
|
||||
"github.com/echochat/backend/pkg/logs"
|
||||
"github.com/echochat/backend/pkg/ws"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrGroupNotFound = errors.New("群聊不存在")
|
||||
ErrGroupDissolved = errors.New("群聊已解散")
|
||||
ErrNotGroupMember = errors.New("你不是该群成员")
|
||||
ErrNotGroupOwner = errors.New("仅群主可执行此操作")
|
||||
ErrNotGroupAdmin = errors.New("仅群主或管理员可执行此操作")
|
||||
ErrGroupFull = errors.New("群成员已满")
|
||||
ErrAlreadyMember = errors.New("该用户已是群成员")
|
||||
ErrCannotKickHigherRole = errors.New("不能操作同级或更高权限的成员")
|
||||
ErrOwnerCannotLeave = errors.New("群主不能退出群聊,请先转让群主")
|
||||
ErrCannotMuteSelf = errors.New("不能禁言自己")
|
||||
ErrAlreadyMuted = errors.New("该成员已被禁言")
|
||||
ErrUserMuted = errors.New("你已被禁言,无法发送消息")
|
||||
ErrGroupAllMuted = errors.New("当前群已开启全体禁言")
|
||||
ErrPendingRequestExists = errors.New("已有待处理的入群申请")
|
||||
ErrJoinRequestNotFound = errors.New("入群申请不存在")
|
||||
)
|
||||
|
||||
// UserInfoProvider 获取用户信息的接口(通过接口注入,由 contact.FriendshipDAO 隐式实现)
|
||||
type UserInfoProvider interface {
|
||||
GetUsersByIDs(ctx context.Context, userIDs []int64) ([]authModel.User, error)
|
||||
}
|
||||
|
||||
// MessageWriter 写入系统消息的接口(由 im.MessageDAO 隐式实现)
|
||||
type MessageWriter interface {
|
||||
Create(ctx context.Context, msg *imModel.Message) error
|
||||
}
|
||||
|
||||
// GroupService 群聊业务服务
|
||||
type GroupService struct {
|
||||
groupDAO *dao.GroupDAO
|
||||
joinRequestDAO *dao.JoinRequestDAO
|
||||
userInfo UserInfoProvider
|
||||
pubsub *ws.PubSub
|
||||
msgWriter MessageWriter
|
||||
}
|
||||
|
||||
// NewGroupService 创建 GroupService 实例
|
||||
func NewGroupService(
|
||||
groupDAO *dao.GroupDAO,
|
||||
joinRequestDAO *dao.JoinRequestDAO,
|
||||
userInfo UserInfoProvider,
|
||||
pubsub *ws.PubSub,
|
||||
msgWriter MessageWriter,
|
||||
) *GroupService {
|
||||
return &GroupService{
|
||||
groupDAO: groupDAO,
|
||||
joinRequestDAO: joinRequestDAO,
|
||||
userInfo: userInfo,
|
||||
pubsub: pubsub,
|
||||
msgWriter: msgWriter,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateGroup 创建群聊
|
||||
func (s *GroupService) CreateGroup(ctx context.Context, ownerID int64, req *dto.CreateGroupRequest) (*dto.GroupDTO, error) {
|
||||
funcName := "service.group_service.CreateGroup"
|
||||
logs.Info(ctx, funcName, "创建群聊",
|
||||
zap.Int64("owner_id", ownerID), zap.String("name", req.Name), zap.Int("member_count", len(req.MemberIDs)))
|
||||
|
||||
group, err := s.groupDAO.CreateGroupWithMembers(ctx, ownerID, req.Name, req.Avatar, req.MemberIDs)
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "创建群聊失败", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.writeSystemMessage(ctx, group.ConversationID, fmt.Sprintf("%s 创建了群聊", s.getUserNickname(ctx, ownerID)))
|
||||
|
||||
allMemberIDs := append([]int64{ownerID}, req.MemberIDs...)
|
||||
s.pushToMembers(ctx, allMemberIDs, 0, "group.created", map[string]interface{}{
|
||||
"group_id": group.ID,
|
||||
"conversation_id": group.ConversationID,
|
||||
"name": group.Name,
|
||||
"owner_id": ownerID,
|
||||
})
|
||||
|
||||
return s.toGroupDTO(group, int(len(req.MemberIDs)+1)), nil
|
||||
}
|
||||
|
||||
// GetGroupDetail 获取群详情(需要是群成员)
|
||||
func (s *GroupService) GetGroupDetail(ctx context.Context, userID, groupID int64) (*dto.GroupDTO, error) {
|
||||
funcName := "service.group_service.GetGroupDetail"
|
||||
logs.Debug(ctx, funcName, "获取群详情",
|
||||
zap.Int64("user_id", userID), zap.Int64("group_id", groupID))
|
||||
|
||||
group, err := s.groupDAO.GetByID(ctx, groupID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrGroupNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = s.groupDAO.GetMember(ctx, group.ConversationID, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrNotGroupMember
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
count, _ := s.groupDAO.GetMemberCount(ctx, group.ConversationID)
|
||||
return s.toGroupDTO(group, int(count)), nil
|
||||
}
|
||||
|
||||
// UpdateGroup 更新群信息(群主或管理员)
|
||||
func (s *GroupService) UpdateGroup(ctx context.Context, userID, groupID int64, req *dto.UpdateGroupRequest) error {
|
||||
funcName := "service.group_service.UpdateGroup"
|
||||
logs.Info(ctx, funcName, "更新群信息",
|
||||
zap.Int64("user_id", userID), zap.Int64("group_id", groupID))
|
||||
|
||||
group, _, err := s.checkGroupAdmin(ctx, groupID, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
updates := make(map[string]interface{})
|
||||
if req.Name != nil {
|
||||
updates["name"] = *req.Name
|
||||
}
|
||||
if req.Avatar != nil {
|
||||
updates["avatar"] = *req.Avatar
|
||||
}
|
||||
if req.Notice != nil {
|
||||
updates["notice"] = *req.Notice
|
||||
}
|
||||
if req.IsSearchable != nil {
|
||||
updates["is_searchable"] = *req.IsSearchable
|
||||
}
|
||||
|
||||
if len(updates) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := s.groupDAO.UpdateGroupInfo(ctx, groupID, updates); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if req.Notice != nil {
|
||||
s.writeSystemMessage(ctx, group.ConversationID,
|
||||
fmt.Sprintf("%s 更新了群公告", s.getUserNickname(ctx, userID)))
|
||||
}
|
||||
|
||||
s.pushToGroupMembers(ctx, group.ConversationID, 0, "group.info.update", map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"conversation_id": group.ConversationID,
|
||||
"operator_id": userID,
|
||||
"updates": updates,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DissolveGroup 解散群聊(仅群主)
|
||||
func (s *GroupService) DissolveGroup(ctx context.Context, userID, groupID int64) error {
|
||||
funcName := "service.group_service.DissolveGroup"
|
||||
logs.Info(ctx, funcName, "解散群聊",
|
||||
zap.Int64("user_id", userID), zap.Int64("group_id", groupID))
|
||||
|
||||
group, err := s.getActiveGroup(ctx, groupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if group.OwnerID != userID {
|
||||
return ErrNotGroupOwner
|
||||
}
|
||||
|
||||
memberIDs, _ := s.groupDAO.GetMemberIDs(ctx, group.ConversationID)
|
||||
|
||||
if err := s.groupDAO.DissolveGroup(ctx, groupID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.writeSystemMessage(ctx, group.ConversationID, "群聊已解散")
|
||||
|
||||
s.pushToMembers(ctx, memberIDs, 0, "group.dissolved", map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"conversation_id": group.ConversationID,
|
||||
"operator_id": userID,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetMembers 获取群成员列表(需要是群成员)
|
||||
func (s *GroupService) GetMembers(ctx context.Context, userID, groupID int64) ([]dto.GroupMemberDTO, error) {
|
||||
funcName := "service.group_service.GetMembers"
|
||||
logs.Debug(ctx, funcName, "获取群成员列表",
|
||||
zap.Int64("user_id", userID), zap.Int64("group_id", groupID))
|
||||
|
||||
group, err := s.getActiveGroup(ctx, groupID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = s.groupDAO.GetMember(ctx, group.ConversationID, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrNotGroupMember
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
members, err := s.groupDAO.GetMembers(ctx, group.ConversationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
userIDs := make([]int64, 0, len(members))
|
||||
for _, m := range members {
|
||||
userIDs = append(userIDs, m.UserID)
|
||||
}
|
||||
|
||||
userMap := make(map[int64]*authModel.User)
|
||||
if len(userIDs) > 0 && s.userInfo != nil {
|
||||
users, uErr := s.userInfo.GetUsersByIDs(ctx, userIDs)
|
||||
if uErr != nil {
|
||||
logs.Error(ctx, funcName, "批量查询用户信息失败", zap.Error(uErr))
|
||||
} else {
|
||||
for i := range users {
|
||||
userMap[users[i].ID] = &users[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
list := make([]dto.GroupMemberDTO, 0, len(members))
|
||||
for _, m := range members {
|
||||
item := dto.GroupMemberDTO{
|
||||
UserID: m.UserID,
|
||||
Nickname: m.Nickname,
|
||||
Role: m.Role,
|
||||
IsMuted: m.IsMuted,
|
||||
}
|
||||
if m.JoinedAt != nil {
|
||||
item.JoinedAt = m.JoinedAt.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
if user, ok := userMap[m.UserID]; ok {
|
||||
item.UserNickname = user.Nickname
|
||||
item.Avatar = user.Avatar
|
||||
}
|
||||
list = append(list, item)
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// InviteMembers 邀请用户入群(群主/管理员)
|
||||
func (s *GroupService) InviteMembers(ctx context.Context, userID, groupID int64, targetIDs []int64) error {
|
||||
funcName := "service.group_service.InviteMembers"
|
||||
logs.Info(ctx, funcName, "邀请入群",
|
||||
zap.Int64("user_id", userID), zap.Int64("group_id", groupID), zap.Int("count", len(targetIDs)))
|
||||
|
||||
group, _, err := s.checkGroupAdmin(ctx, groupID, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
count, err := s.groupDAO.GetMemberCount(ctx, group.ConversationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if int(count)+len(targetIDs) > group.MaxMembers {
|
||||
return ErrGroupFull
|
||||
}
|
||||
|
||||
addedIDs := make([]int64, 0, len(targetIDs))
|
||||
for _, uid := range targetIDs {
|
||||
existing, _ := s.groupDAO.GetMember(ctx, group.ConversationID, uid)
|
||||
if existing != nil {
|
||||
continue
|
||||
}
|
||||
if err := s.groupDAO.AddMember(ctx, group.ConversationID, uid, constants.GroupRoleNormal); err != nil {
|
||||
logs.Error(ctx, funcName, "添加成员失败", zap.Int64("target_id", uid), zap.Error(err))
|
||||
return err
|
||||
}
|
||||
addedIDs = append(addedIDs, uid)
|
||||
}
|
||||
|
||||
if len(addedIDs) > 0 {
|
||||
inviterName := s.getUserNickname(ctx, userID)
|
||||
addedNames := s.getUserNicknames(ctx, addedIDs)
|
||||
s.writeSystemMessage(ctx, group.ConversationID,
|
||||
fmt.Sprintf("%s 邀请 %s 加入了群聊", inviterName, joinNames(addedNames)))
|
||||
|
||||
s.pushToGroupMembers(ctx, group.ConversationID, 0, "group.member.join", map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"conversation_id": group.ConversationID,
|
||||
"user_ids": addedIDs,
|
||||
"operator_id": userID,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// KickMember 踢出群成员(群主/管理员,不能操作同级或更高权限的成员)
|
||||
func (s *GroupService) KickMember(ctx context.Context, userID, groupID, targetID int64) error {
|
||||
funcName := "service.group_service.KickMember"
|
||||
logs.Info(ctx, funcName, "踢出成员",
|
||||
zap.Int64("user_id", userID), zap.Int64("group_id", groupID), zap.Int64("target_id", targetID))
|
||||
|
||||
group, operatorMember, err := s.checkGroupAdmin(ctx, groupID, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
targetMember, err := s.groupDAO.GetMember(ctx, group.ConversationID, targetID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrNotGroupMember
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if targetMember.Role >= operatorMember.Role {
|
||||
return ErrCannotKickHigherRole
|
||||
}
|
||||
|
||||
if err := s.groupDAO.RemoveMember(ctx, group.ConversationID, targetID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
targetName := s.getUserNickname(ctx, targetID)
|
||||
s.writeSystemMessage(ctx, group.ConversationID,
|
||||
fmt.Sprintf("%s 被移出了群聊", targetName))
|
||||
|
||||
s.pushToGroupMembers(ctx, group.ConversationID, 0, "group.member.kicked", map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"conversation_id": group.ConversationID,
|
||||
"user_id": targetID,
|
||||
"operator_id": userID,
|
||||
})
|
||||
s.pushToUser(ctx, targetID, "group.member.kicked", map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"conversation_id": group.ConversationID,
|
||||
"user_id": targetID,
|
||||
"operator_id": userID,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetMemberRole 设置/取消管理员(仅群主)
|
||||
func (s *GroupService) SetMemberRole(ctx context.Context, userID, groupID, targetID int64, role int) error {
|
||||
funcName := "service.group_service.SetMemberRole"
|
||||
logs.Info(ctx, funcName, "设置成员角色",
|
||||
zap.Int64("user_id", userID), zap.Int64("group_id", groupID),
|
||||
zap.Int64("target_id", targetID), zap.Int("role", role))
|
||||
|
||||
group, err := s.getActiveGroup(ctx, groupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if group.OwnerID != userID {
|
||||
return ErrNotGroupOwner
|
||||
}
|
||||
|
||||
_, err = s.groupDAO.GetMember(ctx, group.ConversationID, targetID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrNotGroupMember
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.groupDAO.UpdateMemberRole(ctx, group.ConversationID, targetID, role); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
targetName := s.getUserNickname(ctx, targetID)
|
||||
if role == constants.GroupRoleAdmin {
|
||||
s.writeSystemMessage(ctx, group.ConversationID, fmt.Sprintf("%s 被设为管理员", targetName))
|
||||
} else {
|
||||
s.writeSystemMessage(ctx, group.ConversationID, fmt.Sprintf("%s 被取消管理员", targetName))
|
||||
}
|
||||
|
||||
s.pushToGroupMembers(ctx, group.ConversationID, 0, "group.role.update", map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"conversation_id": group.ConversationID,
|
||||
"user_id": targetID,
|
||||
"role": role,
|
||||
"operator_id": userID,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MuteMember 禁言/解除禁言成员(群主/管理员)
|
||||
func (s *GroupService) MuteMember(ctx context.Context, userID, groupID, targetID int64, isMuted bool) error {
|
||||
funcName := "service.group_service.MuteMember"
|
||||
logs.Info(ctx, funcName, "更新禁言状态",
|
||||
zap.Int64("user_id", userID), zap.Int64("group_id", groupID),
|
||||
zap.Int64("target_id", targetID), zap.Bool("is_muted", isMuted))
|
||||
|
||||
if userID == targetID {
|
||||
return ErrCannotMuteSelf
|
||||
}
|
||||
|
||||
group, operatorMember, err := s.checkGroupAdmin(ctx, groupID, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
targetMember, err := s.groupDAO.GetMember(ctx, group.ConversationID, targetID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrNotGroupMember
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if targetMember.Role >= operatorMember.Role {
|
||||
return ErrCannotKickHigherRole
|
||||
}
|
||||
|
||||
if err := s.groupDAO.UpdateMemberMuted(ctx, group.ConversationID, targetID, isMuted); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.pushToGroupMembers(ctx, group.ConversationID, 0, "group.mute.update", map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"conversation_id": group.ConversationID,
|
||||
"user_id": targetID,
|
||||
"is_muted": isMuted,
|
||||
"operator_id": userID,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// LeaveGroup 退出群聊(群主不能退出,需先转让)
|
||||
func (s *GroupService) LeaveGroup(ctx context.Context, userID, groupID int64) error {
|
||||
funcName := "service.group_service.LeaveGroup"
|
||||
logs.Info(ctx, funcName, "退出群聊",
|
||||
zap.Int64("user_id", userID), zap.Int64("group_id", groupID))
|
||||
|
||||
group, err := s.getActiveGroup(ctx, groupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if group.OwnerID == userID {
|
||||
return ErrOwnerCannotLeave
|
||||
}
|
||||
|
||||
_, err = s.groupDAO.GetMember(ctx, group.ConversationID, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrNotGroupMember
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.groupDAO.RemoveMember(ctx, group.ConversationID, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
userName := s.getUserNickname(ctx, userID)
|
||||
s.writeSystemMessage(ctx, group.ConversationID, fmt.Sprintf("%s 退出了群聊", userName))
|
||||
|
||||
s.pushToGroupMembers(ctx, group.ConversationID, 0, "group.member.leave", map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"conversation_id": group.ConversationID,
|
||||
"user_id": userID,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// TransferOwner 转让群主(仅群主)
|
||||
func (s *GroupService) TransferOwner(ctx context.Context, userID, groupID, newOwnerID int64) error {
|
||||
funcName := "service.group_service.TransferOwner"
|
||||
logs.Info(ctx, funcName, "转让群主",
|
||||
zap.Int64("user_id", userID), zap.Int64("group_id", groupID), zap.Int64("new_owner", newOwnerID))
|
||||
|
||||
group, err := s.getActiveGroup(ctx, groupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if group.OwnerID != userID {
|
||||
return ErrNotGroupOwner
|
||||
}
|
||||
|
||||
_, err = s.groupDAO.GetMember(ctx, group.ConversationID, newOwnerID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrNotGroupMember
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.groupDAO.TransferOwner(ctx, groupID, userID, newOwnerID, group.ConversationID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
oldOwnerName := s.getUserNickname(ctx, userID)
|
||||
newOwnerName := s.getUserNickname(ctx, newOwnerID)
|
||||
s.writeSystemMessage(ctx, group.ConversationID,
|
||||
fmt.Sprintf("%s 将群主转让给了 %s", oldOwnerName, newOwnerName))
|
||||
|
||||
s.pushToGroupMembers(ctx, group.ConversationID, 0, "group.owner.transfer", map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"conversation_id": group.ConversationID,
|
||||
"old_owner_id": userID,
|
||||
"new_owner_id": newOwnerID,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateNickname 修改群内昵称
|
||||
func (s *GroupService) UpdateNickname(ctx context.Context, userID, groupID int64, nickname string) error {
|
||||
funcName := "service.group_service.UpdateNickname"
|
||||
logs.Info(ctx, funcName, "修改群昵称",
|
||||
zap.Int64("user_id", userID), zap.Int64("group_id", groupID))
|
||||
|
||||
group, err := s.getActiveGroup(ctx, groupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = s.groupDAO.GetMember(ctx, group.ConversationID, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrNotGroupMember
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return s.groupDAO.UpdateMemberNickname(ctx, group.ConversationID, userID, nickname)
|
||||
}
|
||||
|
||||
// SubmitJoinRequest 提交入群申请
|
||||
func (s *GroupService) SubmitJoinRequest(ctx context.Context, userID, groupID int64, message string) error {
|
||||
funcName := "service.group_service.SubmitJoinRequest"
|
||||
logs.Info(ctx, funcName, "提交入群申请",
|
||||
zap.Int64("user_id", userID), zap.Int64("group_id", groupID))
|
||||
|
||||
group, err := s.getActiveGroup(ctx, groupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
existing, _ := s.groupDAO.GetMember(ctx, group.ConversationID, userID)
|
||||
if existing != nil {
|
||||
return ErrAlreadyMember
|
||||
}
|
||||
|
||||
pending, _ := s.joinRequestDAO.GetPendingByGroupAndUser(ctx, groupID, userID)
|
||||
if pending != nil {
|
||||
return ErrPendingRequestExists
|
||||
}
|
||||
|
||||
req, err := s.joinRequestDAO.Create(ctx, groupID, userID, message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
adminIDs, _ := s.groupDAO.GetAdminIDs(ctx, group.ConversationID)
|
||||
userName := s.getUserNickname(ctx, userID)
|
||||
s.pushToMembers(ctx, adminIDs, 0, "group.join.request", map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"request_id": req.ID,
|
||||
"user_id": userID,
|
||||
"user_nickname": userName,
|
||||
"message": message,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetJoinRequests 获取入群申请列表(群主/管理员)
|
||||
func (s *GroupService) GetJoinRequests(ctx context.Context, userID, groupID int64) ([]dto.JoinRequestDTO, error) {
|
||||
funcName := "service.group_service.GetJoinRequests"
|
||||
logs.Debug(ctx, funcName, "获取入群申请列表",
|
||||
zap.Int64("user_id", userID), zap.Int64("group_id", groupID))
|
||||
|
||||
_, _, err := s.checkGroupAdmin(ctx, groupID, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
requests, err := s.joinRequestDAO.GetListByGroup(ctx, groupID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
userIDs := make([]int64, 0, len(requests))
|
||||
for _, r := range requests {
|
||||
userIDs = append(userIDs, r.UserID)
|
||||
}
|
||||
|
||||
userMap := make(map[int64]*authModel.User)
|
||||
if len(userIDs) > 0 && s.userInfo != nil {
|
||||
users, uErr := s.userInfo.GetUsersByIDs(ctx, userIDs)
|
||||
if uErr != nil {
|
||||
logs.Error(ctx, funcName, "批量查询用户信息失败", zap.Error(uErr))
|
||||
} else {
|
||||
for i := range users {
|
||||
userMap[users[i].ID] = &users[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
list := make([]dto.JoinRequestDTO, 0, len(requests))
|
||||
for _, r := range requests {
|
||||
item := dto.JoinRequestDTO{
|
||||
ID: r.ID,
|
||||
GroupID: r.GroupID,
|
||||
UserID: r.UserID,
|
||||
Message: r.Message,
|
||||
Status: r.Status,
|
||||
CreatedAt: r.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
if user, ok := userMap[r.UserID]; ok {
|
||||
item.UserNickname = user.Nickname
|
||||
item.UserAvatar = user.Avatar
|
||||
}
|
||||
list = append(list, item)
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// ReviewJoinRequest 审批入群申请(群主/管理员)
|
||||
func (s *GroupService) ReviewJoinRequest(ctx context.Context, userID, groupID, requestID int64, action string) error {
|
||||
funcName := "service.group_service.ReviewJoinRequest"
|
||||
logs.Info(ctx, funcName, "审批入群申请",
|
||||
zap.Int64("user_id", userID), zap.Int64("request_id", requestID), zap.String("action", action))
|
||||
|
||||
group, _, err := s.checkGroupAdmin(ctx, groupID, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := s.joinRequestDAO.GetByID(ctx, requestID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrJoinRequestNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if req.GroupID != groupID || req.Status != constants.JoinRequestStatusPending {
|
||||
return ErrJoinRequestNotFound
|
||||
}
|
||||
|
||||
if action == "approve" {
|
||||
count, _ := s.groupDAO.GetMemberCount(ctx, group.ConversationID)
|
||||
if int(count) >= group.MaxMembers {
|
||||
return ErrGroupFull
|
||||
}
|
||||
|
||||
if err := s.joinRequestDAO.Approve(ctx, requestID, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.groupDAO.AddMember(ctx, group.ConversationID, req.UserID, constants.GroupRoleNormal); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newMemberName := s.getUserNickname(ctx, req.UserID)
|
||||
s.writeSystemMessage(ctx, group.ConversationID, fmt.Sprintf("%s 加入了群聊", newMemberName))
|
||||
|
||||
s.pushToUser(ctx, req.UserID, "group.join.approved", map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"conversation_id": group.ConversationID,
|
||||
"request_id": requestID,
|
||||
})
|
||||
|
||||
s.pushToGroupMembers(ctx, group.ConversationID, 0, "group.member.join", map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"conversation_id": group.ConversationID,
|
||||
"user_ids": []int64{req.UserID},
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return s.joinRequestDAO.Reject(ctx, requestID, userID)
|
||||
}
|
||||
|
||||
// SearchGroups 搜索公开群
|
||||
func (s *GroupService) SearchGroups(ctx context.Context, req *dto.SearchGroupRequest) (*dto.SearchGroupResponse, error) {
|
||||
funcName := "service.group_service.SearchGroups"
|
||||
logs.Debug(ctx, funcName, "搜索群聊", zap.String("keyword", req.Keyword))
|
||||
|
||||
page := req.Page
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
pageSize := req.PageSize
|
||||
if pageSize <= 0 {
|
||||
pageSize = 20
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
groups, total, err := s.groupDAO.SearchGroups(ctx, req.Keyword, offset, pageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := make([]dto.GroupDTO, 0, len(groups))
|
||||
for _, g := range groups {
|
||||
count, _ := s.groupDAO.GetMemberCount(ctx, g.ConversationID)
|
||||
list = append(list, *s.toGroupDTO(&g, int(count)))
|
||||
}
|
||||
|
||||
return &dto.SearchGroupResponse{List: list, Total: total}, nil
|
||||
}
|
||||
|
||||
// SetAllMuted 设置/取消全体禁言(群主/管理员)
|
||||
func (s *GroupService) SetAllMuted(ctx context.Context, userID, groupID int64, isMuted bool) error {
|
||||
funcName := "service.group_service.SetAllMuted"
|
||||
logs.Info(ctx, funcName, "设置全体禁言",
|
||||
zap.Int64("user_id", userID), zap.Int64("group_id", groupID), zap.Bool("is_muted", isMuted))
|
||||
|
||||
group, _, err := s.checkGroupAdmin(ctx, groupID, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.groupDAO.SetAllMuted(ctx, groupID, isMuted); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
operatorName := s.getUserNickname(ctx, userID)
|
||||
if isMuted {
|
||||
s.writeSystemMessage(ctx, group.ConversationID, fmt.Sprintf("%s 开启了全体禁言", operatorName))
|
||||
} else {
|
||||
s.writeSystemMessage(ctx, group.ConversationID, fmt.Sprintf("%s 关闭了全体禁言", operatorName))
|
||||
}
|
||||
|
||||
s.pushToGroupMembers(ctx, group.ConversationID, 0, "group.mute.update", map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"conversation_id": group.ConversationID,
|
||||
"is_all_muted": isMuted,
|
||||
"operator_id": userID,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ====== 内部辅助方法 ======
|
||||
|
||||
// getActiveGroup 获取群聊并校验是否存在且未解散
|
||||
func (s *GroupService) getActiveGroup(ctx context.Context, groupID int64) (*model.Group, error) {
|
||||
group, err := s.groupDAO.GetByID(ctx, groupID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrGroupNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if group.Status == constants.GroupStatusDissolved {
|
||||
return nil, ErrGroupDissolved
|
||||
}
|
||||
return group, nil
|
||||
}
|
||||
|
||||
// checkGroupAdmin 校验用户是群主或管理员
|
||||
func (s *GroupService) checkGroupAdmin(ctx context.Context, groupID, userID int64) (*model.Group, *imMember, error) {
|
||||
group, err := s.getActiveGroup(ctx, groupID)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
member, err := s.groupDAO.GetMember(ctx, group.ConversationID, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil, ErrNotGroupMember
|
||||
}
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if member.Role < constants.GroupRoleAdmin {
|
||||
return nil, nil, ErrNotGroupAdmin
|
||||
}
|
||||
|
||||
return group, &imMember{
|
||||
UserID: member.UserID,
|
||||
Role: member.Role,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// imMember 内部使用的成员简要信息
|
||||
type imMember struct {
|
||||
UserID int64
|
||||
Role int
|
||||
}
|
||||
|
||||
// ====== 推送和系统消息辅助方法 ======
|
||||
|
||||
// pushToUser 向单个用户推送通知
|
||||
func (s *GroupService) pushToUser(ctx context.Context, userID int64, event string, data interface{}) {
|
||||
if s.pubsub == nil {
|
||||
return
|
||||
}
|
||||
push := ws.NewPushMessage(event, data)
|
||||
bytes, err := ws.MarshalPush(push)
|
||||
if err != nil {
|
||||
logs.Error(ctx, "service.group_service.pushToUser", "序列化推送消息失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
if err := s.pubsub.Publish(ctx, userID, bytes); err != nil {
|
||||
logs.Error(ctx, "service.group_service.pushToUser", "推送失败",
|
||||
zap.Int64("user_id", userID), zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// pushToMembers 向指定用户列表推送通知(排除 excludeUID)
|
||||
func (s *GroupService) pushToMembers(ctx context.Context, memberIDs []int64, excludeUID int64, event string, data interface{}) {
|
||||
if s.pubsub == nil || len(memberIDs) == 0 {
|
||||
return
|
||||
}
|
||||
push := ws.NewPushMessage(event, data)
|
||||
bytes, err := ws.MarshalPush(push)
|
||||
if err != nil {
|
||||
logs.Error(ctx, "service.group_service.pushToMembers", "序列化推送消息失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
for _, uid := range memberIDs {
|
||||
if uid == excludeUID {
|
||||
continue
|
||||
}
|
||||
if pErr := s.pubsub.Publish(ctx, uid, bytes); pErr != nil {
|
||||
logs.Error(ctx, "service.group_service.pushToMembers", "推送失败",
|
||||
zap.Int64("user_id", uid), zap.Error(pErr))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// pushToGroupMembers 向群所有成员推送通知(通过 conversationID 查成员)
|
||||
func (s *GroupService) pushToGroupMembers(ctx context.Context, conversationID int64, excludeUID int64, event string, data interface{}) {
|
||||
memberIDs, err := s.groupDAO.GetMemberIDs(ctx, conversationID)
|
||||
if err != nil {
|
||||
logs.Error(ctx, "service.group_service.pushToGroupMembers", "获取成员列表失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
s.pushToMembers(ctx, memberIDs, excludeUID, event, data)
|
||||
}
|
||||
|
||||
// writeSystemMessage 写入系统消息到群会话
|
||||
func (s *GroupService) writeSystemMessage(ctx context.Context, conversationID int64, content string) {
|
||||
if s.msgWriter == nil {
|
||||
return
|
||||
}
|
||||
msg := &imModel.Message{
|
||||
ConversationID: conversationID,
|
||||
SenderID: 0,
|
||||
Type: constants.MessageTypeSystem,
|
||||
Content: content,
|
||||
Status: constants.MessageStatusNormal,
|
||||
}
|
||||
if err := s.msgWriter.Create(ctx, msg); err != nil {
|
||||
logs.Error(ctx, "service.group_service.writeSystemMessage", "写入系统消息失败", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// getUserNickname 获取单个用户昵称(推送文案使用,查询失败返回默认值)
|
||||
func (s *GroupService) getUserNickname(ctx context.Context, userID int64) string {
|
||||
if s.userInfo == nil {
|
||||
return "用户"
|
||||
}
|
||||
users, err := s.userInfo.GetUsersByIDs(ctx, []int64{userID})
|
||||
if err != nil || len(users) == 0 {
|
||||
return "用户"
|
||||
}
|
||||
return users[0].Nickname
|
||||
}
|
||||
|
||||
// getUserNicknames 批量获取用户昵称列表
|
||||
func (s *GroupService) getUserNicknames(ctx context.Context, userIDs []int64) []string {
|
||||
if s.userInfo == nil || len(userIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
users, err := s.userInfo.GetUsersByIDs(ctx, userIDs)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
nameMap := make(map[int64]string, len(users))
|
||||
for _, u := range users {
|
||||
nameMap[u.ID] = u.Nickname
|
||||
}
|
||||
names := make([]string, 0, len(userIDs))
|
||||
for _, id := range userIDs {
|
||||
if n, ok := nameMap[id]; ok {
|
||||
names = append(names, n)
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// joinNames 将名称列表用顿号连接(中文习惯)
|
||||
func joinNames(names []string) string {
|
||||
if len(names) == 0 {
|
||||
return ""
|
||||
}
|
||||
result := names[0]
|
||||
for i := 1; i < len(names); i++ {
|
||||
result += "、" + names[i]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// toGroupDTO 将 model.Group 转换为 dto.GroupDTO
|
||||
func (s *GroupService) toGroupDTO(g *model.Group, memberCount int) *dto.GroupDTO {
|
||||
return &dto.GroupDTO{
|
||||
ID: g.ID,
|
||||
ConversationID: g.ConversationID,
|
||||
Name: g.Name,
|
||||
Avatar: g.Avatar,
|
||||
OwnerID: g.OwnerID,
|
||||
Notice: g.Notice,
|
||||
MaxMembers: g.MaxMembers,
|
||||
MemberCount: memberCount,
|
||||
IsSearchable: g.IsSearchable,
|
||||
IsAllMuted: g.IsAllMuted,
|
||||
Status: g.Status,
|
||||
CreatedAt: g.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
}
|
||||
@@ -180,6 +180,30 @@ func (ctl *IMController) GetTotalUnread(c *gin.Context) {
|
||||
utils.ResponseOK(c, gin.H{"total_unread": count})
|
||||
}
|
||||
|
||||
// GetMessageReadDetail 获取消息已读详情
|
||||
// GET /api/v1/im/messages/:id/reads
|
||||
func (ctl *IMController) GetMessageReadDetail(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, ok := middleware.GetCurrentUserID(c)
|
||||
if !ok {
|
||||
utils.ResponseUnauthorized(c, "无法获取当前用户信息")
|
||||
return
|
||||
}
|
||||
|
||||
messageID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.ResponseBadRequest(c, "消息 ID 格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
result, err := ctl.imService.GetMessageReadDetail(ctx, userID, messageID)
|
||||
if err != nil {
|
||||
ctl.handleError(c, err, "获取已读详情失败")
|
||||
return
|
||||
}
|
||||
utils.ResponseOK(c, result)
|
||||
}
|
||||
|
||||
// handleError 统一业务错误映射
|
||||
// 已知业务错误 → 返回 Service 层定义的具体提示
|
||||
// 未知错误 → 返回 fallbackMsg(未传则默认"服务器内部错误")
|
||||
@@ -203,6 +227,12 @@ func (ctl *IMController) handleError(c *gin.Context, err error, fallbackMsg ...s
|
||||
utils.ResponseForbidden(c, err.Error())
|
||||
case service.ErrDuplicateMsg:
|
||||
utils.ResponseBadRequest(c, err.Error())
|
||||
case service.ErrGroupDissolved:
|
||||
utils.ResponseBadRequest(c, err.Error())
|
||||
case service.ErrGroupAllMuted:
|
||||
utils.ResponseForbidden(c, err.Error())
|
||||
case service.ErrUserMuted:
|
||||
utils.ResponseForbidden(c, err.Error())
|
||||
default:
|
||||
msg := "服务器内部错误"
|
||||
if len(fallbackMsg) > 0 && fallbackMsg[0] != "" {
|
||||
|
||||
@@ -86,13 +86,14 @@ func (d *ConversationDAO) GetUserConversations(ctx context.Context, userID int64
|
||||
err := d.db.WithContext(ctx).
|
||||
Raw(`SELECT c.id, c.type, c.last_message_id, c.last_msg_content, c.last_msg_time, c.last_msg_sender_id,
|
||||
cm.is_pinned, cm.unread_count, cm.clear_before_msg_id,
|
||||
peer.user_id AS peer_user_id
|
||||
cm.is_do_not_disturb, cm.at_me_count,
|
||||
COALESCE(peer.user_id, 0) AS peer_user_id
|
||||
FROM im_conversations c
|
||||
JOIN im_conversation_members cm ON cm.conversation_id = c.id AND cm.user_id = ?
|
||||
LEFT JOIN im_conversation_members peer ON peer.conversation_id = c.id AND peer.user_id != ?
|
||||
LEFT JOIN im_conversation_members peer ON peer.conversation_id = c.id AND peer.user_id != ? AND c.type = ?
|
||||
WHERE cm.is_deleted = false
|
||||
ORDER BY cm.is_pinned DESC, c.last_msg_time DESC NULLS LAST`,
|
||||
userID, userID).
|
||||
userID, userID, constants.ConversationTypePrivate).
|
||||
Scan(&results).Error
|
||||
|
||||
if err != nil {
|
||||
@@ -112,6 +113,8 @@ type ConversationWithMember struct {
|
||||
IsPinned bool `json:"is_pinned"`
|
||||
UnreadCount int `json:"unread_count"`
|
||||
ClearBeforeMsgID int64 `json:"clear_before_msg_id"`
|
||||
IsDoNotDisturb bool `json:"is_do_not_disturb"`
|
||||
AtMeCount int `json:"at_me_count"`
|
||||
PeerUserID int64 `json:"peer_user_id"`
|
||||
}
|
||||
|
||||
@@ -216,6 +219,44 @@ func (d *ConversationDAO) IncrementUnread(ctx context.Context, conversationID, u
|
||||
UpdateColumn("unread_count", gorm.Expr("unread_count + 1")).Error
|
||||
}
|
||||
|
||||
// IncrementAtMeCount 将指定成员的 @提醒计数 +1
|
||||
func (d *ConversationDAO) IncrementAtMeCount(ctx context.Context, conversationID, userID int64) error {
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&model.ConversationMember{}).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, userID).
|
||||
UpdateColumn("at_me_count", gorm.Expr("at_me_count + 1")).Error
|
||||
}
|
||||
|
||||
// ClearAtMeCount 清零指定成员的 @提醒计数
|
||||
func (d *ConversationDAO) ClearAtMeCount(ctx context.Context, conversationID, userID int64) error {
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&model.ConversationMember{}).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, userID).
|
||||
Update("at_me_count", 0).Error
|
||||
}
|
||||
|
||||
// GetMemberDNDMap 批量获取会话成员的免打扰状态(返回 userID → isDoNotDisturb 的映射)
|
||||
func (d *ConversationDAO) GetMemberDNDMap(ctx context.Context, conversationID int64) (map[int64]bool, error) {
|
||||
type memberDND struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
IsDoNotDisturb bool `json:"is_do_not_disturb"`
|
||||
}
|
||||
var members []memberDND
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&model.ConversationMember{}).
|
||||
Select("user_id, is_do_not_disturb").
|
||||
Where("conversation_id = ?", conversationID).
|
||||
Scan(&members).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make(map[int64]bool, len(members))
|
||||
for _, m := range members {
|
||||
result[m.UserID] = m.IsDoNotDisturb
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ClearUnread 清零指定成员的未读消息数
|
||||
func (d *ConversationDAO) ClearUnread(ctx context.Context, conversationID, userID int64, lastMsgID int64) error {
|
||||
return d.db.WithContext(ctx).
|
||||
|
||||
@@ -34,6 +34,7 @@ func (h *EventHandler) registerEvents() {
|
||||
h.hub.RegisterEvent("im.message.send", h.handleSendMessage)
|
||||
h.hub.RegisterEvent("im.message.recall", h.handleRecallMessage)
|
||||
h.hub.RegisterEvent("im.conversation.read", h.handleMarkRead)
|
||||
h.hub.RegisterEvent("im.group.read", h.handleGroupRead)
|
||||
h.hub.RegisterEvent("im.typing", h.handleTyping)
|
||||
}
|
||||
|
||||
@@ -111,6 +112,29 @@ func (h *EventHandler) handleMarkRead(client *ws.Client, msg *ws.Message) {
|
||||
h.sendACK(client, msg, 0, "ok", nil)
|
||||
}
|
||||
|
||||
// handleGroupRead 处理群聊消息标记已读事件
|
||||
func (h *EventHandler) handleGroupRead(client *ws.Client, msg *ws.Message) {
|
||||
funcName := "handler.event_handler.handleGroupRead"
|
||||
ctx := context.Background()
|
||||
|
||||
var req dto.MarkGroupReadRequest
|
||||
if err := json.Unmarshal(msg.Data, &req); err != nil {
|
||||
logs.Warn(ctx, funcName, "解析群已读请求失败",
|
||||
zap.Int64("user_id", client.UserID), zap.Error(err))
|
||||
h.sendACK(client, msg, -1, "请求参数格式错误", nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.imService.MarkGroupMessagesRead(ctx, client.UserID, &req); err != nil {
|
||||
logs.Warn(ctx, funcName, "群已读标记失败",
|
||||
zap.Int64("user_id", client.UserID), zap.Error(err))
|
||||
h.sendACK(client, msg, -1, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
h.sendACK(client, msg, 0, "ok", nil)
|
||||
}
|
||||
|
||||
// handleTyping 处理正在输入事件(通过 PubSub 转发给对方,支持跨实例)
|
||||
func (h *EventHandler) handleTyping(client *ws.Client, msg *ws.Message) {
|
||||
funcName := "handler.event_handler.handleTyping"
|
||||
|
||||
@@ -4,17 +4,24 @@ import "time"
|
||||
|
||||
// ConversationMember 会话成员模型,对应 im_conversation_members 表
|
||||
// 每个会话的每个成员一条记录,存储置顶/未读/软删除等个人视图
|
||||
// 群聊场景下额外使用 Role/Nickname/IsMuted/IsDoNotDisturb/JoinedAt/AtMeCount 字段
|
||||
type ConversationMember struct {
|
||||
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"` // 记录唯一标识
|
||||
ConversationID int64 `json:"conversation_id" gorm:"not null;uniqueIndex:idx_conv_user"` // 所属会话 ID
|
||||
UserID int64 `json:"user_id" gorm:"not null;uniqueIndex:idx_conv_user"` // 成员用户 ID
|
||||
IsPinned bool `json:"is_pinned" gorm:"default:false"` // 是否置顶该会话
|
||||
IsDeleted bool `json:"is_deleted" gorm:"default:false"` // 是否删除该会话(软删除,不影响对方)
|
||||
UnreadCount int `json:"unread_count" gorm:"default:0"` // 该成员在此会话中的未读消息数
|
||||
LastReadMsgID int64 `json:"last_read_msg_id" gorm:"default:0"` // 该成员最后已读消息 ID
|
||||
ClearBeforeMsgID int64 `json:"clear_before_msg_id" gorm:"default:0"` // 清空记录时的消息截止 ID(个人视图,不影响对方)
|
||||
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime;type:timestamp(0)"` // 加入会话时间
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"not null;autoUpdateTime;type:timestamp(0)"` // 最后更新时间
|
||||
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"` // 记录唯一标识
|
||||
ConversationID int64 `json:"conversation_id" gorm:"not null;uniqueIndex:idx_conv_user"` // 所属会话 ID
|
||||
UserID int64 `json:"user_id" gorm:"not null;uniqueIndex:idx_conv_user"` // 成员用户 ID
|
||||
IsPinned bool `json:"is_pinned" gorm:"default:false"` // 是否置顶该会话
|
||||
IsDeleted bool `json:"is_deleted" gorm:"default:false"` // 是否删除该会话(软删除,不影响对方)
|
||||
UnreadCount int `json:"unread_count" gorm:"default:0"` // 该成员在此会话中的未读消息数
|
||||
LastReadMsgID int64 `json:"last_read_msg_id" gorm:"default:0"` // 该成员最后已读消息 ID
|
||||
ClearBeforeMsgID int64 `json:"clear_before_msg_id" gorm:"default:0"` // 清空记录时的消息截止 ID(个人视图,不影响对方)
|
||||
Role int `json:"role" gorm:"not null;default:0"` // 成员角色:0=普通成员,1=管理员,2=群主
|
||||
Nickname string `json:"nickname" gorm:"size:50;default:''"` // 群内昵称(仅群聊有效)
|
||||
IsMuted bool `json:"is_muted" gorm:"not null;default:false"` // 是否被禁言
|
||||
IsDoNotDisturb bool `json:"is_do_not_disturb" gorm:"not null;default:false"` // 是否消息免打扰
|
||||
JoinedAt *time.Time `json:"joined_at" gorm:"type:timestamp(0)"` // 加入群聊时间
|
||||
AtMeCount int `json:"at_me_count" gorm:"default:0"` // 被@提醒未读计数
|
||||
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime;type:timestamp(0)"` // 加入会话时间
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"not null;autoUpdateTime;type:timestamp(0)"` // 最后更新时间
|
||||
}
|
||||
|
||||
// TableName 指定数据库表名
|
||||
|
||||
@@ -1,20 +1,25 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/echochat/backend/pkg/utils"
|
||||
)
|
||||
|
||||
// Message 消息模型,对应 im_messages 表
|
||||
// Type: 1=文本(预留 2=图片 3=语音 4=视频 5=文件)
|
||||
// Type: 1=文本(预留 2=图片 3=语音 4=视频 5=文件),10=系统消息
|
||||
// Status: 1=正常 2=已撤回 3=已删除
|
||||
type Message struct {
|
||||
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"` // 消息唯一标识,自增主键
|
||||
ConversationID int64 `json:"conversation_id" gorm:"not null;index:idx_msg_conv_time;index:idx_msg_conv_id"` // 所属会话 ID
|
||||
SenderID int64 `json:"sender_id" gorm:"not null"` // 发送者用户 ID
|
||||
Type int `json:"type" gorm:"not null;default:1"` // 消息类型:1=文本(预留扩展)
|
||||
Content string `json:"content" gorm:"type:text;not null"` // 消息内容
|
||||
Extra *string `json:"extra" gorm:"type:jsonb"` // 扩展数据(JSON 格式,预留图片/语音等元信息)
|
||||
Status int `json:"status" gorm:"not null;default:1"` // 消息状态:1=正常,2=已撤回,3=已删除
|
||||
ClientMsgID string `json:"client_msg_id" gorm:"size:64;default:''"` // 客户端消息唯一 ID,用于幂等去重
|
||||
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime;type:timestamp(0)"` // 消息发送时间
|
||||
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"` // 消息唯一标识,自增主键
|
||||
ConversationID int64 `json:"conversation_id" gorm:"not null;index:idx_msg_conv_time;index:idx_msg_conv_id"` // 所属会话 ID
|
||||
SenderID int64 `json:"sender_id" gorm:"not null"` // 发送者用户 ID(系统消息为 0)
|
||||
Type int `json:"type" gorm:"not null;default:1"` // 消息类型:1=文本,10=系统消息
|
||||
Content string `json:"content" gorm:"type:text;not null"` // 消息内容
|
||||
Extra *string `json:"extra" gorm:"type:jsonb"` // 扩展数据(JSON 格式,预留图片/语音等元信息)
|
||||
Status int `json:"status" gorm:"not null;default:1"` // 消息状态:1=正常,2=已撤回,3=已删除
|
||||
ClientMsgID string `json:"client_msg_id" gorm:"size:64;default:''"` // 客户端消息唯一 ID,用于幂等去重
|
||||
AtUserIDs utils.Int64Array `json:"at_user_ids" gorm:"type:bigint[]"` // @提醒用户 ID 列表,nil=无@,含 0 表示 @所有人
|
||||
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime;type:timestamp(0)"` // 消息发送时间
|
||||
}
|
||||
|
||||
// TableName 指定数据库表名
|
||||
|
||||
@@ -22,7 +22,8 @@ func ProvideMessageDAO(db *gorm.DB) *dao.MessageDAO {
|
||||
}
|
||||
|
||||
// ProvideIMService 创建 IMService 实例
|
||||
// friendChecker 和 userInfoGetter 由 contact 模块的 FriendshipDAO 隐式实现
|
||||
// friendChecker / userInfoGetter 由 contact.FriendshipDAO 隐式实现
|
||||
// groupInfo 由 group.GroupDAO 隐式实现
|
||||
func ProvideIMService(
|
||||
convDAO *dao.ConversationDAO,
|
||||
msgDAO *dao.MessageDAO,
|
||||
@@ -30,8 +31,10 @@ func ProvideIMService(
|
||||
rdb *redis.Client,
|
||||
friendChecker service.FriendChecker,
|
||||
userInfoGetter service.UserInfoGetter,
|
||||
groupInfo service.GroupInfoGetter,
|
||||
readRecorder service.MessageReadRecorder,
|
||||
) *service.IMService {
|
||||
return service.NewIMService(convDAO, msgDAO, pubsub, rdb, friendChecker, userInfoGetter)
|
||||
return service.NewIMService(convDAO, msgDAO, pubsub, rdb, friendChecker, userInfoGetter, groupInfo, readRecorder)
|
||||
}
|
||||
|
||||
// ProvideIMEventHandler 创建 IM WS 事件处理器并注册事件到 Hub
|
||||
|
||||
@@ -23,5 +23,8 @@ func RegisterRoutes(r *gin.Engine, ctrl *controller.IMController, jwtAuth gin.Ha
|
||||
|
||||
// 未读数
|
||||
authed.GET("/unread", ctrl.GetTotalUnread)
|
||||
|
||||
// 已读回执
|
||||
authed.GET("/messages/:id/reads", ctrl.GetMessageReadDetail)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,25 +23,30 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFriend = errors.New("对方不是你的好友")
|
||||
ErrEmptyContent = errors.New("消息内容不能为空")
|
||||
ErrConvNotFound = errors.New("会话不存在")
|
||||
ErrMsgNotFound = errors.New("消息不存在")
|
||||
ErrNotSender = errors.New("只能撤回自己发送的消息")
|
||||
ErrRecallTimeout = errors.New("超过撤回时限")
|
||||
ErrNotMember = errors.New("你不是该会话的成员")
|
||||
ErrDuplicateMsg = errors.New("重复消息")
|
||||
ErrInvalidMsgType = errors.New("不支持的消息类型")
|
||||
ErrNotFriend = errors.New("对方不是你的好友")
|
||||
ErrEmptyContent = errors.New("消息内容不能为空")
|
||||
ErrConvNotFound = errors.New("会话不存在")
|
||||
ErrMsgNotFound = errors.New("消息不存在")
|
||||
ErrNotSender = errors.New("只能撤回自己发送的消息")
|
||||
ErrRecallTimeout = errors.New("超过撤回时限")
|
||||
ErrNotMember = errors.New("你不是该会话的成员")
|
||||
ErrDuplicateMsg = errors.New("重复消息")
|
||||
ErrInvalidMsgType = errors.New("不支持的消息类型")
|
||||
ErrGroupDissolved = errors.New("群聊已解散")
|
||||
ErrGroupAllMuted = errors.New("当前群已开启全体禁言")
|
||||
ErrUserMuted = errors.New("你已被禁言,无法发送消息")
|
||||
)
|
||||
|
||||
// IMService 即时通讯核心业务服务
|
||||
type IMService struct {
|
||||
convDAO *dao.ConversationDAO
|
||||
msgDAO *dao.MessageDAO
|
||||
pubsub *ws.PubSub
|
||||
rdb *redis.Client
|
||||
friendChecker FriendChecker
|
||||
convDAO *dao.ConversationDAO
|
||||
msgDAO *dao.MessageDAO
|
||||
pubsub *ws.PubSub
|
||||
rdb *redis.Client
|
||||
friendChecker FriendChecker
|
||||
userInfoGetter UserInfoGetter
|
||||
groupInfo GroupInfoGetter
|
||||
readRecorder MessageReadRecorder
|
||||
}
|
||||
|
||||
// NewIMService 创建 IMService 实例
|
||||
@@ -52,6 +57,8 @@ func NewIMService(
|
||||
rdb *redis.Client,
|
||||
friendChecker FriendChecker,
|
||||
userInfoGetter UserInfoGetter,
|
||||
groupInfo GroupInfoGetter,
|
||||
readRecorder MessageReadRecorder,
|
||||
) *IMService {
|
||||
return &IMService{
|
||||
convDAO: convDAO,
|
||||
@@ -60,15 +67,14 @@ func NewIMService(
|
||||
rdb: rdb,
|
||||
friendChecker: friendChecker,
|
||||
userInfoGetter: userInfoGetter,
|
||||
groupInfo: groupInfo,
|
||||
readRecorder: readRecorder,
|
||||
}
|
||||
}
|
||||
|
||||
// SendMessage 发送消息(核心流程)
|
||||
// 1. 校验好友关系
|
||||
// 2. 查找或创建会话
|
||||
// 3. 幂等去重(client_msg_id)
|
||||
// 4. 写入消息 + 更新会话最后消息 + 递增对方未读数
|
||||
// 5. 通过 PubSub 推送给接收方
|
||||
// SendMessage 发送消息(核心流程,同时支持单聊和群聊)
|
||||
// 单聊:校验好友关系 → 查找/创建会话 → 写入消息 → 推送给对方
|
||||
// 群聊:校验群成员+禁言 → 写入消息(含 @信息)→ 推送给所有群成员
|
||||
func (s *IMService) SendMessage(ctx context.Context, senderID int64, req *dto.SendMessageRequest) (*dto.MessageDTO, error) {
|
||||
funcName := "service.im_service.SendMessage"
|
||||
logs.Info(ctx, funcName, "发送消息",
|
||||
@@ -87,40 +93,64 @@ func (s *IMService) SendMessage(ctx context.Context, senderID int64, req *dto.Se
|
||||
}
|
||||
|
||||
convID := req.ConversationID
|
||||
var peerID int64
|
||||
|
||||
if convID == 0 && req.TargetUserID > 0 {
|
||||
isFriend, err := s.friendChecker.IsFriend(ctx, senderID, req.TargetUserID)
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "检查好友关系失败", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
if !isFriend {
|
||||
return nil, ErrNotFriend
|
||||
}
|
||||
|
||||
conv, err := s.getOrCreatePrivateConversation(ctx, senderID, req.TargetUserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
convID = conv.ID
|
||||
peerID = req.TargetUserID
|
||||
return s.sendPrivateMessage(ctx, senderID, req)
|
||||
} else if convID > 0 {
|
||||
member, err := s.convDAO.GetMember(ctx, convID, senderID)
|
||||
conv, err := s.convDAO.GetByID(ctx, convID)
|
||||
if err != nil {
|
||||
return nil, ErrNotMember
|
||||
return nil, ErrConvNotFound
|
||||
}
|
||||
if member == nil {
|
||||
return nil, ErrNotMember
|
||||
if conv.Type == constants.ConversationTypeGroup {
|
||||
return s.sendGroupMessage(ctx, senderID, req)
|
||||
}
|
||||
peerID, err = s.convDAO.GetPeerUserID(ctx, convID, senderID)
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "查询对方用户 ID 失败", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
return nil, ErrConvNotFound
|
||||
return s.sendPrivateMessageByConvID(ctx, senderID, convID, req)
|
||||
}
|
||||
return nil, ErrConvNotFound
|
||||
}
|
||||
|
||||
// sendPrivateMessage 发送单聊消息(首次发送,通过 TargetUserID)
|
||||
func (s *IMService) sendPrivateMessage(ctx context.Context, senderID int64, req *dto.SendMessageRequest) (*dto.MessageDTO, error) {
|
||||
funcName := "service.im_service.sendPrivateMessage"
|
||||
|
||||
isFriend, err := s.friendChecker.IsFriend(ctx, senderID, req.TargetUserID)
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "检查好友关系失败", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
if !isFriend {
|
||||
return nil, ErrNotFriend
|
||||
}
|
||||
|
||||
conv, err := s.getOrCreatePrivateConversation(ctx, senderID, req.TargetUserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.writeAndPushPrivateMessage(ctx, senderID, conv.ID, req.TargetUserID, req)
|
||||
}
|
||||
|
||||
// sendPrivateMessageByConvID 发送单聊消息(已有会话 ID)
|
||||
func (s *IMService) sendPrivateMessageByConvID(ctx context.Context, senderID, convID int64, req *dto.SendMessageRequest) (*dto.MessageDTO, error) {
|
||||
funcName := "service.im_service.sendPrivateMessageByConvID"
|
||||
|
||||
member, err := s.convDAO.GetMember(ctx, convID, senderID)
|
||||
if err != nil || member == nil {
|
||||
return nil, ErrNotMember
|
||||
}
|
||||
|
||||
peerID, err := s.convDAO.GetPeerUserID(ctx, convID, senderID)
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "查询对方用户 ID 失败", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.writeAndPushPrivateMessage(ctx, senderID, convID, peerID, req)
|
||||
}
|
||||
|
||||
// writeAndPushPrivateMessage 单聊:幂等去重 + 写消息 + 推送
|
||||
func (s *IMService) writeAndPushPrivateMessage(ctx context.Context, senderID, convID, peerID int64, req *dto.SendMessageRequest) (*dto.MessageDTO, error) {
|
||||
funcName := "service.im_service.writeAndPushPrivateMessage"
|
||||
|
||||
if req.ClientMsgID != "" {
|
||||
existing, err := s.msgDAO.FindByClientMsgID(ctx, convID, req.ClientMsgID)
|
||||
@@ -160,40 +190,161 @@ func (s *IMService) SendMessage(ctx context.Context, senderID int64, req *dto.Se
|
||||
|
||||
s.incrementTotalUnread(ctx, peerID)
|
||||
|
||||
pushData := map[string]interface{}{
|
||||
"id": msg.ID,
|
||||
"conversation_id": convID,
|
||||
"sender_id": senderID,
|
||||
"type": msg.Type,
|
||||
"content": msg.Content,
|
||||
"client_msg_id": msg.ClientMsgID,
|
||||
"created_at": msg.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
if senderUsers, sErr := s.userInfoGetter.GetUsersByIDs(ctx, []int64{senderID}); sErr == nil && len(senderUsers) > 0 {
|
||||
pushData["sender_name"] = senderUsers[0].Nickname
|
||||
pushData["sender_avatar"] = senderUsers[0].Avatar
|
||||
}
|
||||
pushData := s.buildMessagePushData(ctx, msg, senderID)
|
||||
pushData["conv_type"] = constants.ConversationTypePrivate
|
||||
s.pushToUser(ctx, peerID, "im.message.new", pushData)
|
||||
|
||||
return s.toMessageDTO(msg), nil
|
||||
}
|
||||
|
||||
// RecallMessage 撤回消息(2分钟内)
|
||||
// 撤回成功后,若被撤回消息是会话最后一条,则同步更新会话预览文本
|
||||
func (s *IMService) RecallMessage(ctx context.Context, senderID int64, messageID int64) error {
|
||||
// sendGroupMessage 发送群聊消息
|
||||
func (s *IMService) sendGroupMessage(ctx context.Context, senderID int64, req *dto.SendMessageRequest) (*dto.MessageDTO, error) {
|
||||
funcName := "service.im_service.sendGroupMessage"
|
||||
convID := req.ConversationID
|
||||
|
||||
member, err := s.convDAO.GetMember(ctx, convID, senderID)
|
||||
if err != nil || member == nil {
|
||||
return nil, ErrNotMember
|
||||
}
|
||||
|
||||
if s.groupInfo != nil {
|
||||
groupBrief, gErr := s.groupInfo.GetGroupBrief(ctx, convID)
|
||||
if gErr != nil {
|
||||
logs.Error(ctx, funcName, "获取群信息失败", zap.Error(gErr))
|
||||
return nil, ErrConvNotFound
|
||||
}
|
||||
if groupBrief.Status == constants.GroupStatusDissolved {
|
||||
return nil, ErrGroupDissolved
|
||||
}
|
||||
if groupBrief.IsAllMuted && member.Role < constants.GroupRoleAdmin {
|
||||
return nil, ErrGroupAllMuted
|
||||
}
|
||||
}
|
||||
|
||||
if member.IsMuted {
|
||||
return nil, ErrUserMuted
|
||||
}
|
||||
|
||||
if req.ClientMsgID != "" {
|
||||
existing, err := s.msgDAO.FindByClientMsgID(ctx, convID, req.ClientMsgID)
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "幂等去重查询失败", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
if existing != nil {
|
||||
return s.toMessageDTO(existing), ErrDuplicateMsg
|
||||
}
|
||||
}
|
||||
|
||||
msg := &model.Message{
|
||||
ConversationID: convID,
|
||||
SenderID: senderID,
|
||||
Type: req.Type,
|
||||
Content: req.Content,
|
||||
Status: constants.MessageStatusNormal,
|
||||
ClientMsgID: req.ClientMsgID,
|
||||
}
|
||||
if len(req.AtUserIDs) > 0 {
|
||||
msg.AtUserIDs = req.AtUserIDs
|
||||
}
|
||||
if err := s.msgDAO.Create(ctx, msg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
if err := s.convDAO.UpdateLastMessage(ctx, convID, msg.ID, truncateContent(req.Content, 100), senderID, now); err != nil {
|
||||
logs.Error(ctx, funcName, "更新最后消息失败", zap.Error(err))
|
||||
}
|
||||
|
||||
memberIDs, err := s.convDAO.GetConversationMemberIDs(ctx, convID)
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "获取群成员列表失败", zap.Error(err))
|
||||
return s.toMessageDTO(msg), nil
|
||||
}
|
||||
|
||||
dndMap, dndErr := s.convDAO.GetMemberDNDMap(ctx, convID)
|
||||
if dndErr != nil {
|
||||
logs.Error(ctx, funcName, "获取免打扰状态失败", zap.Error(dndErr))
|
||||
dndMap = make(map[int64]bool)
|
||||
}
|
||||
|
||||
pushData := s.buildMessagePushData(ctx, msg, senderID)
|
||||
pushData["conv_type"] = constants.ConversationTypeGroup
|
||||
if len(req.AtUserIDs) > 0 {
|
||||
pushData["at_user_ids"] = req.AtUserIDs
|
||||
}
|
||||
|
||||
for _, uid := range memberIDs {
|
||||
if uid == senderID {
|
||||
continue
|
||||
}
|
||||
if err := s.convDAO.IncrementUnread(ctx, convID, uid); err != nil {
|
||||
logs.Error(ctx, funcName, "递增未读计数失败", zap.Int64("user_id", uid), zap.Error(err))
|
||||
}
|
||||
|
||||
if !dndMap[uid] {
|
||||
s.incrementTotalUnread(ctx, uid)
|
||||
}
|
||||
|
||||
if len(req.AtUserIDs) > 0 {
|
||||
isAtMe := false
|
||||
for _, atID := range req.AtUserIDs {
|
||||
if atID == uid || atID == 0 {
|
||||
isAtMe = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if isAtMe {
|
||||
if err := s.convDAO.IncrementAtMeCount(ctx, convID, uid); err != nil {
|
||||
logs.Error(ctx, funcName, "递增@计数失败", zap.Int64("user_id", uid), zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s.pushToUser(ctx, uid, "im.message.new", pushData)
|
||||
}
|
||||
|
||||
return s.toMessageDTO(msg), nil
|
||||
}
|
||||
|
||||
// RecallMessage 撤回消息
|
||||
// 单聊:只能撤回自己的消息,2 分钟内
|
||||
// 群聊:自己的消息 2 分钟内撤回;群主/管理员可无时限撤回任何消息
|
||||
func (s *IMService) RecallMessage(ctx context.Context, operatorID int64, messageID int64) error {
|
||||
funcName := "service.im_service.RecallMessage"
|
||||
logs.Info(ctx, funcName, "撤回消息",
|
||||
zap.Int64("sender_id", senderID), zap.Int64("message_id", messageID))
|
||||
zap.Int64("operator_id", operatorID), zap.Int64("message_id", messageID))
|
||||
|
||||
msg, err := s.msgDAO.GetByID(ctx, messageID)
|
||||
if err != nil {
|
||||
return ErrMsgNotFound
|
||||
}
|
||||
if msg.SenderID != senderID {
|
||||
return ErrNotSender
|
||||
|
||||
conv, err := s.convDAO.GetByID(ctx, msg.ConversationID)
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "获取会话信息失败", zap.Error(err))
|
||||
return ErrConvNotFound
|
||||
}
|
||||
if time.Since(msg.CreatedAt).Seconds() > float64(constants.MessageRecallTimeLimit) {
|
||||
return ErrRecallTimeout
|
||||
|
||||
isAdmin := false
|
||||
if conv.Type == constants.ConversationTypeGroup {
|
||||
member, mErr := s.convDAO.GetMember(ctx, msg.ConversationID, operatorID)
|
||||
if mErr != nil || member == nil {
|
||||
return ErrNotMember
|
||||
}
|
||||
isAdmin = member.Role >= constants.GroupRoleAdmin
|
||||
}
|
||||
|
||||
if msg.SenderID == operatorID {
|
||||
if time.Since(msg.CreatedAt).Seconds() > float64(constants.MessageRecallTimeLimit) {
|
||||
if !isAdmin {
|
||||
return ErrRecallTimeout
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if !isAdmin {
|
||||
return ErrNotSender
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.msgDAO.UpdateStatus(ctx, messageID, constants.MessageStatusRecalled); err != nil {
|
||||
@@ -201,16 +352,31 @@ func (s *IMService) RecallMessage(ctx context.Context, senderID int64, messageID
|
||||
return err
|
||||
}
|
||||
|
||||
conv, err := s.convDAO.GetByID(ctx, msg.ConversationID)
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "获取会话信息失败", zap.Error(err))
|
||||
} else if conv.LastMessageID != nil && *conv.LastMessageID == msg.ID {
|
||||
senderInfo, infoErr := s.userInfoGetter.GetUsersByIDs(ctx, []int64{senderID})
|
||||
recallText := "撤回了一条消息"
|
||||
recallText := "撤回了一条消息"
|
||||
if msg.SenderID != operatorID && isAdmin {
|
||||
operatorInfo, infoErr := s.userInfoGetter.GetUsersByIDs(ctx, []int64{operatorID, msg.SenderID})
|
||||
operatorName := "管理员"
|
||||
senderName := "成员"
|
||||
if infoErr == nil {
|
||||
for _, u := range operatorInfo {
|
||||
if u.ID == operatorID {
|
||||
operatorName = u.Nickname
|
||||
}
|
||||
if u.ID == msg.SenderID {
|
||||
senderName = u.Nickname
|
||||
}
|
||||
}
|
||||
}
|
||||
recallText = fmt.Sprintf("管理员 %s 撤回了 %s 的一条消息", operatorName, senderName)
|
||||
} else {
|
||||
senderInfo, infoErr := s.userInfoGetter.GetUsersByIDs(ctx, []int64{operatorID})
|
||||
if infoErr == nil && len(senderInfo) > 0 {
|
||||
recallText = senderInfo[0].Nickname + " 撤回了一条消息"
|
||||
}
|
||||
if updateErr := s.convDAO.UpdateLastMessage(ctx, msg.ConversationID, msg.ID, recallText, senderID, msg.CreatedAt); updateErr != nil {
|
||||
}
|
||||
|
||||
if conv.LastMessageID != nil && *conv.LastMessageID == msg.ID {
|
||||
if updateErr := s.convDAO.UpdateLastMessage(ctx, msg.ConversationID, msg.ID, recallText, operatorID, msg.CreatedAt); updateErr != nil {
|
||||
logs.Error(ctx, funcName, "更新会话预览失败", zap.Error(updateErr))
|
||||
}
|
||||
}
|
||||
@@ -221,20 +387,22 @@ func (s *IMService) RecallMessage(ctx context.Context, senderID int64, messageID
|
||||
return nil
|
||||
}
|
||||
for _, uid := range memberIDs {
|
||||
if uid == senderID {
|
||||
if uid == operatorID {
|
||||
continue
|
||||
}
|
||||
s.pushToUser(ctx, uid, "im.message.recalled", map[string]interface{}{
|
||||
"message_id": messageID,
|
||||
"conversation_id": msg.ConversationID,
|
||||
"sender_id": senderID,
|
||||
"operator_id": operatorID,
|
||||
"sender_id": msg.SenderID,
|
||||
"recall_text": recallText,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetConversationList 获取会话列表(含对方用户信息)
|
||||
// GetConversationList 获取会话列表(含对方用户信息 / 群聊信息)
|
||||
func (s *IMService) GetConversationList(ctx context.Context, userID int64) (*dto.ConversationListResponse, error) {
|
||||
funcName := "service.im_service.GetConversationList"
|
||||
logs.Debug(ctx, funcName, "获取会话列表", zap.Int64("user_id", userID))
|
||||
@@ -245,17 +413,20 @@ func (s *IMService) GetConversationList(ctx context.Context, userID int64) (*dto
|
||||
}
|
||||
|
||||
peerIDs := make([]int64, 0, len(convs))
|
||||
groupConvIDs := make([]int64, 0)
|
||||
for _, c := range convs {
|
||||
if c.PeerUserID > 0 {
|
||||
if c.Type == constants.ConversationTypePrivate && c.PeerUserID > 0 {
|
||||
peerIDs = append(peerIDs, c.PeerUserID)
|
||||
} else if c.Type == constants.ConversationTypeGroup {
|
||||
groupConvIDs = append(groupConvIDs, c.ID)
|
||||
}
|
||||
}
|
||||
|
||||
userMap := make(map[int64]*userBrief)
|
||||
if len(peerIDs) > 0 {
|
||||
users, err := s.userInfoGetter.GetUsersByIDs(ctx, peerIDs)
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "批量查询用户信息失败", zap.Error(err))
|
||||
users, uErr := s.userInfoGetter.GetUsersByIDs(ctx, peerIDs)
|
||||
if uErr != nil {
|
||||
logs.Error(ctx, funcName, "批量查询用户信息失败", zap.Error(uErr))
|
||||
} else {
|
||||
for i := range users {
|
||||
u := users[i]
|
||||
@@ -264,17 +435,31 @@ func (s *IMService) GetConversationList(ctx context.Context, userID int64) (*dto
|
||||
}
|
||||
}
|
||||
|
||||
groupMap := make(map[int64]*dto.GroupBrief)
|
||||
if s.groupInfo != nil && len(groupConvIDs) > 0 {
|
||||
for _, convID := range groupConvIDs {
|
||||
brief, gErr := s.groupInfo.GetGroupBrief(ctx, convID)
|
||||
if gErr != nil {
|
||||
logs.Error(ctx, funcName, "获取群信息失败",
|
||||
zap.Int64("conversation_id", convID), zap.Error(gErr))
|
||||
continue
|
||||
}
|
||||
groupMap[convID] = brief
|
||||
}
|
||||
}
|
||||
|
||||
list := make([]dto.ConversationDTO, 0, len(convs))
|
||||
for _, c := range convs {
|
||||
peerID := c.PeerUserID
|
||||
item := dto.ConversationDTO{
|
||||
ID: c.ID,
|
||||
Type: c.Type,
|
||||
PeerUserID: peerID,
|
||||
PeerUserID: c.PeerUserID,
|
||||
LastMsgContent: c.LastMsgContent,
|
||||
LastMsgSenderID: c.LastMsgSenderID,
|
||||
IsPinned: c.IsPinned,
|
||||
UnreadCount: c.UnreadCount,
|
||||
IsDoNotDisturb: c.IsDoNotDisturb,
|
||||
AtMeCount: c.AtMeCount,
|
||||
}
|
||||
if c.ClearBeforeMsgID > 0 && c.LastMessageID != nil && *c.LastMessageID <= c.ClearBeforeMsgID {
|
||||
item.LastMsgContent = ""
|
||||
@@ -283,10 +468,20 @@ func (s *IMService) GetConversationList(ctx context.Context, userID int64) (*dto
|
||||
if c.LastMsgTime != nil {
|
||||
item.LastMsgTime = c.LastMsgTime.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
if brief, ok := userMap[peerID]; ok {
|
||||
item.PeerNickname = brief.Nickname
|
||||
item.PeerAvatar = brief.Avatar
|
||||
|
||||
if c.Type == constants.ConversationTypePrivate {
|
||||
if brief, ok := userMap[c.PeerUserID]; ok {
|
||||
item.PeerNickname = brief.Nickname
|
||||
item.PeerAvatar = brief.Avatar
|
||||
}
|
||||
} else if c.Type == constants.ConversationTypeGroup {
|
||||
if gBrief, ok := groupMap[c.ID]; ok {
|
||||
item.PeerNickname = gBrief.Name
|
||||
item.PeerAvatar = gBrief.Avatar
|
||||
item.GroupID = gBrief.ID
|
||||
}
|
||||
}
|
||||
|
||||
list = append(list, item)
|
||||
}
|
||||
|
||||
@@ -360,6 +555,133 @@ func (s *IMService) MarkRead(ctx context.Context, userID int64, conversationID i
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarkGroupMessagesRead 标记群聊消息已读(消息级别)
|
||||
// 将指定消息标记为已读 + 推送已读计数变化给消息发送者
|
||||
func (s *IMService) MarkGroupMessagesRead(ctx context.Context, userID int64, req *dto.MarkGroupReadRequest) error {
|
||||
funcName := "service.im_service.MarkGroupMessagesRead"
|
||||
logs.Info(ctx, funcName, "群消息标记已读",
|
||||
zap.Int64("user_id", userID), zap.Int64("conversation_id", req.ConversationID),
|
||||
zap.Int("msg_count", len(req.MessageIDs)))
|
||||
|
||||
member, err := s.convDAO.GetMember(ctx, req.ConversationID, userID)
|
||||
if err != nil || member == nil {
|
||||
return ErrNotMember
|
||||
}
|
||||
|
||||
if s.readRecorder == nil || len(req.MessageIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := s.readRecorder.BatchCreateReads(ctx, req.MessageIDs, userID); err != nil {
|
||||
logs.Error(ctx, funcName, "批量创建已读记录失败", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
if member.AtMeCount > 0 {
|
||||
if err := s.convDAO.ClearAtMeCount(ctx, req.ConversationID, userID); err != nil {
|
||||
logs.Error(ctx, funcName, "清零@计数失败", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
readCounts, err := s.readRecorder.GetReadCountBatch(ctx, req.MessageIDs)
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "获取已读计数失败", zap.Error(err))
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, msgID := range req.MessageIDs {
|
||||
msg, mErr := s.msgDAO.GetByID(ctx, msgID)
|
||||
if mErr != nil || msg.SenderID == userID {
|
||||
continue
|
||||
}
|
||||
count := readCounts[msgID]
|
||||
s.pushToUser(ctx, msg.SenderID, "im.message.read.count", map[string]interface{}{
|
||||
"message_id": msgID,
|
||||
"conversation_id": req.ConversationID,
|
||||
"read_count": count,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetMessageReadDetail 获取消息已读详情(已读/未读用户列表)
|
||||
func (s *IMService) GetMessageReadDetail(ctx context.Context, userID int64, messageID int64) (*dto.GetReadDetailResponse, error) {
|
||||
funcName := "service.im_service.GetMessageReadDetail"
|
||||
logs.Debug(ctx, funcName, "获取已读详情",
|
||||
zap.Int64("user_id", userID), zap.Int64("message_id", messageID))
|
||||
|
||||
msg, err := s.msgDAO.GetByID(ctx, messageID)
|
||||
if err != nil {
|
||||
return nil, ErrMsgNotFound
|
||||
}
|
||||
|
||||
member, err := s.convDAO.GetMember(ctx, msg.ConversationID, userID)
|
||||
if err != nil || member == nil {
|
||||
return nil, ErrNotMember
|
||||
}
|
||||
|
||||
memberIDs, err := s.convDAO.GetConversationMemberIDs(ctx, msg.ConversationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
readUserIDs := make(map[int64]bool)
|
||||
if s.readRecorder != nil {
|
||||
ids, rErr := s.readRecorder.GetReadUserIDs(ctx, messageID)
|
||||
if rErr != nil {
|
||||
logs.Error(ctx, funcName, "获取已读用户列表失败", zap.Error(rErr))
|
||||
} else {
|
||||
for _, id := range ids {
|
||||
readUserIDs[id] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
allUserIDs := make([]int64, 0, len(memberIDs))
|
||||
for _, id := range memberIDs {
|
||||
if id != msg.SenderID {
|
||||
allUserIDs = append(allUserIDs, id)
|
||||
}
|
||||
}
|
||||
|
||||
userMap := make(map[int64]*userBrief)
|
||||
if len(allUserIDs) > 0 {
|
||||
users, uErr := s.userInfoGetter.GetUsersByIDs(ctx, allUserIDs)
|
||||
if uErr != nil {
|
||||
logs.Error(ctx, funcName, "批量查询用户信息失败", zap.Error(uErr))
|
||||
} else {
|
||||
for i := range users {
|
||||
u := users[i]
|
||||
userMap[u.ID] = &userBrief{Nickname: u.Nickname, Avatar: u.Avatar}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var readList, unreadList []dto.MessageReadDetailDTO
|
||||
for _, uid := range allUserIDs {
|
||||
item := dto.MessageReadDetailDTO{
|
||||
UserID: uid,
|
||||
}
|
||||
if brief, ok := userMap[uid]; ok {
|
||||
item.UserNickname = brief.Nickname
|
||||
item.UserAvatar = brief.Avatar
|
||||
}
|
||||
if readUserIDs[uid] {
|
||||
readList = append(readList, item)
|
||||
} else {
|
||||
unreadList = append(unreadList, item)
|
||||
}
|
||||
}
|
||||
|
||||
return &dto.GetReadDetailResponse{
|
||||
ReadList: readList,
|
||||
UnreadList: unreadList,
|
||||
ReadCount: len(readList),
|
||||
TotalCount: len(allUserIDs),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// PinConversation 置顶/取消置顶会话
|
||||
func (s *IMService) PinConversation(ctx context.Context, userID int64, conversationID int64, isPinned bool) error {
|
||||
funcName := "service.im_service.PinConversation"
|
||||
@@ -580,7 +902,7 @@ func (s *IMService) decrementTotalUnread(ctx context.Context, userID int64, coun
|
||||
|
||||
// toMessageDTO 将 model.Message 转换为 dto.MessageDTO
|
||||
func (s *IMService) toMessageDTO(m *model.Message) *dto.MessageDTO {
|
||||
return &dto.MessageDTO{
|
||||
d := &dto.MessageDTO{
|
||||
ID: m.ID,
|
||||
ConversationID: m.ConversationID,
|
||||
SenderID: m.SenderID,
|
||||
@@ -590,6 +912,28 @@ func (s *IMService) toMessageDTO(m *model.Message) *dto.MessageDTO {
|
||||
ClientMsgID: m.ClientMsgID,
|
||||
CreatedAt: m.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
if len(m.AtUserIDs) > 0 {
|
||||
d.AtUserIDs = []int64(m.AtUserIDs)
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// buildMessagePushData 构建消息推送数据(单聊/群聊通用)
|
||||
func (s *IMService) buildMessagePushData(ctx context.Context, msg *model.Message, senderID int64) map[string]interface{} {
|
||||
pushData := map[string]interface{}{
|
||||
"id": msg.ID,
|
||||
"conversation_id": msg.ConversationID,
|
||||
"sender_id": senderID,
|
||||
"type": msg.Type,
|
||||
"content": msg.Content,
|
||||
"client_msg_id": msg.ClientMsgID,
|
||||
"created_at": msg.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
if senderUsers, sErr := s.userInfoGetter.GetUsersByIDs(ctx, []int64{senderID}); sErr == nil && len(senderUsers) > 0 {
|
||||
pushData["sender_name"] = senderUsers[0].Nickname
|
||||
pushData["sender_avatar"] = senderUsers[0].Avatar
|
||||
}
|
||||
return pushData
|
||||
}
|
||||
|
||||
// userBrief 用户简要信息(内部使用)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
|
||||
authModel "github.com/echochat/backend/app/auth/model"
|
||||
"github.com/echochat/backend/app/dto"
|
||||
)
|
||||
|
||||
// FriendChecker 好友关系校验接口
|
||||
@@ -18,3 +19,19 @@ type FriendChecker interface {
|
||||
type UserInfoGetter interface {
|
||||
GetUsersByIDs(ctx context.Context, userIDs []int64) ([]authModel.User, error)
|
||||
}
|
||||
|
||||
// GroupInfoGetter 群信息查询接口(Phase 2c)
|
||||
// 由 group.GroupDAO 隐式实现,通过 Wire 接口注入
|
||||
// IM 模块通过此接口获取群表信息(判断全体禁言、已解散等)
|
||||
// 成员信息(角色/禁言/列表)通过 convDAO 直接查询 im_conversation_members 表
|
||||
type GroupInfoGetter interface {
|
||||
GetGroupBrief(ctx context.Context, conversationID int64) (*dto.GroupBrief, error)
|
||||
}
|
||||
|
||||
// MessageReadRecorder 群消息已读记录接口(Phase 2c)
|
||||
// 由 group.MessageReadDAO 隐式实现
|
||||
type MessageReadRecorder interface {
|
||||
BatchCreateReads(ctx context.Context, messageIDs []int64, userID int64) error
|
||||
GetReadCountBatch(ctx context.Context, messageIDs []int64) (map[int64]int, error)
|
||||
GetReadUserIDs(ctx context.Context, messageID int64) ([]int64, error)
|
||||
}
|
||||
|
||||
@@ -7,13 +7,17 @@ import (
|
||||
authController "github.com/echochat/backend/app/auth/controller"
|
||||
"github.com/echochat/backend/app/auth/service"
|
||||
contactController "github.com/echochat/backend/app/contact/controller"
|
||||
fileController "github.com/echochat/backend/app/file/controller"
|
||||
groupController "github.com/echochat/backend/app/group/controller"
|
||||
imController "github.com/echochat/backend/app/im/controller"
|
||||
imHandler "github.com/echochat/backend/app/im/handler"
|
||||
wsApp "github.com/echochat/backend/app/ws"
|
||||
"github.com/echochat/backend/config"
|
||||
"github.com/echochat/backend/pkg/db"
|
||||
"github.com/echochat/backend/pkg/storage"
|
||||
"github.com/echochat/backend/pkg/ws"
|
||||
"github.com/google/wire"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -23,12 +27,14 @@ type App struct {
|
||||
Config *config.Config
|
||||
DB *gorm.DB
|
||||
Redis *redis.Client
|
||||
MinioClient *minio.Client // MinIO 对象存储客户端
|
||||
AuthService *service.AuthService // Auth 认证服务
|
||||
AuthController *authController.AuthController // 前台认证控制器
|
||||
AdminAuthController *authController.AdminAuthController // 后台认证控制器
|
||||
UserManageController *adminController.UserManageController // 管理端用户管理控制器
|
||||
OnlineController *adminController.OnlineController // 管理端在线监控控制器
|
||||
ContactManageController *adminController.ContactManageController // 管理端好友关系管理控制器
|
||||
GroupManageController *adminController.GroupManageController // 管理端群组管理控制器
|
||||
WSHandler *wsApp.Handler // WebSocket 连接处理器
|
||||
Hub *ws.Hub // WebSocket Hub 连接管理
|
||||
PubSub *ws.PubSub // Redis Pub/Sub 消息路由
|
||||
@@ -37,6 +43,8 @@ type App struct {
|
||||
IMController *imController.IMController // IM 即时通讯控制器
|
||||
IMEventHandler *imHandler.EventHandler // IM WS 事件处理器
|
||||
OfflinePusher *imHandler.OfflinePusher // 离线消息推送器
|
||||
FileController *fileController.FileController // 文件上传控制器
|
||||
GroupController *groupController.GroupController // 群聊管理控制器
|
||||
}
|
||||
|
||||
// NewApp 创建应用实例
|
||||
@@ -44,12 +52,14 @@ func NewApp(
|
||||
cfg *config.Config,
|
||||
gormDB *gorm.DB,
|
||||
redisClient *redis.Client,
|
||||
minioClient *minio.Client,
|
||||
authService *service.AuthService,
|
||||
authCtrl *authController.AuthController,
|
||||
adminAuthCtrl *authController.AdminAuthController,
|
||||
userManageCtrl *adminController.UserManageController,
|
||||
onlineCtrl *adminController.OnlineController,
|
||||
contactManageCtrl *adminController.ContactManageController,
|
||||
groupManageCtrl *adminController.GroupManageController,
|
||||
wsHandler *wsApp.Handler,
|
||||
hub *ws.Hub,
|
||||
pubsub *ws.PubSub,
|
||||
@@ -58,6 +68,8 @@ func NewApp(
|
||||
imCtrl *imController.IMController,
|
||||
imEventHandler *imHandler.EventHandler,
|
||||
offlinePusher *imHandler.OfflinePusher,
|
||||
fileCtrl *fileController.FileController,
|
||||
groupCtrl *groupController.GroupController,
|
||||
) *App {
|
||||
// 注入离线消息推送器到 WS Handler
|
||||
wsHandler.SetOfflinePusher(offlinePusher)
|
||||
@@ -66,12 +78,14 @@ func NewApp(
|
||||
Config: cfg,
|
||||
DB: gormDB,
|
||||
Redis: redisClient,
|
||||
MinioClient: minioClient,
|
||||
AuthService: authService,
|
||||
AuthController: authCtrl,
|
||||
AdminAuthController: adminAuthCtrl,
|
||||
UserManageController: userManageCtrl,
|
||||
OnlineController: onlineCtrl,
|
||||
ContactManageController: contactManageCtrl,
|
||||
GroupManageController: groupManageCtrl,
|
||||
WSHandler: wsHandler,
|
||||
Hub: hub,
|
||||
PubSub: pubsub,
|
||||
@@ -80,6 +94,8 @@ func NewApp(
|
||||
IMController: imCtrl,
|
||||
IMEventHandler: imEventHandler,
|
||||
OfflinePusher: offlinePusher,
|
||||
FileController: fileCtrl,
|
||||
GroupController: groupCtrl,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,12 +114,19 @@ func provideJWTConfig(cfg *config.Config) *config.JWTConfig {
|
||||
return &cfg.JWT
|
||||
}
|
||||
|
||||
// provideMinioConfig 从全局 Config 中提取 MinioConfig
|
||||
func provideMinioConfig(cfg *config.Config) *config.MinioConfig {
|
||||
return &cfg.Minio
|
||||
}
|
||||
|
||||
// InfraSet 基础设施层 Provider Set
|
||||
var InfraSet = wire.NewSet(
|
||||
provideDBConfig,
|
||||
provideRedisConfig,
|
||||
provideJWTConfig,
|
||||
provideMinioConfig,
|
||||
db.NewPostgres,
|
||||
db.NewRedis,
|
||||
storage.NewMinioClient,
|
||||
NewApp,
|
||||
)
|
||||
|
||||
@@ -6,11 +6,16 @@ package provider
|
||||
import (
|
||||
"github.com/echochat/backend/app/admin"
|
||||
"github.com/echochat/backend/app/auth"
|
||||
"github.com/echochat/backend/app/contact"
|
||||
authService "github.com/echochat/backend/app/auth/service"
|
||||
"github.com/echochat/backend/app/contact"
|
||||
contactDAO "github.com/echochat/backend/app/contact/dao"
|
||||
contactService "github.com/echochat/backend/app/contact/service"
|
||||
fileApp "github.com/echochat/backend/app/file"
|
||||
groupApp "github.com/echochat/backend/app/group"
|
||||
groupDAO "github.com/echochat/backend/app/group/dao"
|
||||
groupService "github.com/echochat/backend/app/group/service"
|
||||
imApp "github.com/echochat/backend/app/im"
|
||||
imDAO "github.com/echochat/backend/app/im/dao"
|
||||
imService "github.com/echochat/backend/app/im/service"
|
||||
wsApp "github.com/echochat/backend/app/ws"
|
||||
"github.com/echochat/backend/config"
|
||||
@@ -26,11 +31,17 @@ func InitializeApp(cfg *config.Config) (*App, error) {
|
||||
wsApp.WSSet,
|
||||
contact.ContactSet,
|
||||
imApp.IMSet,
|
||||
fileApp.FileSet,
|
||||
groupApp.GroupSet,
|
||||
wire.Bind(new(wsApp.FriendIDsGetter), new(*contactDAO.FriendshipDAO)),
|
||||
wire.Bind(new(groupService.UserInfoProvider), new(*contactDAO.FriendshipDAO)),
|
||||
wire.Bind(new(imService.GroupInfoGetter), new(*groupDAO.GroupDAO)),
|
||||
wire.Bind(new(imService.MessageReadRecorder), new(*groupDAO.MessageReadDAO)),
|
||||
wire.Bind(new(wsApp.TokenValidator), new(*authService.AuthService)),
|
||||
wire.Bind(new(imService.FriendChecker), new(*contactDAO.FriendshipDAO)),
|
||||
wire.Bind(new(imService.UserInfoGetter), new(*contactDAO.FriendshipDAO)),
|
||||
wire.Bind(new(contactService.OnlineChecker), new(*wsApp.OnlineService)),
|
||||
wire.Bind(new(groupService.MessageWriter), new(*imDAO.MessageDAO)),
|
||||
)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -16,10 +16,16 @@ import (
|
||||
controller3 "github.com/echochat/backend/app/contact/controller"
|
||||
dao3 "github.com/echochat/backend/app/contact/dao"
|
||||
service3 "github.com/echochat/backend/app/contact/service"
|
||||
controller4 "github.com/echochat/backend/app/file/controller"
|
||||
service4 "github.com/echochat/backend/app/file/service"
|
||||
controller5 "github.com/echochat/backend/app/group/controller"
|
||||
dao4 "github.com/echochat/backend/app/group/dao"
|
||||
service5 "github.com/echochat/backend/app/group/service"
|
||||
imApp "github.com/echochat/backend/app/im"
|
||||
"github.com/echochat/backend/app/ws"
|
||||
"github.com/echochat/backend/config"
|
||||
"github.com/echochat/backend/pkg/db"
|
||||
"github.com/echochat/backend/pkg/storage"
|
||||
)
|
||||
|
||||
// Injectors from wire.go:
|
||||
@@ -36,6 +42,11 @@ func InitializeApp(cfg *config.Config) (*App, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
minioConfig := provideMinioConfig(cfg)
|
||||
minioClient, err := storage.NewMinioClient(minioConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
userDAO := dao.NewUserDAO(gormDB)
|
||||
roleDAO := dao.NewRoleDAO(gormDB)
|
||||
jwtConfig := provideJWTConfig(cfg)
|
||||
@@ -62,11 +73,26 @@ func InitializeApp(cfg *config.Config) (*App, error) {
|
||||
// IM 模块初始化
|
||||
conversationDAO := imApp.ProvideConversationDAO(gormDB)
|
||||
messageDAO := imApp.ProvideMessageDAO(gormDB)
|
||||
imService := imApp.ProvideIMService(conversationDAO, messageDAO, pubSub, client, friendshipDAO, friendshipDAO)
|
||||
groupDAO := dao4.NewGroupDAO(gormDB)
|
||||
messageReadDAO := dao4.NewMessageReadDAO(gormDB)
|
||||
imService := imApp.ProvideIMService(conversationDAO, messageDAO, pubSub, client, friendshipDAO, friendshipDAO, groupDAO, messageReadDAO)
|
||||
imEventHandler := imApp.ProvideIMEventHandler(imService, hub)
|
||||
offlinePusher := imApp.ProvideOfflinePusher(imService, conversationDAO, pubSub)
|
||||
imController := imApp.ProvideIMController(imService)
|
||||
|
||||
app := NewApp(cfg, gormDB, client, authService, authController, adminAuthController, userManageController, onlineController, contactManageController, handler, hub, pubSub, onlineService, contactController, imController, imEventHandler, offlinePusher)
|
||||
// File 模块初始化
|
||||
fileService := service4.NewFileService(minioClient, minioConfig)
|
||||
fileController := controller4.NewFileController(fileService)
|
||||
|
||||
// Group 模块初始化
|
||||
joinRequestDAO := dao4.NewJoinRequestDAO(gormDB)
|
||||
groupService := service5.NewGroupService(groupDAO, joinRequestDAO, friendshipDAO, pubSub, messageDAO)
|
||||
groupController := controller5.NewGroupController(groupService)
|
||||
|
||||
// Admin 群组管理初始化
|
||||
groupManageService := service2.NewGroupManageService(gormDB, groupDAO)
|
||||
groupManageController := controller2.NewGroupManageController(groupManageService)
|
||||
|
||||
app := NewApp(cfg, gormDB, client, minioClient, authService, authController, adminAuthController, userManageController, onlineController, contactManageController, groupManageController, handler, hub, pubSub, onlineService, contactController, imController, imEventHandler, offlinePusher, fileController, groupController)
|
||||
return app, nil
|
||||
}
|
||||
|
||||
@@ -45,3 +45,11 @@ log:
|
||||
max_backups: 10 # 保留的旧日志归档文件最大数量,超出的自动删除
|
||||
max_age: 30 # 旧日志归档文件保留天数,超过天数的自动删除
|
||||
compress: false # 是否对归档的旧日志文件进行 gzip 压缩(生产环境建议 true)
|
||||
|
||||
# MinIO 对象存储配置
|
||||
minio:
|
||||
endpoint: "localhost:9000" # MinIO 服务地址
|
||||
access_key: "echochat" # 访问密钥(与 docker-compose 中的 MINIO_ROOT_USER 一致)
|
||||
secret_key: "echochat123456" # 密钥(与 docker-compose 中的 MINIO_ROOT_PASSWORD 一致)
|
||||
bucket: "echochat" # 存储桶名称
|
||||
use_ssl: false # 开发环境不使用 HTTPS
|
||||
|
||||
@@ -40,3 +40,10 @@ log:
|
||||
max_backups: 10
|
||||
max_age: 30
|
||||
compress: false
|
||||
|
||||
minio:
|
||||
endpoint: "minio:9000"
|
||||
access_key: "echochat"
|
||||
secret_key: "echochat123456"
|
||||
bucket: "echochat"
|
||||
use_ssl: false
|
||||
|
||||
@@ -16,6 +16,16 @@ type Config struct {
|
||||
Redis RedisConfig `mapstructure:"redis"`
|
||||
JWT JWTConfig `mapstructure:"jwt"`
|
||||
Log LogConfig `mapstructure:"log"`
|
||||
Minio MinioConfig `mapstructure:"minio"`
|
||||
}
|
||||
|
||||
// MinioConfig MinIO 对象存储配置
|
||||
type MinioConfig struct {
|
||||
Endpoint string `mapstructure:"endpoint"` // MinIO 服务地址(host:port)
|
||||
AccessKey string `mapstructure:"access_key"` // 访问密钥
|
||||
SecretKey string `mapstructure:"secret_key"` // 密钥
|
||||
Bucket string `mapstructure:"bucket"` // 存储桶名称
|
||||
UseSSL bool `mapstructure:"use_ssl"` // 是否使用 HTTPS
|
||||
}
|
||||
|
||||
// ServerConfig HTTP 服务配置
|
||||
|
||||
@@ -8,6 +8,7 @@ require (
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/google/wire v0.7.0
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/minio/minio-go/v7 v7.0.83
|
||||
github.com/redis/go-redis/v9 v9.18.0
|
||||
github.com/spf13/viper v1.21.0
|
||||
go.uber.org/zap v1.27.1
|
||||
@@ -23,14 +24,16 @@ require (
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.6 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
||||
github.com/gin-contrib/sse v1.1.0 // indirect
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.27.0 // indirect
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/goccy/go-json v0.10.4 // indirect
|
||||
github.com/goccy/go-yaml v1.18.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
@@ -39,14 +42,17 @@ require (
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/compress v1.17.11 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/minio/md5-simd v1.1.2 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||
github.com/quic-go/qpack v0.5.1 // indirect
|
||||
github.com/quic-go/quic-go v0.54.0 // indirect
|
||||
github.com/rs/xid v1.6.0 // indirect
|
||||
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
|
||||
github.com/spf13/afero v1.15.0 // indirect
|
||||
|
||||
@@ -15,6 +15,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
|
||||
@@ -25,6 +27,8 @@ github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w
|
||||
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
|
||||
github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk=
|
||||
github.com/gin-gonic/gin v1.11.0/go.mod h1:+iq/FyxlGzII0KHiBGjuNn4UNENUlKbGlNmc+W50Dls=
|
||||
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
@@ -35,8 +39,8 @@ github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHO
|
||||
github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/goccy/go-json v0.10.4 h1:JSwxQzIqKfmFX1swYPpUThQZp/Ka4wzJdK0LWVytLPM=
|
||||
github.com/goccy/go-json v0.10.4/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
|
||||
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
|
||||
@@ -64,6 +68,9 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
|
||||
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
|
||||
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
|
||||
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
@@ -74,6 +81,10 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
|
||||
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
|
||||
github.com/minio/minio-go/v7 v7.0.83 h1:W4Kokksvlz3OKf3OqIlzDNKd4MERlC2oN8YptwJ0+GA=
|
||||
github.com/minio/minio-go/v7 v7.0.83/go.mod h1:57YXpvc5l3rjPdhqNrDsvVlY0qPI6UTk1bflAe+9doY=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
@@ -90,6 +101,8 @@ github.com/redis/go-redis/v9 v9.18.0 h1:pMkxYPkEbMPwRdenAzUNyFNrDgHx9U+DrBabWNfS
|
||||
github.com/redis/go-redis/v9 v9.18.0/go.mod h1:k3ufPphLU5YXwNTUcCRXGxUoF1fqxnhFQmscfkCoDA0=
|
||||
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
|
||||
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=
|
||||
|
||||
38
backend/go-service/pkg/storage/minio.go
Normal file
38
backend/go-service/pkg/storage/minio.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// Package storage 提供对象存储基础设施
|
||||
// 封装 MinIO 客户端初始化和存储桶自动创建逻辑
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/echochat/backend/config"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
// NewMinioClient 初始化 MinIO 客户端并确保存储桶存在
|
||||
// 启动时自动创建配置中指定的 bucket(如不存在)
|
||||
func NewMinioClient(cfg *config.MinioConfig) (*minio.Client, error) {
|
||||
client, err := minio.New(cfg.Endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""),
|
||||
Secure: cfg.UseSSL,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建 MinIO 客户端失败: %w", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
exists, err := client.BucketExists(ctx, cfg.Bucket)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("检查存储桶 %s 失败: %w", cfg.Bucket, err)
|
||||
}
|
||||
|
||||
if !exists {
|
||||
if err := client.MakeBucket(ctx, cfg.Bucket, minio.MakeBucketOptions{}); err != nil {
|
||||
return nil, fmt.Errorf("创建存储桶 %s 失败: %w", cfg.Bucket, err)
|
||||
}
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
70
backend/go-service/pkg/utils/int64_array.go
Normal file
70
backend/go-service/pkg/utils/int64_array.go
Normal file
@@ -0,0 +1,70 @@
|
||||
// Package utils 提供通用工具类型和函数
|
||||
package utils
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Int64Array 自定义 PostgreSQL BIGINT[] 类型
|
||||
// 实现 sql.Scanner 和 driver.Valuer 接口,用于 GORM 与 PostgreSQL 数组字段交互
|
||||
type Int64Array []int64
|
||||
|
||||
// Scan 从数据库读取 PostgreSQL BIGINT[] 格式:{1,2,3}
|
||||
func (a *Int64Array) Scan(src interface{}) error {
|
||||
if src == nil {
|
||||
*a = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
var s string
|
||||
switch v := src.(type) {
|
||||
case []byte:
|
||||
s = string(v)
|
||||
case string:
|
||||
s = v
|
||||
default:
|
||||
return fmt.Errorf("Int64Array.Scan: 不支持的类型 %T", src)
|
||||
}
|
||||
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "{}" || s == "" {
|
||||
*a = Int64Array{}
|
||||
return nil
|
||||
}
|
||||
|
||||
s = strings.Trim(s, "{}")
|
||||
parts := strings.Split(s, ",")
|
||||
result := make(Int64Array, 0, len(parts))
|
||||
for _, p := range parts {
|
||||
p = strings.TrimSpace(p)
|
||||
if p == "" {
|
||||
continue
|
||||
}
|
||||
n, err := strconv.ParseInt(p, 10, 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Int64Array.Scan: 解析 '%s' 失败: %w", p, err)
|
||||
}
|
||||
result = append(result, n)
|
||||
}
|
||||
*a = result
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value 写入数据库时转换为 PostgreSQL BIGINT[] 格式:{1,2,3}
|
||||
func (a Int64Array) Value() (driver.Value, error) {
|
||||
if a == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if len(a) == 0 {
|
||||
return "{}", nil
|
||||
}
|
||||
|
||||
parts := make([]string, len(a))
|
||||
for i, v := range a {
|
||||
parts[i] = strconv.FormatInt(v, 10)
|
||||
}
|
||||
return "{" + strings.Join(parts, ",") + "}", nil
|
||||
}
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"github.com/echochat/backend/app/admin"
|
||||
"github.com/echochat/backend/app/auth"
|
||||
"github.com/echochat/backend/app/contact"
|
||||
fileApp "github.com/echochat/backend/app/file"
|
||||
groupApp "github.com/echochat/backend/app/group"
|
||||
imApp "github.com/echochat/backend/app/im"
|
||||
"github.com/echochat/backend/app/provider"
|
||||
wsApp "github.com/echochat/backend/app/ws"
|
||||
@@ -34,10 +36,12 @@ func Setup(engine *gin.Engine, app *provider.App) {
|
||||
|
||||
// --- 各模块路由注册 ---
|
||||
auth.RegisterRoutes(engine, app.AuthController, app.AdminAuthController, jwtAuth)
|
||||
admin.RegisterRoutes(engine, app.UserManageController, app.OnlineController, app.ContactManageController, jwtAuth)
|
||||
admin.RegisterRoutes(engine, app.UserManageController, app.OnlineController, app.ContactManageController, app.GroupManageController, jwtAuth)
|
||||
wsApp.RegisterRoutes(engine, app.WSHandler)
|
||||
contact.RegisterRoutes(engine, app.ContactController, jwtAuth)
|
||||
imApp.RegisterRoutes(engine, app.IMController, jwtAuth)
|
||||
fileApp.RegisterRoutes(engine, app.FileController, jwtAuth)
|
||||
groupApp.RegisterRoutes(engine, app.GroupController, jwtAuth)
|
||||
|
||||
// [未来] meeting.RegisterRoutes(engine, app.MeetingController, jwtAuth)
|
||||
// [未来] notify.RegisterRoutes(engine, app.NotifyController, jwtAuth)
|
||||
|
||||
Reference in New Issue
Block a user