Only invite default puppet for backfilling when needed

This commit is contained in:
Tulir Asokan 2019-05-30 17:48:22 +03:00
parent 521a8b74aa
commit 02f78155b5
3 changed files with 28 additions and 7 deletions

View file

@ -228,7 +228,7 @@ func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) {
ce.User.Connected = true ce.User.Connected = true
ce.User.ConnectionErrors = 0 ce.User.ConnectionErrors = 0
ce.Reply("Reconnected successfully.") ce.Reply("Reconnected successfully.")
go ce.User.PostLogin() ce.User.PostLogin()
} }
func (handler *CommandHandler) CommandDeleteConnection(ce *CommandEvent) { func (handler *CommandHandler) CommandDeleteConnection(ce *CommandEvent) {

View file

@ -142,6 +142,8 @@ type Portal struct {
backfilling bool backfilling bool
lastMessageTs uint64 lastMessageTs uint64
privateChatBackfillInvitePuppet func()
messages chan PortalMessage messages chan PortalMessage
isPrivate *bool isPrivate *bool
@ -572,16 +574,24 @@ func (portal *Portal) BackfillHistory(user *User, lastMessageTime uint64) error
func (portal *Portal) beginBackfill() func() { func (portal *Portal) beginBackfill() func() {
portal.backfillLock.Lock() portal.backfillLock.Lock()
portal.backfilling = true portal.backfilling = true
var privateChatPuppetInvited bool
var privateChatPuppet *Puppet var privateChatPuppet *Puppet
if portal.IsPrivateChat() { if portal.IsPrivateChat() {
privateChatPuppet = portal.bridge.GetPuppetByJID(portal.Key.Receiver) privateChatPuppet = portal.bridge.GetPuppetByJID(portal.Key.Receiver)
portal.privateChatBackfillInvitePuppet = func() {
if privateChatPuppetInvited {
return
}
privateChatPuppetInvited = true
_, _ = portal.MainIntent().InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: privateChatPuppet.MXID}) _, _ = portal.MainIntent().InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: privateChatPuppet.MXID})
_ = privateChatPuppet.DefaultIntent().EnsureJoined(portal.MXID) _ = privateChatPuppet.DefaultIntent().EnsureJoined(portal.MXID)
} }
}
return func() { return func() {
portal.backfilling = false portal.backfilling = false
portal.privateChatBackfillInvitePuppet = nil
portal.backfillLock.Unlock() portal.backfillLock.Unlock()
if privateChatPuppet != nil { if privateChatPuppet != nil && privateChatPuppetInvited {
_, _ = privateChatPuppet.DefaultIntent().LeaveRoom(portal.MXID) _, _ = privateChatPuppet.DefaultIntent().LeaveRoom(portal.MXID)
} }
} }
@ -593,6 +603,9 @@ func (portal *Portal) FillInitialHistory(user *User) error {
} }
endBackfill := portal.beginBackfill() endBackfill := portal.beginBackfill()
defer endBackfill() defer endBackfill()
if portal.privateChatBackfillInvitePuppet != nil {
portal.privateChatBackfillInvitePuppet()
}
n := portal.bridge.Config.Bridge.InitialHistoryFill n := portal.bridge.Config.Bridge.InitialHistoryFill
portal.log.Infoln("Filling initial history, maximum", n, "messages") portal.log.Infoln("Filling initial history, maximum", n, "messages")
@ -632,6 +645,7 @@ func (portal *Portal) FillInitialHistory(user *User) error {
} }
} }
portal.handleHistory(user, messages) portal.handleHistory(user, messages)
portal.log.Infoln("Initial history fill complete")
return nil return nil
} }
@ -644,6 +658,9 @@ func (portal *Portal) handleHistory(user *User, messages []interface{}) {
continue continue
} }
data := whatsapp.ParseProtoMessage(message) data := whatsapp.ParseProtoMessage(message)
if portal.privateChatBackfillInvitePuppet != nil && message.GetKey().GetFromMe() && portal.IsPrivateChat() {
portal.privateChatBackfillInvitePuppet()
}
portal.handleMessage(PortalMessage{portal.Key.JID, user, data, message.GetMessageTimestamp()}) portal.handleMessage(PortalMessage{portal.Key.JID, user, data, message.GetMessageTimestamp()})
} }
} }

10
user.go
View file

@ -210,7 +210,7 @@ func (user *User) RestoreSession() bool {
user.ConnectionErrors = 0 user.ConnectionErrors = 0
user.SetSession(&sess) user.SetSession(&sess)
user.log.Debugln("Session restored successfully") user.log.Debugln("Session restored successfully")
go user.PostLogin() user.PostLogin()
} }
return true return true
} }
@ -267,7 +267,7 @@ func (user *User) Login(ce *CommandEvent) {
user.JID = strings.Replace(user.Conn.Info.Wid, whatsappExt.OldUserSuffix, whatsappExt.NewUserSuffix, 1) user.JID = strings.Replace(user.Conn.Info.Wid, whatsappExt.OldUserSuffix, whatsappExt.NewUserSuffix, 1)
user.SetSession(&session) user.SetSession(&session)
ce.Reply("Successfully logged in, synchronizing chats...") ce.Reply("Successfully logged in, synchronizing chats...")
go user.PostLogin() user.PostLogin()
} }
type Chat struct { type Chat struct {
@ -292,6 +292,10 @@ func (cl ChatList) Swap(i, j int) {
func (user *User) PostLogin() { func (user *User) PostLogin() {
user.syncLock.Lock() user.syncLock.Lock()
go user.intPostLogin()
}
func (user *User) intPostLogin() {
user.log.Debugln("Waiting a second for contacts to arrive") user.log.Debugln("Waiting a second for contacts to arrive")
// Hacky way to wait for chats and contacts to arrive automatically // Hacky way to wait for chats and contacts to arrive automatically
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
@ -404,7 +408,7 @@ func (user *User) tryReconnect(msg string) {
user.ConnectionErrors = 0 user.ConnectionErrors = 0
user.Connected = true user.Connected = true
_, _ = user.bridge.Bot.SendNotice(user.ManagementRoom, "Reconnected successfully") _, _ = user.bridge.Bot.SendNotice(user.ManagementRoom, "Reconnected successfully")
go user.PostLogin() user.PostLogin()
return return
} }
user.log.Errorln("Error while trying to reconnect after disconnection:", err) user.log.Errorln("Error while trying to reconnect after disconnection:", err)