Blob


1 // apub is an implementation of the ActivityPub protocol.
2 //
3 // https://www.w3.org/TR/activitypub/
4 // https://www.w3.org/TR/activitystreams-core/
5 // https://www.w3.org/TR/activitystreams-vocabulary/
6 package apub
8 import (
9 "bytes"
10 "encoding/json"
11 "errors"
12 "fmt"
13 "io"
14 "net/mail"
15 "strings"
16 "time"
17 )
19 // NormContext is a URL referencing the
20 // normative Activity Streams 2.0 JSON-LD @context definition.
21 // All [Activity] variables should have their AtContext field set to this value.
22 // See [Activity Streams 2.0] section 2.1.
23 //
24 // [Activity Streams 2.0]: https://www.w3.org/TR/activitystreams-core/
25 const NormContext string = "https://www.w3.org/ns/activitystreams"
27 // ContentType is the MIME media type for ActivityPub.
28 const ContentType string = "application/activity+json"
30 // PublicCollection is the ActivityPub ID for the special collection indicating public access.
31 // Any Activity addressed to this collection is meant to be available to all users,
32 // authenticated or not.
33 // See W3C Recommendation ActivityPub Section 5.6.
34 const PublicCollection string = "https://www.w3.org/ns/activitystreams#Public"
36 var ErrNotExist = errors.New("no such activity")
38 // Activity represents the Activity Streams Object core type.
39 // See Activity Streams 2.0, section 4.1.
40 type Activity struct {
41 AtContext string `json:"@context"`
42 ID string `json:"id,omitempty"`
43 Type string `json:"type"`
44 Name string `json:"name,omitempty"`
45 Actor string `json:"actor,omitempty"`
46 Username string `json:"preferredUsername,omitempty"`
47 Summary string `json:"summary,omitempty"`
48 Inbox string `json:"inbox,omitempty"`
49 Outbox string `json:"outbox,omitempty"`
50 To []string `json:"to,omitempty"`
51 CC []string `json:"cc,omitempty"`
52 Followers string `json:"followers,omitempty"`
53 InReplyTo string `json:"inReplyTo,omitempty"`
54 Published *time.Time `json:"published,omitempty"`
55 AttributedTo string `json:"attributedTo,omitempty"`
56 Content string `json:"content,omitempty"`
57 MediaType string `json:"mediaType,omitempty"`
58 Source struct {
59 Content string `json:"content,omitempty"`
60 MediaType string `json:"mediaType,omitempty"`
61 } `json:"source,omitempty"`
62 PublicKey *PublicKey `json:"publicKey,omitempty"`
63 Audience string `json:"audience,omitempty"`
64 Href string `json:"href,omitempty"`
65 Tag []Activity `json:"tag,omitempty"`
66 // Contains a JSON-encoded Activity, or a URL as a JSON string
67 // pointing to an Activity. Use Activity.Unwrap() to access
68 // the enclosed, decoded value.
69 Object json.RawMessage `json:"object,omitempty"`
70 }
72 func (act *Activity) UnmarshalJSON(b []byte) error {
73 type Alias Activity
74 aux := &struct {
75 AtContext interface{} `json:"@context"`
76 Object interface{}
77 *Alias
78 }{
79 Alias: (*Alias)(act),
80 }
81 if err := json.Unmarshal(b, &aux); err != nil {
82 return err
83 }
84 switch v := aux.AtContext.(type) {
85 case string:
86 act.AtContext = v
87 case []interface{}:
88 if vv, ok := v[0].(string); ok {
89 act.AtContext = vv
90 }
91 }
92 return nil
93 }
95 // Unwrap returns the JSON-encoded Activity, if any, enclosed in act.
96 // The Activity may be referenced by ID,
97 // in which case the activity is looked up by client or by
98 // apub.defaultClient if client is nil.
99 func (act *Activity) Unwrap(client *Client) (*Activity, error) {
100 if act.Object == nil {
101 return nil, errors.New("no wrapped activity")
104 var buf io.Reader
105 buf = bytes.NewReader(act.Object)
106 if strings.HasPrefix(string(act.Object), "https") {
107 if client == nil {
108 return Lookup(string(act.Object))
110 return client.Lookup(string(act.Object))
112 return Decode(buf)
115 func Decode(r io.Reader) (*Activity, error) {
116 var a Activity
117 if err := json.NewDecoder(r).Decode(&a); err != nil {
118 return nil, fmt.Errorf("decode activity: %w", err)
120 return &a, nil
123 func DecodeActor(r io.Reader) (*Actor, error) {
124 a, err := Decode(r)
125 if err != nil {
126 return nil, err
128 return activityToActor(a), nil
131 type Actor struct {
132 AtContext string `json:"@context"`
133 ID string `json:"id"`
134 Type string `json:"type"`
135 Name string `json:"name"`
136 Username string `json:"preferredUsername"`
137 Summary string `json:"summary,omitempty"`
138 Inbox string `json:"inbox"`
139 Outbox string `json:"outbox"`
140 Followers string `json:"followers"`
141 Published *time.Time `json:"published,omitempty"`
142 PublicKey PublicKey `json:"publicKey"`
145 type PublicKey struct {
146 ID string `json:"id"`
147 Owner string `json:"owner"`
148 PublicKeyPEM string `json:"publicKeyPem"`
151 // Address generates the most likely address of the Actor.
152 // The Actor's name (not the username) is used as the address' proper name, if present.
153 // Implementors should verify the address using WebFinger.
154 // For example, the address for the Actor ID
155 // https://hachyderm.io/users/otl is:
156 //
157 // "Oliver Lowe" <otl@hachyderm.io>
158 func (a *Actor) Address() *mail.Address {
159 if a.Username == "" && a.Name == "" {
160 return &mail.Address{"", a.ID}
162 trimmed := strings.TrimPrefix(a.ID, "https://")
163 host, _, _ := strings.Cut(trimmed, "/")
164 addr := fmt.Sprintf("%s@%s", a.Username, host)
165 return &mail.Address{a.Name, addr}
168 // FollowersAddress generates a non-standard address representing the Actor's followers
169 // using plus addressing.
170 // It is the Actor's address username part with a "+followers" suffix.
171 // The address cannot be resolved using WebFinger.
172 //
173 // For example, the followers address for Actor ID
174 // https://hachyderm.io/users/otl is:
175 //
176 // "Oliver Lowe (followers)" <otl+followers@hachyderm.io>
177 func (a *Actor) FollowersAddress() *mail.Address {
178 if a.Followers == "" {
179 return &mail.Address{"", ""}
181 addr := a.Address()
182 user, domain, found := strings.Cut(addr.Address, "@")
183 if !found {
184 return &mail.Address{"", ""}
186 addr.Address = fmt.Sprintf("%s+followers@%s", user, domain)
187 if addr.Name != "" {
188 addr.Name += " (followers)"
190 return addr