Skip to content

Commit

Permalink
Improve outbound rule filters
Browse files Browse the repository at this point in the history
  • Loading branch information
benpate committed Feb 17, 2024
1 parent 7d118f2 commit 8497a4b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
35 changes: 33 additions & 2 deletions model/ruleSummary.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"net/url"
"strings"

"github.com/benpate/hannibal/streams"
Expand Down Expand Up @@ -49,8 +50,22 @@ func (rule RuleSummary) IsAllowed(document *streams.Document) bool {
// this rule. (i.e. the document MATCHES the rule)
func (rule RuleSummary) IsDisallowed(document *streams.Document) bool {

// Apply content filters here.
if rule.Type == RuleTypeContent {
switch rule.Type {

case RuleTypeActor:

if document.Actor().ID() != rule.Trigger {
return false
}

case RuleTypeDomain:
if domain, err := url.Parse(document.Actor().ID()); err == nil {
if !strings.HasSuffix(domain.Hostname(), rule.Trigger) {
return false
}
}

case RuleTypeContent:

// If the document does not match the content filter, then it is allowed.
if !rule.matchesContent(document) {
Expand All @@ -74,6 +89,22 @@ func (rule RuleSummary) IsDisallowed(document *streams.Document) bool {
return false
}

func (rule RuleSummary) IsDisallowSend(recipient string) bool {

switch rule.Type {

case RuleTypeActor:
return recipient == rule.Trigger

case RuleTypeDomain:
if domain, err := url.Parse(recipient); err == nil {
return strings.HasSuffix(domain.Hostname(), rule.Trigger)
}
}

return false
}

func (rule RuleSummary) matchesContent(document *streams.Document) bool {

ruleTriggerLowerCase := strings.ToLower(rule.Trigger)
Expand Down
25 changes: 22 additions & 3 deletions service/ruleFilter_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@ import (
"github.com/EmissarySocial/emissary/model"
"github.com/benpate/derp"
"github.com/benpate/hannibal/vocab"
"github.com/davecgh/go-spew/spew"
"github.com/rs/zerolog/log"
)

// AllowSend returns TRUE if this actorID is allowed to receive messages.
func (filter *RuleFilter) AllowSend(actorID string) bool {

const location = "service.RuleFilter.AllowSend"

log.Trace().Str("loc", location).Msg("Testing: " + actorID)

// Guarantee that the actorID is not empty
if actorID == "" {
log.Trace().Str("loc", location).Msg("Ignore Empty actorID")
return false
}

// We don't actually send messages to the public namespace
if actorID == vocab.NamespaceActivityStreamsPublic {
log.Trace().Str("loc", location).Msg("Ignore Public Namespace")
return false
}

Expand All @@ -26,27 +34,38 @@ func (filter *RuleFilter) AllowSend(actorID string) bool {
rules, err := filter.ruleService.QueryByActorAndActions(filter.userID, actorID, allowedActions...)

if err != nil {
derp.Report(derp.Wrap(err, "emissary.RuleFilter.FilterOne", "Error loading rules"))
derp.Report(derp.Wrap(err, location, "Error loading rules"))
return false
}

filter.cache[actorID] = rules
spew.Dump("Found Rules:", rules)
}

return len(filter.cache[actorID]) == 0
for _, rule := range filter.cache[actorID] {
if rule.IsDisallowSend(actorID) {
return false
}
}

return true
}

// ChannelSend inspects the channel of recipients to see if they should receive messages or not.
func (filter *RuleFilter) ChannelSend(ch <-chan model.Follower) <-chan string {

result := make(chan string)

spew.Dump("ChannelSend ======================")
go func() {
defer close(result)

for follower := range ch {
spew.Dump(follower)
if filter.AllowSend(follower.Actor.ProfileURL) {
spew.Dump("allowed")
result <- follower.Actor.ProfileURL
} else {
spew.Dump("not allowed")
}
}
}()
Expand Down
6 changes: 3 additions & 3 deletions service/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,9 @@ func (service *Stream) Publish(user *model.User, stream *model.Stream) error {
return derp.Wrap(err, "service.Stream.Publish", "Error saving stream", stream)
}

// Attempt to pre-load the ActivityStream cache. We don't care about the result.
_, _ = service.activityStreamService.Load(stream.ActivityPubURL())

object := service.JSONLD(stream)

// Create the Activity to send to the User's Outbox
Expand All @@ -623,9 +626,6 @@ func (service *Stream) Publish(user *model.User, stream *model.Stream) error {
return derp.Wrap(err, "service.Stream.Publish", "Error publishing activity", activity)
}

// Attempt to pre-load the ActivityStream cache. We don't care about the result.
_, _ = service.activityStreamService.Load(stream.ActivityPubURL())

// Done.
return nil
}
Expand Down

0 comments on commit 8497a4b

Please sign in to comment.