-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate_message_builder.go
158 lines (134 loc) · 4.19 KB
/
update_message_builder.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
package snapper
import (
"github.com/0xzer/snapper/crypto"
"github.com/0xzer/snapper/data/methods"
"github.com/0xzer/snapper/protos"
"github.com/0xzer/snapper/snaperror"
)
type UpdateMessageBuilder struct {
client *Client
conversationVersion *protos.ConversationVersionInfo
updateAction *protos.UpdateAction
err *snaperror.UpdateMessageBuilderError
}
func (c *Client) NewUpdateMessageBuilder(messageId int64) *UpdateMessageBuilder {
return &UpdateMessageBuilder{
client: c,
conversationVersion: &protos.ConversationVersionInfo{},
updateAction: &protos.UpdateAction{
MessageId: messageId,
SenderId: c.Session.CurrentUser.EncodedUserId,
},
}
}
func (m *UpdateMessageBuilder) SetConversation(conversation *protos.Conversation) *UpdateMessageBuilder {
m.conversationVersion.ConversationVersion = conversation.GetVersion()
m.conversationVersion.ConversationId = conversation.GetConversationId()
return m
}
func (m *UpdateMessageBuilder) SetMessage(messageId int64) *UpdateMessageBuilder {
m.updateAction.MessageId = messageId
return m
}
func (m *UpdateMessageBuilder) SetReaction(intentType int64) *UpdateMessageBuilder {
m.updateAction.Update = &protos.UpdateAction_MessageReaction{
MessageReaction: &protos.ReactionRequest{
NewReaction: &protos.NewReaction{
Reaction: &protos.ReactionType{
Reaction: &protos.ReactionType_IntentType{
IntentType: intentType,
},
},
},
},
}
return m
}
func (m *UpdateMessageBuilder) SetRemoveReaction() *UpdateMessageBuilder {
m.updateAction.Update = &protos.UpdateAction_RemoveReaction{
RemoveReaction: &protos.RemoveReactionRequest{},
}
return m
}
func (m *UpdateMessageBuilder) SetRead() *UpdateMessageBuilder {
m.updateAction.Update = &protos.UpdateAction_Read{
Read: &protos.Read{},
}
return m
}
func (m *UpdateMessageBuilder) SetRelease() *UpdateMessageBuilder {
m.updateAction.Update = &protos.UpdateAction_Release{
Release: &protos.Release{},
}
return m
}
func (m *UpdateMessageBuilder) SetSave() *UpdateMessageBuilder {
m.updateAction.Update = &protos.UpdateAction_Save{
Save: &protos.Save{
EnvelopeEncryption: &protos.EnvelopeEncryption{
Method: &protos.EnvelopeEncryption_None{None: &protos.Empty{}},
},
},
}
return m
}
func (m *UpdateMessageBuilder) SetUnsave() *UpdateMessageBuilder {
m.updateAction.Update = &protos.UpdateAction_Unsave{
Unsave: &protos.Unsave{},
}
return m
}
// delete message
func (m *UpdateMessageBuilder) SetErase() *UpdateMessageBuilder {
m.updateAction.Update = &protos.UpdateAction_Erase{
Erase: &protos.Erase{},
}
return m
}
func (m *UpdateMessageBuilder) SetSaveToCameraRoll() *UpdateMessageBuilder {
m.updateAction.Update = &protos.UpdateAction_SaveToCameraRoll{
SaveToCameraRoll: &protos.SaveToCameraRoll{},
}
return m
}
func (m *UpdateMessageBuilder) SetScreenshot() *UpdateMessageBuilder {
m.updateAction.Update = &protos.UpdateAction_Screenshot{
Screenshot: &protos.Screenshot{},
}
return m
}
func (m *UpdateMessageBuilder) SetScreenRecord() *UpdateMessageBuilder {
m.updateAction.Update = &protos.UpdateAction_ScreenRecord{
ScreenRecord: &protos.ScreenRecord{},
}
return m
}
func (m *UpdateMessageBuilder) SetReplay(currentReplayCount int32) *UpdateMessageBuilder {
m.updateAction.Update = &protos.UpdateAction_Replay{
Replay: &protos.Replay{
CurrentReplayCount: currentReplayCount,
},
}
return m
}
func (m *UpdateMessageBuilder) build() ([]byte, error) {
if m.updateAction.Update == nil {
m.err = &snaperror.UpdateMessageBuilderError{ErrorMessage: "builder must have some action set before building"}
}
if m.updateAction.MessageId == 0 {
m.err = &snaperror.UpdateMessageBuilderError{ErrorMessage: "messageId must be set before building"}
}
if m.err != nil {
return nil, m.err
}
clientResolutionId, _ := crypto.GenerateBitUUID()
nowTimestamp := methods.GetTimestamp()
m.updateAction.UpdateTimestamp = nowTimestamp
m.updateAction.ConversationId = m.conversationVersion.ConversationId
updateMessage := &protos.UpdateContentMessageRequest{
ClientResolutionId: clientResolutionId.Uint64(),
CurrentVersion: m.conversationVersion.GetConversationVersion(),
Update: m.updateAction,
}
return protos.EncodeProtoMessage(updateMessage)
}