-
Notifications
You must be signed in to change notification settings - Fork 13
/
follows.go
120 lines (106 loc) · 2.48 KB
/
follows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package brutalinks
import (
"time"
vocab "github.com/go-ap/activitypub"
"github.com/go-ap/errors"
)
// FollowRequests
type FollowRequests []FollowRequest
// FollowRequest
type FollowRequest struct {
Hash Hash `json:"hash"`
SubmittedAt time.Time `json:"-"`
SubmittedBy *Account `json:"by,omitempty"`
Object *Account `json:"-"`
Metadata *ActivityMetadata `json:"-"`
pub vocab.Item `json:"-"`
Flags FlagBits `json:"flags,omitempty"`
}
// ActivityMetadata
type ActivityMetadata struct {
ID string `json:"-"`
InReplyTo vocab.IRIs `json:"-"`
}
func (f *FollowRequest) ID() Hash {
if f == nil {
return AnonymousHash
}
return f.Hash
}
// FromActivityPub
func (f *FollowRequest) FromActivityPub(it vocab.Item) error {
if f == nil {
return nil
}
if vocab.IsNil(it) {
return errors.Newf("nil item received")
}
f.pub = it
if it.IsLink() {
iri := it.GetLink()
f.Hash.FromActivityPub(iri)
f.Metadata = &ActivityMetadata{
ID: iri.String(),
}
return nil
}
return vocab.OnActivity(it, func(a *vocab.Activity) error {
err := f.Hash.FromActivityPub(a)
if err != nil {
return err
}
wer := new(Account)
err = wer.FromActivityPub(a.Actor)
if err != nil {
return err
}
f.SubmittedBy = wer
wed := new(Account)
err = wed.FromActivityPub(a.Object)
if err != nil {
return err
}
f.Object = wed
f.SubmittedAt = a.Published
f.Metadata = &ActivityMetadata{
ID: string(a.ID),
}
if a.InReplyTo != nil {
f.Metadata.InReplyTo = make(vocab.IRIs, 0)
vocab.OnCollectionIntf(a.InReplyTo, func(col vocab.CollectionInterface) error {
for _, it := range col.Collection() {
f.Metadata.InReplyTo = append(f.Metadata.InReplyTo, it.GetLink())
}
return nil
})
}
return nil
})
}
// Type
func (f *FollowRequest) Type() RenderType {
return FollowType
}
// Date
func (f FollowRequest) Date() time.Time {
return f.SubmittedAt
}
func (f *FollowRequest) Children() *RenderableList {
return nil
}
// Private
func (f *FollowRequest) Private() bool {
return f.Flags&FlagsPrivate == FlagsPrivate
}
// Deleted
func (f *FollowRequest) Deleted() bool {
return f.Flags&FlagsDeleted == FlagsDeleted
}
// IsValid returns if the current follow request has a hash with length greater than 0
func (f *FollowRequest) IsValid() bool {
return f != nil && f.Hash.IsValid()
}
// AP returns the underlying actvitypub item
func (f *FollowRequest) AP() vocab.Item {
return f.pub
}