45 lines
993 B
Go
45 lines
993 B
Go
package entity
|
|
|
|
import "fmt"
|
|
|
|
// Kind selects private_message vs group_message. Same numbers as conversation.Type; not a SQL column.
|
|
type Kind int
|
|
|
|
const (
|
|
KindPrivate Kind = 1
|
|
KindGroup Kind = 2
|
|
)
|
|
|
|
func (k Kind) Valid() bool {
|
|
return k == KindPrivate || k == KindGroup
|
|
}
|
|
|
|
// Message is a persisted IM body. Content is serialized JSON bytes, not a string.
|
|
type Message struct {
|
|
ID int64
|
|
ConversationID int64
|
|
SenderID int64
|
|
ReceiverID int64 // private_message only; 0 for system
|
|
GroupID int64 // group_message only; groups.id 群号
|
|
ClientMsgID string
|
|
Type Type
|
|
Kind Kind
|
|
Content []byte
|
|
Status Status
|
|
CreatedAtMS int64
|
|
}
|
|
|
|
func (m *Message) Payload() (*Content, error) {
|
|
if m == nil {
|
|
return nil, nil
|
|
}
|
|
c, err := UnmarshalContent(m.Content)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if c.Type != m.Type {
|
|
return nil, fmt.Errorf("content: column type %d != payload %d", m.Type, c.Type)
|
|
}
|
|
return c, nil
|
|
}
|