Split Merge into functions

This commit is contained in:
Andrea Maria Piana 2020-12-03 18:04:38 +01:00
parent 156c0de832
commit 14f4c40404
1 changed files with 15 additions and 4 deletions

View File

@ -18,12 +18,22 @@ func (m *MessengerResponse) IsEmpty() bool {
return len(m.Chats) == 0 && len(m.Messages) == 0 && len(m.Contacts) == 0 && len(m.Installations) == 0 && len(m.Invitations) == 0
}
// Merge takes another response and appends the new Chats & new Messages and replaces
// the existing Messages & Chats if they have the same ID
func (m *MessengerResponse) Merge(response *MessengerResponse) error {
if len(response.Contacts)+len(response.Installations)+len(response.EmojiReactions)+len(response.Invitations) != 0 {
return ErrNotImplemented
}
for _, overrideChat := range response.Chats {
m.overrideChats(response.Chats)
m.overrideMessages(response.Messages)
return nil
}
// overrideChats append new chats and override existing ones in response.Chats
func (m *MessengerResponse) overrideChats(chats []*Chat) {
for _, overrideChat := range chats {
var found = false
for idx, chat := range m.Chats {
if chat.ID == overrideChat.ID {
@ -35,8 +45,11 @@ func (m *MessengerResponse) Merge(response *MessengerResponse) error {
m.Chats = append(m.Chats, overrideChat)
}
}
}
for _, overrideMessage := range response.Messages {
// overrideMessages append new messages and override existing ones in response.Messages
func (m *MessengerResponse) overrideMessages(messages []*common.Message) {
for _, overrideMessage := range messages {
var found = false
for idx, chat := range m.Messages {
if chat.ID == overrideMessage.ID {
@ -48,6 +61,4 @@ func (m *MessengerResponse) Merge(response *MessengerResponse) error {
m.Messages = append(m.Messages, overrideMessage)
}
}
return nil
}