videoplayer/service/imService.go

132 lines
3.7 KiB
Go
Raw Normal View History

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-05 21:17:04 +08:00
"videoplayer/worker"
2024-08-02 16:33:52 +08:00
)
func CreateGeneralMessageService(from_id, to_id, msg_type int, content string) (error, uint) {
2024-08-02 16:33:52 +08:00
// 业务逻辑
var err error
var id uint
2024-08-02 16:33:52 +08:00
switch msg_type {
case 1:
//判断是否是好友
friend := dao.FindFriend(from_id, to_id)
if len(friend) == 0 {
return errors.New("not a friend"), 0
}
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)
}
case 2:
err, id = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, 0, content)
case 3:
//user := dao.FindUserByID(to_id)
// 系统消息,需要管理员权限
err, id = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, 0, content)
2024-08-02 16:33:52 +08:00
case 4:
res, _ := dao.GetMsgUserByIndex(from_id, to_id, 4, 1, 0)
if len(res) > 0 {
2024-08-07 15:03:56 +08:00
// 已经有会话记录,返回会话id
return nil, res[0].ID
}
err, id = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, 0, content)
case 5:
res, _ := dao.GetMsgUserByIndex(from_id, to_id, 5, 1, 0)
if len(res) > 0 {
// 已经有会话记录
return errors.New("already have a conversation"), 0
}
//邀请加入群聊请求
err, id = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, from_id, content)
2024-08-02 16:33:52 +08:00
default:
// 未知消息类型
err = errors.New("unknown message type")
}
return err, id
2024-08-02 16:33:52 +08:00
}
func GetMsgUserByIndexService(from_id, to_id, index, msq_type, from_user_id int) ([]dao.Message, error) {
2024-08-02 16:33:52 +08:00
// 业务逻辑
if index <= 0 || index > 100 {
return nil, errors.New("index out of range")
}
if msq_type == 4 {
from_id = from_user_id
}
msgs, err := dao.GetMsgUserByIndex(from_id, to_id, msq_type, index, 0)
2024-08-02 16:33:52 +08:00
return msgs, err
}
// 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))
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)
return dao.AddFriend(from_user_id, to_user_id)
} 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")
}
err := dao.JoinGroup(groupUser[0].GroupID, to_user_id)
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
// 业务逻辑
err, id := dao.CreateGroup(groupName, groupInfo, groupType, groupIcon, user_id)
return err, id
2024-08-02 16:33:52 +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
func GetFriendRequest(user_id int) []dao.User {
//获取好友请求
users := dao.GetFriendRequest(user_id)
return users
}