Debian Codegraph @

link.go

raw symbols SCIP
1 package activitypub
2
3 import (
4 "bytes"
5 "encoding/gob"
6 "fmt"
7
8 "github.com/valyala/fastjson"
9 )
10
11 // LinkTypes represent the valid values for a Link object
12 var LinkTypes = ActivityVocabularyTypes{
13 LinkType,
14 MentionType,
15 }
16
17 type Links interface {
18 Link | IRI
19 }
20
21 // Link describes a qualified, indirect reference to another resource that is closely related to the conceptual model
22 // of Links as established in [RFC5988]. The properties of the Link object are not the properties of the referenced
23 // resource, but are provided as hints for rendering agents to understand how to make use of the resource.
24 // For example, height and width might represent the desired rendered size of a referenced image, rather than the
25 // actual pixel dimensions of the referenced image.
26 // The target URI of the Link is expressed using the required href property. In addition, all Link instances share the
27 // following common set of optional properties as normatively defined by the [Activity Vocabulary]:
28 //
29 // id | name | hreflang | mediaType | rel | height | width
30 //
31 // For example, all Objects can contain an image property whose value describes a graphical representation of the
32 // containing object. This property will typically be used to provide the URL to an image (e.g. JPEG, GIF or PNG)
33 // resource that can be displayed to the user. Any given object might have multiple such visual representations --
34 // multiple screenshots, for instance, or the same image at different resolutions. In [Activity Streams 2.0],
35 // there are essentially three ways of describing such references.
36 //
37 // https://www.w3.org/TR/activitystreams-core/#link
38 type Link struct {
39 // Provides the globally unique identifier for an APObject or Link.
40 ID ID `jsonld:"id,omitempty"`
41 // Identifies the APObject or Link type. Multiple values may be specified.
42 Type Typer `jsonld:"type,omitempty"`
43 // A simple, human-readable, plain-text name for the object.
44 // HTML markup MUST NOT be included. The name MAY be expressed using multiple language-tagged values.
45 Name NaturalLanguageValues `jsonld:"name,omitempty,collapsible"`
46 // A link relation associated with a Link. The value must conform to both the [HTML5] and
47 // [RFC5988](https://tools.ietf.org/html/rfc5988) "link relation" definitions.
48 // In the [HTML5], any string not containing the "space" U+0020, "tab" (U+0009), "LF" (U+000A),
49 // "FF" (U+000C), "CR" (U+000D) or "," (U+002C) characters can be used as a valid link relation.
50 Rel IRI `jsonld:"rel,omitempty"`
51 // When used on a Link, identifies the MIME media type of the referenced resource.
52 MediaType MimeType `jsonld:"mediaType,omitempty"`
53 // On a Link, specifies a hint as to the rendering height in device-independent pixels of the linked resource.
54 Height uint `jsonld:"height,omitempty"`
55 // On a Link, specifies a hint as to the rendering width in device-independent pixels of the linked resource.
56 Width uint `jsonld:"width,omitempty"`
57 // Identifies an entity that provides a preview of this object.
58 Preview Item `jsonld:"preview,omitempty"`
59 // The target resource pointed to by a Link.
60 Href IRI `jsonld:"href,omitempty"`
61 // Hints as to the language used by the target resource.
62 // Value must be a [BCP47](https://tools.ietf.org/html/bcp47) Language-Tag.
63 HrefLang LangRef `jsonld:"hrefLang,omitempty"`
64 }
65
66 // Mention is a specialized Link that represents a @mention.
67 type Mention = Link
68
69 // LinkNew initializes a new Link
70 func LinkNew(id ID, typ ActivityVocabularyType) *Link {
71 if !LinkTypes.Match(typ) {
72 typ = LinkType
73 }
74 return &Link{ID: id, Type: typ}
75 }
76
77 // MentionNew initializes a new Mention
78 func MentionNew(id ID) *Mention {
79 return &Mention{ID: id, Type: MentionType}
80 }
81
82 // IsLink validates if current Link is a Link
83 func (l Link) IsLink() bool {
84 return LinkTypes.Match(l.GetType())
85 }
86
87 // IsObject validates if current Link is an GetID
88 func (l Link) IsObject() bool {
89 return append(ObjectTypes, ObjectType).Match(l.GetType())
90 }
91
92 // IsCollection returns false for Link objects
93 func (l Link) IsCollection() bool {
94 return false
95 }
96
97 // GetID returns the ID corresponding to the Link object
98 func (l Link) GetID() ID {
99 return l.ID
100 }
101
102 // GetLink returns the IRI corresponding to the current Link
103 func (l Link) GetLink() IRI {
104 return IRI(l.ID)
105 }
106
107 // GetType returns the Type corresponding to the Mention object
108 func (l Link) GetType() Typer {
109 return l.Type
110 }
111
112 // Match returns whether the receiver matches the ActivityVocabularyType arguments.
113 func (l Link) Match(tt ...ActivityVocabularyType) bool {
114 return ActivityVocabularyTypes(tt).Match(l.Type)
115 }
116
117 // MarshalJSON encodes the receiver object to a JSON document.
118 func (l Link) MarshalJSON() ([]byte, error) {
119 b := make([]byte, 0)
120 JSONWrite(&b, '{')
121
122 if JSONWriteLinkValue(&b, l) {
123 JSONWrite(&b, '}')
124 return b, nil
125 }
126 return nil, nil
127 }
128
129 // UnmarshalJSON decodes an incoming JSON document into the receiver object.
130 func (l *Link) UnmarshalJSON(data []byte) error {
131 p := fastjson.Parser{}
132 val, err := p.ParseBytes(data)
133 if err != nil {
134 return err
135 }
136
137 return jsonLoadToLink(val, l)
138 }
139
140 // Equals verifies if our receiver Link is equals with the "with" Item
141 func (l *Link) Equals(other Item) bool {
142 if l == nil {
143 return IsNil(other)
144 }
145 otherLink, err := ToLink(other)
146 if err != nil {
147 return false
148 }
149 return l.equal(*otherLink)
150 }
151
152 // equal verifies if our receiver Link is equals with the "with" Link
153 func (l Link) equal(with Link) bool {
154 if !l.ID.Equal(with.ID) {
155 return false
156 }
157 if !TypesEqual(l.Type, with.Type) {
158 return false
159 }
160 if l.HrefLang != with.HrefLang {
161 return false
162 }
163 if !l.Href.Equal(with.Href) {
164 return false
165 }
166 if l.Rel != with.Rel {
167 return false
168 }
169 if !l.Name.Equal(with.Name) {
170 return false
171 }
172 if l.Height != with.Height {
173 return false
174 }
175 if l.Width != with.Width {
176 return false
177 }
178 if !ItemsEqual(l.Preview, with.Preview) {
179 return false
180 }
181 return true
182 }
183
184 // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
185 func (l *Link) UnmarshalBinary(data []byte) error {
186 return l.GobDecode(data)
187 }
188
189 // MarshalBinary implements the encoding.BinaryMarshaler interface.
190 func (l Link) MarshalBinary() ([]byte, error) {
191 return l.GobEncode()
192 }
193
194 func (l Link) GobEncode() ([]byte, error) {
195 mm := make(map[string][]byte)
196 hasData, err := mapLinkProperties(mm, l)
197 if err != nil {
198 return nil, err
199 }
200 if !hasData {
201 return []byte{}, nil
202 }
203 bb := bytes.Buffer{}
204 g := gob.NewEncoder(&bb)
205 if err := g.Encode(mm); err != nil {
206 return nil, err
207 }
208 return bb.Bytes(), nil
209 }
210
211 func (l *Link) GobDecode(data []byte) error {
212 if len(data) == 0 {
213 return nil
214 }
215 mm, err := gobDecodeObjectAsMap(data)
216 if err != nil {
217 return err
218 }
219 return unmapLinkProperties(mm, l)
220 }
221
222 func (l Link) Format(s fmt.State, verb rune) {
223 switch verb {
224 case 's', 'v':
225 _, _ = fmt.Fprintf(s, "%T[%s] { %s }", l, l.Type, l.Href)
226 }
227 }
228
229 // OnLink calls function fn on the "it" LinkOrIRI if it can be asserted to type *Link
230 //
231 // This function should be safe to use for all types with a structure compatible
232 // with the Link type
233 func OnLink(it LinkOrIRI, fn func(*Link) error) error {
234 if it == nil {
235 return nil
236 }
237 if IsItemCollection(it) {
238 return callOnItemCollection(it, OnLink, fn)
239 }
240 ob, err := ToLink(it)
241 if err != nil {
242 return err
243 }
244 return fn(ob)
245 }