Skip to content

Commit 293a0b1

Browse files
committed
Add BuildContact and CRUD functionality for contacts
1 parent c9652e4 commit 293a0b1

1 file changed

Lines changed: 40 additions & 9 deletions

File tree

appstate/encode.go

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ type PatchInfo struct {
3333
Timestamp time.Time
3434
// Type is the app state type being mutated.
3535
Type WAPatchName
36+
// Operation is SET / REMOVE
37+
Operation waServerSync.SyncdMutation_SyncdOperation
3638
// Mutations contains the individual mutations to apply to the app state in this patch.
3739
Mutations []MutationInfo
3840
}
@@ -54,7 +56,8 @@ func BuildMuteAbs(target types.JID, mute bool, muteEndTimestamp *int64) PatchInf
5456
muteEndTimestamp = proto.Int64(-1)
5557
}
5658
return PatchInfo{
57-
Type: WAPatchRegularHigh,
59+
Type: WAPatchRegularHigh,
60+
Operation: waServerSync.SyncdMutation_SET,
5861
Mutations: []MutationInfo{{
5962
Index: []string{IndexMute, target.String()},
6063
Version: 2,
@@ -68,6 +71,27 @@ func BuildMuteAbs(target types.JID, mute bool, muteEndTimestamp *int64) PatchInf
6871
}
6972
}
7073

74+
func BuildContact(target types.JID, fullName string, add bool) PatchInfo {
75+
operation := waServerSync.SyncdMutation_SET
76+
if !add {
77+
operation = waServerSync.SyncdMutation_REMOVE
78+
}
79+
return PatchInfo{
80+
Type: WAPatchCriticalUnblockLow,
81+
Operation: operation,
82+
Mutations: []MutationInfo{{
83+
Index: []string{IndexContact, target.String()},
84+
Version: 2,
85+
Value: &waSyncAction.SyncActionValue{
86+
ContactAction: &waSyncAction.ContactAction{
87+
FullName: &fullName,
88+
SaveOnPrimaryAddressbook: proto.Bool(add),
89+
},
90+
},
91+
}},
92+
}
93+
}
94+
7195
func newPinMutationInfo(target types.JID, pin bool) MutationInfo {
7296
return MutationInfo{
7397
Index: []string{IndexPin, target.String()},
@@ -83,7 +107,8 @@ func newPinMutationInfo(target types.JID, pin bool) MutationInfo {
83107
// BuildPin builds an app state patch for pinning or unpinning a chat.
84108
func BuildPin(target types.JID, pin bool) PatchInfo {
85109
return PatchInfo{
86-
Type: WAPatchRegularLow,
110+
Type: WAPatchRegularLow,
111+
Operation: waServerSync.SyncdMutation_SET,
87112
Mutations: []MutationInfo{
88113
newPinMutationInfo(target, pin),
89114
},
@@ -115,6 +140,7 @@ func BuildArchive(target types.JID, archive bool, lastMessageTimestamp time.Time
115140

116141
result := PatchInfo{
117142
Type: WAPatchRegularLow,
143+
Operation: waServerSync.SyncdMutation_SET,
118144
Mutations: mutations,
119145
}
120146

@@ -155,7 +181,8 @@ func newLabelChatMutation(target types.JID, labelID string, labeled bool) Mutati
155181
// BuildLabelChat builds an app state patch for labeling or un(labeling) a chat.
156182
func BuildLabelChat(target types.JID, labelID string, labeled bool) PatchInfo {
157183
return PatchInfo{
158-
Type: WAPatchRegular,
184+
Type: WAPatchRegular,
185+
Operation: waServerSync.SyncdMutation_SET,
159186
Mutations: []MutationInfo{
160187
newLabelChatMutation(target, labelID, labeled),
161188
},
@@ -177,7 +204,8 @@ func newLabelMessageMutation(target types.JID, labelID, messageID string, labele
177204
// BuildLabelMessage builds an app state patch for labeling or un(labeling) a message.
178205
func BuildLabelMessage(target types.JID, labelID, messageID string, labeled bool) PatchInfo {
179206
return PatchInfo{
180-
Type: WAPatchRegular,
207+
Type: WAPatchRegular,
208+
Operation: waServerSync.SyncdMutation_SET,
181209
Mutations: []MutationInfo{
182210
newLabelMessageMutation(target, labelID, messageID, labeled),
183211
},
@@ -201,7 +229,8 @@ func newLabelEditMutation(labelID string, labelName string, labelColor int32, de
201229
// BuildLabelEdit builds an app state patch for editing a label.
202230
func BuildLabelEdit(labelID string, labelName string, labelColor int32, deleted bool) PatchInfo {
203231
return PatchInfo{
204-
Type: WAPatchRegular,
232+
Type: WAPatchRegular,
233+
Operation: waServerSync.SyncdMutation_SET,
205234
Mutations: []MutationInfo{
206235
newLabelEditMutation(labelID, labelName, labelColor, deleted),
207236
},
@@ -223,7 +252,8 @@ func newSettingPushNameMutation(pushName string) MutationInfo {
223252
// BuildSettingPushName builds an app state patch for setting the push name.
224253
func BuildSettingPushName(pushName string) PatchInfo {
225254
return PatchInfo{
226-
Type: WAPatchCriticalBlock,
255+
Type: WAPatchCriticalBlock,
256+
Operation: waServerSync.SyncdMutation_SET,
227257
Mutations: []MutationInfo{
228258
newSettingPushNameMutation(pushName),
229259
},
@@ -253,7 +283,8 @@ func BuildStar(target, sender types.JID, messageID types.MessageID, fromMe, star
253283
senderJID = "0"
254284
}
255285
return PatchInfo{
256-
Type: WAPatchRegularHigh,
286+
Type: WAPatchRegularHigh,
287+
Operation: waServerSync.SyncdMutation_SET,
257288
Mutations: []MutationInfo{
258289
newStarMutation(targetJID, senderJID, messageID, isFromMe, starred),
259290
},
@@ -296,11 +327,11 @@ func (proc *Processor) EncodePatch(ctx context.Context, keyID []byte, state Hash
296327
return nil, fmt.Errorf("failed to encrypt mutation: %w", err)
297328
}
298329

299-
valueMac := generateContentMAC(waServerSync.SyncdMutation_SET, encryptedContent, keyID, keys.ValueMAC)
330+
valueMac := generateContentMAC(patchInfo.Operation, encryptedContent, keyID, keys.ValueMAC)
300331
indexMac := concatAndHMAC(sha256.New, keys.Index, indexBytes)
301332

302333
mutations = append(mutations, &waServerSync.SyncdMutation{
303-
Operation: waServerSync.SyncdMutation_SET.Enum(),
334+
Operation: patchInfo.Operation.Enum(),
304335
Record: &waServerSync.SyncdRecord{
305336
Index: &waServerSync.SyncdIndex{Blob: indexMac},
306337
Value: &waServerSync.SyncdValue{Blob: append(encryptedContent, valueMac...)},

0 commit comments

Comments
 (0)