golang-im聊天
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

message.go 2.2 KiB

2 lat temu
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package model
  2. import (
  3. "gim/pkg/logger"
  4. "gim/pkg/pb"
  5. "gim/pkg/util"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. // Message 消息
  11. type Message struct {
  12. Id int64 // 自增主键
  13. UserId int64 // 所属类型id
  14. RequestId int64 // 请求id
  15. SenderType int32 // 发送者类型
  16. SenderId int64 // 发送者账户id
  17. ReceiverType int32 // 接收者账户id
  18. ReceiverId int64 // 接收者id,如果是单聊信息,则为user_id,如果是群组消息,则为group_id
  19. ToUserIds string // 需要@的用户id列表,多个用户用,隔开
  20. Type int // 消息类型
  21. Content []byte // 消息内容
  22. Seq int64 // 消息同步序列
  23. SendTime time.Time // 消息发送时间
  24. Status int32 // 创建时间
  25. }
  26. func (m *Message) MessageToPB() *pb.Message {
  27. return &pb.Message{
  28. Sender: &pb.Sender{
  29. SenderType: pb.SenderType(m.SenderType),
  30. SenderId: m.SenderId,
  31. },
  32. ReceiverType: pb.ReceiverType(m.ReceiverType),
  33. ReceiverId: m.ReceiverId,
  34. ToUserIds: UnformatUserIds(m.ToUserIds),
  35. MessageType: pb.MessageType(m.Type),
  36. MessageContent: m.Content,
  37. Seq: m.Seq,
  38. SendTime: util.UnixMilliTime(m.SendTime),
  39. Status: pb.MessageStatus(m.Status),
  40. }
  41. }
  42. func FormatUserIds(userId []int64) string {
  43. build := strings.Builder{}
  44. for i, v := range userId {
  45. build.WriteString(strconv.FormatInt(v, 10))
  46. if i != len(userId)-1 {
  47. build.WriteString(",")
  48. }
  49. }
  50. return build.String()
  51. }
  52. func UnformatUserIds(userIdStr string) []int64 {
  53. if userIdStr == "" {
  54. return []int64{}
  55. }
  56. toUserIdStrs := strings.Split(userIdStr, ",")
  57. toUserIds := make([]int64, 0, len(toUserIdStrs))
  58. for i := range toUserIdStrs {
  59. userId, err := strconv.ParseInt(toUserIdStrs[i], 10, 64)
  60. if err != nil {
  61. logger.Sugar.Error(err)
  62. continue
  63. }
  64. toUserIds = append(toUserIds, userId)
  65. }
  66. return toUserIds
  67. }
  68. func MessagesToPB(messages []Message) []*pb.Message {
  69. pbMessages := make([]*pb.Message, 0, len(messages))
  70. for i := range messages {
  71. pbMessage := messages[i].MessageToPB()
  72. if pbMessages != nil {
  73. pbMessages = append(pbMessages, pbMessage)
  74. }
  75. }
  76. return pbMessages
  77. }