2024-04-11 09:45:35 +00:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package delivery
|
|
|
|
|
|
|
|
import (
|
2024-06-20 17:06:20 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2024-04-11 09:45:35 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/httpclient"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Delivery wraps an httpclient.Request{}
|
|
|
|
// to add ActivityPub ID IRI fields of the
|
|
|
|
// outgoing activity, so that deliveries may
|
|
|
|
// be indexed (and so, dropped from queue)
|
|
|
|
// by any of these possible ID IRIs.
|
|
|
|
type Delivery struct {
|
|
|
|
// ActorID contains the ActivityPub
|
|
|
|
// actor ID IRI (if any) of the activity
|
|
|
|
// being sent out by this request.
|
|
|
|
ActorID string
|
|
|
|
|
|
|
|
// ObjectID contains the ActivityPub
|
|
|
|
// object ID IRI (if any) of the activity
|
|
|
|
// being sent out by this request.
|
|
|
|
ObjectID string
|
|
|
|
|
|
|
|
// TargetID contains the ActivityPub
|
|
|
|
// target ID IRI (if any) of the activity
|
|
|
|
// being sent out by this request.
|
|
|
|
TargetID string
|
|
|
|
|
|
|
|
// Request is the prepared (+ wrapped)
|
|
|
|
// httpclient.Client{} request that
|
|
|
|
// constitutes this ActivtyPub delivery.
|
2024-07-30 11:58:31 +00:00
|
|
|
Request *httpclient.Request
|
2024-04-11 09:45:35 +00:00
|
|
|
|
|
|
|
// internal fields.
|
|
|
|
next time.Time
|
|
|
|
}
|
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
// delivery is an internal type
|
|
|
|
// for Delivery{} that provides
|
|
|
|
// a json serialize / deserialize
|
|
|
|
// able shape that minimizes data.
|
|
|
|
type delivery struct {
|
|
|
|
ActorID string `json:"actor_id,omitempty"`
|
|
|
|
ObjectID string `json:"object_id,omitempty"`
|
|
|
|
TargetID string `json:"target_id,omitempty"`
|
|
|
|
Method string `json:"method,omitempty"`
|
|
|
|
Header map[string][]string `json:"header,omitempty"`
|
|
|
|
URL string `json:"url,omitempty"`
|
|
|
|
Body []byte `json:"body,omitempty"`
|
2024-04-11 09:45:35 +00:00
|
|
|
}
|
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
// Serialize will serialize the delivery data as data blob for storage,
|
|
|
|
// note that this will flatten some of the data, dropping signing funcs.
|
|
|
|
func (dlv *Delivery) Serialize() ([]byte, error) {
|
|
|
|
var body []byte
|
2024-04-11 09:45:35 +00:00
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
if dlv.Request.GetBody != nil {
|
|
|
|
// Fetch a fresh copy of request body.
|
|
|
|
rbody, err := dlv.Request.GetBody()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-11 09:45:35 +00:00
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
// Read request body into memory.
|
|
|
|
body, err = io.ReadAll(rbody)
|
2024-04-11 09:45:35 +00:00
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
// Done with body.
|
|
|
|
_ = rbody.Close()
|
2024-04-11 09:45:35 +00:00
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-26 12:50:46 +00:00
|
|
|
}
|
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
// Marshal as internal JSON type.
|
|
|
|
return json.Marshal(delivery{
|
|
|
|
ActorID: dlv.ActorID,
|
|
|
|
ObjectID: dlv.ObjectID,
|
|
|
|
TargetID: dlv.TargetID,
|
|
|
|
Method: dlv.Request.Method,
|
|
|
|
Header: dlv.Request.Header,
|
|
|
|
URL: dlv.Request.URL.String(),
|
|
|
|
Body: body,
|
|
|
|
})
|
2024-04-11 09:45:35 +00:00
|
|
|
}
|
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
// Deserialize will attempt to deserialize a blob of task data,
|
|
|
|
// which will involve unflattening previously serialized data and
|
|
|
|
// leave delivery incomplete, still requiring signing func setup.
|
|
|
|
func (dlv *Delivery) Deserialize(data []byte) error {
|
|
|
|
var idlv delivery
|
2024-04-26 12:50:46 +00:00
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
// Unmarshal as internal JSON type.
|
|
|
|
err := json.Unmarshal(data, &idlv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-04-26 12:50:46 +00:00
|
|
|
}
|
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
// Copy over simplest fields.
|
|
|
|
dlv.ActorID = idlv.ActorID
|
|
|
|
dlv.ObjectID = idlv.ObjectID
|
|
|
|
dlv.TargetID = idlv.TargetID
|
2024-04-11 09:45:35 +00:00
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
var body io.Reader
|
2024-04-11 09:45:35 +00:00
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
if idlv.Body != nil {
|
|
|
|
// Create new body reader from data.
|
|
|
|
body = bytes.NewReader(idlv.Body)
|
2024-04-11 09:45:35 +00:00
|
|
|
}
|
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
// Create a new request object from unmarshaled details.
|
|
|
|
r, err := http.NewRequest(idlv.Method, idlv.URL, body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-04-11 09:45:35 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 11:58:31 +00:00
|
|
|
// Copy over any stored header values.
|
|
|
|
for key, values := range idlv.Header {
|
|
|
|
for _, value := range values {
|
|
|
|
r.Header.Add(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
// Wrap request in httpclient type.
|
|
|
|
dlv.Request = httpclient.WrapRequest(r)
|
2024-04-11 09:45:35 +00:00
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
return nil
|
2024-04-11 09:45:35 +00:00
|
|
|
}
|
|
|
|
|
2024-06-20 17:06:20 +00:00
|
|
|
// backoff returns a valid (>= 0) backoff duration.
|
|
|
|
func (dlv *Delivery) backoff() time.Duration {
|
|
|
|
if dlv.next.IsZero() {
|
|
|
|
return 0
|
2024-04-11 09:45:35 +00:00
|
|
|
}
|
2024-06-20 17:06:20 +00:00
|
|
|
return time.Until(dlv.next)
|
2024-04-11 09:45:35 +00:00
|
|
|
}
|