Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 8 additions & 19 deletions filter/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,27 @@ import (

"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/policyserv/filter/classification"
"github.com/matrix-org/policyserv/filter/confidence"
"github.com/matrix-org/policyserv/notifiers"
)

type auditContext struct {
Event gomatrixserverlib.PDU
IsSpam bool
FinalVectors confidence.Vectors
IncrementalVectors []confidence.Vectors
FilterResponses map[string][]classification.Classification
CommunityId string
Event gomatrixserverlib.PDU
IsSpam bool
FilterResponses map[string][]classification.Classification
CommunityId string

lock sync.Mutex // use a lock instead of a sync.Map because sync.Map doesn't support generics (and library support appears lacking in quality)
notifier notifiers.MatrixNotifier
}

func newAuditContext(notifier notifiers.MatrixNotifier, communityId string, event gomatrixserverlib.PDU) (*auditContext, error) {
return &auditContext{
Event: event,
FilterResponses: make(map[string][]classification.Classification),
IncrementalVectors: make([]confidence.Vectors, 0),
CommunityId: communityId,
Event: event,
FilterResponses: make(map[string][]classification.Classification),
CommunityId: communityId,

// Populated later
IsSpam: false,
FinalVectors: nil,
IsSpam: false,

// Internal
lock: sync.Mutex{},
Expand All @@ -51,12 +46,6 @@ func (c *auditContext) AppendFilterResponse(filterName string, classifications [
c.FilterResponses[filterName] = classifications
}

func (c *auditContext) AppendSetGroupVectors(vectors confidence.Vectors) {
c.lock.Lock()
defer c.lock.Unlock()
c.IncrementalVectors = append(c.IncrementalVectors, vectors)
}

func (c *auditContext) Publish() error {
c.lock.Lock()
defer c.lock.Unlock()
Expand Down
3 changes: 0 additions & 3 deletions filter/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ func (s *Set) CheckEvent(ctx context.Context, event gomatrixserverlib.PDU, media
return nil, errors.Join(fmt.Errorf("error at group %d", i), err)
}
vecs = s.combineVectors(vecs, v)

auditCtx.AppendSetGroupVectors(v)
}
auditCtx.FinalVectors = vecs
auditCtx.IsSpam = s.IsSpamResponse(ctx, vecs)
go func(auditCtx *auditContext, s *Set) { // run the audit publishing async to avoid blocking the hot path any more than required
err := auditCtx.Publish()
Expand Down
Loading