-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
148 lines (127 loc) · 3.92 KB
/
update.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
package main
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
)
func (fs *FeedServ) HandleFeedEvent(_ mautrix.EventSource, evt *event.Event) {
log := fs.Log.With().
Str("event_id", evt.ID.String()).
Str("sender", evt.Sender.String()).
Str("room_id", evt.RoomID.String()).
Str("action", "new message").
Logger()
feed, ok := fs.Config.feedsByRoomID[evt.RoomID]
if !ok {
log.Debug().Msg("Dropping event in feed without room")
return
}
log = log.With().Str("feed_id", feed.id).Logger()
log.Debug().Msg("Received new event in feed room")
feed.updateLock.Lock()
defer feed.updateLock.Unlock()
feed.pushEvent(log, evt)
fs.regenerateFeed(feed, log)
if err := fs.purgeCloudflareCache(feed); err != nil {
log.Error().Err(err).Msg("Failed to purge Cloudflare cache")
}
}
func (feed *FeedConfig) pushEvent(log zerolog.Logger, evt *event.Event) {
content := evt.Content.AsMessage()
if edits := content.RelatesTo.GetReplaceID(); edits != "" {
log = log.With().Str("edit_target_event_id", edits.String()).Logger()
existingEvt, found := feed.entries.Get(edits)
if !found {
log.Warn().Msg("Couldn't find edit target event")
return
} else if existingEvt.Sender != evt.Sender {
log.Warn().
Str("orig_sender", existingEvt.Sender.String()).
Msg("Dropping edit of message by different sender")
return
} else {
log.Info().
Str("original_event_id", existingEvt.ID.String()).
Msg("Overriding content of original event with edit")
existingEvt.Content.Parsed = content.NewContent
existingEvt.Type = evt.Type
existingEvt.Mautrix.EditedAt = time.UnixMilli(evt.Timestamp).UTC()
existingEvt.Mautrix.LastEditID = evt.ID
}
} else {
feed.entries.Push(evt.ID, evt)
}
}
func (fs *FeedServ) regenerateFeed(feed *FeedConfig, log zerolog.Logger) {
log.Debug().Msg("Regenerating feed")
start := time.Now()
oldJSONHash := feed.jsonHash
var err error
feed.json, feed.jsonHash, err = fs.generateJSONFeed(feed)
if err != nil {
log.Err(err).Msg("Failed to generate JSON feed")
return
}
gorillaFeed := fs.generateGorillaFeed(feed)
var buf bytes.Buffer
if err = gorillaFeed.WriteRss(&buf); err != nil {
log.Err(err).Msg("Failed to generate RSS feed")
} else {
feed.rss = buf.Bytes()
feed.rssHash = fmt.Sprintf(`"%x"`, sha256.Sum256(feed.rss))
}
buf = bytes.Buffer{}
if err = gorillaFeed.WriteAtom(&buf); err != nil {
log.Err(err).Msg("Failed to generate Atom feed")
} else {
feed.atom = buf.Bytes()
feed.atomHash = fmt.Sprintf(`"%x"`, sha256.Sum256(feed.atom))
}
feed.lastUpdate = time.Now().UTC()
log.Info().
Str("old_json_hash", oldJSONHash).
Str("new_json_hash", feed.jsonHash).
Int("item_count", feed.entries.Size()).
Dur("duration", time.Since(start)).
Msg("Feed updated successfully")
}
var cloudflareClient = &http.Client{Timeout: time.Second * 10}
type cloudflarePurgeRequest struct {
Files []string `json:"files"`
}
func (fs *FeedServ) purgeCloudflareCache(feed *FeedConfig) error {
if fs.Config.CloudflareToken == "" {
return nil
}
data := cloudflarePurgeRequest{[]string{
fs.Config.PublicURL + feed.id,
fs.Config.PublicURL + feed.id + ".json",
fs.Config.PublicURL + feed.id + ".rss",
fs.Config.PublicURL + feed.id + ".atom",
}}
body, err := json.Marshal(data)
if err != nil {
return err
}
url := "https://api.cloudflare.com/client/v4/zones/" + fs.Config.CloudflareZoneID + "/purge_cache"
log.Debug().Msgf("Calling Cloudflare purge: %s with body: %s", url, body)
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(body))
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+fs.Config.CloudflareToken)
resp, err := cloudflareClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
log.Debug().Int("status_code", resp.StatusCode).Msg("Got Cloudflare purge response")
return nil
}