/
sync.go
106 lines (95 loc) · 2.54 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
package internal
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
)
// LoadSyncData loads sync data from disk or by doing a /sync request
func LoadSyncData(hsURL, token, tempFile string) (json.RawMessage, error) {
syncData := loadDataFromDisk(tempFile)
if syncData != nil {
log.Printf("Loaded sync data from %s\n", tempFile)
return syncData, nil
}
// We need to do a remote hit to the homeserver
httpCli := &http.Client{}
filterStr := url.QueryEscape(`{"event_format":"federation", "room":{"timeline":{"limit":50}}}`)
attempts := 0
var body []byte
for attempts < 20 {
attempts++
// Perform the sync
log.Println("Performing /sync...")
filterReq, err := http.NewRequest("GET", hsURL+"/_matrix/client/v3/sync?filter="+filterStr, nil)
if err != nil {
log.Printf("failed to create sync request: %s\n", err)
continue
}
body, err = doRequest(httpCli, filterReq, token)
if err != nil {
log.Printf("failed to perform sync request: %s\n", err)
continue
}
break
}
if body == nil {
return nil, fmt.Errorf("failed to perform /sync")
}
// dump it straight to disk first
err := ioutil.WriteFile(tempFile, body, 0644)
if err != nil {
log.Printf("WARNING: failed to write sync data to disk: %s", err)
}
return body, nil
}
func loadDataFromDisk(tempFile string) json.RawMessage {
data, err := ioutil.ReadFile(tempFile)
if err != nil {
if os.IsNotExist(err) {
return nil
}
log.Panicf("FATAL: failed to read data from disk: %s", err)
}
return data
}
func doRequest(httpCli *http.Client, req *http.Request, token string) ([]byte, error) {
req.Header.Set("Authorization", "Bearer "+token)
res, err := httpCli.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to perform request: %w", err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("response returned %s", res.Status)
}
out := bytes.NewBuffer(nil)
// non-fatal if we have no content length
cl, _ := strconv.Atoi(res.Header.Get("Content-Length"))
counter := &writeCounter{
contentLength: cl,
}
if _, err = io.Copy(out, io.TeeReader(res.Body, counter)); err != nil {
return nil, err
}
return out.Bytes(), nil
}
type writeCounter struct {
contentLength int
total int
}
func (wc *writeCounter) Write(p []byte) (int, error) {
n := len(p)
wc.total += n
// wipe current line
fmt.Fprintf(os.Stderr, "\r%s", strings.Repeat(" ", 80))
fmt.Fprintf(os.Stderr, "\rDownloading... %d / %d KB complete", wc.total/1024, wc.contentLength/1024)
return n, nil
}