/
backfill.go
288 lines (270 loc) · 9.11 KB
/
backfill.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
// mautrix-gmessages - A Matrix-Google Messages puppeting bridge.
// Copyright (C) 2023 Tulir Asokan
//
// 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"
"crypto/sha256"
"encoding/base64"
"fmt"
"time"
"github.com/rs/zerolog"
"go.mau.fi/util/random"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-gmessages/database"
)
func (portal *Portal) initialForwardBackfill(user *User, markRead, allowNotify bool) {
// This is only called from CreateMatrixRoom which locks forwardBackfillLock
defer portal.forwardBackfillLock.Unlock()
log := portal.zlog.With().
Str("action", "initial forward backfill").
Logger()
ctx := log.WithContext(context.TODO())
portal.forwardBackfill(ctx, user, time.Time{}, portal.bridge.Config.Bridge.Backfill.InitialLimit, markRead, allowNotify)
}
const recentBackfillDelay = 15 * time.Second
type pendingBackfill struct {
cancel context.CancelFunc
lastMessageID string
lastMessageTS time.Time
}
func (portal *Portal) missedForwardBackfill(ctx context.Context, user *User, lastMessageTS time.Time, lastMessageID string, markRead, markReadIfNoBackfill bool) {
if portal.bridge.Config.Bridge.Backfill.MissedLimit == 0 {
if markRead && markReadIfNoBackfill {
user.markSelfReadFull(ctx, portal, lastMessageID)
}
return
}
log := portal.zlog.With().
Str("action", "missed forward backfill").
Str("latest_message_id", lastMessageID).
Logger()
ctx = log.WithContext(ctx)
if portal.hasSyncedThisRun && !lastMessageTS.IsZero() && time.Since(lastMessageTS) < 5*time.Minute && portal.lastMessageTS.Before(lastMessageTS) {
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(ctx)
prev := portal.pendingRecentBackfill.Swap(&pendingBackfill{cancel: cancel, lastMessageID: lastMessageID, lastMessageTS: lastMessageTS})
if prev != nil {
prev.cancel()
}
log.Debug().Msg("Delaying missed forward backfill as latest message is new")
select {
case <-time.After(recentBackfillDelay):
case <-ctx.Done():
log.Debug().Msg("Backfill was cancelled by a newer backfill")
return
}
}
portal.forwardBackfillLock.Lock()
defer portal.forwardBackfillLock.Unlock()
portal.hasSyncedThisRun = true
if !lastMessageTS.IsZero() {
if portal.lastMessageTS.IsZero() {
lastMsg, err := portal.bridge.DB.Message.GetLastInChat(ctx, portal.Key)
if err != nil {
log.Err(err).Msg("Failed to get last message in chat")
return
} else if lastMsg == nil {
log.Debug().Msg("No messages in chat")
} else {
portal.lastMessageTS = lastMsg.Timestamp
}
}
if !lastMessageTS.After(portal.lastMessageTS) {
log.Trace().
Time("latest_message_ts", lastMessageTS).
Str("latest_message_id", lastMessageID).
Time("last_bridged_ts", portal.lastMessageTS).
Msg("Nothing to backfill")
if markRead && markReadIfNoBackfill {
user.markSelfReadFull(ctx, portal, lastMessageID)
}
return
}
}
log.Info().
Time("latest_message_ts", lastMessageTS).
Str("latest_message_id", lastMessageID).
Time("last_bridged_ts", portal.lastMessageTS).
Msg("Backfilling missed messages")
portal.forwardBackfill(ctx, user, portal.lastMessageTS, portal.bridge.Config.Bridge.Backfill.MissedLimit, markRead, true)
}
func (portal *Portal) deterministicEventID(messageID string, part int) id.EventID {
data := fmt.Sprintf("%s/gmessages/%s/%d", portal.MXID, messageID, part)
sum := sha256.Sum256([]byte(data))
return id.EventID(fmt.Sprintf("$%s:messages.google.com", base64.RawURLEncoding.EncodeToString(sum[:])))
}
func (portal *Portal) forwardBackfill(ctx context.Context, user *User, after time.Time, limit int, markRead, allowNotify bool) bool {
if limit == 0 {
return false
}
log := zerolog.Ctx(ctx)
resp, err := user.Client.FetchMessages(portal.ID, int64(limit), nil)
if err != nil {
portal.zlog.Error().Err(err).Msg("Failed to fetch messages")
return false
}
log.Debug().
Int64("total_messages", resp.TotalMessages).
Int("message_count", len(resp.Messages)).
Msg("Got message chunk to backfill")
batchSending := portal.bridge.SpecVersions.Supports(mautrix.BeeperFeatureBatchSending)
converted := make([]*ConvertedMessage, 0, len(resp.Messages))
maxTS := time.Time{}
for i := len(resp.Messages) - 1; i >= 0; i-- {
evt := resp.Messages[i]
isTooOld := !time.UnixMicro(evt.Timestamp).After(after)
if portal.handleExistingMessage(ctx, user, evt, isTooOld, nil) || isTooOld {
continue
}
c := portal.convertGoogleMessage(ctx, user, evt, batchSending, nil)
if c == nil {
continue
}
converted = append(converted, c)
if c.Timestamp.After(maxTS) {
maxTS = c.Timestamp
}
}
if len(converted) == 0 {
log.Debug().Msg("Didn't get any converted messages")
return false
}
log.Debug().
Int("converted_count", len(converted)).
Msg("Converted messages for backfill")
if batchSending {
var markReadBy id.UserID
if markRead {
markReadBy = user.MXID
}
allowNotify = allowNotify && time.Since(maxTS) < 24*time.Hour
portal.backfillSendBatch(ctx, converted, markReadBy, allowNotify)
} else {
lastEventID := portal.backfillSendLegacy(ctx, converted)
if markRead && user.DoublePuppetIntent != nil {
err = user.DoublePuppetIntent.MarkRead(ctx, portal.MXID, lastEventID)
if err != nil {
log.Err(err).Msg("Failed to mark room as read after backfill")
}
}
}
if maxTS.After(portal.lastMessageTS) {
portal.lastMessageTS = maxTS
}
return true
}
func (portal *Portal) backfillSendBatch(ctx context.Context, converted []*ConvertedMessage, markReadBy id.UserID, allowNotify bool) {
log := zerolog.Ctx(ctx)
events := make([]*event.Event, 0, len(converted))
dbMessages := make([]*database.Message, 0, len(converted))
for _, msg := range converted {
dbm := portal.bridge.DB.Message.New()
dbm.Chat = portal.Key
dbm.RoomID = portal.MXID
dbm.ID = msg.ID
dbm.Sender = msg.SenderID
dbm.Timestamp = msg.Timestamp
dbm.Status.Type = msg.Status
dbm.Status.PartCount = msg.PartCount
dbm.Status.MediaStatus = msg.MediaStatus
dbm.Status.MediaParts = make(map[string]database.MediaPart, len(msg.Parts))
if msg.DontBridge {
dbm.MXID = id.EventID(fmt.Sprintf("$fake::%s", random.String(37)))
dbMessages = append(dbMessages, dbm)
continue
}
for i, part := range msg.Parts {
content := event.Content{
Parsed: part.Content,
Raw: part.Extra,
}
eventType := event.EventMessage
var err error
eventType, err = portal.encrypt(ctx, msg.Intent, &content, eventType)
if err != nil {
log.Err(err).Str("message_id", msg.ID).Int("part", i).Msg("Failed to encrypt event")
continue
}
msg.Intent.AddDoublePuppetValue(&content)
evt := &event.Event{
Sender: msg.Intent.UserID,
Type: eventType,
Timestamp: msg.Timestamp.UnixMilli(),
ID: portal.deterministicEventID(msg.ID, i),
RoomID: portal.MXID,
Content: content,
}
events = append(events, evt)
if dbm.MXID == "" {
dbm.MXID = evt.ID
if part.PendingMedia {
dbm.Status.MediaParts[""] = database.MediaPart{PendingMedia: true}
}
} else {
dbm.Status.MediaParts[part.ID] = database.MediaPart{
EventID: evt.ID,
PendingMedia: part.PendingMedia,
}
}
}
if dbm.MXID != "" {
dbMessages = append(dbMessages, dbm)
}
}
if len(dbMessages) == 0 {
log.Warn().
Int("converted_count", len(converted)).
Int("event_count", len(events)).
Int("db_count", len(dbMessages)).
Msg("Didn't get any parts to send from converted messages")
return
}
forward := true
log.Debug().
Int("event_count", len(events)).
Bool("forward", forward).
Bool("mark_read", markReadBy != "").
Bool("notify", markReadBy == "").
Msg("Sending batch of messages")
_, err := portal.MainIntent().BeeperBatchSend(ctx, portal.MXID, &mautrix.ReqBeeperBatchSend{
Forward: forward,
MarkReadBy: markReadBy,
SendNotification: forward && markReadBy == "" && allowNotify,
Events: events,
})
if err != nil {
log.Err(err).Msg("Failed to send batch of messages")
return
}
err = portal.bridge.DB.Message.MassInsert(ctx, dbMessages)
if err != nil {
log.Err(err).Msg("Failed to insert messages to database")
}
}
func (portal *Portal) backfillSendLegacy(ctx context.Context, converted []*ConvertedMessage) id.EventID {
var lastEventID id.EventID
eventIDs := make(map[string]id.EventID)
for _, msg := range converted {
msgEventIDs := portal.sendMessageParts(ctx, msg, eventIDs)
if len(msgEventIDs) > 0 {
eventIDs[msg.ID] = msgEventIDs[0]
lastEventID = msgEventIDs[len(msgEventIDs)-1]
}
}
return lastEventID
}