forked from tulir/mautrix-appservice-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
110 lines (93 loc) · 2.33 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
// Copyright (c) 2019 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package appservice
import (
"encoding/json"
"fmt"
"runtime/debug"
"time"
"maunium.net/go/mautrix"
)
type Syncer struct {
*AppService
}
func (as *Syncer) OnFailedSync(res *mautrix.RespSync, err error) (time.Duration, error) {
as.Log.Errorln("Sync errored:", err)
return 10 * time.Second, nil
}
func (as *Syncer) ProcessResponse(resp *mautrix.RespSync, since string) (err error) {
if since == "" {
return nil
}
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("ProcessResponse panicked! since=%s panic=%s\n%s", since, r, debug.Stack())
}
}()
for roomID, roomData := range resp.Rooms.Join {
for _, evt := range roomData.State.Events {
evt.RoomID = roomID
as.UpdateState(evt)
as.Events <- evt
}
for _, evt := range roomData.Timeline.Events {
evt.RoomID = roomID
as.Events <- evt
}
}
for roomID, roomData := range resp.Rooms.Invite {
for _, evt := range roomData.State.Events {
evt.RoomID = roomID
as.UpdateState(evt)
as.Events <- evt
}
}
for roomID, roomData := range resp.Rooms.Leave {
for _, evt := range roomData.Timeline.Events {
if evt.StateKey != nil {
evt.RoomID = roomID
as.UpdateState(evt)
as.Events <- evt
}
}
}
return
}
func (as *Syncer) GetFilterJSON(_ string) json.RawMessage {
return json.RawMessage(`{"room":{"timeline":{"limit":50}}}`)
}
type Store struct {
*AppService
}
func (as *Store) SaveFilterID(_, filterID string) {
as.Sync.FilterID = filterID
}
func (as *Store) LoadFilterID(_ string) string {
return as.Sync.FilterID
}
func (as *Store) SaveNextBatch(_, nextBatch string) {
as.Sync.NextBatch = nextBatch
}
func (as *Store) LoadNextBatch(_ string) string {
return as.Sync.NextBatch
}
func (as *Store) SaveRoom(_ *mautrix.Room) {}
func (as *Store) LoadRoom(roomID string) *mautrix.Room {
return nil
}
func (as *AppService) startSync() {
client := as.BotClient()
client.Syncer = &Syncer{as}
client.Store = &Store{as}
as.Log.Infoln("Starting syncing")
err := client.Sync()
if err != nil {
as.Log.Errorln("Sync returned error:", err)
}
}
func (as *AppService) stopSync() {
as.BotClient().StopSync()
}