Fix whoAmI to use json marshalling instead of string concatenation

also add a a comment to formatjson
master
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 { func (a *Actor) whoAmI() string {
return `{"@context":["https://www.w3.org/ns/activitystreams"],
"type": "` + a.actorType + `", self := make(map[string]interface{})
"id": "` + baseURL + a.Name + `", self["@context"] = context()
"name": "` + a.Name + `", self["type"] = a.actorType
"preferredUsername": "` + a.Name + `", self["id"] = baseURL + a.Name
"summary": "` + a.summary + `", self["name"] = a.Name
"inbox": "` + baseURL + a.Name + `/inbox", self["preferredUsername"] = a.Name
"outbox": "` + baseURL + a.Name + `/outbox", self["summary"] = a.summary
"followers": "` + baseURL + a.Name + `/peers/followers", self["inbox"] = baseURL + a.Name + "/inbox"
"following": "` + baseURL + a.Name + `/peers/following", self["outbox"] = baseURL + a.Name + "/outbox"
"publicKey": { self["followers"] = baseURL + a.Name + "/peers/followers"
"id": "` + baseURL + a.Name + `#main-key", self["following"] = baseURL + a.Name + "/peers/following"
"owner": "` + baseURL + a.Name + `", self["publicKey"] = map[string]string{
"publicKeyPem": "` + strings.ReplaceAll(a.publicKeyPem, "\n", "\\n") + `" "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) { func (a *Actor) newItemID() (hash string, url string) {

View File

@ -1,12 +1,13 @@
package activityserve package activityserve
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"github.com/gologme/log"
"net/http" "net/http"
"encoding/json"
"bytes" "github.com/gologme/log"
) )
// RemoteActor is a type that holds an actor // RemoteActor is a type that holds an actor
@ -36,7 +37,7 @@ func NewRemoteActor(iri string) (RemoteActor, error) {
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)
} }
} }
return RemoteActor{ return RemoteActor{
@ -60,7 +61,7 @@ func get(iri string) (info map[string]interface{}, err error) {
log.Info(err) log.Info(err)
return 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("User-Agent", userAgent+" "+version)
req.Header.Add("Accept-Charset", "utf-8") req.Header.Add("Accept-Charset", "utf-8")

View File

@ -38,6 +38,8 @@ func PrettyPrintJSON(theJSON []byte) {
log.Info(dst) log.Info(dst)
} }
// FormatJSON formats json with tabs and
// returns the new string
func FormatJSON(theJSON []byte) string { func FormatJSON(theJSON []byte) string {
dst := new(bytes.Buffer) dst := new(bytes.Buffer)
json.Indent(dst, theJSON, "", "\t") json.Indent(dst, theJSON, "", "\t")