jlelse
/
jsonpub
Archived
1
Fork 0

Send updates

This commit is contained in:
Jan-Lukas Else 2020-03-23 18:37:17 +01:00
parent 447ce3702c
commit c6ab26b63d
2 changed files with 15 additions and 15 deletions

View File

@ -21,7 +21,6 @@ import (
type Actor struct {
Name, iri, feed string
followers map[string]interface{}
lastItem string
privateKey crypto.PrivateKey
publicKeyID string
}
@ -29,7 +28,6 @@ type Actor struct {
type ActorToSave struct {
Name string
Followers map[string]interface{}
LastItem string
}
func GetActor(name, iri, feed, pk string) (Actor, error) {
@ -56,7 +54,6 @@ func GetActor(name, iri, feed, pk string) (Actor, error) {
iri: iri,
feed: feed,
followers: make(map[string]interface{}),
lastItem: "",
privateKey: privateKey,
publicKeyID: iri + "#main-key",
}
@ -80,9 +77,6 @@ func GetActor(name, iri, feed, pk string) (Actor, error) {
if savedActor.Followers != nil {
actor.followers = savedActor.Followers
}
if savedActor.LastItem != "" {
actor.lastItem = savedActor.LastItem
}
return actor, nil
}
@ -96,7 +90,6 @@ func (a *Actor) save() error {
actorToSave := ActorToSave{
Name: a.Name,
Followers: a.followers,
LastItem: a.lastItem,
}
actorJSON, err := json.MarshalIndent(actorToSave, "", "\t")
if err != nil {
@ -139,8 +132,8 @@ func (a *Actor) PostArticle(url string) error {
}
create["object"] = article
go a.sendToFollowers(create)
// Boost article if it contains "inReplyTo"
if article["inReplyTo"] != nil {
// Boost article if it contains "inReplyTo"
announce := make(map[string]interface{})
announce["@context"] = context()
announce["id"] = url + "#Announce"
@ -151,6 +144,15 @@ func (a *Actor) PostArticle(url string) error {
announce["published"] = article["published"]
go a.sendToFollowers(announce)
}
// Send update event if it contains "updated" and "updated" != "published"
if article["updated"] != nil && article["published"] != nil && article["updated"] != article["published"] {
update := make(map[string]interface{})
update["@context"] = context()
update["type"] = "Update"
update["object"] = url
update["actor"] = a.iri
go a.sendToFollowers(update)
}
return nil
}

12
http.go
View File

@ -6,6 +6,7 @@ import (
"github.com/gorilla/mux"
"io/ioutil"
"log"
"math"
"net/http"
"net/url"
"os"
@ -26,14 +27,11 @@ func Serve() {
fmt.Println(actor.feed, err.Error())
continue
}
// Post latest article
if articles[0] != actor.lastItem {
err = actor.PostArticle(articles[0])
// Post or update latest 5 article
for _, article := range articles[0:int(math.Min(float64(len(articles)-1), 4))] {
err = actor.PostArticle(article)
if err != nil {
fmt.Println("Posting", articles[0], "failed")
} else {
actor.lastItem = articles[0]
_ = actor.save()
fmt.Println("Posting", article, "failed")
}
}
}