Skip to content

Commit

Permalink
Fixed loading of followup moderation activities
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusor committed Jan 27, 2021
1 parent aa0c03b commit cfe9c21
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 138 deletions.
8 changes: 8 additions & 0 deletions app/accounts.go
Expand Up @@ -72,6 +72,14 @@ type Account struct {
Children AccountPtrCollection `json:"-"`
}

var ValidActorTypes = pub.ActivityVocabularyTypes{
pub.PersonType,
pub.ServiceType,
pub.GroupType,
pub.ApplicationType,
pub.OrganizationType,
}

func (a Account) ID() Hash {
return a.Hash
}
Expand Down
125 changes: 36 additions & 89 deletions app/converter.go
Expand Up @@ -705,95 +705,42 @@ func (c *TagCollection) FromActivityPub(col pub.ItemCollection) error {
return nil
}

func LoadFromActivityPubObject(it pub.Item) (Renderable, error) {
if it == nil {
return nil, errors.Newf("nil ActivityPub object")
}
typ := it.GetType()
if !(pub.ObjectTypes.Contains(typ) || pub.ActorTypes.Contains(typ)) {
return nil, errors.Newf("invalid ActivityPub object")
}
var result Renderable
var err error
if pub.ObjectTypes.Contains(typ) {
err = pub.OnObject(it, func(ob *pub.Object) error {
i := new(Item)
if err = i.FromActivityPub(ob); err == nil {
result = i
}
return err
})
}
if pub.ActorTypes.Contains(typ) {
err = pub.OnActor(it, func(ac *pub.Actor) error {
var err error
a := new(Account)
if err = a.FromActivityPub(ac); err == nil {
result = a
}
return err
})
}
return result, err
}

