forked from davecheney/pub
/
fetchers.go
261 lines (236 loc) · 6.41 KB
/
fetchers.go
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package activitypub
import (
"context"
"fmt"
"net/url"
"strings"
"time"
"github.com/bardic/pub/internal/algorithms"
"github.com/bardic/pub/internal/snowflake"
"github.com/bardic/pub/models"
"gorm.io/gorm"
)
type RemoteActorFetcher struct {
// signAs is the account that will be used to sign the request
signAs *models.Account
}
func NewRemoteActorFetcher(signAs *models.Account) *RemoteActorFetcher {
return &RemoteActorFetcher{
signAs: signAs,
}
}
func (f *RemoteActorFetcher) Fetch(ctx context.Context, uri string) (*models.Actor, error) {
c, err := NewClient(f.signAs)
if err != nil {
return nil, err
}
var actor struct {
Type string `json:"type"`
// The Actor's unique global identifier.
ID string `json:"id"`
Inbox string `json:"inbox"`
Outbox string `json:"outbox"`
PreferredUsername string `json:"preferredUsername"`
Name string `json:"name"`
Summary string `json:"summary"`
Icon struct {
Type string `json:"type"`
MediaType string `json:"mediaType"`
URL string `json:"url"`
} `json:"icon"`
Image struct {
Type string `json:"type"`
MediaType string `json:"mediaType"`
URL string `json:"url"`
} `json:"image"`
Endpoints struct {
SharedInbox string `json:"sharedInbox"`
} `json:"endpoints"`
ManuallyApprovesFollowers bool `json:"manuallyApprovesFollowers"`
Published time.Time `json:"published"`
PublicKey struct {
ID string `json:"id"`
Owner string `json:"owner"`
PublicKeyPem string `json:"publicKeyPem"`
} `json:"publicKey"`
Attachments []Attachment `json:"attachment"`
}
if err := c.Fetch(ctx, uri, &actor); err != nil {
return nil, err
}
published := actor.Published
if published.IsZero() {
published = time.Now()
}
u, err := url.Parse(actor.ID)
if err != nil {
return nil, err
}
return &models.Actor{
ID: snowflake.TimeToID(published),
Type: models.ActorType(actor.Type),
Name: actor.PreferredUsername,
Domain: u.Host,
URI: actor.ID,
DisplayName: actor.Name,
Locked: actor.ManuallyApprovesFollowers,
Note: actor.Summary,
Avatar: actor.Icon.URL,
Header: actor.Image.URL,
InboxURL: actor.Inbox,
OutboxURL: actor.Outbox,
SharedInboxURL: actor.Endpoints.SharedInbox,
PublicKey: []byte(actor.PublicKey.PublicKeyPem),
Attributes: attachmentsToActorAttributes(actor.Attachments),
}, nil
}
func attachmentsToActorAttributes(attachments []Attachment) []*models.ActorAttribute {
return algorithms.Map(
algorithms.Filter(
attachments,
propertyType("PropertyValue"),
),
objToActorAttribute,
)
}
func objToActorAttribute(a Attachment) *models.ActorAttribute {
return &models.ActorAttribute{
Name: a.Name,
Value: a.Value,
}
}
func propertyType(t string) func(Attachment) bool {
return func(a Attachment) bool {
return a.Type == t
}
}
type RemoteStatusFetcher struct {
signAs *models.Account
db *gorm.DB
}
func NewRemoteStatusFetcher(signAs *models.Account, db *gorm.DB) *RemoteStatusFetcher {
return &RemoteStatusFetcher{
signAs: signAs,
db: db,
}
}
func (f *RemoteStatusFetcher) Fetch(uri string) (*models.Status, error) {
fmt.Println("RemoteStatusFetcher.Fetch", uri)
ctx, cancel := context.WithTimeout(f.db.Statement.Context, 5*time.Second)
defer cancel()
c, err := NewClient(f.signAs)
if err != nil {
return nil, err
}
var status Status
if err := c.Fetch(ctx, uri, &status); err != nil {
return nil, err
}
switch status.Type {
case "Note", "Question":
// cool
default:
return nil, fmt.Errorf("unsupported type %q", status.Type)
}
var visibility string
for _, recipient := range status.To {
switch recipient {
case "https://www.w3.org/ns/activitystreams#Public":
visibility = "public"
case status.AttributedTo + "/followers":
visibility = "limited"
}
}
if visibility == "" {
for _, recipient := range status.CC {
switch recipient {
case "https://www.w3.org/ns/activitystreams#Public":
visibility = "public"
case status.AttributedTo + "/followers":
visibility = "limited"
}
}
}
if visibility == "" {
return nil, fmt.Errorf("unsupported visibility %q: %v", visibility, status)
}
conv := &models.Conversation{
Visibility: models.Visibility(visibility),
}
var inReplyTo *models.Status
if status.InReplyTo != "" {
inReplyTo, err = models.NewStatuses(f.db).FindOrCreate(status.InReplyTo, f.Fetch)
if err != nil {
return nil, err
}
conv = inReplyTo.Conversation
}
actors := NewRemoteActorFetcher(f.signAs)
actor, err := models.NewActors(f.db).FindOrCreate(status.AttributedTo, actors.Fetch)
if err != nil {
return nil, err
}
st := &models.Status{
ID: snowflake.TimeToID(status.Published),
UpdatedAt: status.Updated,
ActorID: actor.ID,
Actor: actor,
Conversation: conv,
InReplyToID: inReplyToID(inReplyTo),
InReplyToActorID: inReplyToActorID(inReplyTo),
Sensitive: status.Sensitive,
SpoilerText: status.Summary,
Visibility: conv.Visibility,
URI: status.ID,
Note: status.Content,
Attachments: attachmentsToStatusAttachments(status.Attachments),
}
for _, tag := range status.Tags {
switch tag.Type {
case "Mention":
mention, err := models.NewActors(f.db).FindOrCreate(tag.Href, actors.Fetch)
if err != nil {
return nil, err
}
st.Mentions = append(st.Mentions, models.StatusMention{
StatusID: st.ID,
ActorID: mention.ID,
Actor: mention,
})
case "Hashtag":
st.Tags = append(st.Tags, models.StatusTag{
StatusID: st.ID,
Tag: &models.Tag{
Name: strings.TrimLeft(tag.Name, "#"),
},
})
}
}
if len(status.OneOf) > 0 {
poll := &models.StatusPoll{
StatusID: st.ID,
ExpiresAt: status.EndTime,
Multiple: false,
}
for _, option := range status.OneOf {
if option.Type != "Note" {
return nil, fmt.Errorf("invalid poll option type: %q", option.Type)
}
poll.Options = append(poll.Options, models.StatusPollOption{
Title: option.Name,
Count: option.Replies.TotalItems,
})
}
st.Poll = poll
}
return st, nil
}
func attachmentsToStatusAttachments(attachments []any) []*models.StatusAttachment {
return algorithms.Map(
algorithms.Map(
attachments,
mapFromAny,
),
objToStatusAttachment,
)
}