| 1 |
package activitypub |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/gob" |
| 6 |
"fmt" |
| 7 |
"slices" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/valyala/fastjson" |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
type ActivityVocabularyType string |
| 15 |
|
| 16 |
|
| 17 |
type ActivityVocabularyTypes []ActivityVocabularyType |
| 18 |
|
| 19 |
|
| 20 |
var Types = ActivityVocabularyTypes{ |
| 21 |
LinkType, |
| 22 |
MentionType, |
| 23 |
|
| 24 |
ArticleType, |
| 25 |
AudioType, |
| 26 |
DocumentType, |
| 27 |
EventType, |
| 28 |
ImageType, |
| 29 |
NoteType, |
| 30 |
PageType, |
| 31 |
PlaceType, |
| 32 |
ProfileType, |
| 33 |
RelationshipType, |
| 34 |
TombstoneType, |
| 35 |
VideoType, |
| 36 |
|
| 37 |
QuestionType, |
| 38 |
|
| 39 |
CollectionType, |
| 40 |
OrderedCollectionType, |
| 41 |
CollectionPageType, |
| 42 |
OrderedCollectionPageType, |
| 43 |
|
| 44 |
ApplicationType, |
| 45 |
GroupType, |
| 46 |
OrganizationType, |
| 47 |
PersonType, |
| 48 |
ServiceType, |
| 49 |
|
| 50 |
AcceptType, |
| 51 |
AddType, |
| 52 |
AnnounceType, |
| 53 |
BlockType, |
| 54 |
CreateType, |
| 55 |
DeleteType, |
| 56 |
DislikeType, |
| 57 |
FlagType, |
| 58 |
FollowType, |
| 59 |
IgnoreType, |
| 60 |
InviteType, |
| 61 |
JoinType, |
| 62 |
LeaveType, |
| 63 |
LikeType, |
| 64 |
ListenType, |
| 65 |
MoveType, |
| 66 |
OfferType, |
| 67 |
RejectType, |
| 68 |
ReadType, |
| 69 |
RemoveType, |
| 70 |
TentativeRejectType, |
| 71 |
TentativeAcceptType, |
| 72 |
UndoType, |
| 73 |
UpdateType, |
| 74 |
ViewType, |
| 75 |
|
| 76 |
ArriveType, |
| 77 |
TravelType, |
| 78 |
QuestionType, |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
func EmptyTypes(tt ...ActivityVocabularyType) bool { |
| 83 |
if len(tt) == 0 { |
| 84 |
return true |
| 85 |
} |
| 86 |
return len(flattenTypes(tt...)) == 0 |
| 87 |
} |
| 88 |
|
| 89 |
func (t ActivityVocabularyTypes) AsTypes() ActivityVocabularyTypes { |
| 90 |
return t |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
func (t ActivityVocabularyTypes) Match(other Typer) bool { |
| 95 |
return AnyTypes(t...).Match(types(other)...) |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
func (t ActivityVocabularyTypes) MarshalJSON() ([]byte, error) { |
| 100 |
b := []byte{} |
| 101 |
if !JSONWriteActivityVocabularyTypes(&b, t) { |
| 102 |
return nil, fmt.Errorf("error JSON encoding ActivityVocabularyTypes") |
| 103 |
} |
| 104 |
return b, nil |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
func (t *ActivityVocabularyTypes) UnmarshalJSON(b []byte) error { |
| 109 |
if t == nil { |
| 110 |
return fmt.Errorf("nil ActivityVocabularyTypes receiver") |
| 111 |
} |
| 112 |
p := fastjson.Parser{} |
| 113 |
val, err := p.ParseBytes(b) |
| 114 |
if err != nil { |
| 115 |
return err |
| 116 |
} |
| 117 |
if types := JSONGetTypes(val); types != nil { |
| 118 |
if typ, ok := types.(ActivityVocabularyType); ok { |
| 119 |
*t = ActivityVocabularyTypes{typ} |
| 120 |
} |
| 121 |
if typ, ok := types.(ActivityVocabularyTypes); ok { |
| 122 |
*t = typ |
| 123 |
} |
| 124 |
} |
| 125 |
return nil |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
func (a ActivityVocabularyTypes) GobEncode() ([]byte, error) { |
| 130 |
switch len(a) { |
| 131 |
case 0: |
| 132 |
return nil, nil |
| 133 |
case 1: |
| 134 |
return a[0].GobEncode() |
| 135 |
default: |
| 136 |
tt := make([][]byte, len(a)) |
| 137 |
for i, ty := range a { |
| 138 |
b, err := ty.GobEncode() |
| 139 |
if err != nil { |
| 140 |
return nil, err |
| 141 |
} |
| 142 |
tt[i] = b |
| 143 |
} |
| 144 |
b := bytes.Buffer{} |
| 145 |
err := gob.NewEncoder(&b).Encode(tt) |
| 146 |
return b.Bytes(), err |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
func (a *ActivityVocabularyTypes) GobDecode(data []byte) error { |
| 152 |
if len(data) == 0 { |
| 153 |
return nil |
| 154 |
} |
| 155 |
|
| 156 |
if data[0] == '[' { |
| 157 |
tt := [][]byte{} |
| 158 |
g := gob.NewDecoder(bytes.NewReader(data)) |
| 159 |
if err := g.Decode(&tt); err != nil { |
| 160 |
return err |
| 161 |
} |
| 162 |
types := make(ActivityVocabularyTypes, len(tt)) |
| 163 |
for i, it := range tt { |
| 164 |
if err := types[i].GobDecode(it); err != nil { |
| 165 |
return err |
| 166 |
} |
| 167 |
} |
| 168 |
if a == nil { |
| 169 |
a = &types |
| 170 |
} else { |
| 171 |
*a = types |
| 172 |
} |
| 173 |
} else { |
| 174 |
at := NilType |
| 175 |
if err := at.GobDecode(data); err != nil { |
| 176 |
return err |
| 177 |
} |
| 178 |
if a == nil { |
| 179 |
a = &ActivityVocabularyTypes{at} |
| 180 |
} else { |
| 181 |
*a = ActivityVocabularyTypes{at} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return nil |
| 186 |
} |
| 187 |
|
| 188 |
func HasTypes(it ActivityObject) bool { |
| 189 |
if it == nil || it.GetType() == nil { |
| 190 |
return false |
| 191 |
} |
| 192 |
return !EmptyTypes(it.GetType().AsTypes()...) |
| 193 |
} |
| 194 |
|
| 195 |
func TypesEqual(m1, m2 Typer) bool { |
| 196 |
matcherIsNilOrZero := func(m Typer) bool { |
| 197 |
if m == nil { |
| 198 |
return true |
| 199 |
} |
| 200 |
all := m.AsTypes() |
| 201 |
if len(all) == 0 { |
| 202 |
return true |
| 203 |
} |
| 204 |
for _, t := range all { |
| 205 |
if t != NilType { |
| 206 |
return false |
| 207 |
} |
| 208 |
} |
| 209 |
return true |
| 210 |
} |
| 211 |
if m1 == nil || m2 == nil { |
| 212 |
return matcherIsNilOrZero(m1) && matcherIsNilOrZero(m2) |
| 213 |
} |
| 214 |
|
| 215 |
t1 := m1.AsTypes() |
| 216 |
t2 := m2.AsTypes() |
| 217 |
return AnyTypes(t1...).Match(t2...) |
| 218 |
} |
| 219 |
|
| 220 |
func (a ActivityVocabularyType) MarshalJSON() ([]byte, error) { |
| 221 |
if len(a) == 0 { |
| 222 |
return nil, nil |
| 223 |
} |
| 224 |
b := make([]byte, 0) |
| 225 |
JSONWriteStringValue(&b, string(a)) |
| 226 |
return b, nil |
| 227 |
} |
| 228 |
|
| 229 |
func (t ActivityVocabularyType) String() string { |
| 230 |
return string(t) |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
func (a ActivityVocabularyType) GobEncode() ([]byte, error) { |
| 235 |
return []byte(a), nil |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
func (a *ActivityVocabularyType) GobDecode(data []byte) error { |
| 240 |
*a = ActivityVocabularyType(data) |
| 241 |
return nil |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
func (a *ActivityVocabularyType) UnmarshalBinary(data []byte) error { |
| 246 |
return a.GobDecode(data) |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
func (a ActivityVocabularyType) MarshalBinary() ([]byte, error) { |
| 251 |
return a.GobEncode() |
| 252 |
} |
| 253 |
|
| 254 |
func (a ActivityVocabularyType) AsTypes() ActivityVocabularyTypes { |
| 255 |
return ActivityVocabularyTypes{a} |
| 256 |
} |
| 257 |
|
| 258 |
func (a ActivityVocabularyTypes) String() string { |
| 259 |
if len(a) == 0 { |
| 260 |
return "" |
| 261 |
} |
| 262 |
s := make([]string, 0, len(a)) |
| 263 |
for _, tt := range a { |
| 264 |
s = append(s, string(tt)) |
| 265 |
} |
| 266 |
return strings.Join(s, ", ") |
| 267 |
} |
| 268 |
|
| 269 |
type TypesMatcher interface { |
| 270 |
Match(...ActivityVocabularyType) bool |
| 271 |
} |
| 272 |
|
| 273 |
type MatcherFn func(...ActivityVocabularyType) bool |
| 274 |
|
| 275 |
func (mfn MatcherFn) Match(tt ...ActivityVocabularyType) bool { |
| 276 |
return mfn(tt...) |
| 277 |
} |
| 278 |
|
| 279 |
func AllTypes(toMatch ...ActivityVocabularyType) MatcherFn { |
| 280 |
return func(toCheck ...ActivityVocabularyType) bool { |
| 281 |
if len(toMatch) == 0 { |
| 282 |
if len(toCheck) == 0 { |
| 283 |
return true |
| 284 |
} |
| 285 |
} |
| 286 |
for _, search := range toMatch { |
| 287 |
if !slices.Contains(toCheck, search) { |
| 288 |
return false |
| 289 |
} |
| 290 |
} |
| 291 |
return true |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
func flattenTypes(typ ...ActivityVocabularyType) ActivityVocabularyTypes { |
| 296 |
result := make(ActivityVocabularyTypes, 0, len(typ)) |
| 297 |
for _, t := range typ { |
| 298 |
if t == NilType { |
| 299 |
continue |
| 300 |
} |
| 301 |
result = append(result, t) |
| 302 |
} |
| 303 |
return result |
| 304 |
} |
| 305 |
|
| 306 |
func AnyTypes(toMatch ...ActivityVocabularyType) MatcherFn { |
| 307 |
return func(toCheck ...ActivityVocabularyType) bool { |
| 308 |
if len(toMatch) == 0 || len(toCheck) == 0 { |
| 309 |
|
| 310 |
|
| 311 |
return len(flattenTypes(toCheck...)) == 0 && len(flattenTypes(toMatch...)) == 0 |
| 312 |
} |
| 313 |
for _, search := range toMatch { |
| 314 |
if slices.Contains(toCheck, search) { |
| 315 |
return true |
| 316 |
} |
| 317 |
} |
| 318 |
return false |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
func (a ActivityVocabularyType) Match(other Typer) (match bool) { |
| 324 |
if other == nil { |
| 325 |
return a == NilType |
| 326 |
} |
| 327 |
tt := types(other) |
| 328 |
if a == NilType && EmptyTypes(tt...) { |
| 329 |
return true |
| 330 |
} |
| 331 |
for _, search := range tt { |
| 332 |
if match = a == search; match { |
| 333 |
break |
| 334 |
} |
| 335 |
} |
| 336 |
return match |
| 337 |
} |