-
Notifications
You must be signed in to change notification settings - Fork 11
/
hubbub.go
93 lines (76 loc) · 2.18 KB
/
hubbub.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
package api
import (
"fmt"
"net/http"
"path"
"strconv"
"time"
"github.com/urandom/readeef/content"
"github.com/urandom/readeef/content/repo"
"github.com/urandom/readeef/log"
"github.com/urandom/readeef/parser"
"github.com/urandom/readeef/pool"
)
func hubbubRegistration(
service repo.Service,
log log.Log,
) http.HandlerFunc {
feedRepo := service.FeedRepo()
subRepo := service.SubscriptionRepo()
return func(w http.ResponseWriter, r *http.Request) {
feedID, err := strconv.ParseInt(path.Base(r.URL.Path), 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
f, err := feedRepo.Get(content.FeedID(feedID), content.User{})
if err != nil {
fatal(w, log, fmt.Sprintf("Error getting feed %d", feedID)+": %+v", err)
return
}
s, err := subRepo.Get(f)
if err != nil {
fatal(w, log, "Error getting feed subscription: %+v", err)
return
}
log.Infoln("Receiving hubbub event " + r.Form.Get("hub.mode") + " for " + f.String())
switch r.Form.Get("hub.mode") {
case "subscribe":
if lease, err := strconv.Atoi(r.Form.Get("hub.lease_seconds")); err == nil {
s.LeaseDuration = int64(lease) * int64(time.Second)
}
s.VerificationTime = time.Now()
s.SubscriptionFailure = false
w.Write([]byte(r.Form.Get("hub.challenge")))
case "unsubscribe":
s.SubscriptionFailure = true
w.Write([]byte(r.Form.Get("hub.challenge")))
case "denied":
w.Write([]byte{})
log.Printf("Unable to subscribe to '%s': %s\n", r.Form.Get("hub.topic"), r.Form.Get("hub.reason"))
default:
w.Write([]byte{})
buf := pool.Buffer.Get()
defer pool.Buffer.Put(buf)
if _, err := buf.ReadFrom(r.Body); err != nil {
log.Print(err)
return
}
if pf, err := parser.ParseFeed(buf.Bytes(), parser.ParseRss2, parser.ParseAtom, parser.ParseRss1); err == nil {
f.Refresh(pf)
if _, err = feedRepo.Update(&f); err != nil {
log.Printf("Error updating feed %s: %+v", f, err)
return
}
} else {
log.Printf("Error parsing feed from subscription %s: %+v", s, err)
return
}
return
}
if err = subRepo.Update(s); err != nil {
log.Printf("Error updating subscription %s: %+v\n", s, err)
return
}
}
}