func LoadFromActivityPubActivity(it pub.Item) (Renderable, error) {
if it == nil {
return nil, errors.Newf("nil ActivityPub item")
func LoadFromActivityPubItem(it pub.Item) (Renderable, error) {
var (
result Renderable
err error
typ = it.GetType()
)

if typ == pub.FollowType {
f := new(FollowRequest)
err = f.FromActivityPub(it)
result = f
}
if ValidContentManagementTypes.Contains(typ) {
item := new(Item)
err = item.FromActivityPub(it)
result = item
}
if ValidAppreciationTypes.Contains(typ) {
vot := new(Vote)
err = vot.FromActivityPub(it)
result = vot
}
if ValidModerationActivityTypes.Contains(typ) {
op := new(ModerationOp)
err = op.FromActivityPub(it)
result = op
}
if ValidActorTypes.Contains(typ) {
acc := new(Account)
err = acc.FromActivityPub(it)
result = acc
}
if ValidContentTypes.Contains(typ) {
item := new(Item)
err = item.FromActivityPub(it)
result = item
}
typ := it.GetType()

if !pub.ActivityTypes.Contains(typ) {
return LoadFromActivityPubObject(it)
}
var result Renderable
err := pub.OnActivity(it, func(act *pub.Activity) error {
ob := act.Object
switch typ {
case pub.DeleteType:
fallthrough
case pub.UpdateType:
fallthrough
case pub.CreateType:
// Item or Account
if ob.IsObject() {
var err error
result, err = LoadFromActivityPubObject(ob)
return err
}
case pub.LikeType:
fallthrough
case pub.DislikeType:
// Vote
v := new(Vote)
err := v.FromActivityPub(act)
if err != nil {
return err
}
result = v
case pub.FollowType:
// FollowRequest
f := new(FollowRequest)
err := f.FromActivityPub(act)
if err != nil {
return err
}
result = f
break
case pub.FlagType:
fallthrough
case pub.BlockType:
fallthrough
case pub.IgnoreType:
// ModerationOp
m := new(ModerationOp)
err := m.FromActivityPub(act)
if err != nil {
return err
}
result = m
}
return nil
})
return result, err
}
12 changes: 6 additions & 6 deletions app/filters.go
Expand Up @@ -66,7 +66,7 @@ func DefaultFilters(next http.Handler) http.Handler {
f.Type = CreateActivitiesFilter
f.Object = new(Filters)
f.Object.OP = nilIRIs
f.Object.Type = ActivityTypesFilter(ValidItemTypes...)
f.Object.Type = ActivityTypesFilter(ValidContentTypes...)
m := ContextListingModel(r.Context())
m.Title = "Newest items"
ctx := context.WithValue(r.Context(), FilterCtxtKey, []*Filters{f})
Expand Down Expand Up @@ -121,7 +121,7 @@ func fedFilters(r *http.Request) *Filters {
f.Type = CreateActivitiesFilter
f.Object = &Filters{}
f.Object.OP = nilIRIs
f.Object.Type = ActivityTypesFilter(ValidItemTypes...)
f.Object.Type = ActivityTypesFilter(ValidContentTypes...)
f.Actor = &Filters{}
return f
}
Expand Down Expand Up @@ -160,7 +160,7 @@ func DomainFiltersMw(next http.Handler) http.Handler {
EqualsString(MimeTypeText),
EqualsString(MimeTypeHTML),
}
f.Object.Type = ActivityTypesFilter(ValidItemTypes...)
f.Object.Type = ActivityTypesFilter(ValidContentTypes...)
m.Title = fmt.Sprintf("Discussion items")
}
f.Object.OP = nilIRIs
Expand All @@ -185,7 +185,7 @@ func TagFiltersMw(next http.Handler) http.Handler {
fc := new(Filters)
fc.Type = CreateActivitiesFilter
fc.Object = new(Filters)
fc.Object.Type = ActivityTypesFilter(ValidItemTypes...)
fc.Object.Type = ActivityTypesFilter(ValidContentTypes...)
fc.Object.Tag = tagsFilter(tag)

fa := new(Filters)
Expand Down Expand Up @@ -288,11 +288,11 @@ type moderationFilter struct {

var (
modSubmissionsObjectFilter = &Filters{
Type: ActivityTypesFilter(ValidItemTypes...),
Type: ActivityTypesFilter(ValidContentTypes...),
InReplTo: nilIRIs,
}
modCommentsObjectFilter = &Filters{
Type: ActivityTypesFilter(ValidItemTypes...),
Type: ActivityTypesFilter(ValidContentTypes...),
InReplTo: notNilIRIs,
}
modAccountsObjectFilter = &Filters{
Expand Down
16 changes: 16 additions & 0 deletions app/items.go
Expand Up @@ -26,6 +26,22 @@ type ItemMetadata struct {
Icon ImageMetadata `json:"icon,omitempty"`
}

var ValidContentTypes = pub.ActivityVocabularyTypes{
pub.ArticleType,
pub.NoteType,
pub.LinkType,
pub.PageType,
pub.DocumentType,
pub.VideoType,
pub.AudioType,
}

var ValidContentManagementTypes = pub.ActivityVocabularyTypes{
pub.UpdateType,
pub.CreateType,
pub.DeleteType,
}

type Identifiable interface {
Id() int64
}
Expand Down
45 changes: 30 additions & 15 deletions app/moderation.go
Expand Up @@ -213,6 +213,29 @@ func (m *ModerationOp) Deleted() bool {
return m.Flags&FlagsDeleted == FlagsDeleted
}

func GetRenderableByType(typ pub.ActivityVocabularyType) Renderable {
var result Renderable
if ValidAppreciationTypes.Contains(typ) {
result = new(Vote)
}
if ValidModerationActivityTypes.Contains(typ) {
result = new(ModerationOp)
}
if ValidActorTypes.Contains(typ) {
result = new(Account)
}
if ValidContentTypes.Contains(typ) {
result = new(Item)
}
return result
}


func loadItemActorOrActivityFromModerationActivityObject (it pub.Item) Renderable {
result, _ := LoadFromActivityPubItem(it)
return result
}

func (m *ModerationOp) FromActivityPub(it pub.Item) error {
if m == nil {
return nil
Expand All @@ -236,21 +259,13 @@ func (m *ModerationOp) FromActivityPub(it pub.Item) error {
m.Icon = icon(strings.ToLower(string(a.Type)))
wer.FromActivityPub(a.Actor)
m.SubmittedBy = wer
if strings.Contains(a.Object.GetLink().String(), "actors") {
ob := new(Account)
if err := ob.FromActivityPub(a.Object); err == nil {
m.Object = ob
} else {
m.Object = &DeletedAccount
}
}
if strings.Contains(a.Object.GetLink().String(), "objects") {
ob := new(Item)
if err := ob.FromActivityPub(a.Object); err == nil {
m.Object = ob
} else {
m.Object = &DeletedItem
}
if a.Object.IsCollection() {
pub.OnCollectionIntf(a.Object, func(c pub.CollectionInterface) error {
m.Object = loadItemActorOrActivityFromModerationActivityObject(c.Collection().First())
return nil
})
} else {
m.Object = loadItemActorOrActivityFromModerationActivityObject(a.Object)
}
reason := new(Item)
pub.OnObject(a, func(o *pub.Object) error {
Expand Down
27 changes: 1 addition & 26 deletions app/repository.go
Expand Up @@ -42,31 +42,6 @@ func (r repository) BaseURL() pub.IRI {
return r.fedbox.baseURL
}

var ValidActorTypes = pub.ActivityVocabularyTypes{
pub.PersonType,
pub.ServiceType,
pub.GroupType,
pub.ApplicationType,
pub.OrganizationType,
}

var ValidItemTypes = pub.ActivityVocabularyTypes{
pub.ArticleType,
pub.NoteType,
pub.LinkType,
pub.PageType,
pub.DocumentType,
pub.VideoType,
pub.AudioType,
}

// @deprecated
var ValidActivityTypes = pub.ActivityVocabularyTypes{
pub.CreateType,
pub.LikeType,
pub.FollowType,
}

// Repository middleware
func (h handler) Repository(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -1365,7 +1340,7 @@ func (r *repository) ActorCollection(ctx context.Context, fn CollectionFn, ff ..
return nil
}
if ob.IsObject() {
if ValidItemTypes.Contains(ob.GetType()) {
if ValidContentTypes.Contains(ob.GetType()) {
i := Item{}
i.FromActivityPub(ob)
if validItem(i, f) {
Expand Down
4 changes: 2 additions & 2 deletions app/webfinger.go
Expand Up @@ -37,11 +37,11 @@ var (
Type: ActivityTypesFilter(ValidActorTypes...),
}
postsFilter = &Filters{
Type: ActivityTypesFilter(ValidItemTypes...),
Type: ActivityTypesFilter(ValidContentTypes...),
OP: nilIRIs,
}
allFilter = &Filters{
Type: ActivityTypesFilter(ValidItemTypes...),
Type: ActivityTypesFilter(ValidContentTypes...),
}
)

Expand Down

0 comments on commit cfe9c21

Please sign in to comment.