群聊优化
This commit is contained in:
@@ -643,14 +643,20 @@ func ChangeMemberRoleHandler(c *gin.Context) {
|
||||
memberID := c.Param("user_id")
|
||||
|
||||
var req struct {
|
||||
Role int8 `json:"role" binding:"required"`
|
||||
Role *int8 `json:"role"` // 使用指针类型,允许 0 值(普通成员)
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.BadRequest(c, "参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := service.RoomSvc.ChangeMemberRole(roomID, operatorID.(string), memberID, req.Role); err != nil {
|
||||
// 手动验证 role 是否提供
|
||||
if req.Role == nil {
|
||||
utils.BadRequest(c, "参数错误: role 字段必须提供")
|
||||
return
|
||||
}
|
||||
|
||||
if err := service.RoomSvc.ChangeMemberRole(roomID, operatorID.(string), memberID, *req.Role); err != nil {
|
||||
utils.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
@@ -105,8 +106,8 @@ func CreateUserHandler(c *gin.Context) {
|
||||
*/
|
||||
func UpdateUserHandler(c *gin.Context) {
|
||||
var req struct {
|
||||
ID string `json:"id" binding:"required"`
|
||||
Updates map[string]interface{} `json:"updates" binding:"required"`
|
||||
ID string `json:"id" binding:"required"`
|
||||
Updates map[string]interface{} `json:"updates" binding:"required"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
@@ -119,9 +120,70 @@ func UpdateUserHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 如果更新了头像或名称,通知好友
|
||||
if service.ContactSvc != nil && service.ChatSvc != nil {
|
||||
if _, hasName := req.Updates["name"]; hasName {
|
||||
go notifyFriendsOfProfileUpdate(req.ID, req.Updates)
|
||||
} else if _, hasAvatar := req.Updates["avatar"]; hasAvatar {
|
||||
go notifyFriendsOfProfileUpdate(req.ID, req.Updates)
|
||||
}
|
||||
}
|
||||
|
||||
utils.Success(c, "更新成功")
|
||||
}
|
||||
|
||||
/**
|
||||
* notifyFriendsOfProfileUpdate
|
||||
* 功能:通知好友用户信息更新
|
||||
*/
|
||||
func notifyFriendsOfProfileUpdate(userID string, updates map[string]interface{}) {
|
||||
if service.ContactSvc == nil || service.ChatSvc == nil || service.UserSvc == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取更新后的用户信息
|
||||
user, err := service.UserSvc.GetUserByID(userID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取好友列表
|
||||
friendIDs, err := service.ContactSvc.GetFriendUserIDs(userID)
|
||||
if err != nil || len(friendIDs) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// 构建通知消息
|
||||
notifExtra := map[string]interface{}{
|
||||
"type": "profile_update",
|
||||
"user_id": userID,
|
||||
"name": user.Name,
|
||||
"avatar": user.Avatar,
|
||||
"updates": updates,
|
||||
}
|
||||
notifExtraJSON, _ := json.Marshal(notifExtra)
|
||||
|
||||
// 发送给所有好友
|
||||
for _, friendID := range friendIDs {
|
||||
notifMsg := model.ChatMessage{
|
||||
RoomID: userID,
|
||||
SenderUserID: userID,
|
||||
ReceiverUserID: friendID,
|
||||
MessageType: model.MessageTypeFriendNotif,
|
||||
Content: user.Name + " 更新了个人资料",
|
||||
Extra: string(notifExtraJSON),
|
||||
}
|
||||
|
||||
// 不持久化此类通知,直接推送
|
||||
pushMsg := model.WsPayload{
|
||||
RequestType: "receive_message",
|
||||
Data: notifMsg,
|
||||
}
|
||||
msgBytes, _ := json.Marshal(pushMsg)
|
||||
service.ChatSvc.DispatchMessage(friendID, msgBytes)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DeleteUserHandler
|
||||
* 功能:删除用户
|
||||
@@ -152,4 +214,3 @@ func generateUserID() string {
|
||||
random := rand.Intn(1000000)
|
||||
return fmt.Sprintf("user_%d_%d", timestamp, random)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user