Skip to content

Commit

Permalink
send Update activity on bio change
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Jan 6, 2024
1 parent d650bb4 commit abec346
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
24 changes: 23 additions & 1 deletion front/bio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand All @@ -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)))
}
2 changes: 1 addition & 1 deletion front/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, &note)
err = outbox.UpdateNote(r.Context, r.DB, &note)
} else {
err = outbox.Create(r.Context, r.Log, r.DB, &note, r.User)
}
Expand Down
2 changes: 1 addition & 1 deletion outbox/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
32 changes: 31 additions & 1 deletion outbox/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit abec346

Please sign in to comment.