1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
// 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 (
"bytes"
"encoding/json"
"io"
"net/http"
"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.
Request *httpclient.Request
// internal fields.
next time.Time
}
// 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"`
}
// 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
if dlv.Request.GetBody != nil {
// Fetch a fresh copy of request body.
rbody, err := dlv.Request.GetBody()
if err != nil {
return nil, err
}
// Read request body into memory.
body, err = io.ReadAll(rbody)
// Done with body.
_ = rbody.Close()
if err != nil {
return nil, err
}
}
// 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,
})
}
// 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
// Unmarshal as internal JSON type.
err := json.Unmarshal(data, &idlv)
if err != nil {
return err
}
// Copy over simplest fields.
dlv.ActorID = idlv.ActorID
dlv.ObjectID = idlv.ObjectID
dlv.TargetID = idlv.TargetID
var body io.Reader
if idlv.Body != nil {
// Create new body reader from data.
body = bytes.NewReader(idlv.Body)
}
// Create a new request object from unmarshaled details.
r, err := http.NewRequest(idlv.Method, idlv.URL, body)
if err != nil {
return err
}
// Copy over any stored header values.
for key, values := range idlv.Header {
for _, value := range values {
r.Header.Add(key, value)
}
}
// Wrap request in httpclient type.
dlv.Request = httpclient.WrapRequest(r)
return nil
}
// backoff returns a valid (>= 0) backoff duration.
func (dlv *Delivery) backoff() time.Duration {
if dlv.next.IsZero() {
return 0
}
return time.Until(dlv.next)
}
|