From f3e0c1bd613e5460206ed5148d733099c086ed4d Mon Sep 17 00:00:00 2001 From: Michael Demetriou Date: Fri, 8 Nov 2019 10:41:06 +0200 Subject: [PATCH] Fix whoAmI to use json marshalling instead of string concatenation also add a a comment to formatjson --- actor.go | 35 +++++++++++++++++++---------------- remoteActor.go | 11 ++++++----- util.go | 2 ++ 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/actor.go b/actor.go index 968288f..ddeeb22 100644 --- a/actor.go +++ b/actor.go @@ -286,22 +286,25 @@ func (a *Actor) save() error { } func (a *Actor) whoAmI() string { - return `{"@context":["https://www.w3.org/ns/activitystreams"], - "type": "` + a.actorType + `", - "id": "` + baseURL + a.Name + `", - "name": "` + a.Name + `", - "preferredUsername": "` + a.Name + `", - "summary": "` + a.summary + `", - "inbox": "` + baseURL + a.Name + `/inbox", - "outbox": "` + baseURL + a.Name + `/outbox", - "followers": "` + baseURL + a.Name + `/peers/followers", - "following": "` + baseURL + a.Name + `/peers/following", - "publicKey": { - "id": "` + baseURL + a.Name + `#main-key", - "owner": "` + baseURL + a.Name + `", - "publicKeyPem": "` + strings.ReplaceAll(a.publicKeyPem, "\n", "\\n") + `" - } - }` + + self := make(map[string]interface{}) + self["@context"] = context() + self["type"] = a.actorType + self["id"] = baseURL + a.Name + self["name"] = a.Name + self["preferredUsername"] = a.Name + self["summary"] = a.summary + self["inbox"] = baseURL + a.Name + "/inbox" + self["outbox"] = baseURL + a.Name + "/outbox" + self["followers"] = baseURL + a.Name + "/peers/followers" + self["following"] = baseURL + a.Name + "/peers/following" + self["publicKey"] = map[string]string{ + "id" : baseURL + a.Name + "#main-key", + "owner" : baseURL + a.Name, + "publicKeyPem" : strings.ReplaceAll(a.publicKeyPem, "\n", "\\n"), + } + selfString, _ := json.Marshal(self) + return string(selfString) } func (a *Actor) newItemID() (hash string, url string) { diff --git a/remoteActor.go b/remoteActor.go index f68b7d9..0658fb6 100644 --- a/remoteActor.go +++ b/remoteActor.go @@ -1,12 +1,13 @@ package activityserve import ( + "bytes" + "encoding/json" "fmt" "io/ioutil" - "github.com/gologme/log" "net/http" - "encoding/json" - "bytes" + + "github.com/gologme/log" ) // RemoteActor is a type that holds an actor @@ -36,7 +37,7 @@ func NewRemoteActor(iri string) (RemoteActor, error) { endpoints = info["endpoints"].(map[string]interface{}) if val, ok := endpoints["sharedInbox"]; ok { sharedInbox = val.(string) - } + } } return RemoteActor{ @@ -60,7 +61,7 @@ func get(iri string) (info map[string]interface{}, err error) { log.Info(err) return } - req.Header.Add("Accept", "application/activity+json; profile=\"https://www.w3.org/ns/activitystreams\"") + req.Header.Add("Accept", "application/activity+json") req.Header.Add("User-Agent", userAgent+" "+version) req.Header.Add("Accept-Charset", "utf-8") diff --git a/util.go b/util.go index 3cebb82..a694a42 100644 --- a/util.go +++ b/util.go @@ -38,6 +38,8 @@ func PrettyPrintJSON(theJSON []byte) { log.Info(dst) } +// FormatJSON formats json with tabs and +// returns the new string func FormatJSON(theJSON []byte) string { dst := new(bytes.Buffer) json.Indent(dst, theJSON, "", "\t")