diff --git a/front/bio.go b/front/bio.go index ebab97ab..02901c8b 100644 --- a/front/bio.go +++ b/front/bio.go @@ -20,6 +20,7 @@ import ( "crypto/sha256" "github.com/dimkr/tootik/front/text" "github.com/dimkr/tootik/front/text/plain" + "github.com/dimkr/tootik/outbox" "net/url" "time" "unicode/utf8" @@ -60,7 +61,16 @@ func bio(w text.Writer, r *request) { return } - if _, err := r.Exec( + tx, err := r.DB.BeginTx(r.Context, nil) + if err != nil { + r.Log.Warn("Failed to update summary", "error", err) + w.Error() + return + } + defer tx.Rollback() + + if _, err := tx.ExecContext( + r.Context, "update persons set actor = json_set(actor, '$.summary', $1, '$.updated', $2) where id = $3", plain.ToHTML(summary, nil), now.Format(time.RFC3339Nano), @@ -71,5 +81,17 @@ func bio(w text.Writer, r *request) { return } + if err := outbox.UpdateActor(r.Context, tx, r.User.ID); err != nil { + r.Log.Error("Failed to update summary", "error", err) + w.Error() + return + } + + if err := tx.Commit(); err != nil { + r.Log.Error("Failed to update summary", "error", err) + w.Error() + return + } + w.Redirectf("/users/outbox/%x", sha256.Sum256([]byte(r.User.ID))) } diff --git a/front/post.go b/front/post.go index abd885d3..17f7d59f 100644 --- a/front/post.go +++ b/front/post.go @@ -208,7 +208,7 @@ func post(w text.Writer, r *request, oldNote *ap.Object, inReplyTo *ap.Object, t if oldNote != nil { note.Published = oldNote.Published note.Updated = &now - err = outbox.Update(r.Context, r.DB, ¬e) + err = outbox.UpdateNote(r.Context, r.DB, ¬e) } else { err = outbox.Create(r.Context, r.Log, r.DB, ¬e, r.User) } diff --git a/outbox/poll.go b/outbox/poll.go index 16486484..8bf2d14f 100644 --- a/outbox/poll.go +++ b/outbox/poll.go @@ -102,7 +102,7 @@ func UpdatePollResults(ctx context.Context, log *slog.Logger, db *sql.DB) error log.Info("Updating poll results", "poll", poll.ID) - if err := Update(ctx, db, poll); err != nil { + if err := UpdateNote(ctx, db, poll); err != nil { log.Warn("Failed to update poll results", "poll", poll.ID, "error", err) } } diff --git a/outbox/update.go b/outbox/update.go index 909ba768..4d46b265 100644 --- a/outbox/update.go +++ b/outbox/update.go @@ -27,7 +27,7 @@ import ( "time" ) -func Update(ctx context.Context, db *sql.DB, note *ap.Object) error { +func UpdateNote(ctx context.Context, db *sql.DB, note *ap.Object) error { body, err := json.Marshal(note) if err != nil { return fmt.Errorf("failed to marshal note: %w", err) @@ -178,3 +178,33 @@ func Update(ctx context.Context, db *sql.DB, note *ap.Object) error { return nil } + +func UpdateActor(ctx context.Context, tx *sql.Tx, actorID string) error { + updateID := fmt.Sprintf("https://%s/update/%x", cfg.Domain, sha256.Sum256([]byte(fmt.Sprintf("%s|%d", actorID, time.Now().UnixNano())))) + + to := ap.Audience{} + to.Add(ap.Public) + + update, err := json.Marshal(ap.Activity{ + Context: "https://www.w3.org/ns/activitystreams", + ID: updateID, + Type: ap.UpdateActivity, + Actor: actorID, + Object: actorID, + To: to, + }) + if err != nil { + return fmt.Errorf("failed to marshal update: %w", err) + } + + if _, err := tx.ExecContext( + ctx, + `INSERT INTO outbox (activity, sender) VALUES(?,?)`, + string(update), + actorID, + ); err != nil { + return fmt.Errorf("failed to insert update activity: %w", err) + } + + return nil +}