filters

package module
v0.0.0-...-3f90b49 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 16 Imported by: 15

Documentation

Overview

Package filters contains helper functions to be used by the storage implementations for filtering out elements at load time.

Example (CheckAll_Match)
package main

import (
	"fmt"

	vocab "github.com/go-ap/activitypub"
)

type ty = vocab.ActivityVocabularyType

func main() {
	collection := vocab.ItemCollection{
		// doesn't match due to Actor ID
		vocab.Create{
			Type:   ty("Create"),
			Actor:  vocab.IRI("https://example.com/bob"),
			Object: vocab.IRI("https//example.com/test"),
		},
		// doesn't match due to nil Object
		vocab.Create{
			Type:  ty("Create"),
			Actor: vocab.IRI("https://example.com/jdoe"),
		},
		// match
		vocab.Create{
			Type:   ty("Create"),
			Actor:  vocab.IRI("https://example.com/jdoe"),
			Object: vocab.IRI("https//example.com/test"),
		},
		// match
		vocab.Create{
			Type: ty("Create"),
			Actor: vocab.Person{
				ID:   "https://example.com/jdoe1",
				Name: vocab.DefaultNaturalLanguage("JohnDoe"),
			},
			Object: vocab.IRI("https//example.com/test"),
		},
		// doesn't match due to the activity Type
		vocab.Follow{Type: ty("Follow")},
		// doesn't match due to Arrive being an intransitive activity
		vocab.Arrive{Type: ty("Arrive")},
		// doesn't match due to Question being an intransitive activity
		vocab.Question{Type: ty("Question")},
	}
	// This filters all activities that are not:
	// Create activities,
	// published by an Actor with the ID https://example.com/authors/jdoe, or with the name "JohnDoe"
	// and, which have an object with a non nil ID.
	filterFn := All(
		HasType("Create"),
		Actor(
			Any(
				SameID("https://example.com/jdoe"),
				NameIs("JohnDoe"),
			),
		),
		Object(Not(NilID)),
	)

	result := make(vocab.ItemCollection, 0)
	for _, it := range collection {
		if filterFn.Match(it) {
			result = append(result, it)
		}
	}
	output, _ := vocab.MarshalJSON(result)
	fmt.Printf("Result[%d]: %s", len(result), output)
}
Output:

Result[2]: [{"type":"Create","actor":"https://example.com/jdoe","object":"https//example.com/test"},{"type":"Create","actor":{"id":"https://example.com/jdoe1","name":"JohnDoe"},"object":"https//example.com/test"}]

Index

Examples

Constants

View Source
const (
	// ActorsType is a constant that represents the URL path for the local actors collection.
	// It is used as the parent for all To IDs
	ActorsType = vocab.CollectionPath("actors")
	// ActivitiesType is a constant that represents the URL path for the local activities collection
	// It is used as the parent for all Activity IDs
	ActivitiesType = vocab.CollectionPath("activities")
	// ObjectsType is a constant that represents the URL path for the local objects collection
	// It is used as the parent for all non To, non Activity Object IDs
	ObjectsType = vocab.CollectionPath("objects")

	// BlockedType is an internally used collection, to store a list of actors the actor has blocked
	BlockedType = vocab.CollectionPath("blocked")

	// IgnoredType is an internally used collection, to store a list of actors the actor has ignored
	IgnoredType = vocab.CollectionPath("ignored")
)
View Source
const (
	ByID                = index.ByID
	ByType              = index.ByType
	ByName              = index.ByName
	ByPreferredUsername = index.ByPreferredUsername
	BySummary           = index.BySummary
	ByContent           = index.ByContent
	ByActor             = index.ByActor
	ByObject            = index.ByObject
	ByRecipients        = index.ByRecipients
	ByAttributedTo      = index.ByAttributedTo
	ByInReplyTo         = index.ByInReplyTo
)
View Source
const MaxItems int = 100

Variables

TODO(marius): here we need a better separation between the collections which are exposed in the HTTP API

