Merge branch 'refs/heads/feature-im-gitea'
This commit is contained in:
commit
44e2240637
25
dao/im.go
25
dao/im.go
|
|
@ -193,6 +193,25 @@ func QuitGroup(group_id, user_id int) error {
|
|||
return res.Error
|
||||
}
|
||||
|
||||
// 根据群id查找群
|
||||
func FindGroup(group_id int) []Group {
|
||||
var groups []Group
|
||||
DB.Debug().Where("id = ?", group_id).Find(&groups)
|
||||
return groups
|
||||
}
|
||||
|
||||
// 删除群聊
|
||||
func DeleteGroup(group_id int, auth_id int) error {
|
||||
res := DB.Debug().Delete(&Group{}, "id = ? and auth_id = ?", group_id, auth_id)
|
||||
return res.Error
|
||||
}
|
||||
|
||||
// 删除群里的用户
|
||||
func DeleteGroupUsers(group_id int) error {
|
||||
res := DB.Debug().Delete(&GroupUser{}, "group_id = ?", group_id)
|
||||
return res.Error
|
||||
}
|
||||
|
||||
func FindFriend(from_user_id, to_user_id int) []Friend {
|
||||
var friends []Friend
|
||||
DB.Debug().Where("user_id = ? and friend_id = ?", from_user_id, to_user_id).Find(&friends)
|
||||
|
|
@ -252,3 +271,9 @@ func FindGroupUsers(group_id int) []GroupUser {
|
|||
DB.Debug().Where("group_id = ?", group_id).Find(&groupUsers)
|
||||
return groupUsers
|
||||
}
|
||||
|
||||
func FindGroupByNameLike(groupName string) []Group {
|
||||
var groups []Group
|
||||
DB.Debug().Where("group_name like ?", "%"+groupName+"%").Find(&groups)
|
||||
return groups
|
||||
}
|
||||
|
|
|
|||
|
|
@ -112,12 +112,25 @@ func DelFriendOrGroup(c *gin.Context) {
|
|||
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
|
||||
return
|
||||
}
|
||||
//退出群聊
|
||||
err2 := service.QuitGroupService(cid, req.GroupID)
|
||||
if err2 == nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"})
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{"error": err2.Error(), "code": proto.OperationFailed, "message": "failed"})
|
||||
}
|
||||
} else if req.Type == 3 {
|
||||
//群主解散群
|
||||
if req.GroupID == 0 {
|
||||
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
|
||||
return
|
||||
}
|
||||
err2 := service.DelGroupService(cid, req.GroupID)
|
||||
if err2 == nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"})
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{"error": err2.Error(), "code": proto.OperationFailed, "message": "failed"})
|
||||
}
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -194,11 +194,13 @@ func SearchHandler(c *gin.Context) {
|
|||
if err := c.ShouldBind(&req_data); err == nil {
|
||||
if req_data.ID != -1 {
|
||||
user := service.GetUserByID(req_data.ID)
|
||||
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": user})
|
||||
group := service.GetGroupByID(req_data.ID)
|
||||
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": user, "group": group})
|
||||
return
|
||||
} else if req_data.Keyword != "" {
|
||||
users := service.GetUserByNameLike(req_data.Keyword)
|
||||
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": users})
|
||||
groups := service.GetGroupByNameLike(req_data.Keyword)
|
||||
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": users, "group": groups})
|
||||
return
|
||||
} else {
|
||||
c.JSON(200, gin.H{"code": proto.ParameterError, "message": "error", "data": "无ID 与 关键字"})
|
||||
|
|
|
|||
|
|
@ -179,9 +179,35 @@ func QuitGroupService(user_id, group_id int) error {
|
|||
err := dao.QuitGroup(group_id, user_id)
|
||||
return err
|
||||
}
|
||||
func DelGroupService(user_id, group_id int) error {
|
||||
//查找群聊
|
||||
groups := dao.FindGroup(group_id)
|
||||
if len(groups) == 0 {
|
||||
return errors.New("no such group")
|
||||
}
|
||||
if groups[0].AuthID != user_id {
|
||||
return errors.New("no permission")
|
||||
}
|
||||
//删除群聊
|
||||
err := dao.DeleteGroup(group_id, user_id)
|
||||
err = dao.DeleteGroupUsers(group_id)
|
||||
return err
|
||||
}
|
||||
|
||||
func GetGroups(user_id int) []dao.Group {
|
||||
//获取群聊
|
||||
groups := dao.GetGroups(user_id)
|
||||
return groups
|
||||
}
|
||||
|
||||
func GetGroupByID(group_id int) []dao.Group {
|
||||
//获取群聊
|
||||
groups := dao.FindGroupByID(group_id)
|
||||
return groups
|
||||
}
|
||||
|
||||
func GetGroupByNameLike(name string) []dao.Group {
|
||||
//获取群聊
|
||||
groups := dao.FindGroupByNameLike(name)
|
||||
return groups
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue