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

View File

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

View File

@ -27,7 +27,7 @@ func Serve() {
continue
}
// 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)
if err != nil {
fmt.Println("Posting", article, "failed")
@ -79,7 +79,7 @@ func Serve() {
}
switch activity["type"] {
case "Follow":
actor.Accept(activity)
actor.Accept(&activity)
case "Undo":
{
if object, ok := activity["object"].(map[string]interface{}); ok {

View File

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