Fix whoAmI to use json marshalling instead of string concatenation

also add a a comment to formatjson
This commit is contained in:
Michael Demetriou 2019-11-08 10:41:06 +02:00
parent 898c97700f
commit f3e0c1bd61
3 changed files with 27 additions and 21 deletions

View file

@ -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) {

View file

@ -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
@ -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")

View file

@ -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")