From 8d0d5ff504d18922faae7f631b05db10d41d5905 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 23 May 2019 22:57:52 +0300 Subject: [PATCH] Switch sync command to use syncPortals and syncPuppets methods used at startup --- commands.go | 22 ++++++++++------------ user.go | 6 +++--- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/commands.go b/commands.go index 2665ddf..1fff5c0 100644 --- a/commands.go +++ b/commands.go @@ -294,21 +294,19 @@ func (handler *CommandHandler) CommandSync(ce *CommandEvent) { handler.log.Debugln("Importing all contacts of", user) _, err := user.Conn.Contacts() if err != nil { - handler.log.Errorln("Error on update of contacts of user", user, ":", err) + user.log.Errorln("Error updating contacts:", err) + ce.Reply("Failed to sync contact list (see logs for details)") + return + } + _, err = user.Conn.Chats() + if err != nil { + user.log.Errorln("Error updating chats:", err) + ce.Reply("Failed to sync chat list (see logs for details)") return } - for jid, contact := range user.Conn.Store.Contacts { - if strings.HasSuffix(jid, whatsappExt.NewUserSuffix) { - puppet := user.bridge.GetPuppetByJID(contact.Jid) - puppet.Sync(user, contact) - } else { - portal := user.bridge.GetPortalByJID(database.GroupPortalKey(contact.Jid)) - if len(portal.MXID) > 0 || create { - portal.Sync(user, contact) - } - } - } + user.syncPuppets() + user.syncPortals(create) ce.Reply("Imported contacts successfully.") } diff --git a/user.go b/user.go index ff09156..d4916bd 100644 --- a/user.go +++ b/user.go @@ -289,11 +289,11 @@ func (user *User) PostLogin() { user.log.Debugln("Waited a second, have", len(user.Conn.Store.Chats), "chats and", len(user.Conn.Store.Contacts), "contacts") go user.syncPuppets() - user.syncPortals() + user.syncPortals(false) user.syncLock.Unlock() } -func (user *User) syncPortals() { +func (user *User) syncPortals(createAll bool) { var chats ChatList for _, chat := range user.Conn.Store.Chats { ts, err := strconv.ParseUint(chat.LastMessageTime, 10, 64) @@ -318,7 +318,7 @@ func (user *User) syncPortals() { break } create := (chat.LastMessageTime >= user.LastConnection && user.LastConnection > 0) || i < limit - if len(chat.Portal.MXID) > 0 || create { + if len(chat.Portal.MXID) > 0 || create || createAll { chat.Portal.Sync(user, chat.Contact) err := chat.Portal.BackfillHistory(user, chat.LastMessageTime) if err != nil {