jlelse
/
jsonpub
Archived
1
Fork 0

Try to reduce memory usage

This commit is contained in:
Jan-Lukas Else 2020-04-11 21:26:48 +02:00
parent e41cd3b07f
commit 7b9473f6fc
4 changed files with 29 additions and 31 deletions

View File

@ -129,7 +129,7 @@ func (a *Actor) PostArticle(url string) error {
return errors.New("failed to decode fetched article") return errors.New("failed to decode fetched article")
} }
create["object"] = article create["object"] = article
go a.sendToFollowers(create) a.sendToFollowers(&create)
// Boost article if it contains "inReplyTo" // Boost article if it contains "inReplyTo"
if article["inReplyTo"] != nil { if article["inReplyTo"] != nil {
announce := make(map[string]interface{}) announce := make(map[string]interface{})
@ -140,7 +140,7 @@ func (a *Actor) PostArticle(url string) error {
announce["actor"] = a.iri announce["actor"] = a.iri
announce["to"] = []string{"https://www.w3.org/ns/activitystreams#Public"} announce["to"] = []string{"https://www.w3.org/ns/activitystreams#Public"}
announce["published"] = article["published"] announce["published"] = article["published"]
go a.sendToFollowers(announce) a.sendToFollowers(&announce)
} }
// Send update event if it contains "updated" and "updated" != "published" // Send update event if it contains "updated" and "updated" != "published"
if article["updated"] != nil && article["published"] != nil && article["updated"] != article["published"] { if article["updated"] != nil && article["published"] != nil && article["updated"] != article["published"] {
@ -149,7 +149,7 @@ func (a *Actor) PostArticle(url string) error {
update["type"] = "Update" update["type"] = "Update"
update["object"] = url update["object"] = url
update["actor"] = a.iri update["actor"] = a.iri
go a.sendToFollowers(update) a.sendToFollowers(&update)
} }
return nil return nil
} }
@ -157,8 +157,8 @@ func (a *Actor) PostArticle(url string) error {
// signedHTTPPost performs an HTTP post on behalf of Actor with the // signedHTTPPost performs an HTTP post on behalf of Actor with the
// request-target, date, host and digest headers signed // request-target, date, host and digest headers signed
// with the actor's private key. // with the actor's private key.
func (a *Actor) signedHTTPPost(content map[string]interface{}, to string) (err error) { func (a *Actor) signedHTTPPost(content *map[string]interface{}, to string) (err error) {
b, err := json.Marshal(content) b, err := json.Marshal(*content)
if err != nil { if err != nil {
return return
} }
@ -210,7 +210,7 @@ func (a *Actor) RemoveFollower(iri string) error {
} }
// send to followers sends a batch of http posts to each one of the followers // send to followers sends a batch of http posts to each one of the followers
func (a *Actor) sendToFollowers(activity map[string]interface{}) { func (a *Actor) sendToFollowers(activity *map[string]interface{}) {
recipients := make([]string, len(a.followers)) recipients := make([]string, len(a.followers))
i := 0 i := 0
for _, inbox := range a.followers { for _, inbox := range a.followers {
@ -229,12 +229,12 @@ func (a *Actor) newID() (hash string, url string) {
} }
// Accept a follow request // Accept a follow request
func (a *Actor) Accept(follow map[string]interface{}) { func (a *Actor) Accept(follow *map[string]interface{}) {
// it's a follow, write it down // it's a follow, write it down
newFollower := follow["actor"].(string) newFollower := (*follow)["actor"].(string)
fmt.Println("New follow request:", newFollower) fmt.Println("New follow request:", newFollower)
// check we aren't following ourselves // check we aren't following ourselves
if newFollower == follow["object"] { if newFollower == (*follow)["object"] {
// actor and object are equal // actor and object are equal
return return
} }
@ -247,21 +247,19 @@ func (a *Actor) Accept(follow map[string]interface{}) {
// Add or update follower // Add or update follower
_ = a.NewFollower(newFollower, follower.inbox) _ = a.NewFollower(newFollower, follower.inbox)
// remove @context from the inner activity // remove @context from the inner activity
delete(follow, "@context") delete(*follow, "@context")
accept := make(map[string]interface{}) accept := make(map[string]interface{})
accept["@context"] = "https://www.w3.org/ns/activitystreams" accept["@context"] = "https://www.w3.org/ns/activitystreams"
accept["to"] = follow["actor"] accept["to"] = (*follow)["actor"]
_, accept["id"] = a.newID() _, accept["id"] = a.newID()
accept["actor"] = a.iri accept["actor"] = a.iri
accept["object"] = follow accept["object"] = *follow
accept["type"] = "Accept" accept["type"] = "Accept"
go func() { err = a.signedHTTPPost(&accept, follower.inbox)
err = a.signedHTTPPost(accept, follower.inbox) if err != nil {
if err != nil { fmt.Println("Failed to accept:", follower.iri)
fmt.Println("Failed to accept:", follower.iri) fmt.Println(err.Error())
fmt.Println(err.Error()) } else {
} else { fmt.Println("Accepted:", follower.iri)
fmt.Println("Accepted:", follower.iri) }
}
}()
} }

View File

@ -7,7 +7,7 @@ import (
"net/http" "net/http"
) )
func allFeedItems(url string) ([]string, error) { func allFeedItems(url string) (*[]string, error) {
jsonFeed := &struct { jsonFeed := &struct {
Items []struct { Items []struct {
Url string `json:"url"` Url string `json:"url"`
@ -30,5 +30,5 @@ func allFeedItems(url string) ([]string, error) {
for _, item := range jsonFeed.Items { for _, item := range jsonFeed.Items {
allUrls = append(allUrls, item.Url) allUrls = append(allUrls, item.Url)
} }
return allUrls, nil return &allUrls, nil
} }

View File

@ -27,7 +27,7 @@ func Serve() {
continue continue
} }
// Post or update latest 5 article // Post or update latest 5 article
for _, article := range articles[0:int(math.Min(float64(len(articles)-1), 4))] { for _, article := range (*articles)[0:int(math.Min(float64(len(*articles)-1), 4))] {
err = actor.PostArticle(article) err = actor.PostArticle(article)
if err != nil { if err != nil {
fmt.Println("Posting", article, "failed") fmt.Println("Posting", article, "failed")
@ -79,7 +79,7 @@ func Serve() {
} }
switch activity["type"] { switch activity["type"] {
case "Follow": case "Follow":
actor.Accept(activity) actor.Accept(&activity)
case "Undo": case "Undo":
{ {
if object, ok := activity["object"].(map[string]interface{}); ok { if object, ok := activity["object"].(map[string]interface{}); ok {

View File

@ -17,11 +17,11 @@ func NewRemoteActor(iri string) (RemoteActor, error) {
if err != nil { if err != nil {
return RemoteActor{}, err return RemoteActor{}, err
} }
inbox := info["inbox"].(string) inbox := (*info)["inbox"].(string)
var endpoints map[string]interface{} var endpoints map[string]interface{}
var sharedInbox string var sharedInbox string
if info["endpoints"] != nil { if (*info)["endpoints"] != nil {
endpoints = info["endpoints"].(map[string]interface{}) endpoints = (*info)["endpoints"].(map[string]interface{})
if val, ok := endpoints["sharedInbox"]; ok { if val, ok := endpoints["sharedInbox"]; ok {
sharedInbox = val.(string) sharedInbox = val.(string)
} }
@ -33,7 +33,7 @@ func NewRemoteActor(iri string) (RemoteActor, error) {
}, err }, err
} }
func get(iri string) (info map[string]interface{}, err error) { func get(iri string) (info *map[string]interface{}, err error) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
req, err := http.NewRequest("GET", iri, buf) req, err := http.NewRequest("GET", iri, buf)
if err != nil { if err != nil {
@ -49,11 +49,11 @@ func get(iri string) (info map[string]interface{}, err error) {
if !isSuccess(resp.StatusCode) { if !isSuccess(resp.StatusCode) {
return return
} }
var e interface{} var e map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&e) err = json.NewDecoder(resp.Body).Decode(&e)
if err != nil { if err != nil {
return return
} }
info = e.(map[string]interface{}) info = &e
return return
} }