This commit is contained in:
parent
98191fd098
commit
d272fa90b4
20 changed files with 574 additions and 27 deletions
15
web/public/api/activitypub/activitypub.go
Normal file
15
web/public/api/activitypub/activitypub.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func BuildActivitypubRouter() http.Handler {
|
||||
router := http.NewServeMux()
|
||||
router.HandleFunc("/user/{id}", users)
|
||||
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprint(w, "in ap")
|
||||
})
|
||||
return router
|
||||
}
|
96
web/public/api/activitypub/user.go
Normal file
96
web/public/api/activitypub/user.go
Normal file
|
@ -0,0 +1,96 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
webutils "git.mstar.dev/mstar/goutils/http"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"git.mstar.dev/mstar/linstrom/storage-new"
|
||||
"git.mstar.dev/mstar/linstrom/storage-new/dbgen"
|
||||
)
|
||||
|
||||
var baseLdContext = []any{
|
||||
"https://www.w3.org/ns/activitystreams",
|
||||
"https://w3id.org/security/v1",
|
||||
}
|
||||
|
||||
func users(w http.ResponseWriter, r *http.Request) {
|
||||
type OutboundKey struct {
|
||||
Id string `json:"id"`
|
||||
Owner string `json:"owner"`
|
||||
Pem string `json:"publicKeyPem"`
|
||||
}
|
||||
type Outbound struct {
|
||||
Context []any `json:"@context"`
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
PreferredUsername string `json:"preferredUsername"`
|
||||
Inbox string `json:"inbox"`
|
||||
// FIXME: Public key stuff is borken. Focus on fixing
|
||||
// PublicKey OutboundKey `json:"publicKey"`
|
||||
}
|
||||
userId := r.PathValue("id")
|
||||
user, err := dbgen.User.Where(dbgen.User.ID.Eq(userId)).First()
|
||||
if err != nil {
|
||||
webutils.ProblemDetails(w, 500, "/errors/db-failure", "internal database failure", nil, nil)
|
||||
if storage.HandleReconnectError(err) {
|
||||
log.Warn().Msg("Connection to db lost. Reconnect attempt started")
|
||||
} else {
|
||||
log.Error().Err(err).Msg("Failed to get total user count from db")
|
||||
}
|
||||
return
|
||||
}
|
||||
// fmt.Println(x509.ParsePKCS1PublicKey(user.PublicKey))
|
||||
|
||||
apUrl := userIdToApUrl(user.ID)
|
||||
data := Outbound{
|
||||
Context: baseLdContext,
|
||||
Id: apUrl,
|
||||
Type: "Person",
|
||||
PreferredUsername: user.DisplayName,
|
||||
Inbox: apUrl + "/inbox",
|
||||
// PublicKey: OutboundKey{
|
||||
// Id: apUrl + "#main-key",
|
||||
// Owner: apUrl,
|
||||
// Pem: keyBytesToPem(user.PublicKey),
|
||||
// },
|
||||
}
|
||||
|
||||
encoded, err := json.Marshal(data)
|
||||
w.Header().Add("Content-Type", "application/activity+json")
|
||||
fmt.Fprint(w, string(encoded))
|
||||
}
|
||||
|
||||
/*
|
||||
Fine. You win JsonLD. I can't get you to work properly. I'll just treat you like normal json then
|
||||
Fuck you.
|
||||
|
||||
If anyone wants to get this shit working *the propper way* with JsonLD, here's the
|
||||
original code
|
||||
var chain goap.BaseApChain = &goap.EmptyBaseObject{}
|
||||
chain = goap.AppendUDIdData(chain, apUrl)
|
||||
chain = goap.AppendUDTypeData(chain, "Person")
|
||||
chain = goap.AppendASPreferredNameData(chain, goap.ValueValue[string]{Value: user.DisplayName})
|
||||
chain = goap.AppendW3SecurityPublicKeyData(
|
||||
chain,
|
||||
apUrl+"#main-key",
|
||||
apUrl,
|
||||
keyBytesToPem(user.PublicKey),
|
||||
)
|
||||
|
||||
chainMap := chain.MarshalToMap()
|
||||
proc := ld.NewJsonLdProcessor()
|
||||
options := ld.NewJsonLdOptions("")
|
||||
tmp, tmperr := json.Marshal(chainMap)
|
||||
fmt.Println(string(tmp), tmperr)
|
||||
data, err := proc.Compact(chainMap, baseLdContext, options)
|
||||
// data, err := goap.Compact(chain, baseLdContext)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to marshal ap chain")
|
||||
webutils.ProblemDetailsStatusOnly(w, 500)
|
||||
return
|
||||
}
|
||||
*/
|
25
web/public/api/activitypub/util.go
Normal file
25
web/public/api/activitypub/util.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
|
||||
"git.mstar.dev/mstar/linstrom/config"
|
||||
)
|
||||
|
||||
func userIdToApUrl(id string) string {
|
||||
return fmt.Sprintf(
|
||||
"%s/api/ap/users/%s",
|
||||
config.GlobalConfig.General.GetFullPublicUrl(),
|
||||
id,
|
||||
)
|
||||
}
|
||||
|
||||
func keyBytesToPem(bytes []byte) string {
|
||||
block := pem.Block{
|
||||
Type: "PUBLIC KEY",
|
||||
Headers: nil,
|
||||
Bytes: bytes,
|
||||
}
|
||||
return string(pem.EncodeToMemory(&block))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue