Skip to content

Commit 65d7b4d

Browse files
committed
wip
1 parent 99b5608 commit 65d7b4d

File tree

14 files changed

+124
-83
lines changed

14 files changed

+124
-83
lines changed

front/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (h *Handler) delete(w text.Writer, r *Request, args ...string) {
4444
return
4545
}
4646

47-
if err := h.Queue.Delete(r.Context, h.Config, h.DB, r.User, &note); err != nil {
47+
if err := h.Queue.Delete(r.Context, h.DB, r.User, &note); err != nil {
4848
r.Log.Error("Failed to delete post", "note", note.ID, "error", err)
4949
w.Error()
5050
return

front/post.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func (h *Handler) post(w text.Writer, r *Request, oldNote *ap.Object, inReplyTo
247247

248248
note.Updated = now
249249

250-
err = h.Queue.UpdateNote(r.Context, h.Config, h.DB, r.User, &note)
250+
err = h.Queue.UpdateNote(r.Context, h.DB, r.User, &note)
251251
} else {
252252
err = h.Queue.Create(r.Context, h.Config, h.DB, &note, r.User)
253253
}

inbox/accept.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ import (
2525
"github.com/dimkr/tootik/ap"
2626
)
2727

28-
// Accept queues an Accept activity for delivery.
29-
func (q *Queue) Accept(ctx context.Context, followed *ap.Actor, follower, followID string, tx *sql.Tx) error {
28+
func (q *Queue) accept(ctx context.Context, followed *ap.Actor, follower, followID string, tx *sql.Tx) error {
3029
id, err := q.NewID(followed.ID, "accept")
3130
if err != nil {
3231
return err
@@ -61,11 +60,16 @@ func (q *Queue) Accept(ctx context.Context, followed *ap.Actor, follower, follow
6160
string(j),
6261
followed.ID,
6362
); err != nil {
64-
return fmt.Errorf("failed to accept %s: %w", followID, err)
63+
return err
6564
}
6665

67-
if err := q.ProcessLocalActivity(ctx, tx, followed, &accept, string(j)); err != nil {
68-
return fmt.Errorf("failed to accept %s: %w", followID, err)
66+
return q.ProcessLocalActivity(ctx, tx, followed, &accept, string(j))
67+
}
68+
69+
// Accept queues an Accept activity for delivery.
70+
func (q *Queue) Accept(ctx context.Context, followed *ap.Actor, follower, followID string, tx *sql.Tx) error {
71+
if err := q.accept(ctx, followed, follower, followID, tx); err != nil {
72+
return fmt.Errorf("failed to accept %s from %s by %s: %w", followID, follower, followed.ID, err)
6973
}
7074

7175
return nil

inbox/announce.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ import (
2626
"github.com/dimkr/tootik/ap"
2727
)
2828

29-
// Announce queues an Announce activity for delivery.
30-
func (q *Queue) Announce(ctx context.Context, tx *sql.Tx, actor *ap.Actor, note *ap.Object) error {
31-
now := time.Now()
29+
func (q *Queue) announce(ctx context.Context, tx *sql.Tx, actor *ap.Actor, note *ap.Object) error {
3230
announceID, err := q.NewID(actor.ID, "announce")
3331
if err != nil {
3432
return err
@@ -46,7 +44,7 @@ func (q *Queue) Announce(ctx context.Context, tx *sql.Tx, actor *ap.Actor, note
4644
ID: announceID,
4745
Type: ap.Announce,
4846
Actor: actor.ID,
49-
Published: ap.Time{Time: now},
47+
Published: ap.Time{Time: time.Now()},
5048
To: to,
5149
CC: cc,
5250
Object: note.ID,
@@ -64,11 +62,16 @@ func (q *Queue) Announce(ctx context.Context, tx *sql.Tx, actor *ap.Actor, note
6462
string(j),
6563
actor.ID,
6664
); err != nil {
67-
return fmt.Errorf("failed to insert announce activity: %w", err)
65+
return err
6866
}
6967

70-
if err := q.ProcessLocalActivity(ctx, tx, actor, &announce, string(j)); err != nil {
71-
return fmt.Errorf("failed to insert announce activity: %w", err)
68+
return q.ProcessLocalActivity(ctx, tx, actor, &announce, string(j))
69+
}
70+
71+
// Announce queues an Announce activity for delivery.
72+
func (q *Queue) Announce(ctx context.Context, tx *sql.Tx, actor *ap.Actor, note *ap.Object) error {
73+
if err := q.announce(ctx, tx, actor, note); err != nil {
74+
return fmt.Errorf("failed to announce %s by %s: %w", note.ID, actor.ID, err)
7275
}
7376

7477
return nil

inbox/create.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ const maxDeliveryQueueSize = 128
3131

3232
var ErrDeliveryQueueFull = errors.New("delivery queue is full")
3333

34-
// Create queues a Create activity for delivery.
35-
func (q *Queue) Create(ctx context.Context, cfg *cfg.Config, db *sql.DB, post *ap.Object, author *ap.Actor) error {
34+
func (q *Queue) create(ctx context.Context, cfg *cfg.Config, db *sql.DB, post *ap.Object, author *ap.Actor) error {
3635
id, err := q.NewID(author.ID, "create")
3736
if err != nil {
3837
return err
@@ -64,24 +63,29 @@ func (q *Queue) Create(ctx context.Context, cfg *cfg.Config, db *sql.DB, post *a
6463

6564
tx, err := db.BeginTx(ctx, nil)
6665
if err != nil {
67-
return fmt.Errorf("failed to begin transaction: %w", err)
66+
return err
6867
}
6968
defer tx.Rollback()
7069

7170
if _, err = tx.ExecContext(ctx, `insert into outbox (cid, activity, sender) values (?,jsonb(?),?)`, ap.Canonical(create.ID), string(j), author.ID); err != nil {
72-
return fmt.Errorf("failed to insert Create: %w", err)
71+
return err
7372
}
7473

7574
if err := q.ProcessLocalActivity(ctx, tx, author, &create, string(j)); err != nil {
76-
return fmt.Errorf("failed to insert Create: %w", err)
75+
return err
7776
}
7877

7978
if _, err = tx.ExecContext(ctx, `insert into feed(follower, note, author, inserted) values(?, jsonb(?), jsonb(?), unixepoch())`, author.ID, post, author); err != nil {
80-
return fmt.Errorf("failed to insert Create: %w", err)
79+
return err
8180
}
8281

83-
if err := tx.Commit(); err != nil {
84-
return fmt.Errorf("failed to create note: %w", err)
82+
return tx.Commit()
83+
}
84+
85+
// Create queues a Create activity for delivery.
86+
func (q *Queue) Create(ctx context.Context, cfg *cfg.Config, db *sql.DB, post *ap.Object, author *ap.Actor) error {
87+
if err := q.create(ctx, cfg, db, post, author); err != nil {
88+
return fmt.Errorf("failed to create %s by %s: %w", post.ID, author.ID, err)
8589
}
8690

8791
return nil

inbox/delete.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ import (
2323
"fmt"
2424

2525
"github.com/dimkr/tootik/ap"
26-
"github.com/dimkr/tootik/cfg"
2726
)
2827

29-
// Delete queues a Delete activity for delivery.
30-
func (q *Queue) Delete(ctx context.Context, cfg *cfg.Config, db *sql.DB, actor *ap.Actor, note *ap.Object) error {
28+
func (q *Queue) delete(ctx context.Context, db *sql.DB, actor *ap.Actor, note *ap.Object) error {
3129
delete := ap.Activity{
3230
Context: "https://www.w3.org/ns/activitystreams",
3331
ID: note.ID + "#delete",
@@ -48,7 +46,7 @@ func (q *Queue) Delete(ctx context.Context, cfg *cfg.Config, db *sql.DB, actor *
4846

4947
tx, err := db.BeginTx(ctx, nil)
5048
if err != nil {
51-
return fmt.Errorf("failed to begin transaction: %w", err)
49+
return err
5250
}
5351
defer tx.Rollback()
5452

@@ -58,7 +56,7 @@ func (q *Queue) Delete(ctx context.Context, cfg *cfg.Config, db *sql.DB, actor *
5856
`UPDATE outbox SET sent = 1 WHERE activity->>'$.object.id' = ? AND activity->>'$.type' = 'Create'`,
5957
note.ID,
6058
); err != nil {
61-
return fmt.Errorf("failed to insert delete activity: %w", err)
59+
return err
6260
}
6361

6462
if _, err := tx.ExecContext(
@@ -68,15 +66,20 @@ func (q *Queue) Delete(ctx context.Context, cfg *cfg.Config, db *sql.DB, actor *
6866
string(j),
6967
note.AttributedTo,
7068
); err != nil {
71-
return fmt.Errorf("failed to insert delete activity: %w", err)
69+
return err
7270
}
7371

7472
if err := q.ProcessLocalActivity(ctx, tx, actor, &delete, string(j)); err != nil {
75-
return fmt.Errorf("failed to insert delete activity: %w", err)
73+
return err
7674
}
7775

78-
if err := tx.Commit(); err != nil {
79-
return fmt.Errorf("failed to delete note: %w", err)
76+
return tx.Commit()
77+
}
78+
79+
// Delete queues a Delete activity for delivery.
80+
func (q *Queue) Delete(ctx context.Context, db *sql.DB, actor *ap.Actor, note *ap.Object) error {
81+
if err := q.delete(ctx, db, actor, note); err != nil {
82+
return fmt.Errorf("failed to delete %s by %s: %w", note.ID, actor.ID)
8083
}
8184

8285
return nil

inbox/deleter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (d *Deleter) deletePosts(ctx context.Context) (bool, error) {
101101
return false, err
102102
}
103103

104-
if err := d.Delete(ctx, d.Config, d.DB, &author, &note); err != nil {
104+
if err := d.Delete(ctx, d.DB, &author, &note); err != nil {
105105
return false, err
106106
}
107107

inbox/follow.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ import (
2525
"github.com/dimkr/tootik/ap"
2626
)
2727

28-
// Follow queues a Follow activity for delivery.
29-
func (q *Queue) Follow(ctx context.Context, follower *ap.Actor, followed string, db *sql.DB) error {
28+
func (q *Queue) follow(ctx context.Context, follower *ap.Actor, followed string, db *sql.DB) error {
3029
if followed == follower.ID {
3130
return fmt.Errorf("%s cannot follow %s", follower.ID, followed)
3231
}
@@ -55,7 +54,7 @@ func (q *Queue) Follow(ctx context.Context, follower *ap.Actor, followed string,
5554

5655
tx, err := db.BeginTx(ctx, nil)
5756
if err != nil {
58-
return fmt.Errorf("failed to begin transaction: %w", err)
57+
return err
5958
}
6059
defer tx.Rollback()
6160

@@ -66,15 +65,20 @@ func (q *Queue) Follow(ctx context.Context, follower *ap.Actor, followed string,
6665
string(j),
6766
follower.ID,
6867
); err != nil {
69-
return fmt.Errorf("failed to insert follow activity: %w", err)
68+
return err
7069
}
7170

7271
if err := q.ProcessLocalActivity(ctx, tx, follower, &follow, string(j)); err != nil {
73-
return fmt.Errorf("failed to insert follow activity: %w", err)
72+
return err
7473
}
7574

76-
if err := tx.Commit(); err != nil {
77-
return fmt.Errorf("%s failed to follow %s: %w", follower.ID, followed, err)
75+
return tx.Commit()
76+
}
77+
78+
// Follow queues a Follow activity for delivery.
79+
func (q *Queue) Follow(ctx context.Context, follower *ap.Actor, followed string, db *sql.DB) error {
80+
if err := q.follow(ctx, follower, followed, db); err != nil {
81+
return fmt.Errorf("failed to follow %s by %s: %w", followed, follower.ID, err)
7882
}
7983

8084
return nil

inbox/move.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ import (
2525
"github.com/dimkr/tootik/ap"
2626
)
2727

28-
// Move queues a Move activity for delivery.
29-
func (q *Queue) Move(ctx context.Context, db *sql.DB, from *ap.Actor, to string) error {
30-
now := time.Now()
31-
28+
func (q *Queue) move(ctx context.Context, db *sql.DB, from *ap.Actor, to string) error {
3229
aud := ap.Audience{}
3330
aud.Add(from.Followers)
3431

@@ -57,14 +54,14 @@ func (q *Queue) Move(ctx context.Context, db *sql.DB, from *ap.Actor, to string)
5754
ctx,
5855
`update persons set actor = jsonb_set(actor, '$.movedTo', $1, '$.updated', $2) where id = $3`,
5956
to,
60-
now.Format(time.RFC3339Nano),
57+
time.Now().Format(time.RFC3339Nano),
6158
from.ID,
6259
); err != nil {
63-
return fmt.Errorf("failed to insert Move: %w", err)
60+
return err
6461
}
6562

6663
if err := q.UpdateActor(ctx, tx, from.ID); err != nil {
67-
return fmt.Errorf("failed to insert Move: %w", err)
64+
return err
6865
}
6966

7067
if _, err := tx.ExecContext(
@@ -74,11 +71,16 @@ func (q *Queue) Move(ctx context.Context, db *sql.DB, from *ap.Actor, to string)
7471
&move,
7572
from.ID,
7673
); err != nil {
77-
return fmt.Errorf("failed to insert Move: %w", err)
74+
return err
7875
}
7976

80-
if err := tx.Commit(); err != nil {
81-
return fmt.Errorf("failed to insert Move: %w", err)
77+
return tx.Commit()
78+
}
79+
80+
// Move queues a Move activity for delivery.
81+
func (q *Queue) Move(ctx context.Context, db *sql.DB, from *ap.Actor, to string) error {
82+
if err := q.move(ctx, db, from, to); err != nil {
83+
return fmt.Errorf("failed to move %s to %s: %w", from.ID, to, err)
8284
}
8385

8486
return nil

inbox/poll.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (p *Poller) Run(ctx context.Context) error {
108108

109109
slog.Info("Updating poll results", "poll", poll.ID)
110110

111-
if err := p.Queue.UpdateNote(ctx, p.Config, p.DB, authors[pollID], poll); err != nil {
111+
if err := p.Queue.UpdateNote(ctx, p.DB, authors[pollID], poll); err != nil {
112112
slog.Warn("Failed to update poll results", "poll", poll.ID, "error", err)
113113
}
114114
}

0 commit comments

Comments
 (0)