support deleting queued http requests by target ID

This commit is contained in:
kim 2024-04-03 16:58:59 +01:00
parent 8fdd5b18aa
commit d7e14d5dbb
4 changed files with 36 additions and 5 deletions

View file

@ -79,6 +79,7 @@ func (f *federate) DeleteAccount(ctx context.Context, account *gtsmodel.Account)
// (this stops any queued likes, boosts, creates etc).
f.state.Queues.APRequests.Delete("ActorID", account.URI)
f.state.Queues.APRequests.Delete("ObjectID", account.URI)
f.state.Queues.APRequests.Delete("TargetID", account.URI)
// Parse relevant URI(s).
outboxIRI, err := parseURI(account.OutboxURI)
@ -230,6 +231,7 @@ func (f *federate) DeleteStatus(ctx context.Context, status *gtsmodel.Status) er
// Drop any queued outgoing http requests for status,
// (this stops any queued likes, boosts, creates etc).
f.state.Queues.APRequests.Delete("ObjectID", status.URI)
f.state.Queues.APRequests.Delete("TargetID", status.URI)
// Ensure the status model is fully populated.
if err := f.state.DB.PopulateStatus(ctx, status); err != nil {

View file

@ -63,6 +63,9 @@ type APRequest struct {
// ObjectID ...
ObjectID string
// TargetID ...
TargetID string
// Request ...
Request *http.Request
}

View file

@ -39,7 +39,9 @@ func (q *Queues) Init() {
func (q *Queues) initHTTPRequest() {
q.APRequests.Init(structr.QueueConfig[*APRequest]{
Indices: []structr.IndexConfig{
{Fields: "ActorID", Multiple: true},
{Fields: "ObjectID", Multiple: true},
{Fields: "TargetID", Multiple: true},
{Fields: "Request.URL.Host", Multiple: true},
},
})

View file

@ -51,8 +51,9 @@ func (t *transport) BatchDeliver(ctx context.Context, obj map[string]interface{}
}
// Extract object IDs.
objID := getObjectID(obj)
actID := getActorID(obj)
objID := getObjectID(obj)
tgtID := getTargetID(obj)
for _, to := range recipients {
// Skip delivery to recipient if it is "us".
@ -60,8 +61,14 @@ func (t *transport) BatchDeliver(ctx context.Context, obj map[string]interface{}
continue
}
// Prepare new outgoing http client request.
req, err := t.prepare(ctx, objID, actID, b, to)
// Prepare http client request.
req, err := t.prepare(ctx,
actID,
objID,
tgtID,
b,
to,
)
if err != nil {
errs.Append(err)
continue
@ -92,8 +99,9 @@ func (t *transport) Deliver(ctx context.Context, obj map[string]interface{}, to
// Prepare http client request.
req, err := t.prepare(ctx,
getObjectID(obj),
getActorID(obj),
getObjectID(obj),
getTargetID(obj),
b,
to,
)
@ -112,8 +120,9 @@ func (t *transport) Deliver(ctx context.Context, obj map[string]interface{}, to
// request object with signing function.
func (t *transport) prepare(
ctx context.Context,
objectID string,
actorID string,
objectID string,
targetID string,
data []byte,
to *url.URL,
) (
@ -142,7 +151,9 @@ func (t *transport) prepare(
req.Header.Add("Accept-Charset", "utf-8")
return &queue.APRequest{
ActorID: actorID,
ObjectID: objectID,
TargetID: targetID,
Request: req,
}, nil
}
@ -172,3 +183,16 @@ func getActorID(obj map[string]interface{}) string {
return ""
}
}
// getTargetID extracts a target ID from 'serialized' ActivityPub object map.
func getTargetID(obj map[string]interface{}) string {
switch t := obj["target"].(type) {
case string:
return t
case map[string]interface{}:
id, _ := t["id"].(string)
return id
default:
return ""
}
}