Skip to content

Commit 7e804f1

Browse files
committed
Add BuildContact and CRUD functionality for contacts
1 parent 97deed8 commit 7e804f1

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

appstate/encode.go

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type PatchInfo struct {
3232
Timestamp time.Time
3333
// Type is the app state type being mutated.
3434
Type WAPatchName
35+
// Operation is SET / REMOVE
36+
Operation waServerSync.SyncdMutation_SyncdOperation
3537
// Mutations contains the individual mutations to apply to the app state in this patch.
3638
Mutations []MutationInfo
3739
}
@@ -46,7 +48,8 @@ func BuildMute(target types.JID, mute bool, muteDuration time.Duration) PatchInf
4648
}
4749

4850
return PatchInfo{
49-
Type: WAPatchRegularHigh,
51+
Type: WAPatchRegularHigh,
52+
Operation: waServerSync.SyncdMutation_SET,
5053
Mutations: []MutationInfo{{
5154
Index: []string{IndexMute, target.String()},
5255
Version: 2,
@@ -60,6 +63,27 @@ func BuildMute(target types.JID, mute bool, muteDuration time.Duration) PatchInf
6063
}
6164
}
6265

66+
func BuildContact(target types.JID, fullName string, add bool) PatchInfo {
67+
operation := waServerSync.SyncdMutation_SET
68+
if !add {
69+
operation = waServerSync.SyncdMutation_REMOVE
70+
}
71+
return PatchInfo{
72+
Type: WAPatchCriticalUnblockLow,
73+
Operation: operation,
74+
Mutations: []MutationInfo{{
75+
Index: []string{IndexContact, target.String()},
76+
Version: 2,
77+
Value: &waSyncAction.SyncActionValue{
78+
ContactAction: &waSyncAction.ContactAction{
79+
FullName: &fullName,
80+
SaveOnPrimaryAddressbook: proto.Bool(add),
81+
},
82+
},
83+
}},
84+
}
85+
}
86+
6387
func newPinMutationInfo(target types.JID, pin bool) MutationInfo {
6488
return MutationInfo{
6589
Index: []string{IndexPin, target.String()},
@@ -75,7 +99,8 @@ func newPinMutationInfo(target types.JID, pin bool) MutationInfo {
7599
// BuildPin builds an app state patch for pinning or unpinning a chat.
76100
func BuildPin(target types.JID, pin bool) PatchInfo {
77101
return PatchInfo{
78-
Type: WAPatchRegularLow,
102+
Type: WAPatchRegularLow,
103+
Operation: waServerSync.SyncdMutation_SET,
79104
Mutations: []MutationInfo{
80105
newPinMutationInfo(target, pin),
81106
},
@@ -119,6 +144,7 @@ func BuildArchive(target types.JID, archive bool, lastMessageTimestamp time.Time
119144

120145
result := PatchInfo{
121146
Type: WAPatchRegularLow,
147+
Operation: waServerSync.SyncdMutation_SET,
122148
Mutations: mutations,
123149
}
124150

@@ -140,7 +166,8 @@ func newLabelChatMutation(target types.JID, labelID string, labeled bool) Mutati
140166
// BuildLabelChat builds an app state patch for labeling or un(labeling) a chat.
141167
func BuildLabelChat(target types.JID, labelID string, labeled bool) PatchInfo {
142168
return PatchInfo{
143-
Type: WAPatchRegular,
169+
Type: WAPatchRegular,
170+
Operation: waServerSync.SyncdMutation_SET,
144171
Mutations: []MutationInfo{
145172
newLabelChatMutation(target, labelID, labeled),
146173
},
@@ -162,7 +189,8 @@ func newLabelMessageMutation(target types.JID, labelID, messageID string, labele
162189
// BuildLabelMessage builds an app state patch for labeling or un(labeling) a message.
163190
func BuildLabelMessage(target types.JID, labelID, messageID string, labeled bool) PatchInfo {
164191
return PatchInfo{
165-
Type: WAPatchRegular,
192+
Type: WAPatchRegular,
193+
Operation: waServerSync.SyncdMutation_SET,
166194
Mutations: []MutationInfo{
167195
newLabelMessageMutation(target, labelID, messageID, labeled),
168196
},
@@ -186,7 +214,8 @@ func newLabelEditMutation(labelID string, labelName string, labelColor int32, de
186214
// BuildLabelEdit builds an app state patch for editing a label.
187215
func BuildLabelEdit(labelID string, labelName string, labelColor int32, deleted bool) PatchInfo {
188216
return PatchInfo{
189-
Type: WAPatchRegular,
217+
Type: WAPatchRegular,
218+
Operation: waServerSync.SyncdMutation_SET,
190219
Mutations: []MutationInfo{
191220
newLabelEditMutation(labelID, labelName, labelColor, deleted),
192221
},
@@ -208,7 +237,8 @@ func newSettingPushNameMutation(pushName string) MutationInfo {
208237
// BuildSettingPushName builds an app state patch for setting the push name.
209238
func BuildSettingPushName(pushName string) PatchInfo {
210239
return PatchInfo{
211-
Type: WAPatchCriticalBlock,
240+
Type: WAPatchCriticalBlock,
241+
Operation: waServerSync.SyncdMutation_SET,
212242
Mutations: []MutationInfo{
213243
newSettingPushNameMutation(pushName),
214244
},
@@ -238,7 +268,8 @@ func BuildStar(target, sender types.JID, messageID types.MessageID, fromMe, star
238268
senderJID = "0"
239269
}
240270
return PatchInfo{
241-
Type: WAPatchRegularHigh,
271+
Type: WAPatchRegularHigh,
272+
Operation: waServerSync.SyncdMutation_SET,
242273
Mutations: []MutationInfo{
243274
newStarMutation(targetJID, senderJID, messageID, isFromMe, starred),
244275
},
@@ -281,11 +312,11 @@ func (proc *Processor) EncodePatch(keyID []byte, state HashState, patchInfo Patc
281312
return nil, fmt.Errorf("failed to encrypt mutation: %w", err)
282313
}
283314

284-
valueMac := generateContentMAC(waServerSync.SyncdMutation_SET, encryptedContent, keyID, keys.ValueMAC)
315+
valueMac := generateContentMAC(patchInfo.Operation, encryptedContent, keyID, keys.ValueMAC)
285316
indexMac := concatAndHMAC(sha256.New, keys.Index, indexBytes)
286317

287318
mutations = append(mutations, &waServerSync.SyncdMutation{
288-
Operation: waServerSync.SyncdMutation_SET.Enum(),
319+
Operation: patchInfo.Operation.Enum(),
289320
Record: &waServerSync.SyncdRecord{
290321
Index: &waServerSync.SyncdIndex{Blob: indexMac},
291322
Value: &waServerSync.SyncdValue{Blob: append(encryptedContent, valueMac...)},

0 commit comments

Comments
 (0)