-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.go
203 lines (147 loc) · 3.68 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package chat
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"strings"
"time"
chatpb "github.com/code-payments/code-protobuf-api/generated/go/chat/v1"
"github.com/code-payments/code-server/pkg/pointer"
)
type ChatType uint8
const (
ChatTypeUnknown ChatType = iota
ChatTypeInternal // todo: better name, or split into the various buckets (eg. Code Team vs Cash Transactions)
ChatTypeExternalApp
)
type ChatId [32]byte
type Chat struct {
Id uint64
ChatId ChatId
ChatType ChatType
ChatTitle string // The message sender
IsVerified bool
CodeUser string // Always a receiver of messages
ReadPointer *string
IsMuted bool
IsUnsubscribed bool
CreatedAt time.Time
}
type Message struct {
Id uint64
ChatId ChatId
MessageId string
Data []byte
IsSilent bool
ContentLength uint8
Timestamp time.Time
}
func GetChatId(sender, receiver string, isVerified bool) ChatId {
combined := []byte(fmt.Sprintf("%s:%s:%v", sender, receiver, isVerified))
if strings.Compare(sender, receiver) > 0 {
combined = []byte(fmt.Sprintf("%s:%s:%v", receiver, sender, isVerified))
}
return sha256.Sum256(combined)
}
func (c ChatId) ToProto() *chatpb.ChatId {
return &chatpb.ChatId{
Value: c[:],
}
}
func ChatIdFromProto(proto *chatpb.ChatId) ChatId {
var chatId ChatId
copy(chatId[:], proto.Value)
return chatId
}
func (c ChatId) String() string {
return hex.EncodeToString(c[:])
}
func (r *Chat) Validate() error {
expectedChatId := GetChatId(r.CodeUser, r.ChatTitle, r.IsVerified)
if !bytes.Equal(r.ChatId[:], expectedChatId[:]) {
return errors.New("chat id is invalid")
}
if r.ChatType == ChatTypeUnknown {
return errors.New("chat type is required")
}
if len(r.ChatTitle) == 0 {
return errors.New("chat title is required")
}
if len(r.CodeUser) == 0 {
return errors.New("code user is required")
}
if r.ReadPointer != nil && len(*r.ReadPointer) == 0 {
return errors.New("read pointer is required when set")
}
return nil
}
func (r *Chat) Clone() Chat {
var chatIdCopy ChatId
copy(chatIdCopy[:], r.ChatId[:])
return Chat{
Id: r.Id,
ChatId: chatIdCopy,
ChatType: r.ChatType,
ChatTitle: r.ChatTitle,
IsVerified: r.IsVerified,
CodeUser: r.CodeUser,
ReadPointer: pointer.StringCopy(r.ReadPointer),
IsMuted: r.IsMuted,
IsUnsubscribed: r.IsUnsubscribed,
CreatedAt: r.CreatedAt,
}
}
func (r *Chat) CopyTo(dst *Chat) {
dst.Id = r.Id
copy(dst.ChatId[:], r.ChatId[:])
dst.ChatType = r.ChatType
dst.ChatTitle = r.ChatTitle
dst.IsVerified = r.IsVerified
dst.CodeUser = r.CodeUser
dst.ReadPointer = pointer.StringCopy(r.ReadPointer)
dst.IsMuted = r.IsMuted
dst.IsUnsubscribed = r.IsUnsubscribed
dst.CreatedAt = r.CreatedAt
}
func (r *Message) Validate() error {
if len(r.Data) == 0 {
return errors.New("data is required")
}
if len(r.MessageId) == 0 {
return errors.New("message id is required")
}
if r.ContentLength <= 0 {
return errors.New("content length must be positive")
}
if r.Timestamp.IsZero() {
return errors.New("timestamp is required")
}
return nil
}
func (r *Message) Clone() Message {
var chatIdCopy ChatId
copy(chatIdCopy[:], r.ChatId[:])
dataCopy := make([]byte, len(r.Data))
copy(dataCopy, r.Data)
return Message{
Id: r.Id,
ChatId: chatIdCopy,
MessageId: r.MessageId,
Data: dataCopy,
IsSilent: r.IsSilent,
ContentLength: r.ContentLength,
Timestamp: r.Timestamp,
}
}
func (r *Message) CopyTo(dst *Message) {
dst.Id = r.Id
dst.ChatId = r.ChatId
dst.MessageId = r.MessageId
dst.Data = make([]byte, len(r.Data))
copy(dst.Data, r.Data)
dst.IsSilent = r.IsSilent
dst.ContentLength = r.ContentLength
dst.Timestamp = r.Timestamp
}