forked from mautrix/imessage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackfillqueue.go
332 lines (296 loc) · 11 KB
/
backfillqueue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// 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"
"errors"
"fmt"
"time"
"github.com/rs/zerolog"
"go.mau.fi/util/dbutil"
log "maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-imessage/database"
"go.mau.fi/mautrix-imessage/imessage"
)
type BackfillQueue struct {
BackfillQuery *database.BackfillQuery
reCheckChannel chan bool
log log.Logger
}
func (bq *BackfillQueue) ReCheck() {
bq.log.Infofln("Sending re-checks to channel")
go func() {
bq.reCheckChannel <- true
}()
}
func (bq *BackfillQueue) GetNextBackfill(userID id.UserID) *database.Backfill {
for {
if backfill := bq.BackfillQuery.GetNext(userID); backfill != nil {
backfill.MarkDispatched()
return backfill
}
select {
case <-bq.reCheckChannel:
case <-time.After(time.Minute):
}
}
}
func (user *User) HandleBackfillRequestsLoop(ctx context.Context) {
log := zerolog.Ctx(ctx).With().Str("component", "backfill_requests_loop").Logger()
for {
if count, err := user.bridge.DB.Backfill.Count(ctx, user.MXID); err != nil {
user.setBackfillError(log, err, "Failed to get the number of backfills")
return
} else if incompleteCount, err := user.bridge.DB.Backfill.IncompleteCount(ctx, user.MXID); err != nil {
user.setBackfillError(log, err, "Failed to get the number of incomplete backfills")
return
} else if count > 0 && incompleteCount == 0 {
log.Info().
Int("num_backfills", count).
Msg("No incomplete backfills, setting status to done")
user.setBackfillDone(log)
time.Sleep(5 * time.Second)
continue
}
log.Info().Msg("Getting backfill request")
req := user.BackfillQueue.GetNextBackfill(user.MXID)
log.Info().Interface("req", req).Msg("Handling backfill request")
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)
}
}
}
type BackfillStatus string
const (
BackfillStatusUnknown BackfillStatus = "unknown"
BackfillStatusLockedByAnotherDevice BackfillStatus = "locked_by_another_device"
BackfillStatusRunning BackfillStatus = "running"
BackfillStatusError BackfillStatus = "error"
BackfillStatusDone BackfillStatus = "done"
)
type BackfillInfo struct {
Status BackfillStatus `json:"status"`
Error string `json:"error,omitempty"`
}
func (user *User) GetBackfillInfo() BackfillInfo {
backfillInfo := BackfillInfo{Status: BackfillStatusUnknown}
if user.backfillStatus != "" {
backfillInfo.Status = user.backfillStatus
}
if user.backfillError != nil {
backfillInfo.Error = user.backfillError.Error()
}
return backfillInfo
}
func (user *User) setBackfillError(log zerolog.Logger, err error, msg string) {
log.Err(err).Msg(msg)
user.backfillStatus = BackfillStatusError
user.backfillError = fmt.Errorf("%s: %w", msg, err)
}
type BackfillStateAccountData struct {
DeviceID id.DeviceID `json:"device_id"`
Done bool `json:"done"`
}
func (user *User) setBackfillDone(log zerolog.Logger) {
log.Info().
Str("device_id", user.bridge.Config.Bridge.DeviceID).
Msg("Setting backfill state account data to done")
err := user.bridge.Bot.SetAccountData("fi.mau.imessage.backfill_state", &BackfillStateAccountData{
DeviceID: id.DeviceID(user.bridge.Config.Bridge.DeviceID),
Done: true,
})
if err != nil {
user.setBackfillError(log, err, "failed to set backfill state account data")
return
}
user.backfillStatus = BackfillStatusDone
}
func (user *User) runOnlyBackfillMode() {
log := user.bridge.ZLog.With().Str("mode", "backfill_only").Logger()
ctx := log.WithContext(context.Background())
// Start the backfill queue. We always want this running so that the
// desktop app can request the backfill status.
user.handleHistorySyncsLoop(ctx)
if !user.bridge.SpecVersions.Supports(mautrix.BeeperFeatureBatchSending) {
user.setBackfillError(log, nil, "The homeserver does not support Beeper's batch send endpoint")
return
}
if user.bridge.Config.Bridge.DeviceID == "" {
user.setBackfillError(log, nil, "No device ID set in the config")
return
}
var backfillState BackfillStateAccountData
err := user.bridge.Bot.GetAccountData("fi.mau.imessage.backfill_state", &backfillState)
if err != nil {
if !errors.Is(err, mautrix.MNotFound) {
user.setBackfillError(log, err, "Error fetching backfill state account data")
return
}
} else if backfillState.DeviceID.String() != user.bridge.Config.Bridge.DeviceID {
user.backfillStatus = BackfillStatusLockedByAnotherDevice
log.Warn().
Str("device_id", backfillState.DeviceID.String()).
Msg("Backfill already locked for a different device")
return
} else if backfillState.Done {
log.Info().
Str("device_id", backfillState.DeviceID.String()).
Msg("Backfill already completed")
user.backfillStatus = BackfillStatusDone
return
}
if count, err := user.bridge.DB.Backfill.Count(ctx, user.MXID); err != nil {
user.setBackfillError(log, err, "Failed to get the number of backfills")
return
} else if incompleteCount, err := user.bridge.DB.Backfill.IncompleteCount(ctx, user.MXID); err != nil {
user.setBackfillError(log, err, "Failed to get the number of incomplete backfills")
return
} else if count > 0 && incompleteCount == 0 {
log.Info().
Int("num_backfills", count).
Msg("No incomplete backfills, setting status to done")
user.setBackfillDone(log)
return
} else {
err = user.bridge.Crypto.ShareKeys(context.Background())
if err != nil {
user.setBackfillError(log, err, "Error sharing keys")
}
err = user.bridge.Bot.SetAccountData("fi.mau.imessage.backfill_state", &BackfillStateAccountData{
DeviceID: id.DeviceID(user.bridge.Config.Bridge.DeviceID),
Done: false,
})
if err != nil {
user.setBackfillError(log, err, "failed to set backfill state account data")
return
}
user.backfillStatus = BackfillStatusRunning
if count == 0 {
log.Info().Msg("Starting backfill")
user.getRoomsForBackfillAndEnqueue(ctx)
} else {
log.Info().
Int("num_backfills", count).
Int("num_incomplete_backfills", incompleteCount).
Msg("Resuming backfill")
// No need to do anything else because the history syncs loop is
// already running
}
}
}
func (user *User) getRoomsForBackfillAndEnqueue(ctx context.Context) {
log := zerolog.Ctx(ctx).With().Str("method", "getRoomsForBackfillAndEnqueue").Logger()
// Get every chat from the database
chats, err := user.bridge.IM.GetChatsWithMessagesAfter(imessage.AppleEpoch)
if err != nil {
user.setBackfillError(log, err, "Error retrieving all chats")
return
}
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 {
user.setBackfillError(log, err,
fmt.Sprintf("Error getting chat info for %s from database", chat.ChatGUID))
return
}
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 {
user.setBackfillError(log, err, "Error starting creation of backfill rooms")
return
}
// Wait for the rooms to be created.
var roomInfoResp 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,
}, &roomInfoResp)
if err != nil {
user.setBackfillError(log, err, "Error requesting backfill room info")
return
}
if roomInfoResp.Status == RoomCreationForBackfillStatusDone {
break
} else if roomInfoResp.Status == RoomCreationForBackfillStatusError {
user.setBackfillError(log, fmt.Errorf(roomInfoResp.Error), "Error requesting backfill room IDs")
return
} else if roomInfoResp.Status == RoomCreationForBackfillStatusInProgress {
log.Info().Msg("Backfill room creation still in progress, waiting 5 seconds")
time.Sleep(5 * time.Second)
} else {
user.setBackfillError(log, fmt.Errorf("Unknown status %s", roomInfoResp.Status), "Error requesting backfill room IDs")
return
}
}
// Create all of the portals locally and enqueue backfill requests for
// all of them.
txn, err := user.bridge.DB.Begin()
{
portals := []*Portal{}
var i int
for _, chatIdentifier := range chats {
roomInfo := roomInfoResp.Rooms[chatIdentifier.ChatGUID]
portal := user.bridge.GetPortalByGUIDWithTransaction(txn, chatIdentifier.ChatGUID)
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 {
user.setBackfillError(log, err, "Error committing backfill room IDs")
return
}
}