-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate_conversation_builder.go
123 lines (106 loc) · 4.16 KB
/
update_conversation_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
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 UpdateConversationBuilder struct {
client *Client
updateRequest *protos.UpdateConversationRequest
err *snaperror.UpdateConversationBuilderError
}
func (c *Client) NewUpdateConversationBuilder(conversation *protos.Conversation) *UpdateConversationBuilder {
clientResolutionId, _ := crypto.GenerateBitUUID()
return &UpdateConversationBuilder{
client: c,
updateRequest: &protos.UpdateConversationRequest{
ConversationId: conversation.GetConversationId(),
CurrentVersion: conversation.GetVersion(),
ClientResolutionId: clientResolutionId.Uint64(),
},
}
}
// for reuse
func (cb *UpdateConversationBuilder) SetConversation(conversation *protos.Conversation) *UpdateConversationBuilder {
cb.updateRequest.ConversationId = conversation.GetConversationId()
cb.updateRequest.CurrentVersion = conversation.GetVersion()
return cb
}
func (cb *UpdateConversationBuilder) SetRetentionPolicy(policy *protos.RetentionPolicy) *UpdateConversationBuilder {
cb.updateRequest.UpdateConversationAction = &protos.UpdateConversationRequest_RetentionPolicy{
RetentionPolicy: &protos.UpdateConversationRetentionPolicy{
SelfUserId: cb.client.Session.CurrentUser.EncodedUserId,
RetentionPolicy: policy,
},
}
return cb
}
func (cb *UpdateConversationBuilder) SetRead(lastMessageId int64) *UpdateConversationBuilder {
cb.updateRequest.UpdateConversationAction = &protos.UpdateConversationRequest_Read{
Read: &protos.UpdateConversationRead{
SelfUserId: cb.client.Session.CurrentUser.EncodedUserId,
ReadConversationMessageData: &protos.ReadConversationMessageData{
LastMessageId: lastMessageId,
},
},
}
return cb
}
func (cb *UpdateConversationBuilder) SetNotificationPreference(preference protos.ChatNotificationPreference) *UpdateConversationBuilder {
notificationPref := &protos.UpdateConversationRequest_NotificationPreference{
NotificationPreference: &protos.UpdateConversationNotificationPreference{
SelfUserId: cb.client.Session.CurrentUser.EncodedUserId,
SomeInt2: 1,
Setting: preference,
},
}
if preference == protos.ChatNotificationPreference_ALL_MESSAGES {
notificationPref.NotificationPreference.SomeInt = 1
}
cb.updateRequest.UpdateConversationAction = notificationPref
return cb
}
func (cb *UpdateConversationBuilder) SetTitle(newTitle string, oldTitle string) *UpdateConversationBuilder {
cb.updateRequest.UpdateConversationAction = &protos.UpdateConversationRequest_ConversationTitle{
ConversationTitle: &protos.UpdateConversationTitle{
SelfUserId: cb.client.Session.CurrentUser.EncodedUserId,
OldTitle: oldTitle,
NewTitle: newTitle,
},
}
return cb
}
func (cb *UpdateConversationBuilder) SetClearFromFeed() *UpdateConversationBuilder {
cb.updateRequest.UpdateConversationAction = &protos.UpdateConversationRequest_ClearFromChatFeed{
ClearFromChatFeed: &protos.UpdateConversationClearFromFeed{
SelfUserId: cb.client.Session.CurrentUser.EncodedUserId,
UpdateTimestamp: methods.GetTimestamp(),
},
}
return cb
}
func (cb *UpdateConversationBuilder) SetLeaveConversation() *UpdateConversationBuilder {
cb.updateRequest.UpdateConversationAction = &protos.UpdateConversationRequest_LeaveConversation{
LeaveConversation: &protos.UpdateConversationLeave{
SelfUserId: cb.client.Session.CurrentUser.EncodedUserId,
SelfUserIdLeft: cb.client.Session.CurrentUser.EncodedUserId,
},
}
return cb
}
func (cb *UpdateConversationBuilder) AddParticipants(participants []*protos.UUID) *UpdateConversationBuilder {
cb.updateRequest.UpdateConversationAction = &protos.UpdateConversationRequest_AddParticipants{
AddParticipants: &protos.UpdateConversationAddParticipants{
SelfUserId: cb.client.Session.CurrentUser.EncodedUserId,
Participants: participants,
},
}
return cb
}
func (cb *UpdateConversationBuilder) build() ([]byte, error) {
if cb.updateRequest.UpdateConversationAction == nil {
cb.err = &snaperror.UpdateConversationBuilderError{ErrorMessage: "builder must have some action set before building"}
}
return protos.EncodeProtoMessage(cb.updateRequest)
}