-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sumner Evans <sumner@beeper.com>
- Loading branch information
1 parent
1af5233
commit 8502167
Showing
9 changed files
with
383 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
// mautrix-imessage - A Matrix-iMessage puppeting bridge. | ||
// Copyright (C) 2023 Tulir Asokan, Sumner Evans | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
log "maunium.net/go/maulogger/v2" | ||
"maunium.net/go/mautrix" | ||
"maunium.net/go/mautrix/id" | ||
"maunium.net/go/mautrix/util/dbutil" | ||
|
||
"go.mau.fi/mautrix-imessage/database" | ||
"go.mau.fi/mautrix-imessage/imessage" | ||
) | ||
|
||
type BackfillQueue struct { | ||
BackfillQuery *database.BackfillQuery | ||
reCheckChannels []chan bool | ||
log log.Logger | ||
} | ||
|
||
func (bq *BackfillQueue) ReCheck() { | ||
bq.log.Infofln("Sending re-checks to %d channels", len(bq.reCheckChannels)) | ||
for _, channel := range bq.reCheckChannels { | ||
go func(c chan bool) { | ||
c <- true | ||
}(channel) | ||
} | ||
} | ||
|
||
func (bq *BackfillQueue) GetNextBackfill(userID id.UserID, reCheckChannel chan bool) *database.Backfill { | ||
for { | ||
if backfill := bq.BackfillQuery.GetNext(userID); backfill != nil { | ||
backfill.MarkDispatched() | ||
return backfill | ||
} | ||
|
||
select { | ||
case <-reCheckChannel: | ||
case <-time.After(time.Minute): | ||
} | ||
} | ||
} | ||
|
||
func (user *User) HandleBackfillRequestsLoop() { | ||
reCheckChannel := make(chan bool) | ||
user.BackfillQueue.reCheckChannels = append(user.BackfillQueue.reCheckChannels, reCheckChannel) | ||
|
||
for { | ||
user.log.Infofln("Getting backfill request") | ||
req := user.BackfillQueue.GetNextBackfill(user.MXID, reCheckChannel) | ||
user.log.Infofln("Handling backfill request %s", req) | ||
|
||
portal := user.bridge.GetPortalByGUID(req.PortalGUID) | ||
user.backfillInChunks(req, portal) | ||
req.MarkDone() | ||
} | ||
} | ||
|
||
func (user *User) EnqueueImmediateBackfill(txn dbutil.Execable, priority int, portal *Portal, earliestBridgedTimestamp time.Time) { | ||
maxMessages := user.bridge.Config.Bridge.Backfill.Immediate.MaxEvents | ||
initialBackfill := user.bridge.DB.Backfill.NewWithValues(user.MXID, priority, portal.GUID, nil, &earliestBridgedTimestamp, maxMessages, maxMessages, 0) | ||
initialBackfill.Insert(txn) | ||
} | ||
|
||
func (user *User) EnqueueDeferredBackfills(txn dbutil.Execable, portals []*Portal, startIdx int) { | ||
now := time.Now() | ||
numPortals := len(portals) | ||
for stageIdx, backfillStage := range user.bridge.Config.Bridge.Backfill.Deferred { | ||
for portalIdx, portal := range portals { | ||
var startDate *time.Time = nil | ||
if backfillStage.StartDaysAgo > 0 { | ||
startDaysAgo := now.AddDate(0, 0, -backfillStage.StartDaysAgo) | ||
startDate = &startDaysAgo | ||
} | ||
backfillMessages := user.bridge.DB.Backfill.NewWithValues( | ||
user.MXID, startIdx+stageIdx*numPortals+portalIdx, portal.GUID, startDate, nil, backfillStage.MaxBatchEvents, -1, backfillStage.BatchDelay) | ||
backfillMessages.Insert(txn) | ||
} | ||
} | ||
} | ||
|
||
func (user *User) runOnlyBackfillMode() { | ||
log := user.bridge.ZLog.With().Str("mode", "backfill_only").Logger() | ||
ctx := log.WithContext(context.Background()) | ||
|
||
err := user.bridge.Crypto.ShareOneTimeKeys(context.Background()) | ||
if err != nil { | ||
// TODO do something more intelligent here | ||
log.Fatal().Err(err).Msg("Error sharing keys") | ||
} | ||
|
||
if !user.bridge.SpecVersions.Supports(mautrix.BeeperFeatureBatchSending) { | ||
log.Fatal().Msg("The homeserver does not support Beeper's batch send endpoint, are you sure you connected to the correct endpoint?") | ||
} | ||
|
||
// TODO exit with a specific code if there's nothing more to backfill | ||
|
||
// Start the backfill queue | ||
user.handleHistorySyncsLoop() | ||
|
||
if count, err := user.bridge.DB.Backfill.Count(ctx); err != nil { | ||
log.Fatal().Err(err).Msg("Error retrieving backfill queue count") | ||
} else if count > 0 { | ||
// If there are already items in the backfill queue, then we just need to | ||
// run the backfill queue. | ||
return | ||
} | ||
|
||
// Get every chat from the database | ||
chats, err := user.bridge.IM.GetChatsWithMessagesAfter(imessage.AppleEpoch) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Error retrieving all chats") | ||
} | ||
|
||
chatGUIDs := make([]string, len(chats)) | ||
chatInfos := make([]*imessage.ChatInfo, len(chats)) | ||
for i, chat := range chats { | ||
chatGUIDs[i] = chat.ChatGUID | ||
chatInfos[i], err = user.bridge.IM.GetChatInfo(chat.ChatGUID, chat.ThreadID) | ||
if err != nil { | ||
log.Fatal().Err(err).Str("chat_guid", chat.ChatGUID).Msg("Error getting chat info from database") | ||
} | ||
chatInfos[i].JSONChatGUID = chatInfos[i].Identifier.String() | ||
} | ||
|
||
// Ask the cloud bridge to create room IDs for every one of the chats. | ||
client := user.CustomIntent().Client | ||
url := client.BuildURL(mautrix.BaseURLPath{ | ||
"_matrix", "asmux", "mxauth", "appservice", user.MXID.Localpart(), "imessagecloud", | ||
"exec", "create_rooms_for_backfill"}) | ||
_, err = client.MakeRequest("POST", url, NewRoomBackfillRequest{ | ||
Chats: chatInfos, | ||
}, nil) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Error starting creation of backfill rooms") | ||
} | ||
|
||
// Wait for the rooms to be created. | ||
var resp RoomInfoForBackfillResponse | ||
for { | ||
url = client.BuildURL(mautrix.BaseURLPath{ | ||
"_matrix", "asmux", "mxauth", "appservice", user.MXID.Localpart(), "imessagecloud", | ||
"exec", "get_room_info_for_backfill"}) | ||
_, err = client.MakeRequest("POST", url, RoomInfoForBackfillRequest{ | ||
ChatGUIDs: chatGUIDs, | ||
}, &resp) | ||
if err != nil { | ||
// TODO do something more intelligent here | ||
log.Fatal().Err(err).Msg("Error requesting backfill room info") | ||
} | ||
|
||
if resp.Status == RoomCreationForBackfillStatusDone { | ||
break | ||
} else if resp.Status == RoomCreationForBackfillStatusError { | ||
// TODO do something more intelligent here | ||
log.Fatal().Str("error", resp.Error).Msg("Error requesting backfill room IDs") | ||
} else if resp.Status == RoomCreationForBackfillStatusInProgress { | ||
log.Info().Msg("Backfill room creation still in progress, waiting 5 seconds") | ||
time.Sleep(5 * time.Second) | ||
} else { | ||
// TODO do something more intelligent here | ||
log.Fatal().Err(err).Str("status", string(resp.Status)).Msg("Unknown status") | ||
} | ||
} | ||
|
||
// Create all of the portals locally and enqueue backfill requests for | ||
// all of them. | ||
portals := []*Portal{} | ||
txn, err := user.bridge.DB.Begin() | ||
{ | ||
var i int | ||
for guid, roomInfo := range resp.Rooms { | ||
portal := user.bridge.GetPortalByGUIDWithTransaction(txn, guid) | ||
portal.MXID = roomInfo.RoomID | ||
portal.Update(txn) | ||
portals = append(portals, portal) | ||
user.EnqueueImmediateBackfill(txn, i, portal, time.UnixMilli(roomInfo.EarliestBridgedTimestamp)) | ||
i++ | ||
} | ||
user.EnqueueDeferredBackfills(txn, portals, i) | ||
} | ||
if err = txn.Commit(); err != nil { | ||
// TODO do something intelligent here | ||
log.Fatal().Err(err).Msg("Error committing backfill room IDs") | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.