Debian Codegraph @

helpers.go

raw symbols SCIP
1 package activitypub
2
3 import (
4 "fmt"
5 "time"
6 )
7
8 // WithLinkFn represents a function type that can be used as a parameter for OnLink helper function
9 type WithLinkFn func(*Link) error
10
11 // WithObjectFn represents a function type that can be used as a parameter for OnObject helper function
12 type WithObjectFn func(*Object) error
13
14 // WithActivityFn represents a function type that can be used as a parameter for OnActivity helper function
15 type WithActivityFn func(*Activity) error
16
17 // WithIntransitiveActivityFn represents a function type that can be used as a parameter for OnIntransitiveActivity helper function
18 type WithIntransitiveActivityFn func(*IntransitiveActivity) error
19
20 // WithCollectionInterfaceFn represents a function type that can be used as a parameter for OnCollectionIntf helper function
21 type WithCollectionInterfaceFn func(CollectionInterface) error
22
23 // WithCollectionFn represents a function type that can be used as a parameter for OnCollection helper function
24 type WithCollectionFn func(*Collection) error
25
26 // WithCollectionPageFn represents a function type that can be used as a parameter for OnCollectionPage helper function
27 type WithCollectionPageFn func(*CollectionPage) error
28
29 // WithOrderedCollectionFn represents a function type that can be used as a parameter for OnOrderedCollection helper function
30 type WithOrderedCollectionFn func(*OrderedCollection) error
31
32 // WithOrderedCollectionPageFn represents a function type that can be used as a parameter for OnOrderedCollectionPage helper function
33 type WithOrderedCollectionPageFn func(*OrderedCollectionPage) error
34
35 // WithItemCollectionFn represents a function type that can be used as a parameter for OnItemCollection helper function
36 type WithItemCollectionFn func(*ItemCollection) error
37
38 // WithIRIsFn represents a function type that can be used as a parameter for OnIRIs helper function
39 type WithIRIsFn func(*IRIs) error
40
41 func To[T Item](it Item) (*T, error) {
42 if ob, ok := it.(T); ok {
43 return &ob, nil
44 }
45 return nil, fmt.Errorf("invalid cast for object %T", it)
46 }
47
48 // On handles in a generic way the call to fn(*T) if the "it" Item can be asserted to one of the Objects type.
49 // It also covers the case where "it" is a collection of items that match the assertion.
50 func On[T Item](it Item, fn func(*T) error) error {
51 if !IsItemCollection(it) {
52 ob, err := To[T](it)
53 if err != nil {
54 return err
55 }
56 return fn(ob)
57 }
58 return OnItemCollection(it, func(col *ItemCollection) error {
59 for _, it := range *col {
60 if err := On[T](it, fn); err != nil {
61 return err
62 }
63 }
64 return nil
65 })
66 }
67
68 // OnCollectionIntf calls function fn on it Item if it can be asserted to a type
69 // that implements the CollectionInterface
70 //
71 // This function should be called if Item represents a collection of ActivityPub
72 // objects. It basically wraps functionality for the different collection types
73 // supported by the package.
74 func OnCollectionIntf(it Item, fn WithCollectionInterfaceFn) error {
75 if it == nil {
76 return nil
77 }
78 typ := it.GetType()
79 switch {
80 case CollectionOfItems.Match(typ):
81 col, err := ToItemCollection(it)
82 if err != nil {
83 return err
84 }
85 return fn(col)
86 case CollectionOfIRIs.Match(typ):
87 col, err := ToIRIs(it)
88 if err != nil {
89 return err
90 }
91 itCol := col.Collection()
92 return fn(&itCol)
93 case CollectionType.Match(typ):
94 col, err := ToCollection(it)
95 if err != nil {
96 return err
97 }
98 return fn(col)
99 case CollectionPageType.Match(typ):
100 return OnCollectionPage(it, func(p *CollectionPage) error {
101 col, err := ToCollectionPage(p)
102 if err != nil {
103 return err
104 }
105 return fn(col)
106 })
107 case OrderedCollectionType.Match(typ):
108 col, err := ToOrderedCollection(it)
109 if err != nil {
110 return err
111 }
112 return fn(col)
113 case OrderedCollectionPageType.Match(typ):
114 return OnOrderedCollectionPage(it, func(p *OrderedCollectionPage) error {
115 col, err := ToOrderedCollectionPage(p)
116 if err != nil {
117 return err
118 }
119 return fn(col)
120 })
121 default:
122 return fmt.Errorf("%T[%s] can't be converted to a Collection type", it, it.GetType())
123 }
124 }
125
126 // ItemOrderTimestamp is used for ordering a ItemCollection slice using the slice.Sort function
127 // It orders i1 and i2 based on their Published and Updated timestamps, whichever is later.
128 func ItemOrderTimestamp(i1, i2 LinkOrIRI) bool {
129 if IsNil(i1) {
130 return !IsNil(i2)
131 } else if IsNil(i2) {
132 return false
133 }
134
135 var t1 time.Time
136 var t2 time.Time
137 if IsObject(i1) {
138 o1, e1 := ToObject(i1)
139 if e1 != nil {
140 return false
141 }
142 t1 = o1.Published
143 if o1.Updated.After(t1) {
144 t1 = o1.Updated
145 }
146 }
147 if IsObject(i2) {
148 o2, e2 := ToObject(i2)
149 if e2 != nil {
150 return false
151 }
152 t2 = o2.Published
153 if o2.Updated.After(t2) {
154 t2 = o2.Updated
155 }
156 }
157 return t1.After(t2)
158 }
159
160 func notEmptyLink(l *Link) bool {
161 return len(l.ID) > 0 ||
162 LinkTypes.Match(l.GetType()) ||
163 len(l.MediaType) > 0 ||
164 l.Preview != nil ||
165 l.Name != nil ||
166 len(l.Href) > 0 ||
167 len(l.Rel) > 0 ||
168 l.HrefLang.Valid() ||
169 l.Height > 0 ||
170 l.Width > 0
171 }
172
173 func notEmptyObject(o *Object) bool {
174 if o == nil {
175 return false
176 }
177 return len(o.ID) > 0 ||
178 HasTypes(o) ||
179 ActivityTypes.Match(o.GetType()) ||
180 o.Attachment != nil ||
181 o.AttributedTo != nil ||
182 o.Audience != nil ||
183 o.BCC != nil ||
184 o.Bto != nil ||
185 o.CC != nil ||
186 o.Context != nil ||
187 o.Duration > 0 ||
188 !o.EndTime.IsZero() ||
189 o.Generator != nil ||
190 o.Icon != nil ||
191 o.Image != nil ||
192 o.InReplyTo != nil ||
193 o.Likes != nil ||
194 o.Location != nil ||
195 len(o.MediaType) > 0 ||
196 len(o.Name) > 0 ||
197 len(o.Content) > 0 ||
198 len(o.Summary) > 0 ||
199 o.Preview != nil ||
200 !o.Published.IsZero() ||
201 o.Replies != nil ||
202 o.Shares != nil ||
203 o.Source.MediaType != "" ||
204 len(o.Source.Content) > 0 ||
205 !o.StartTime.IsZero() ||
206 o.Tag != nil ||
207 o.To != nil ||
208 !o.Updated.IsZero() ||
209 o.URL != nil
210 }
211
212 func notEmptyInstransitiveActivity(i *IntransitiveActivity) bool {
213 notEmpty := i.Actor != nil ||
214 i.Target != nil ||
215 i.Result != nil ||
216 i.Origin != nil ||
217 i.Instrument != nil
218 if notEmpty {
219 return true
220 }
221 _ = OnObject(i, func(ob *Object) error {
222 notEmpty = notEmptyObject(ob)
223 return nil
224 })
225 return notEmpty
226 }
227
228 func notEmptyActivity(a *Activity) bool {
229 var notEmpty bool
230 _ = OnIntransitiveActivity(a, func(i *IntransitiveActivity) error {
231 notEmpty = notEmptyInstransitiveActivity(i)
232 return nil
233 })
234 return notEmpty || a.Object != nil
235 }
236
237 func notEmptyActor(a *Actor) bool {
238 var notEmpty bool
239 _ = OnObject(a, func(o *Object) error {
240 notEmpty = notEmptyObject(o)
241 return nil
242 })
243 return notEmpty ||
244 a.Inbox != nil ||
245 a.Outbox != nil ||
246 a.Following != nil ||
247 a.Followers != nil ||
248 a.Liked != nil ||
249 a.PreferredUsername != nil ||
250 a.Endpoints != nil ||
251 a.Streams != nil ||
252 len(a.PublicKey.ID)+len(a.PublicKey.Owner)+len(a.PublicKey.PublicKeyPem) > 0
253 }
254
255 // NotEmpty tells us if a Item interface value has a non nil value for various types
256 // that implement
257 func NotEmpty(i Item) bool {
258 if IsNil(i) {
259 return false
260 }
261 var notEmpty bool
262 if IsIRI(i) {
263 notEmpty = len(i.GetLink()) > 0
264 }
265 if i.IsCollection() {
266 _ = OnCollectionIntf(i, func(c CollectionInterface) error {
267 notEmpty = c != nil || len(c.Collection()) > 0
268 return nil
269 })
270 }
271 if ActivityTypes.Match(i.GetType()) {
272 _ = OnActivity(i, func(a *Activity) error {
273 notEmpty = notEmptyActivity(a)
274 return nil
275 })
276 } else if ActorTypes.Match(i.GetType()) {
277 _ = OnActor(i, func(a *Actor) error {
278 notEmpty = notEmptyActor(a)
279 return nil
280 })
281 } else if i.IsLink() {
282 _ = OnLink(i, func(l *Link) error {
283 notEmpty = notEmptyLink(l)
284 return nil
285 })
286 } else {
287 _ = OnObject(i, func(o *Object) error {
288 notEmpty = notEmptyObject(o)
289 return nil
290 })
291 }
292 return notEmpty
293 }
294
295 // DerefItem unpacks an Item into an ItemCollection.
296 // If the Item is a slice type, like [IRIs], or [ItemCollection], it returns an [ItemCollection] corresponding to that
297 func DerefItem(it Item) ItemCollection {
298 if IsNil(it) {
299 return nil
300 }
301
302 var items ItemCollection
303 if IsIRIs(it) {
304 if col, err := ToIRIs(it); err == nil {
305 items = col.Collection()
306 }
307 } else if IsItemCollection(it) {
308 if col, err := ToItemCollection(it); err == nil {
309 items = *col
310 }
311 } else {
312 items = ItemCollection{it}
313 }
314 return items
315 }
316
317 func callOnItemCollection[T Objects | Links, F func(*T) error](it LinkOrIRI, callFn func(LinkOrIRI, F) error, fn F) error {
318 return OnItemCollection(it, func(col *ItemCollection) error {
319 for _, ob := range *col {
320 if IsLink(ob) {
321 continue
322 }
323 if err := callFn(ob, fn); err != nil {
324 return err
325 }
326 }
327 return nil
328 })
329 }