-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sync.go
128 lines (118 loc) · 3.92 KB
/
sync.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
// mautrix-syncproxy - A /sync proxy for encrypted Matrix appservices.
// Copyright (C) 2021 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"
"errors"
"fmt"
"time"
"maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/appservice"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
var everything = []event.Type{{Type: "*"}}
var nothing = mautrix.FilterPart{NotTypes: everything}
var syncFilter = &mautrix.Filter{
Presence: nothing,
AccountData: nothing,
Room: mautrix.RoomFilter{
IncludeLeave: false,
Ephemeral: nothing,
AccountData: nothing,
State: nothing,
Timeline: nothing,
},
}
const initialSyncRetrySleep = 2 * time.Second
const maxSyncRetryInterval = 120 * time.Second
func (target *SyncTarget) sync(ctx context.Context) error {
var filterID string
if resp, err := target.client.CreateFilter(syncFilter); err != nil {
return fmt.Errorf("failed to create filter: %w", err)
} else {
filterID = resp.FilterID
}
var otkCountSent bool
var prevOTKCount mautrix.OTKCount
syncLog := ctx.Value(logContextKey).(maulogger.Logger)
retryIn := initialSyncRetrySleep
for {
resp, err := target.client.SyncRequest(30000, target.NextBatch, filterID, false, event.PresenceOffline, ctx)
if err != nil {
if errors.Is(err, mautrix.MUnknownToken) {
return err
} else if ctx.Err() != nil {
if err != ctx.Err() {
syncLog.Debugfln("Sync returned error %v, but context had different error %v", err, ctx.Err())
}
return ctx.Err()
}
syncLog.Warnfln("Error syncing: %v. Retrying in %v", err, retryIn)
select {
case <-time.After(retryIn):
case <-ctx.Done():
syncLog.Debugfln("Context returned error while waiting to retry sync")
return ctx.Err()
}
retryIn *= 2
if retryIn > maxSyncRetryInterval {
retryIn = maxSyncRetryInterval
}
continue
}
retryIn = initialTransactionRetrySleep
if len(resp.ToDevice.Events) > 0 || resp.DeviceOTKCount != prevOTKCount || !otkCountSent || len(resp.DeviceLists.Changed) > 0 {
txn := syncToTransaction(resp, target.UserID, target.DeviceID, resp.DeviceOTKCount != prevOTKCount || !otkCountSent)
prevOTKCount = resp.DeviceOTKCount
otkCountSent = true
err = target.tryPostTransaction(ctx, txn, nil)
if err != nil {
return fmt.Errorf("error sending transaction: %w", err)
}
}
syncLog.Debugln("Storing new next batch token:", resp.NextBatch)
err = target.SetNextBatch(resp.NextBatch)
if err != nil {
syncLog.Warnln("Failed to store next batch in database:", err)
}
}
}
func syncToTransaction(resp *mautrix.RespSync, userID id.UserID, deviceID id.DeviceID, sendOTKs bool) *appservice.Transaction {
var txn appservice.Transaction
if resp != nil {
if len(resp.ToDevice.Events) > 0 {
txn.EphemeralEvents = resp.ToDevice.Events
txn.MSC2409EphemeralEvents = txn.EphemeralEvents
for _, evt := range txn.EphemeralEvents {
evt.ToUserID = userID
evt.ToDeviceID = deviceID
}
}
if len(resp.DeviceLists.Changed) > 0 || len(resp.DeviceLists.Left) > 0 {
txn.DeviceLists = &resp.DeviceLists
txn.MSC3202DeviceLists = txn.DeviceLists
}
if sendOTKs {
txn.DeviceOTKCount = map[id.UserID]mautrix.OTKCount{
userID: resp.DeviceOTKCount,
}
txn.MSC3202DeviceOTKCount = txn.DeviceOTKCount
}
}
return &txn
}