linstrom/web/public/api/activitypub/note.go

43 lines
1.2 KiB
Go

package activitypub
import (
"encoding/json"
"fmt"
"net/http"
webutils "git.mstar.dev/mstar/goutils/http"
"github.com/rs/zerolog/hlog"
"gorm.io/gorm"
"git.mstar.dev/mstar/linstrom/activitypub"
"git.mstar.dev/mstar/linstrom/activitypub/translators"
"git.mstar.dev/mstar/linstrom/storage-new"
)
func objectNote(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
log := hlog.FromRequest(r)
note, err := translators.NoteFromStorage(r.Context(), id)
switch err {
case gorm.ErrRecordNotFound:
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusNotFound)
return
case nil:
note.Context = activitypub.BaseLdContext
data, err := json.Marshal(note)
if err != nil {
log.Error().Err(err).Any("activity", note).Msg("Failed to marshal create activity")
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/activity+json")
_, _ = fmt.Fprint(w, string(data))
default:
if storage.HandleReconnectError(err) {
log.Error().Err(err).Msg("Connection failed, restart attempt started")
} else {
log.Error().Err(err).Msg("Failed to get create activity from db")
}
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
}
}