(activities,actors,objects) and the ones that are internal (blocked,ignored)
View Source
var ContentEmpty = contentCheck("", naturalLanguageEmpty)

ContentEmpty checks an vocab.Object's Content, *and*, in the case of an vocab.Actor also the PreferredUsername to be empty. If *all* of the values are empty, the function returns true.

Please note that the logic of this check is different from ContentIs and ContentLike.

View Source
var NameEmpty = nameCheck("", naturalLanguageEmpty)

NameEmpty checks an vocab.Object's Name, *and*, in the case of an vocab.Actor also its PreferredUsername to be empty. If *all* of the values are empty, the function returns true.

Please note that the logic of this check is different from NameIs and NameLike.

View Source
var NilAttributedTo = attributedToNil{}
View Source
var NilContext = contextNil{}
View Source
var NilID = idNil{}

NilID checks if the vocab.Object's ID property matches any of the two magic values that denote an empty value: vocab.NilID = "-", or vocab.EmptyID = ""

View Source
var NilIRI = iriNil{}

NilIRI checks if the activitypub.Item's IRI that matches any of the two magic values that denote an empty value: activitypub.NilID = "-", or activitypub.EmptyID = ""

View Source
var NilInReplyTo = inReplyToNil{}
View Source
var NilItem = itemNil{}

NilItem checks if the activitypub.Item is nil

View Source
var NotNilID = Not(NilID)

NotNilID checks if the vocab.Object's ID property is not nil

View Source
var NotNilIRI = Not(iriNil{})

NotNilIRI checks if the activitypub.Object's URL property matches any of the two magic values that denote an empty value: activitypub.NilID = "-", or activitypub.EmptyID = ""

View Source
var NotNilItem = Not(itemNil{})

NotNilItem checks if the activitypub.Object is not nil

View Source
var PreferredUsernameEmpty = preferredUsername("", naturalLanguageEmpty)

PreferredUsernameEmpty checks an vocab.Actors's PreferredUsername to be empty. If *all* of the values are empty, the function returns true.

Please note that the logic of this check is different from PreferredUsernameIs and PreferredUsernameLike.

View Source
var SummaryEmpty = summaryCheck("", naturalLanguageEmpty)

SummaryEmpty checks an vocab.Object's Summary, *and*, in the case of an vocab.Actor also the PreferredUsername to be empty. If *all* of the values are empty, the function returns true.

Please note that the logic of this check is different from SummaryIs and SummaryLike.

Functions

func Counted

func Counted(fns ...Check) int

func CursorFromItem

func CursorFromItem(it vocab.Item, filters ...Check) (vocab.Item, vocab.Item, vocab.Item)

func FirstPage

func FirstPage() pagValues

FirstPage returns the default url.Values for getting to the first page of a collection.

func GetLimit

func GetLimit(f ...Check) int

func GetWhereClauses

func GetWhereClauses(f ...Check) ([]string, []any)

func MatchRaw

func MatchRaw(filters Checks, raw []byte) bool

func MaxCount

func MaxCount(fns ...Check) int

func NextPage

func NextPage(it vocab.Item) pagValues

func NextPageFromCollection

func NextPageFromCollection(it vocab.CollectionInterface) vocab.IRI

func PaginateCollection

func PaginateCollection(it vocab.Item, filters ...Check) vocab.Item

PaginateCollection is a function that populates the received collection

func PaginatorValues

func PaginatorValues(q url.Values) pagValues

func PrevPage

func PrevPage(it vocab.Item) pagValues

func PrevPageFromCollection

func PrevPageFromCollection(it vocab.CollectionInterface) vocab.IRI

func RawMatcher

func RawMatcher(filters Checks) func([]byte) bool

func SearchIndex

func SearchIndex(i *index.Index, ff ...Check) ([]vocab.IRI, error)

SearchIndex does a fast index search for the received filters.

