linstrom/web/public/api/activitypub/user.go
mstar d272fa90b4
Some checks are pending
/ test (push) Waiting to run
AP stuff almost works
2025-04-09 17:35:31 +02:00

96 lines
2.8 KiB
Go

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
}
*/