Cleanup, store likes
Some checks failed
/ docker (push) Failing after 3m1s

- Clean up some logging
- Properly store likes in db
This commit is contained in:
Melody Becker 2025-05-06 22:18:28 +02:00
parent c7f875a9c5
commit 03ca524c99
Signed by: mstar
SSH key fingerprint: SHA256:vkXfS9FG2pVNVfvDrzd1VW9n8VJzqqdKQGljxxX8uK8
12 changed files with 222 additions and 44 deletions

View file

@ -1,7 +1,6 @@
package activitypub
import (
"fmt"
"net/http"
)
@ -15,8 +14,5 @@ func BuildActivitypubRouter() http.Handler {
router.HandleFunc("/activity/reject/{id}", activityReject)
router.HandleFunc("/activity/update/{id}", activityUpdate)
router.HandleFunc("/note/{id}", objectNote)
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, "in ap")
})
return router
}

View file

@ -0,0 +1,193 @@
package activitypub
import (
"database/sql"
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
webutils "git.mstar.dev/mstar/goutils/http"
"git.mstar.dev/mstar/goutils/other"
"github.com/rs/zerolog/hlog"
"gorm.io/gorm"
"git.mstar.dev/mstar/linstrom/activitypub"
"git.mstar.dev/mstar/linstrom/storage-new/dbgen"
"git.mstar.dev/mstar/linstrom/storage-new/models"
)
var objectIdRegex = regexp.MustCompile(
`https?://.+/api/activitypub/[a-z]+(?:/[a-z]+)?/([a-zA-Z0-9\-]+)`,
)
func userInbox(w http.ResponseWriter, r *http.Request) {
log := hlog.FromRequest(r)
userId := r.PathValue("id")
body, err := io.ReadAll(r.Body)
log.Info().
Err(err).
Str("userId", userId).
Bytes("body", body).
Any("headers", r.Header).
Msg("Inbox message")
data := map[string]any{}
err = json.Unmarshal(body, &data)
if err != nil {
webutils.ProblemDetails(
w,
http.StatusBadRequest,
"/errors/bad-request-data",
"Bad activity data",
other.IntoPointer("Body to inbox needs to be json"),
nil,
)
return
}
if _, ok := data["@context"]; !ok {
webutils.ProblemDetails(
w,
http.StatusBadRequest,
"/errors/bad-request-data",
"Bad activity data",
other.IntoPointer("Request data needs to contain context"),
nil,
)
return
}
objectType, ok := data["type"].(string)
if !ok {
webutils.ProblemDetails(
w,
http.StatusBadRequest,
"/errors/bad-request-data",
"Bad activity data",
other.IntoPointer(`Request data needs to contain a field "type" with a string value`),
nil,
)
return
}
_, ok = data["id"].(string)
if !ok {
webutils.ProblemDetails(
w,
http.StatusBadRequest,
"/errors/bad-request-data",
"Bad activity data",
other.IntoPointer(`Request data needs to contain a field "id" with a string value`),
nil,
)
return
}
switch objectType {
case "Like":
handleLike(w, r, data)
default:
webutils.ProblemDetailsStatusOnly(w, 500)
}
}
func handleLike(w http.ResponseWriter, r *http.Request, object map[string]any) {
log := hlog.FromRequest(r)
activityId := object["id"].(string)
likerUrl, ok := object["actor"].(string)
if !ok {
webutils.ProblemDetails(
w,
http.StatusBadRequest,
"/errors/bad-request-data",
"Bad activity data",
other.IntoPointer(`Request data needs to contain a field "actor" with a string value`),
nil,
)
return
}
// TODO: Account for case where object is embedded in like
targetUrl, ok := object["object"].(string)
if !ok {
webutils.ProblemDetails(
w,
http.StatusBadRequest,
"/errors/bad-request-data",
"Bad activity data",
other.IntoPointer(`Request data needs to contain a field "object" with a string value`),
nil,
)
return
}
targetIdMatches := objectIdRegex.FindStringSubmatch(targetUrl)
if len(targetIdMatches) != 2 {
log.Error().
Strs("match-results", targetIdMatches).
Str("url", targetUrl).
Msg("Url didn't match id extractor regex")
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
return
}
targetId := targetIdMatches[1]
// Assume likes can only happen on notes for now
// Thus check if a note with that Id exists at all
note, err := dbgen.Note.Where(dbgen.Note.ID.Eq(targetId)).First()
switch err {
case nil:
case gorm.ErrRecordNotFound:
webutils.ProblemDetails(
w,
http.StatusBadRequest,
"/errors/bad-request-data",
"Bad activity data",
other.IntoPointer("There is no note with the target id"),
nil,
)
return
default:
log.Error().Err(err).Str("note-id", targetId).Msg("Failed to get note from db")
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
return
}
// Import liker after verifying that target note is correct
liker, err := activitypub.ImportRemoteAccountByAPUrl(likerUrl)
if err != nil {
log.Error().
Err(err).
Str("liker-url", likerUrl).
Msg("Failed to import liking remote account")
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
return
}
reaction := models.Reaction{
Note: *note,
NoteId: note.ID,
Reactor: *liker,
ReactorId: liker.ID,
Emote: nil,
EmoteId: sql.NullInt64{Valid: false},
}
tx := dbgen.Q.Begin()
err = tx.Reaction.Create(&reaction)
if err != nil {
_ = tx.Rollback()
log.Error().Err(err).Any("raw-reaction", reaction).Msg("Failed to store reaction in db")
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
}
// TODO: Create corresponding activity too
activity := models.Activity{
Id: activityId,
Type: string(models.ActivityLike),
ObjectId: fmt.Sprint(reaction.ID),
ObjectType: uint32(models.ActivitystreamsActivityTargetReaction),
}
err = tx.Activity.Create(&activity)
if err != nil {
_ = tx.Rollback()
log.Error().Err(err).Any("raw-reaction", reaction).Msg("Failed to store reaction in db")
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
}
err = tx.Commit()
if err != nil {
log.Error().Err(err).Msg("Failed to commit reaction transaction to db")
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
}
}

View file

@ -3,7 +3,6 @@ package activitypub
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
@ -140,18 +139,6 @@ func users(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, string(encoded))
}
func userInbox(w http.ResponseWriter, r *http.Request) {
log := hlog.FromRequest(r)
userId := r.PathValue("id")
data, err := io.ReadAll(r.Body)
log.Info().
Err(err).
Str("userId", userId).
Bytes("body", data).
Any("headers", r.Header).
Msg("Inbox message")
}
/*
Fine. You win JsonLD. I can't get you to work properly. I'll just treat you like normal json then
Fuck you.