2024-08-02 16:33:52 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2024-08-05 21:17:04 +08:00
|
|
|
"strconv"
|
|
|
|
|
"time"
|
2024-08-02 16:33:52 +08:00
|
|
|
"videoplayer/dao"
|
2024-08-16 15:25:10 +08:00
|
|
|
"videoplayer/proto"
|
2024-08-05 21:17:04 +08:00
|
|
|
"videoplayer/worker"
|
2024-08-02 16:33:52 +08:00
|
|
|
)
|
|
|
|
|
|
2024-08-16 15:25:10 +08:00
|
|
|
func CreateGeneralMessageService(from_id, to_id, msg_type, group_id int, content string) (error, uint) {
|
2024-08-02 16:33:52 +08:00
|
|
|
// 业务逻辑
|
|
|
|
|
var err error
|
2024-08-04 21:01:06 +08:00
|
|
|
var id uint
|
2024-08-02 16:33:52 +08:00
|
|
|
switch msg_type {
|
2024-08-16 15:25:10 +08:00
|
|
|
case proto.MSG_TYPE_SIMPLE:
|
2024-09-21 13:14:20 +08:00
|
|
|
//判断是否是好友,判断是否存在缓存,不存在则设置缓存,存在则判断是否是好友
|
|
|
|
|
if worker.IsContainKey("user_"+strconv.Itoa(to_id)+"_friends") == false {
|
|
|
|
|
//设置好友缓存
|
|
|
|
|
isSuccess := SetFriendCache(from_id)
|
|
|
|
|
//设置失败,直接查询数据库
|
|
|
|
|
if !isSuccess {
|
|
|
|
|
friend := dao.FindFriend(from_id, to_id)
|
|
|
|
|
if len(friend) == 0 {
|
|
|
|
|
return errors.New("未添加好友"), 0
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//判断是否是好友-redis方式
|
|
|
|
|
is_f := worker.IsContainSet("user_"+strconv.Itoa(to_id)+"_friends", strconv.Itoa(from_id))
|
|
|
|
|
if !is_f {
|
|
|
|
|
return errors.New("未添加好友"), 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//判断是否是好友-redis方式
|
|
|
|
|
is_f := worker.IsContainSet("user_"+strconv.Itoa(to_id)+"_friends", strconv.Itoa(from_id))
|
|
|
|
|
if !is_f {
|
|
|
|
|
return errors.New("未添加好友"), 0
|
|
|
|
|
}
|
2024-08-04 21:01:06 +08:00
|
|
|
}
|
2024-09-21 13:14:20 +08:00
|
|
|
|
2024-08-04 21:01:06 +08:00
|
|
|
err, id = dao.CreateSimpleMessage(from_id, to_id, content)
|
2024-08-05 21:17:04 +08:00
|
|
|
res := worker.GetRedis("user_" + strconv.Itoa(to_id) + "_status_v2")
|
|
|
|
|
if res == "1" {
|
|
|
|
|
//在线,存入redis
|
|
|
|
|
worker.PushRedisListWithExpire("user_"+strconv.Itoa(to_id)+"_msg_ids", strconv.Itoa(int(id)), time.Second*300)
|
|
|
|
|
}
|
2024-08-16 15:25:10 +08:00
|
|
|
case proto.MSG_TYPE_GROUP:
|
|
|
|
|
if from_id == 0 || group_id == 0 || content == "" {
|
|
|
|
|
return errors.New("参数错误"), 0
|
|
|
|
|
}
|
2024-09-21 13:14:20 +08:00
|
|
|
//判断是否在群里
|
|
|
|
|
if worker.IsContainKey("group_"+strconv.Itoa(group_id)+"_users") == false {
|
|
|
|
|
//设置群缓存
|
|
|
|
|
isSuccess := SetGroupCache(group_id)
|
|
|
|
|
//设置失败,直接查询数据库
|
|
|
|
|
if !isSuccess {
|
|
|
|
|
groupUser := dao.FindGroupUser(from_id, group_id)
|
|
|
|
|
if len(groupUser) == 0 {
|
|
|
|
|
return errors.New("用户不在群里"), 0
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//判断该用户是否在群里-redis方式
|
|
|
|
|
is_g := worker.IsContainSet("group_"+strconv.Itoa(group_id)+"_users", strconv.Itoa(from_id))
|
|
|
|
|
if !is_g {
|
|
|
|
|
return errors.New("用户不在群里"), 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//判断该用户是否在群里-redis方式
|
|
|
|
|
is_g := worker.IsContainSet("group_"+strconv.Itoa(group_id)+"_users", strconv.Itoa(from_id))
|
|
|
|
|
if !is_g {
|
|
|
|
|
return errors.New("用户不在群里"), 0
|
|
|
|
|
}
|
2024-08-16 15:25:10 +08:00
|
|
|
}
|
2024-09-21 13:14:20 +08:00
|
|
|
|
2024-08-16 15:25:10 +08:00
|
|
|
err, id = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, group_id, content)
|
|
|
|
|
//获取群里的用户
|
|
|
|
|
users := dao.FindGroupUsers(group_id)
|
|
|
|
|
for _, user := range users {
|
|
|
|
|
if user.UserID == from_id {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
res := worker.GetRedis("user_" + strconv.Itoa(user.UserID) + "_status_v2")
|
|
|
|
|
if res == "1" {
|
|
|
|
|
//在线,存入redis
|
|
|
|
|
worker.PushRedisListWithExpire("user_"+strconv.Itoa(user.UserID)+"_msg_ids", strconv.Itoa(int(id)), time.Second*300)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-04 21:01:06 +08:00
|
|
|
case 3:
|
|
|
|
|
//user := dao.FindUserByID(to_id)
|
|
|
|
|
// 系统消息,需要管理员权限
|
2024-08-16 15:25:10 +08:00
|
|
|
err, id = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, group_id, content)
|
|
|
|
|
case proto.MSG_TYPE_FRIEND:
|
|
|
|
|
res1 := dao.FindFriend(from_id, to_id)
|
|
|
|
|
if len(res1) > 0 {
|
|
|
|
|
// 已经有会话记录
|
|
|
|
|
return errors.New("已是好友关系"), 0
|
|
|
|
|
}
|
2024-08-04 21:01:06 +08:00
|
|
|
res, _ := dao.GetMsgUserByIndex(from_id, to_id, 4, 1, 0)
|
|
|
|
|
if len(res) > 0 {
|
2024-08-07 15:03:56 +08:00
|
|
|
// 已经有会话记录,返回会话id
|
2024-08-16 15:25:10 +08:00
|
|
|
return errors.New("已有请求"), res[0].ID
|
2024-08-04 21:01:06 +08:00
|
|
|
}
|
2024-08-16 15:25:10 +08:00
|
|
|
err, id = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, group_id, content)
|
2024-09-21 13:14:20 +08:00
|
|
|
|
2024-08-16 15:25:10 +08:00
|
|
|
case proto.MSG_TYPE_GROUP_ADD:
|
|
|
|
|
//加入群聊请求
|
2024-08-20 10:50:35 +08:00
|
|
|
//判断是否在群里
|
|
|
|
|
groupUser := dao.FindGroupUser(from_id, group_id)
|
|
|
|
|
if len(groupUser) > 0 {
|
|
|
|
|
return errors.New("已在群里"), 0
|
|
|
|
|
}
|
2024-08-20 15:25:13 +08:00
|
|
|
//查看是否已经发送过请求
|
|
|
|
|
res, _ := dao.GetMsgUserGroupReq(from_id, group_id)
|
|
|
|
|
if len(res) > 0 {
|
|
|
|
|
return errors.New("已发送请求"), res[0].ID
|
|
|
|
|
}
|
2024-08-20 10:50:35 +08:00
|
|
|
err, id = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, group_id, content)
|
2024-08-16 15:25:10 +08:00
|
|
|
|
|
|
|
|
case proto.MSG_TYPE_GROUP_INVI:
|
|
|
|
|
//邀请加群,直接加入
|
|
|
|
|
//判断邀请人是否在群里
|
|
|
|
|
groupUser := dao.FindGroupUser(from_id, group_id)
|
|
|
|
|
if len(groupUser) == 0 {
|
|
|
|
|
return errors.New("邀请人不在群里"), 0
|
|
|
|
|
}
|
|
|
|
|
//判断被邀请人是否在群里
|
|
|
|
|
groupUser = dao.FindGroupUser(to_id, group_id)
|
|
|
|
|
if len(groupUser) > 0 {
|
|
|
|
|
return errors.New("已在群里"), 0
|
2024-08-04 21:01:06 +08:00
|
|
|
}
|
2024-08-16 15:25:10 +08:00
|
|
|
err, id = dao.JoinGroup(group_id, to_id)
|
2024-09-21 13:14:20 +08:00
|
|
|
if err == nil {
|
|
|
|
|
//设置群缓存,如果存在缓存则加入
|
|
|
|
|
if worker.IsContainKey("group_"+strconv.Itoa(group_id)+"_users") == true {
|
|
|
|
|
//将用户加入群缓存
|
|
|
|
|
worker.SetRedisSetAdd("group_"+strconv.Itoa(group_id)+"_users", strconv.Itoa(to_id))
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-02 16:33:52 +08:00
|
|
|
default:
|
|
|
|
|
// 未知消息类型
|
|
|
|
|
err = errors.New("unknown message type")
|
|
|
|
|
}
|
2024-08-04 21:01:06 +08:00
|
|
|
return err, id
|
2024-08-02 16:33:52 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-16 15:25:10 +08:00
|
|
|
func GetMsgUserByIndexService(from_id, to_id, index, msq_type, from_user_id, group_id int) ([]dao.Message, error) {
|
2024-08-02 16:33:52 +08:00
|
|
|
// 业务逻辑
|
2024-08-16 15:25:10 +08:00
|
|
|
var msgs []dao.Message
|
|
|
|
|
var err error
|
2024-08-02 16:33:52 +08:00
|
|
|
if index <= 0 || index > 100 {
|
|
|
|
|
return nil, errors.New("index out of range")
|
|
|
|
|
}
|
2024-08-16 15:25:10 +08:00
|
|
|
if msq_type == proto.MSG_TYPE_FRIEND {
|
2024-08-04 21:01:06 +08:00
|
|
|
from_id = from_user_id
|
|
|
|
|
}
|
2024-08-16 15:25:10 +08:00
|
|
|
msgs, err = dao.GetMsgUserByIndex(from_id, to_id, msq_type, index, 0)
|
|
|
|
|
//群聊消息
|
|
|
|
|
|
2024-08-02 16:33:52 +08:00
|
|
|
return msgs, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-04 21:01:06 +08:00
|
|
|
// AddFriendService 通过消息id添加好友,和加入群聊
|
|
|
|
|
// id 消息id
|
|
|
|
|
// from_user_id 发送消息的用户id
|
|
|
|
|
// to_user_id 接收消息的用户id,及接受用户id
|
|
|
|
|
func AddFriendService(id, from_user_id, to_user_id int) error {
|
|
|
|
|
// 业务逻辑
|
|
|
|
|
res := dao.FindMessageByID(uint(id))
|
2024-08-20 15:25:13 +08:00
|
|
|
if len(res) == 0 {
|
|
|
|
|
return errors.New("no such message")
|
|
|
|
|
}
|
2024-08-04 21:01:06 +08:00
|
|
|
if res[0].FromUserID == to_user_id && res[0].ToUserID == from_user_id {
|
|
|
|
|
friend := dao.FindFriend(from_user_id, to_user_id)
|
|
|
|
|
if len(friend) > 0 {
|
|
|
|
|
return errors.New("already a friend")
|
|
|
|
|
}
|
|
|
|
|
dao.UpdateMessageStatus(res[0].ID, 1)
|
2024-09-21 13:14:20 +08:00
|
|
|
res2 := dao.AddFriend(from_user_id, to_user_id)
|
|
|
|
|
if res2 == nil {
|
|
|
|
|
//设置好友缓存
|
|
|
|
|
if worker.IsContainKey("user_"+strconv.Itoa(from_user_id)+"_friends") == true {
|
|
|
|
|
worker.SetRedisSetAdd("user_"+strconv.Itoa(from_user_id)+"_friends", strconv.Itoa(to_user_id))
|
|
|
|
|
}
|
|
|
|
|
if worker.IsContainKey("user_"+strconv.Itoa(to_user_id)+"_friends") == true {
|
|
|
|
|
worker.SetRedisSetAdd("user_"+strconv.Itoa(to_user_id)+"_friends", strconv.Itoa(from_user_id))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res2
|
2024-08-04 21:01:06 +08:00
|
|
|
} else if res[0].ToUserID == from_user_id && res[0].GroupID == to_user_id {
|
|
|
|
|
//加入群聊
|
|
|
|
|
//查看是否已经加入
|
|
|
|
|
groupUser := dao.FindGroupUser(from_user_id, to_user_id)
|
|
|
|
|
if len(groupUser) > 0 {
|
|
|
|
|
return errors.New("already in the group")
|
|
|
|
|
}
|
2024-08-16 15:25:10 +08:00
|
|
|
err, _ := dao.JoinGroup(groupUser[0].GroupID, to_user_id)
|
2024-08-04 21:01:06 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = dao.UpdateMessageStatus(res[0].ID, 1)
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
return errors.New("no such message,cannot add friend")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateGroup 创建群聊
|
|
|
|
|
// groupName 群聊名称
|
|
|
|
|
// groupInfo 群聊信息
|
|
|
|
|
// groupType 群聊类型
|
|
|
|
|
// groupIcon 群聊图标
|
|
|
|
|
// user_id 创建群的用户id
|
|
|
|
|
func CreateGroup(groupName, groupInfo, groupType, groupIcon string, user_id int) (error, uint) {
|
2024-08-02 16:33:52 +08:00
|
|
|
// 业务逻辑
|
2024-08-04 21:01:06 +08:00
|
|
|
err, id := dao.CreateGroup(groupName, groupInfo, groupType, groupIcon, user_id)
|
|
|
|
|
return err, id
|
2024-08-02 16:33:52 +08:00
|
|
|
}
|
2024-08-07 15:31:25 +08:00
|
|
|
|
|
|
|
|
type FGRet struct {
|
|
|
|
|
Friends []dao.FriendRet `json:"friends"`
|
|
|
|
|
Groups []dao.Group `json:"groups"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetFriendList(user_id int) FGRet {
|
|
|
|
|
//获取好友id和群id
|
|
|
|
|
friends := dao.FindFriends(user_id)
|
|
|
|
|
groups := dao.FindGroups(user_id)
|
|
|
|
|
var fg FGRet
|
|
|
|
|
fg.Friends = friends
|
|
|
|
|
fg.Groups = groups
|
|
|
|
|
return fg
|
|
|
|
|
}
|
2024-08-09 20:52:47 +08:00
|
|
|
|
2024-08-09 20:58:56 +08:00
|
|
|
func GetFriendRequest(user_id int) []dao.FriendRequest {
|
2024-08-09 20:52:47 +08:00
|
|
|
//获取好友请求
|
|
|
|
|
users := dao.GetFriendRequest(user_id)
|
|
|
|
|
return users
|
|
|
|
|
}
|
2024-08-16 15:25:10 +08:00
|
|
|
|
|
|
|
|
func DelFriendService(user_id, friend_id int) error {
|
|
|
|
|
//删除好友
|
|
|
|
|
err := dao.DeleteFriend(user_id, friend_id)
|
2024-09-21 13:14:20 +08:00
|
|
|
//删除好友缓存
|
|
|
|
|
if err == nil && worker.IsContainKey("user_"+strconv.Itoa(user_id)+"_friends") == true {
|
|
|
|
|
worker.SetRedisSetRemove("user_"+strconv.Itoa(user_id)+"_friends", strconv.Itoa(friend_id))
|
|
|
|
|
}
|
2024-08-16 15:25:10 +08:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func QuitGroupService(user_id, group_id int) error {
|
|
|
|
|
//退出群聊
|
|
|
|
|
err := dao.QuitGroup(group_id, user_id)
|
2024-09-21 13:14:20 +08:00
|
|
|
//删除群缓存
|
|
|
|
|
if err == nil && worker.IsContainKey("group_"+strconv.Itoa(group_id)+"_users") == true {
|
|
|
|
|
worker.SetRedisSetRemove("group_"+strconv.Itoa(group_id)+"_users", strconv.Itoa(user_id))
|
|
|
|
|
}
|
2024-08-16 15:25:10 +08:00
|
|
|
return err
|
|
|
|
|
}
|
2024-08-20 10:27:42 +08:00
|
|
|
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
|
|
|
|
|
}
|
2024-08-16 15:25:10 +08:00
|
|
|
|
|
|
|
|
func GetGroups(user_id int) []dao.Group {
|
|
|
|
|
//获取群聊
|
|
|
|
|
groups := dao.GetGroups(user_id)
|
|
|
|
|
return groups
|
|
|
|
|
}
|
2024-08-20 10:27:42 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
2024-08-20 15:25:13 +08:00
|
|
|
|
|
|
|
|
func GetGroupRequestUsers(user_id int) []dao.FriendRequest {
|
|
|
|
|
//获取群聊请求
|
|
|
|
|
users := dao.GetGroupRequestUsers(user_id)
|
|
|
|
|
return users
|
|
|
|
|
}
|
2024-09-21 13:14:20 +08:00
|
|
|
|
|
|
|
|
// 设置用户朋友关系缓存
|
|
|
|
|
func SetFriendCache(user_id int) bool {
|
|
|
|
|
//获取好友id
|
|
|
|
|
friends := dao.FindFriends(user_id)
|
|
|
|
|
var ids []string
|
|
|
|
|
for _, friend := range friends {
|
|
|
|
|
ids = append(ids, strconv.Itoa(friend.ID))
|
|
|
|
|
}
|
|
|
|
|
res := worker.SetRedisSet("user_"+strconv.Itoa(user_id)+"_friends", ids, time.Hour*12)
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置用户群关系缓存
|
|
|
|
|
func SetGroupCache(group_id int) bool {
|
|
|
|
|
//获取好友id
|
|
|
|
|
users := dao.FindGroupUsers(group_id)
|
|
|
|
|
var ids []string
|
|
|
|
|
for _, user := range users {
|
|
|
|
|
ids = append(ids, strconv.Itoa(user.UserID))
|
|
|
|
|
}
|
|
|
|
|
res := worker.SetRedisSet("group_"+strconv.Itoa(group_id)+"_groups", ids, time.Hour*12)
|
|
|
|
|
return res
|
|
|
|
|
}
|