apub_core/
activitypub.rs

1//! Traits desccribing basic activitystreams types
2
3use std::{rc::Rc, sync::Arc};
4
5use url::Url;
6
7static PUBLIC: &str = "https://www.w3.org/ns/activitystreams#Public";
8
9/// A type that represents a Public Key
10pub trait PublicKey {
11    /// The Public Key's ID
12    fn id(&self) -> &Url;
13
14    /// The Public Key's Owner
15    fn owner(&self) -> &Url;
16
17    /// The Public Key's pem-encoded value
18    fn public_key_pem(&self) -> &str;
19}
20
21/// A type that represents an Object
22pub trait Object {
23    /// The Kind of object, e.g. Note, Video, Image, etc.
24    type Kind;
25
26    /// The Object's ID
27    fn id(&self) -> &Url;
28
29    /// The kind of Object
30    fn kind(&self) -> &Self::Kind;
31}
32
33/// An object that has associated delivery fields
34pub trait DeliverableObject: Object {
35    /// Who the object is intended for
36    fn to(&self) -> &[Url];
37
38    /// Additional actors the object should be delivered to
39    fn cc(&self) -> &[Url];
40
41    /// Determine whether an object is public
42    fn is_public(&self) -> bool {
43        self.to()
44            .iter()
45            .chain(self.cc())
46            .any(|url| url.as_str() == PUBLIC)
47    }
48
49    /// Determine if a given actor is in the list of recipients for this object
50    fn has_named_recipient(&self, actor_id: &Url) -> bool {
51        self.to().iter().chain(self.cc()).any(|url| url == actor_id)
52    }
53}
54
55/// A type that represents an Activity
56pub trait Activity: DeliverableObject {
57    /// The Actor performing the activity
58    fn actor_id(&self) -> &Url;
59
60    /// The object being performed upon
61    fn object_id(&self) -> &Url;
62}
63
64/// A type that represents an Actor
65pub trait Actor: Object {
66    /// The actor's inbox
67    fn inbox(&self) -> &Url;
68
69    /// The actor's outbox
70    fn outbox(&self) -> &Url;
71
72    /// The actor's preferred username (handle)
73    fn preferred_username(&self) -> &str;
74
75    /// The actor's public key ID
76    fn public_key_id(&self) -> &Url;
77
78    /// The actor's display name
79    fn name(&self) -> Option<&str>;
80
81    /// The actor's shared inbox
82    fn shared_inbox(&self) -> Option<&Url>;
83
84    /// Determine the name of an actor
85    fn canonical_name(&self) -> &str {
86        self.name().unwrap_or_else(|| self.preferred_username())
87    }
88
89    /// Determine which inbox an object should be delievered to
90    fn delivery_inbox<O: DeliverableObject>(&self, obj: &O) -> &Url {
91        if obj.is_public() {
92            self.shared_inbox().unwrap_or_else(|| self.inbox())
93        } else {
94            self.inbox()
95        }
96    }
97}
98
99impl<'a, T> PublicKey for &'a T
100where
101    T: PublicKey,
102{
103    fn id(&self) -> &Url {
104        T::id(self)
105    }
106
107    fn owner(&self) -> &Url {
108        T::owner(self)
109    }
110
111    fn public_key_pem(&self) -> &str {
112        T::public_key_pem(self)
113    }
114}
115
116impl<'a, T> PublicKey for &'a mut T
117where
118    T: PublicKey,
119{
120    fn id(&self) -> &Url {
121        T::id(self)
122    }
123
124    fn owner(&self) -> &Url {
125        T::owner(self)
126    }
127
128    fn public_key_pem(&self) -> &str {
129        T::public_key_pem(self)
130    }
131}
132
133impl<T> PublicKey for Box<T>
134where
135    T: PublicKey,
136{
137    fn id(&self) -> &Url {
138        T::id(self)
139    }
140
141    fn owner(&self) -> &Url {
142        T::owner(self)
143    }
144
145    fn public_key_pem(&self) -> &str {
146        T::public_key_pem(self)
147    }
148}
149
150impl<T> PublicKey for Rc<T>
151where
152    T: PublicKey,
153{
154    fn id(&self) -> &Url {
155        T::id(self)
156    }
157
158    fn owner(&self) -> &Url {
159        T::owner(self)
160    }
161
162    fn public_key_pem(&self) -> &str {
163        T::public_key_pem(self)
164    }
165}
166
167impl<T> PublicKey for Arc<T>
168where
169    T: PublicKey,
170{
171    fn id(&self) -> &Url {
172        T::id(self)
173    }
174
175    fn owner(&self) -> &Url {
176        T::owner(self)
177    }
178
179    fn public_key_pem(&self) -> &str {
180        T::public_key_pem(self)
181    }
182}
183
184impl<'a, T> Object for &'a T
185where
186    T: Object,
187{
188    type Kind = T::Kind;
189
190    fn id(&self) -> &Url {
191        T::id(self)
192    }
193
194    fn kind(&self) -> &Self::Kind {
195        T::kind(self)
196    }
197}
198
199impl<'a, T> Object for &'a mut T
200where
201    T: Object,
202{
203    type Kind = T::Kind;
204
205    fn id(&self) -> &Url {
206        T::id(self)
207    }
208
209    fn kind(&self) -> &Self::Kind {
210        T::kind(self)
211    }
212}
213
214impl<T> Object for Box<T>
215where
216    T: Object,
217{
218    type Kind = T::Kind;
219
220    fn id(&self) -> &Url {
221        T::id(self)
222    }
223
224    fn kind(&self) -> &Self::Kind {
225        T::kind(self)
226    }
227}
228
229impl<T> Object for Rc<T>
230where
231    T: Object,
232{
233    type Kind = T::Kind;
234
235    fn id(&self) -> &Url {
236        T::id(self)
237    }
238
239    fn kind(&self) -> &Self::Kind {
240        T::kind(self)
241    }
242}
243
244impl<T> Object for Arc<T>
245where
246    T: Object,
247{
248    type Kind = T::Kind;
249
250    fn id(&self) -> &Url {
251        T::id(self)
252    }
253
254    fn kind(&self) -> &Self::Kind {
255        T::kind(self)
256    }
257}
258
259impl<'a, T> DeliverableObject for &'a T
260where
261    T: DeliverableObject,
262{
263    fn to(&self) -> &[Url] {
264        T::to(self)
265    }
266
267    fn cc(&self) -> &[Url] {
268        T::cc(self)
269    }
270}
271
272impl<'a, T> DeliverableObject for &'a mut T
273where
274    T: DeliverableObject,
275{
276    fn to(&self) -> &[Url] {
277        T::to(self)
278    }
279
280    fn cc(&self) -> &[Url] {
281        T::cc(self)
282    }
283}
284
285impl<T> DeliverableObject for Box<T>
286where
287    T: DeliverableObject,
288{
289    fn to(&self) -> &[Url] {
290        T::to(self)
291    }
292
293    fn cc(&self) -> &[Url] {
294        T::cc(self)
295    }
296}
297
298impl<T> DeliverableObject for Rc<T>
299where
300    T: DeliverableObject,
301{
302    fn to(&self) -> &[Url] {
303        T::to(self)
304    }
305
306    fn cc(&self) -> &[Url] {
307        T::cc(self)
308    }
309}
310
311impl<T> DeliverableObject for Arc<T>
312where
313    T: DeliverableObject,
314{
315    fn to(&self) -> &[Url] {
316        T::to(self)
317    }
318
319    fn cc(&self) -> &[Url] {
320        T::cc(self)
321    }
322}
323
324impl<'a, T> Activity for &'a T
325where
326    T: Activity,
327{
328    fn actor_id(&self) -> &Url {
329        T::actor_id(self)
330    }
331
332    fn object_id(&self) -> &Url {
333        T::object_id(self)
334    }
335}
336
337impl<'a, T> Activity for &'a mut T
338where
339    T: Activity,
340{
341    fn actor_id(&self) -> &Url {
342        T::actor_id(self)
343    }
344
345    fn object_id(&self) -> &Url {
346        T::object_id(self)
347    }
348}
349
350impl<T> Activity for Box<T>
351where
352    T: Activity,
353{
354    fn actor_id(&self) -> &Url {
355        T::actor_id(self)
356    }
357
358    fn object_id(&self) -> &Url {
359        T::object_id(self)
360    }
361}
362
363impl<T> Activity for Rc<T>
364where
365    T: Activity,
366{
367    fn actor_id(&self) -> &Url {
368        T::actor_id(self)
369    }
370
371    fn object_id(&self) -> &Url {
372        T::object_id(self)
373    }
374}
375
376impl<T> Activity for Arc<T>
377where
378    T: Activity,
379{
380    fn actor_id(&self) -> &Url {
381        T::actor_id(self)
382    }
383
384    fn object_id(&self) -> &Url {
385        T::object_id(self)
386    }
387}
388
389impl<'a, T> Actor for &'a T
390where
391    T: Actor,
392{
393    fn inbox(&self) -> &Url {
394        T::inbox(self)
395    }
396
397    fn outbox(&self) -> &Url {
398        T::outbox(self)
399    }
400
401    fn preferred_username(&self) -> &str {
402        T::preferred_username(self)
403    }
404
405    fn public_key_id(&self) -> &Url {
406        T::public_key_id(self)
407    }
408
409    fn name(&self) -> Option<&str> {
410        T::name(self)
411    }
412
413    fn shared_inbox(&self) -> Option<&Url> {
414        T::shared_inbox(self)
415    }
416}
417
418impl<'a, T> Actor for &'a mut T
419where
420    T: Actor,
421{
422    fn inbox(&self) -> &Url {
423        T::inbox(self)
424    }
425
426    fn outbox(&self) -> &Url {
427        T::outbox(self)
428    }
429
430    fn preferred_username(&self) -> &str {
431        T::preferred_username(self)
432    }
433
434    fn public_key_id(&self) -> &Url {
435        T::public_key_id(self)
436    }
437
438    fn name(&self) -> Option<&str> {
439        T::name(self)
440    }
441
442    fn shared_inbox(&self) -> Option<&Url> {
443        T::shared_inbox(self)
444    }
445}
446
447impl<T> Actor for Box<T>
448where
449    T: Actor,
450{
451    fn inbox(&self) -> &Url {
452        T::inbox(self)
453    }
454
455    fn outbox(&self) -> &Url {
456        T::outbox(self)
457    }
458
459    fn preferred_username(&self) -> &str {
460        T::preferred_username(self)
461    }
462
463    fn public_key_id(&self) -> &Url {
464        T::public_key_id(self)
465    }
466
467    fn name(&self) -> Option<&str> {
468        T::name(self)
469    }
470
471    fn shared_inbox(&self) -> Option<&Url> {
472        T::shared_inbox(self)
473    }
474}
475
476impl<T> Actor for Rc<T>
477where
478    T: Actor,
479{
480    fn inbox(&self) -> &Url {
481        T::inbox(self)
482    }
483
484    fn outbox(&self) -> &Url {
485        T::outbox(self)
486    }
487
488    fn preferred_username(&self) -> &str {
489        T::preferred_username(self)
490    }
491
492    fn public_key_id(&self) -> &Url {
493        T::public_key_id(self)
494    }
495
496    fn name(&self) -> Option<&str> {
497        T::name(self)
498    }
499
500    fn shared_inbox(&self) -> Option<&Url> {
501        T::shared_inbox(self)
502    }
503}
504
505impl<T> Actor for Arc<T>
506where
507    T: Actor,
508{
509    fn inbox(&self) -> &Url {
510        T::inbox(self)
511    }
512
513    fn outbox(&self) -> &Url {
514        T::outbox(self)
515    }
516
517    fn preferred_username(&self) -> &str {
518        T::preferred_username(self)
519    }
520
521    fn public_key_id(&self) -> &Url {
522        T::public_key_id(self)
523    }
524
525    fn name(&self) -> Option<&str> {
526        T::name(self)
527    }
528
529    fn shared_inbox(&self) -> Option<&Url> {
530        T::shared_inbox(self)
531    }
532}