This commit is contained in:
parent
8ec486cfc1
commit
440399f8d2
8 changed files with 171 additions and 9 deletions
|
@ -12,6 +12,7 @@ import (
|
|||
|
||||
webutils "git.mstar.dev/mstar/goutils/http"
|
||||
"git.mstar.dev/mstar/goutils/other"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/rs/zerolog/hlog"
|
||||
"gorm.io/gorm"
|
||||
|
||||
|
@ -668,4 +669,88 @@ func handleReject(w http.ResponseWriter, r *http.Request, object map[string]any)
|
|||
}
|
||||
}
|
||||
|
||||
func handleCreate(w http.ResponseWriter, r *http.Request, object map[string]any) {}
|
||||
func handleCreate(w http.ResponseWriter, r *http.Request, object map[string]any) {
|
||||
log := hlog.FromRequest(r)
|
||||
activity := ActivityCreate{}
|
||||
err := mapstructure.Decode(object, &activity)
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Err(err).
|
||||
Any("raw", object).
|
||||
Msg("Failed to marshal create activity to proper type")
|
||||
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
actingUser, err := activitypub.ImportRemoteAccountByAPUrl(activity.Actor)
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Err(err).
|
||||
Str("actor", activity.Actor).
|
||||
Msg("Failed to import remote actor for note")
|
||||
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
switch val := activity.Object.(type) {
|
||||
case string:
|
||||
activitypub.ImportRemoteNote(val)
|
||||
return
|
||||
case map[string]any:
|
||||
default:
|
||||
webutils.ProblemDetails(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
"/errors/bad-request-data",
|
||||
"bad request data",
|
||||
other.IntoPointer("Bad object data for create activity. Must be a struct or string"),
|
||||
nil,
|
||||
)
|
||||
return
|
||||
}
|
||||
obj := activity.Object.(map[string]any)
|
||||
// Dumb hack since published timestamp is still a string at this point
|
||||
tmpTime, err := time.Parse(time.RFC3339, obj["published"].(string))
|
||||
if err != nil {
|
||||
delete(obj, "published")
|
||||
} else {
|
||||
obj["published"] = tmpTime
|
||||
}
|
||||
objectNote := ObjectNote{}
|
||||
err = mapstructure.Decode(obj, &objectNote)
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Err(err).
|
||||
Any("raw", activity.Object).
|
||||
Msg("Failed to unmarshal create object into note")
|
||||
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if objectNote.Type != "Note" {
|
||||
webutils.ProblemDetails(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
"/errors/bad-request-data",
|
||||
"bad request data",
|
||||
other.IntoPointer("Bad object data for create activity. object.type must be 'Note'"),
|
||||
nil,
|
||||
)
|
||||
return
|
||||
}
|
||||
dbNote := models.Note{
|
||||
ID: objectNote.Id,
|
||||
CreatedAt: objectNote.Published,
|
||||
Creator: *actingUser,
|
||||
CreatorId: actingUser.ID,
|
||||
Remote: true,
|
||||
RawContent: objectNote.Content,
|
||||
OriginId: actingUser.ServerId,
|
||||
}
|
||||
if objectNote.Summary != nil {
|
||||
dbNote.ContentWarning = sql.NullString{Valid: true, String: *objectNote.Summary}
|
||||
}
|
||||
err = dbgen.Note.Create(&dbNote)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Any("note", dbNote).Msg("Failed to create note in db")
|
||||
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue