aptok/remoteActor.go

105 lines
2.5 KiB
Go
Raw Normal View History

2019-09-10 11:21:39 +03:00
package activityserve
import (
"encoding/json"
"encoding/pem"
"github.com/gologme/log"
"crypto/x509"
"crypto/rsa"
2019-09-10 11:21:39 +03:00
)
// RemoteActor is a type that holds an actor
// that we want to interact with
2019-09-10 11:21:39 +03:00
type RemoteActor struct {
iri, outbox, inbox, sharedInbox string
2023-04-28 21:02:47 +03:00
url string
info map[string]interface{}
publicKey *rsa.PublicKey
2019-09-10 11:21:39 +03:00
}
// NewRemoteActor returns a remoteActor which holds
// all the info required for an actor we want to
// interact with (not essentially sitting in our instance)
func NewRemoteActor(iri string) (RemoteActor, error) {
info, err := get(iri)
if err != nil {
log.Info("Couldn't get remote actor information")
log.Info(err)
return RemoteActor{}, err
}
publicKeyBlock := info["publicKey"].(map[string]interface {})
publicKeyPem := publicKeyBlock["publicKeyPem"].(string)
spkiBlock, _ := pem.Decode([]byte(publicKeyPem))
var spkiKey *rsa.PublicKey
pubInterface, _ := x509.ParsePKIXPublicKey(spkiBlock.Bytes)
spkiKey = pubInterface.(*rsa.PublicKey)
outbox, _ := info["outbox"].(string)
inbox, _ := info["inbox"].(string)
2023-04-28 21:02:47 +03:00
url, _ := info["url"].(string)
var endpoints map[string]interface{}
var sharedInbox string
if info["endpoints"] != nil {
endpoints = info["endpoints"].(map[string]interface{})
if val, ok := endpoints["sharedInbox"]; ok {
sharedInbox = val.(string)
}
}
2019-09-10 11:21:39 +03:00
return RemoteActor{
iri: iri,
outbox: outbox,
inbox: inbox,
sharedInbox: sharedInbox,
2023-04-28 21:02:47 +03:00
url: url,
publicKey: spkiKey,
2019-09-10 11:21:39 +03:00
}, err
}
func (ra RemoteActor) getLatestPosts(number int) (map[string]interface{}, error) {
return get(ra.outbox)
}
func get(iri string) (info map[string]interface{}, err error) {
sactor,err := GetActor("server","Internal service actor","Service")
2019-09-10 11:21:39 +03:00
if err != nil {
log.Info("Failed to get service actor")
2019-09-10 11:21:39 +03:00
log.Info(err)
return
}
responseData,err := sactor.signedHTTPGet(iri)
2019-09-10 11:21:39 +03:00
if err != nil {
log.Info("Failed to make signed http get request")
2019-09-10 11:21:39 +03:00
log.Info(err)
return
}
var e interface{}
err = json.Unmarshal([]byte(responseData), &e)
2019-09-10 11:21:39 +03:00
if err != nil {
log.Info("something went wrong when unmarshalling the json")
log.Info(err)
return
2019-09-10 11:21:39 +03:00
}
info = e.(map[string]interface{})
return
}
2019-10-08 15:22:59 +03:00
// GetInbox returns the inbox url of the actor
func (ra RemoteActor) GetInbox() string {
return ra.inbox
}
// GetSharedInbox returns the inbox url of the actor
func (ra RemoteActor) GetSharedInbox() string {
if ra.sharedInbox == "" {
return ra.inbox
}
return ra.sharedInbox
}
2023-04-28 21:02:47 +03:00
func (ra RemoteActor) URL() string {
return ra.url
}