Add hook to post latest article from feed
This commit is contained in:
parent
35a409a739
commit
bea069bcc1
12
actor.go
12
actor.go
|
@ -21,7 +21,7 @@ import (
|
|||
type Actor struct {
|
||||
Name, iri, feed string
|
||||
followers map[string]interface{}
|
||||
sentItems map[string]bool
|
||||
lastItem string
|
||||
privateKey crypto.PrivateKey
|
||||
publicKeyID string
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ type Actor struct {
|
|||
type ActorToSave struct {
|
||||
Name string
|
||||
Followers map[string]interface{}
|
||||
SentItems map[string]bool
|
||||
LastItem string
|
||||
}
|
||||
|
||||
func GetActor(name, iri, feed, pk string) (Actor, error) {
|
||||
|
@ -56,7 +56,7 @@ func GetActor(name, iri, feed, pk string) (Actor, error) {
|
|||
iri: iri,
|
||||
feed: feed,
|
||||
followers: make(map[string]interface{}),
|
||||
sentItems: make(map[string]bool),
|
||||
lastItem: "",
|
||||
privateKey: privateKey,
|
||||
publicKeyID: iri + "#main-key",
|
||||
}
|
||||
|
@ -80,8 +80,8 @@ func GetActor(name, iri, feed, pk string) (Actor, error) {
|
|||
if savedActor.Followers != nil {
|
||||
actor.followers = savedActor.Followers
|
||||
}
|
||||
if savedActor.SentItems != nil {
|
||||
actor.sentItems = savedActor.SentItems
|
||||
if savedActor.LastItem != "" {
|
||||
actor.lastItem = savedActor.LastItem
|
||||
}
|
||||
return actor, nil
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ func (a *Actor) save() error {
|
|||
actorToSave := ActorToSave{
|
||||
Name: a.Name,
|
||||
Followers: a.followers,
|
||||
SentItems: a.sentItems,
|
||||
LastItem: a.lastItem,
|
||||
}
|
||||
actorJSON, err := json.MarshalIndent(actorToSave, "", "\t")
|
||||
if err != nil {
|
||||
|
|
10
go.mod
10
go.mod
|
@ -3,9 +3,13 @@ module codeberg.org/jlelse/jsonpub
|
|||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9
|
||||
github.com/andybalholm/cascadia v1.1.0 // indirect
|
||||
github.com/dchest/uniuri v0.0.0-20200228104902-7aecb25e1fe5
|
||||
github.com/go-fed/httpsig v0.1.1-0.20190924171022-f4c36041199d
|
||||
github.com/gorilla/mux v1.7.3
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 // indirect
|
||||
github.com/google/go-cmp v0.4.0 // indirect
|
||||
github.com/gorilla/mux v1.7.4
|
||||
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 // indirect
|
||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a // indirect
|
||||
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
|
||||
willnorris.com/go/webmention v0.0.0-20200126231626-5a55fff6bf71
|
||||
)
|
||||
|
|
23
go.sum
23
go.sum
|
@ -1,24 +1,39 @@
|
|||
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
|
||||
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
|
||||
github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9 h1:74lLNRzvsdIlkTgfDSMuaPjBr4cf6k7pwQQANm/yLKU=
|
||||
github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9/go.mod h1:GgB8SF9nRG+GqaDtLcwJZsQFhcogVCJ79j4EdT0c2V4=
|
||||
github.com/andybalholm/cascadia v1.1.0 h1:BuuO6sSfQNFRu1LppgbD25Hr2vLYW25JvxHs5zzsLTo=
|
||||
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
|
||||
github.com/dchest/uniuri v0.0.0-20200228104902-7aecb25e1fe5 h1:RAV05c0xOkJ3dZGS0JFybxFKZ2WMLabgx3uXnd7rpGs=
|
||||
github.com/dchest/uniuri v0.0.0-20200228104902-7aecb25e1fe5/go.mod h1:GgB8SF9nRG+GqaDtLcwJZsQFhcogVCJ79j4EdT0c2V4=
|
||||
github.com/go-fed/httpsig v0.1.1-0.20190924171022-f4c36041199d h1:+uoOvOnNDgsYbWtAij4xP6Rgir3eJGjocFPxBJETU/U=
|
||||
github.com/go-fed/httpsig v0.1.1-0.20190924171022-f4c36041199d/go.mod h1:T56HUNYZUQ1AGUzhAYPugZfp36sKApVnGBgKlIY+aIE=
|
||||
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
|
||||
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
||||
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
|
||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
|
||||
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||
github.com/wsxiaoys/terminal v0.0.0-20160513160801-0940f3fc43a0/go.mod h1:IXCdmsXIht47RaVFLEdVnh1t+pgYtTAhQGj73kz+2DM=
|
||||
golang.org/x/crypto v0.0.0-20180527072434-ab813273cd59 h1:hk3yo72LXLapY9EXVttc3Z1rLOxT9IuAPPX3GpY2+jo=
|
||||
golang.org/x/crypto v0.0.0-20180527072434-ab813273cd59/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM=
|
||||
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3 h1:czFLhve3vsQetD6JOJ8NZZvGQIXlnN3/yXxbT6/awxI=
|
||||
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0=
|
||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/sys v0.0.0-20180525142821-c11f84a56e43 h1:PvnWIWTbA7gsEBkKjt0HV9hckYfcqYv8s/ju7ArZ0do=
|
||||
golang.org/x/sys v0.0.0-20180525142821-c11f84a56e43/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
|
||||
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
willnorris.com/go/webmention v0.0.0-20200126231626-5a55fff6bf71 h1:F//bgirx4BIsJYHrqAYCZHODn0gSRej/KfueFlarXhs=
|
||||
willnorris.com/go/webmention v0.0.0-20200126231626-5a55fff6bf71/go.mod h1:p+ZRAsZS2pzZ6kX3GKWYurf3WZI2ygj7VbR8NM8qwfM=
|
||||
|
|
27
http.go
27
http.go
|
@ -10,11 +10,34 @@ import (
|
|||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"time"
|
||||
"willnorris.com/go/webmention"
|
||||
)
|
||||
|
||||
func Serve() {
|
||||
|
||||
hookHandler := func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("Fetch feeds: ", time.Now().Format(time.RFC3339))
|
||||
for _, actor := range actors {
|
||||
fmt.Println(actor.feed)
|
||||
articles, err := allFeedItems(actor.feed)
|
||||
if err != nil {
|
||||
fmt.Println(actor.feed, err.Error())
|
||||
continue
|
||||
}
|
||||
// Post latest article
|
||||
if articles[0] != actor.lastItem {
|
||||
err = actor.PostArticle(articles[0])
|
||||
if err != nil {
|
||||
fmt.Println("Posting", articles[0], "failed")
|
||||
} else {
|
||||
actor.lastItem = articles[0]
|
||||
_ = actor.save()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
webfingerHandler := func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("content-type", "application/jrd+json; charset=utf-8")
|
||||
name := r.URL.Query().Get("resource") // should be something like acct:user@example.com
|
||||
|
@ -114,9 +137,9 @@ func Serve() {
|
|||
|
||||
// Add the handlers to a HTTP server
|
||||
gorilla := mux.NewRouter()
|
||||
gorilla.HandleFunc("/hook", hookHandler).Methods(http.MethodPost)
|
||||
gorilla.HandleFunc("/.well-known/webfinger", webfingerHandler)
|
||||
gorilla.HandleFunc("/{actor}/inbox", inboxHandler)
|
||||
gorilla.HandleFunc("/{actor}/inbox/", inboxHandler)
|
||||
gorilla.PathPrefix("/{actor}/inbox").HandlerFunc(inboxHandler).Methods(http.MethodPost)
|
||||
http.Handle("/", gorilla)
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8081", nil))
|
||||
|
|
38
main.go
38
main.go
|
@ -1,45 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
Setup()
|
||||
SetupActors()
|
||||
go func() {
|
||||
ticker := time.NewTicker(60 * time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case t := <-ticker.C:
|
||||
fmt.Println("Fetch feeds: ", t.Format(time.RFC3339))
|
||||
for _, actor := range actors {
|
||||
fmt.Println(actor.feed)
|
||||
articles, err := allFeedItems(actor.feed)
|
||||
if err != nil {
|
||||
fmt.Println(actor.feed, err.Error())
|
||||
continue
|
||||
}
|
||||
// Prevent map from getting to big
|
||||
oldSentItems := actor.sentItems
|
||||
actor.sentItems = make(map[string]bool)
|
||||
for _, article := range articles {
|
||||
if oldSentItems[article] == false {
|
||||
fmt.Println("Send", article)
|
||||
err = actor.PostArticle(article)
|
||||
if err == nil {
|
||||
actor.sentItems[article] = true
|
||||
}
|
||||
} else {
|
||||
actor.sentItems[article] = true
|
||||
}
|
||||
}
|
||||
_ = actor.save()
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
Serve()
|
||||
}
|
||||
|
|
Reference in New Issue