Example
activities := []vocab.LinkOrIRI{
	// NOTE(marius): if the object for the Create activity is not indexed independently, like we have here,
	// it will not be findable in the index, and the composite search by activity type: Create,
	// and object ID:https://federated.local/objects/1 will fail.
	// In a previous version of this example this object was embedded in the activity directly,
	// but we decided that the logic to add embedded objects to the index, would be too complicated,
	// so this is the compromise.
	&vocab.Object{
		ID:   "https://federated.local/objects/1",
		Type: vocab.PageType,
		Name: vocab.NaturalLanguageValues{vocab.DefaultLang: vocab.Content("Link to example.com")},
		URL:  vocab.IRI("https://example.com"),
	},
	&vocab.Activity{
		ID:     "https://federated.local/1",
		Type:   vocab.CreateType,
		To:     vocab.ItemCollection{vocab.IRI("https://federated.local/~jdoe")},
		Actor:  vocab.IRI("https://federated.local/~jdoe"),
		Object: vocab.IRI("https://federated.local/objects/1"),
	},
	&vocab.Activity{
		ID:     "https://federated.local/2",
		Type:   vocab.LikeType,
		To:     vocab.ItemCollection{vocab.IRI("https://federated.local/~jdoe")},
		Actor:  vocab.IRI("https://federated.local/~jdoe"),
		Object: vocab.IRI("https://federated.local/objects/1"),
	},
	&vocab.Activity{
		ID:     "https://federated.local/3",
		Type:   vocab.DislikeType,
		To:     vocab.ItemCollection{vocab.IRI("https://federated.local/~jdoe")},
		Actor:  vocab.IRI("https://federated.local/~jdoe"),
		Object: vocab.IRI("https://federated.local/objects/1"),
	},
	&vocab.Activity{
		ID:     "https://federated.local/4",
		Type:   vocab.FlagType,
		To:     vocab.ItemCollection{vocab.IRI("https://federated.local/~jdoe")},
		Actor:  vocab.IRI("https://federated.local/~jdoe"),
		Object: vocab.IRI("https://federated.local/objects/1"),
	},
}

in := index.Full()
// Add the activities to the index
in.Add(activities...)

findCreate := Checks{
	HasType(vocab.CreateType),
	Object(SameID("https://federated.local/objects/1")),
}
iris, err := SearchIndex(in, findCreate...)
fmt.Printf("Find Create:\n")
fmt.Printf("Error: %v\n", err)
fmt.Printf("IRIs: %#v\n", iris)

findBlock := Checks{
	HasType(vocab.FlagType),
	AttributedToLike("https://federated.local/~jdoe"),
}
iris, err = SearchIndex(in, findBlock...)
fmt.Printf("Find Flag:\n")
fmt.Printf("Error: %v\n", err)
fmt.Printf("IRIs: %#v\n", iris)
Output:

