Skip to content

Commit

Permalink
Remove holding for last message
Browse files Browse the repository at this point in the history
Fixed by removing portal.encrypt for tapbacks
  • Loading branch information
joshuafhiggins committed May 12, 2024
1 parent 850a0fb commit f396092
Showing 1 changed file with 7 additions and 49 deletions.
56 changes: 7 additions & 49 deletions historysync.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,17 @@ type messageIndex struct {
Index int
}

func (portal *Portal) convertBackfill(messages []*imessage.Message) ([]*event.Event, []messageWithIndex, map[messageIndex]int, bool, *imessage.Message, error) {
func (portal *Portal) convertBackfill(messages []*imessage.Message) ([]*event.Event, []messageWithIndex, map[messageIndex]int, bool, error) {
events := make([]*event.Event, 0, len(messages))
metas := make([]messageWithIndex, 0, len(messages))
metaIndexes := make(map[messageIndex]int, len(messages))
unreadThreshold := time.Duration(portal.bridge.Config.Bridge.Backfill.UnreadHoursThreshold) * time.Hour
var isRead bool
var lastMessage *imessage.Message
for _, msg := range messages {
if msg.Tapback != nil {
continue
}

//Skip the last message in the array, we will add it later to correct inbox sorting
if msg == messages[len(messages)-1] && len(messages) > 1 /* we call this function again with one element in the array for the last message, so we'll want to process it */ {
portal.log.Debugln("Skipping message", msg.GUID, "in backfill, last one in the convo")
lastMessage = msg
continue
}

intent := portal.getIntentForMessage(msg, nil)
converted := portal.convertiMessage(msg, intent)
for index, conv := range converted {
Expand All @@ -179,7 +171,7 @@ func (portal *Portal) convertBackfill(messages []*imessage.Message) ([]*event.Ev
var err error
evt.Type, err = portal.encrypt(intent, &evt.Content, evt.Type)
if err != nil {
return nil, nil, nil, false, nil, err
return nil, nil, nil, false, err
}
intent.AddDoublePuppetValue(&evt.Content)
if portal.bridge.Config.Homeserver.Software == bridgeconfig.SoftwareHungry {
Expand All @@ -192,29 +184,21 @@ func (portal *Portal) convertBackfill(messages []*imessage.Message) ([]*event.Ev
}
isRead = msg.IsRead || msg.IsFromMe || (unreadThreshold >= 0 && time.Since(msg.Time) > unreadThreshold)
}
return events, metas, metaIndexes, isRead, lastMessage, nil
return events, metas, metaIndexes, isRead, nil
}

func (portal *Portal) convertTapbacks(messages []*imessage.Message) ([]*event.Event, []messageWithIndex, map[messageIndex]int, bool, *imessage.Message, error) {
func (portal *Portal) convertTapbacks(messages []*imessage.Message) ([]*event.Event, []messageWithIndex, map[messageIndex]int, bool, error) {
events := make([]*event.Event, 0, len(messages))
metas := make([]messageWithIndex, 0, len(messages))
metaIndexes := make(map[messageIndex]int, len(messages))
unreadThreshold := time.Duration(portal.bridge.Config.Bridge.Backfill.UnreadHoursThreshold) * time.Hour
var isRead bool
var lastMessage *imessage.Message
for _, msg := range messages {
//Only want tapbacks
if msg.Tapback == nil {
continue
}

//Skip the last message in the array, we will add it later to correct inbox sorting
if msg == messages[len(messages)-1] && len(messages) > 1 /* we call this function again with one element in the array for the last message, so we'll want to process it */ {
portal.log.Debugln("Skipping message", msg.GUID, "in backfill, last one in the convo")
lastMessage = msg
continue
}

intent := portal.getIntentForMessage(msg, nil)
dbMessage := portal.bridge.DB.Message.GetByGUID(portal.GUID, msg.Tapback.TargetGUID, msg.Tapback.TargetPart)
if dbMessage == nil {
Expand All @@ -238,11 +222,6 @@ func (portal *Portal) convertTapbacks(messages []*imessage.Message) ([]*event.Ev
},
}

var err error
evt.Type, err = portal.encrypt(intent, &evt.Content, evt.Type)
if err != nil {
return nil, nil, nil, false, nil, err
}
intent.AddDoublePuppetValue(&evt.Content)
if portal.bridge.Config.Homeserver.Software == bridgeconfig.SoftwareHungry {
evt.ID = portal.deterministicEventID(msg.GUID, 0)
Expand All @@ -254,7 +233,7 @@ func (portal *Portal) convertTapbacks(messages []*imessage.Message) ([]*event.Ev

isRead = msg.IsRead || msg.IsFromMe || (unreadThreshold >= 0 && time.Since(msg.Time) > unreadThreshold)
}
return events, metas, metaIndexes, isRead, lastMessage, nil
return events, metas, metaIndexes, isRead, nil
}

func (portal *Portal) sendBackfill(backfillID string, messages []*imessage.Message, forward, forwardIfNoMessages, markAsRead bool) (success bool) {
Expand Down Expand Up @@ -294,7 +273,7 @@ func (portal *Portal) sendBackfill(backfillID string, messages []*imessage.Messa
validMessages = append(validMessages, msg)
}

events, metas, metaIndexes, isRead, lastMessage, err := portal.convertBackfill(validMessages)
events, metas, metaIndexes, isRead, err := portal.convertBackfill(validMessages)
if err != nil {
portal.log.Errorfln("Failed to convert messages for backfill: %v", err)
return false
Expand All @@ -311,7 +290,7 @@ func (portal *Portal) sendBackfill(backfillID string, messages []*imessage.Messa
portal.addBackfillToDB(metas, eventIDs, idMap, backfillID)

//We have to process tapbacks after all other messages because we need texts in the DB in order to target them
events, metas, metaIndexes, isRead, lastTapback, err := portal.convertTapbacks(validMessages)
events, metas, metaIndexes, isRead, err = portal.convertTapbacks(validMessages)
if err != nil {
portal.log.Errorfln("Failed to convert tapbacks for backfill: %v", err)
return false
Expand All @@ -327,27 +306,6 @@ func (portal *Portal) sendBackfill(backfillID string, messages []*imessage.Messa
}
portal.addBackfillToDB(metas, eventIDs, idMap, backfillID)

//Process the last message in the conversation that we skipped before in converting,
// this is dumb but Beeper sorts its inbox on when the event was recieved and not the timestamp
var lastMessageArray []*imessage.Message
if lastMessage != nil {
lastMessageArray = append(lastMessageArray, lastMessage)
events, metas, metaIndexes, isRead, _, err = portal.convertBackfill(lastMessageArray)
} else if lastTapback != nil {
lastMessageArray = append(lastMessageArray, lastTapback)
events, metas, metaIndexes, isRead, _, err = portal.convertTapbacks(lastMessageArray)
}
if err != nil {
portal.log.Errorfln("Failed to convert last message for backfill: %v", err)
return false
}

eventIDs, sendErr = portal.sendBackfillToMatrixServer(batchSending, forward, forwardIfNoMessages, markAsRead, isRead, events, metas, metaIndexes)
if sendErr != nil {
return false
}
portal.addBackfillToDB(metas, eventIDs, idMap, backfillID)

portal.log.Infofln("Finished backfill %s", backfillID)
return true
}
Expand Down

0 comments on commit f396092

Please sign in to comment.