Send updates
This commit is contained in:
parent
447ce3702c
commit
c6ab26b63d
18
actor.go
18
actor.go
|
@ -21,7 +21,6 @@ import (
|
||||||
type Actor struct {
|
type Actor struct {
|
||||||
Name, iri, feed string
|
Name, iri, feed string
|
||||||
followers map[string]interface{}
|
followers map[string]interface{}
|
||||||
lastItem string
|
|
||||||
privateKey crypto.PrivateKey
|
privateKey crypto.PrivateKey
|
||||||
publicKeyID string
|
publicKeyID string
|
||||||
}
|
}
|
||||||
|
@ -29,7 +28,6 @@ type Actor struct {
|
||||||
type ActorToSave struct {
|
type ActorToSave struct {
|
||||||
Name string
|
Name string
|
||||||
Followers map[string]interface{}
|
Followers map[string]interface{}
|
||||||
LastItem string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetActor(name, iri, feed, pk string) (Actor, error) {
|
func GetActor(name, iri, feed, pk string) (Actor, error) {
|
||||||
|
@ -56,7 +54,6 @@ func GetActor(name, iri, feed, pk string) (Actor, error) {
|
||||||
iri: iri,
|
iri: iri,
|
||||||
feed: feed,
|
feed: feed,
|
||||||
followers: make(map[string]interface{}),
|
followers: make(map[string]interface{}),
|
||||||
lastItem: "",
|
|
||||||
privateKey: privateKey,
|
privateKey: privateKey,
|
||||||
publicKeyID: iri + "#main-key",
|
publicKeyID: iri + "#main-key",
|
||||||
}
|
}
|
||||||
|
@ -80,9 +77,6 @@ func GetActor(name, iri, feed, pk string) (Actor, error) {
|
||||||
if savedActor.Followers != nil {
|
if savedActor.Followers != nil {
|
||||||
actor.followers = savedActor.Followers
|
actor.followers = savedActor.Followers
|
||||||
}
|
}
|
||||||
if savedActor.LastItem != "" {
|
|
||||||
actor.lastItem = savedActor.LastItem
|
|
||||||
}
|
|
||||||
return actor, nil
|
return actor, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,7 +90,6 @@ func (a *Actor) save() error {
|
||||||
actorToSave := ActorToSave{
|
actorToSave := ActorToSave{
|
||||||
Name: a.Name,
|
Name: a.Name,
|
||||||
Followers: a.followers,
|
Followers: a.followers,
|
||||||
LastItem: a.lastItem,
|
|
||||||
}
|
}
|
||||||
actorJSON, err := json.MarshalIndent(actorToSave, "", "\t")
|
actorJSON, err := json.MarshalIndent(actorToSave, "", "\t")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -139,8 +132,8 @@ func (a *Actor) PostArticle(url string) error {
|
||||||
}
|
}
|
||||||
create["object"] = article
|
create["object"] = article
|
||||||
go a.sendToFollowers(create)
|
go a.sendToFollowers(create)
|
||||||
if article["inReplyTo"] != nil {
|
|
||||||
// Boost article if it contains "inReplyTo"
|
// Boost article if it contains "inReplyTo"
|
||||||
|
if article["inReplyTo"] != nil {
|
||||||
announce := make(map[string]interface{})
|
announce := make(map[string]interface{})
|
||||||
announce["@context"] = context()
|
announce["@context"] = context()
|
||||||
announce["id"] = url + "#Announce"
|
announce["id"] = url + "#Announce"
|
||||||
|
@ -151,6 +144,15 @@ func (a *Actor) PostArticle(url string) error {
|
||||||
announce["published"] = article["published"]
|
announce["published"] = article["published"]
|
||||||
go a.sendToFollowers(announce)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
12
http.go
12
http.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
@ -26,14 +27,11 @@ func Serve() {
|
||||||
fmt.Println(actor.feed, err.Error())
|
fmt.Println(actor.feed, err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Post latest article
|
// Post or update latest 5 article
|
||||||
if articles[0] != actor.lastItem {
|
for _, article := range articles[0:int(math.Min(float64(len(articles)-1), 4))] {
|
||||||
err = actor.PostArticle(articles[0])
|
err = actor.PostArticle(article)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Posting", articles[0], "failed")
|
fmt.Println("Posting", article, "failed")
|
||||||
} else {
|
|
||||||
actor.lastItem = articles[0]
|
|
||||||
_ = actor.save()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue
Block a user