Find Create:
Error: <nil>
IRIs: []activitypub.IRI{https://federated.local/1}
Find Flag:
Error: <nil>
IRIs: []activitypub.IRI{https://federated.local/4}

func ToValues

func ToValues(ff ...Check) url.Values

ToValues encodes the "ff" list of Check entries into a url.Values map

func ValidActivityCollection

func ValidActivityCollection(typ vocab.CollectionPath) bool

ValidActivityCollection shows if the current ActivityPub end-point type is a valid collection for handling Activities

func ValidCollection

func ValidCollection(typ vocab.CollectionPath) bool

ValidCollection shows if the current ActivityPub end-point type is a valid collection

func ValidObjectCollection

func ValidObjectCollection(typ vocab.CollectionPath) bool

ValidObjectCollection shows if the current ActivityPub end-point type is a valid collection for handling Objects

func VocabularyTypesFilter

func VocabularyTypesFilter(types ...string) vocab.ActivityVocabularyTypes

VocabularyTypesFilter converts the received list of strings to a list of ActivityVocabularyTypes that can be used with the HasType filter function. The individual strings are not validated against the known vocabulary types.

Types

type Check

type Check interface {
	Match(vocab.Item) bool
}

Check represents an interface for a filter that can be applied on a vocab.Item and it returns true if it matches and false if it does not.

func Actor

func Actor(fns ...Check) Check

func After

func After(fns ...Check) Check

After checks the activitypub.Item against a specified "fn" filter function. This should be used when iterating over a collection, and it resolves to true after fn returns true and to false check.

Due to relying on the static check function return value the After is not reentrant.

func All

func All(fns ...Check) Check

All aggregates a list of individual Check functions into a single Check which resolves true if all individual members resolve as true, and false otherwise. It is equivalent to a sequence of AND operators.

func Any

func Any(fns ...Check) Check

Any aggregates a list of individual Check functions into a single Check which resolves to false if all the individual members resolve as false, and true if any of them resolves as true. It is equivalent to a sequence of OR operators.

func AttributedToLike

func AttributedToLike(frag string) Check

AttributedToLike creates a filter that checks the vocab.IRI against the attributedTo property of the item it gets applied on using a similarity match.

func Authorized

func Authorized(iri vocab.IRI) Check

Authorized creates a filter that checks the vocab.IRI against the recipients list of the item it gets applied on. The ActivityStreams Public Namespace IRI gets special treatment, because servers use it to signify that the audience of an object is public. The rules for matching this filter are like follows:

  • for Objects we check their attributedTo property, and their recipients (to, bto, cc, bcc and audience)
  • for Activities and Intransitive Activities we also check the actor property.

func Before

func Before(fn ...Check) Check

Before checks the activitypub.Item against a specified "fn" filter function. This should be used when iterating over a collection, and it resolves to true check the fn has returned true and to false after.

Due to relying on the static check function return value the function is not reentrant.

func ContentIs

func ContentIs(cont string) Check

ContentIs checks an vocab.Object's Content against the "cont" value. If any of the Language Ref map values match the value, the function returns true.

func ContentLike

func ContentLike(cont string) Check

ContentLike checks an vocab.Object's Content property against the "cont" value. If any of the Language Ref map values contains the value as a substring, the function returns true.

func ContextLike

func ContextLike(frag string) Check

func HasType

func HasType(ty ...vocab.ActivityVocabularyType) Check

HasType checks an activitypub.Object's Type property against a series of values. If any of the ty values matches, the function returns true.

func IDLike

func IDLike(frag string) Check

IDLike

func IRILike

func IRILike(frag string) Check

IRILike

func InReplyToLike

func InReplyToLike(frag string) Check

func MaxCountCheck

func MaxCountCheck(fns ...Check) Check

func NameIs

func NameIs(name string) Check

NameIs checks an vocab.Object's Name, or, in the case of an vocab.Actor also the PreferredUsername against the "name" value. If any of the Language Ref map values match the value, the function returns true.

func NameLike

func NameLike(name string) Check

NameLike checks an vocab.Object's Name, or, in the case of an vocab.Actor // also the PreferredUsername against the "name" value. If any of the Language Ref map values contains the value as a substring, the function returns true.

func Not

func Not(fn Check) Check

Not negates the result of a Check function. It is equivalent to a unary NOT operator.

func Object

func Object(fns ...Check) Check

func PreferredUsernameIs

func PreferredUsernameIs(name string) Check

PreferredUsernameIs checks an vocab.Actor's PreferredUsername against the "name" value. If any of the Language Ref map values match the value, the function returns true.

func PreferredUsernameLike

func PreferredUsernameLike(name string) Check

PreferredUsernameLike checks an vocab.Actor's PreferredUsername against the "name" value. If any of the Language Ref map values contains the value as a substring, the function returns true.

func Recipients

func Recipients(iri vocab.IRI) Check

Recipients creates a filter that checks the vocab.IRI against the recipients list of the item it gets applied on.

func SameAttributedTo

func SameAttributedTo(iri vocab.IRI) Check

SameAttributedTo creates a filter that checks the vocab.IRI against the attributedTo property of the item it gets applied on.

func SameContext

func SameContext(iri vocab.IRI) Check

func SameID

func SameID(i vocab.IRI) Check

SameID checks a vocab.Object's ID property against the received iri.

func SameIRI

func SameIRI(iri vocab.IRI) Check

SameIRI checks an activitypub.Object's IRI

func SameInReplyTo

func SameInReplyTo(iri vocab.IRI) Check

SameInReplyTo checks an activitypub.Object's InReplyTo

func SameURL

func SameURL(iri vocab.IRI) Check

SameURL checks an activitypub.Object's IRI

func SummaryIs

func SummaryIs(sum string) Check

SummaryIs checks an vocab.Object's Summary against the "sum" value. If any of the Language Ref map values match the value, the function returns true.

func SummaryLike

func SummaryLike(sum string) Check

SummaryLike checks an vocab.Object's Summary property against the "sum" value. If any of the Language Ref map values contains the value as a substring, the function returns true.

func Tag

func Tag(fns ...Check) Check

func Target

func Target(fns ...Check) Check

func URLLike

func URLLike(frag string) Check

func URLNil

func URLNil() Check

func WithMaxCount

func WithMaxCount(max int) Check

WithMaxCount is used to limit a collection's items count to the 'max' value. It can be used from slicing from the first element of the collection to max. Due to relying on the static max value the function is not reentrant.

type Checks

type Checks []Check

Checks aggregates a list of Check functions to be tested on a vocab.Item.

func ActivityChecks

func ActivityChecks(fns ...Check) Checks

func ActorChecks

func ActorChecks(fns ...Check) Checks

func AfterChecks

func AfterChecks(fns ...Check) Checks

func AuthorizedChecks

func AuthorizedChecks(fns ...Check) Checks

AuthorizedChecks returns all the Authorized checks in the fns slice. It recurses if there are Any or All checks, which is not always what you'd want, so take care.

func BeforeChecks

func BeforeChecks(fns ...Check) Checks

func CursorChecks

func CursorChecks(fns ...Check) Checks

func FilterChecks

func FilterChecks(fns ...Check) Checks

func FromIRI

func FromIRI(i vocab.IRI) (Checks, error)

func FromURL

func FromURL(u url.URL) Checks

func FromValues

func FromValues(q url.Values) Checks

func IDChecks

func IDChecks(fns ...Check) Checks

func IntransitiveActivityChecks

func IntransitiveActivityChecks(fns ...Check) Checks

func ItemChecks

func ItemChecks(fns ...Check) Checks

func ObjectChecks

func ObjectChecks(fns ...Check) Checks

func PaginationChecks

func PaginationChecks(fns ...Check) Checks

func RecipientsChecks

func RecipientsChecks(fns ...Check) Checks

RecipientsChecks returns all the Recipients checks in the fns slice. It recurses if there are Any or All checks, which is not always what you'd want, so take care.

func TagChecks

func TagChecks(fns ...Check) Checks

func TargetChecks

func TargetChecks(fns ...Check) Checks

func TopLevelChecks

func TopLevelChecks(fns ...Check) Checks

TopLevelChecks returns only the checks that don't need a fully loaded and dereferenced Item This is helpful for raw filtering, where an Activity raw document does not have a full representation for its object, actor, target, etc.

func TypeChecks

func TypeChecks(fns ...Check) Checks

func (Checks) Filter

func (ff Checks) Filter(item vocab.Item) vocab.Item

func (Checks) IndexMatch

func (ff Checks) IndexMatch(indexes map[index.Type]index.Indexable) *roaring64.Bitmap

func (Checks) Paginate

func (ff Checks) Paginate(item vocab.Item) vocab.Item

func (Checks) Run

func (ff Checks) Run(item vocab.Item) vocab.Item

type CompStr

type CompStr = qstring.ComparativeString

func StringDifferent

func StringDifferent(s string) CompStr

func StringEquals

func StringEquals(s string) CompStr

func StringLike

func StringLike(s string) CompStr

type CompStrs

type CompStrs []CompStr

func (CompStrs) Contains

func (cs CompStrs) Contains(f CompStr) bool

type KeysetPaginator

type KeysetPaginator interface {
	Before() string
	After() string
	Count() int
}

KeysetPaginator

type OffsetPaginator

type OffsetPaginator interface {
	Count() int
	Page() int
}

OffsetPaginator

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL