| 1 |
package activitypub |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/gob" |
| 6 |
"fmt" |
| 7 |
|
| 8 |
"github.com/valyala/fastjson" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
var LinkTypes = ActivityVocabularyTypes{ |
| 13 |
LinkType, |
| 14 |
MentionType, |
| 15 |
} |
| 16 |
|
| 17 |
type Links interface { |
| 18 |
Link | IRI |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
type Link struct { |
| 39 |
|
| 40 |
ID ID `jsonld:"id,omitempty"` |
| 41 |
|
| 42 |
Type Typer `jsonld:"type,omitempty"` |
| 43 |
|
| 44 |
|
| 45 |
Name NaturalLanguageValues `jsonld:"name,omitempty,collapsible"` |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
Rel IRI `jsonld:"rel,omitempty"` |
| 51 |
|
| 52 |
MediaType MimeType `jsonld:"mediaType,omitempty"` |
| 53 |
|
| 54 |
Height uint `jsonld:"height,omitempty"` |
| 55 |
|
| 56 |
Width uint `jsonld:"width,omitempty"` |
| 57 |
|
| 58 |
Preview Item `jsonld:"preview,omitempty"` |
| 59 |
|
| 60 |
Href IRI `jsonld:"href,omitempty"` |
| 61 |
|
| 62 |
|
| 63 |
HrefLang LangRef `jsonld:"hrefLang,omitempty"` |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
type Mention = Link |
| 68 |
|
| 69 |
|
| 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 |
|
| 78 |
func MentionNew(id ID) *Mention { |
| 79 |
return &Mention{ID: id, Type: MentionType} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
func (l Link) IsLink() bool { |
| 84 |
return LinkTypes.Match(l.GetType()) |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
func (l Link) IsObject() bool { |
| 89 |
return append(ObjectTypes, ObjectType).Match(l.GetType()) |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
func (l Link) IsCollection() bool { |
| 94 |
return false |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
func (l Link) GetID() ID { |
| 99 |
return l.ID |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
func (l Link) GetLink() IRI { |
| 104 |
return IRI(l.ID) |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
func (l Link) GetType() Typer { |
| 109 |
return l.Type |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
func (l Link) Match(tt ...ActivityVocabularyType) bool { |
| 114 |
return ActivityVocabularyTypes(tt).Match(l.Type) |
| 115 |
} |
| 116 |
|
| 117 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 185 |
func (l *Link) UnmarshalBinary(data []byte) error { |
| 186 |
return l.GobDecode(data) |
| 187 |
} |
| 188 |
|
| 189 |
|
| 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 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 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 |
} |