Share backfill start/end code between initial and recovery fills

This commit is contained in:
Tulir Asokan 2019-05-30 17:25:56 +03:00
parent c4751f4953
commit 521a8b74aa

View File

@ -530,12 +530,10 @@ func (portal *Portal) BackfillHistory(user *User, lastMessageTime uint64) error
if !portal.bridge.Config.Bridge.RecoverHistory { if !portal.bridge.Config.Bridge.RecoverHistory {
return nil return nil
} }
portal.backfillLock.Lock()
portal.backfilling = true endBackfill := portal.beginBackfill()
defer func() { defer endBackfill()
portal.backfilling = false
portal.backfillLock.Unlock()
}()
lastMessage := portal.bridge.DB.Message.GetLastInChat(portal.Key) lastMessage := portal.bridge.DB.Message.GetLastInChat(portal.Key)
if lastMessage == nil { if lastMessage == nil {
return nil return nil
@ -571,22 +569,31 @@ func (portal *Portal) BackfillHistory(user *User, lastMessageTime uint64) error
return nil return nil
} }
func (portal *Portal) FillInitialHistory(user *User) error { func (portal *Portal) beginBackfill() func() {
if portal.bridge.Config.Bridge.InitialHistoryFill == 0 {
return nil
}
portal.backfillLock.Lock() portal.backfillLock.Lock()
portal.backfilling = true portal.backfilling = true
defer func() {
portal.backfilling = false
portal.backfillLock.Unlock()
}()
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.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() {
portal.backfilling = false
portal.backfillLock.Unlock()
if privateChatPuppet != nil {
_, _ = privateChatPuppet.DefaultIntent().LeaveRoom(portal.MXID)
}
}
}
func (portal *Portal) FillInitialHistory(user *User) error {
if portal.bridge.Config.Bridge.InitialHistoryFill == 0 {
return nil
}
endBackfill := portal.beginBackfill()
defer endBackfill()
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")
var messages []interface{} var messages []interface{}
@ -625,9 +632,6 @@ func (portal *Portal) FillInitialHistory(user *User) error {
} }
} }
portal.handleHistory(user, messages) portal.handleHistory(user, messages)
if privateChatPuppet != nil {
_, _ = privateChatPuppet.DefaultIntent().LeaveRoom(portal.MXID)
}
return nil return nil
} }