/
fetchers.go
167 lines (149 loc) · 4.06 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
package activitypub
import (
"github.com/davecheney/pub/internal/algorithms"
"github.com/davecheney/pub/models"
)
func attachmentsToActorAttributes(attachments []Attachment) []*models.ActorAttachment {
return algorithms.Map(
algorithms.Filter(
attachments,
propertyType("PropertyValue"),
),
objToActorAttribute,
)
}
func objToActorAttribute(a Attachment) *models.ActorAttachment {
return &models.ActorAttachment{
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).FindOrCreateByURI(status.InReplyTo)
// if err != nil {
// return nil, err
// }
// conv = inReplyTo.Conversation
// }
// actor, err := models.NewActors(f.db).FindOrCreateByURI(status.AttributedTo)
// if err != nil {
// return nil, err
// }
// st := &models.Status{
// ObjectID: snowflake.TimeToID(status.Published),
// UpdatedAt: status.Updated,
// ActorID: actor.ObjectID,
// Actor: actor,
// Conversation: conv,
// InReplyToID: inReplyToID(inReplyTo),
// InReplyToActorID: inReplyToActorID(inReplyTo),
// Visibility: conv.Visibility,
// }
// 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,
// )
// }