f05ce478f0
* Add Pushserver component with Pushers API Co-authored-by: Tommie Gannert <tommie@gannert.se> Co-authored-by: Dan Peleg <dan@globekeeper.com> * Wire Pushserver component Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com> * Add PushGatewayClient. The full event format is required for Sytest. * Add a pushrules module. * Change user API account creation to use the new pushrules module's defaults. Introduces "scope" as required by client API, and some small field tweaks to make some 61push Sytests pass. * Add push rules query/put API in Pushserver. This manipulates account data over User API, and fires sync messages for changes. Those sync messages should, according to an existing TODO in clientapi, be moved to userapi. Forks clientapi/producers/syncapi.go to pushserver/ for later extension. * Add clientapi routes for push rules to Pushserver. A cleanup would be to move more of the name-splitting logic into pushrules.go, to depollute routing.go. * Output rooms.join.unread_notifications in /sync. This is the read-side. Pushserver will be the write-side. * Implement pushserver/storage for notifications. * Use PushGatewayClient and the pushrules module in Pushserver's room consumer. * Use one goroutine per user to avoid locking up the entire server for one bad push gateway. * Split pushing by format. * Send one device per push. Sytest does not support coalescing multiple devices into one push. Matches Synapse. Either we change Sytest, or remove the group-by-url-and-format logic. * Write OutputNotificationData from push server. Sync API is already the consumer. * Implement read receipt consumers in Pushserver. Supports m.read and m.fully_read receipts. * Add clientapi route for /unstable/notifications. * Rename to UpsertPusher for clarity and handle pusher update * Fix linter errors * Ignore body.Close() error check * Fix push server internal http wiring * Add 40 newly passing 61push tests to whitelist * Add next 12 newly passing 61push tests to whitelist * Send notification data before notifying users in EDU server consumer * NATS JetStream * Goodbye sarama * Fix `NewStreamTokenFromString` * Consume on the correct topic for the roomserver * Don't panic, NAK instead * Move push notifications into the User API * Don't set null values since that apparently causes Element upsetti * Also set omitempty on conditions * Fix bug so that we don't override the push rules unnecessarily * Tweak defaults * Update defaults * More tweaks * Move `/notifications` onto `r0`/`v3` mux * User API will consume events and read/fully read markers from the sync API with stream positions, instead of consuming directly Co-authored-by: Piotr Kozimor <p1996k@gmail.com> Co-authored-by: Tommie Gannert <tommie@gannert.se> Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
166 lines
4.3 KiB
Go
166 lines
4.3 KiB
Go
package pushrules
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
)
|
|
|
|
// A RuleSetEvaluator encapsulates context to evaluate an event
|
|
// against a rule set.
|
|
type RuleSetEvaluator struct {
|
|
ec EvaluationContext
|
|
ruleSet []kindAndRules
|
|
}
|
|
|
|
// An EvaluationContext gives a RuleSetEvaluator access to the
|
|
// environment, for rules that require that.
|
|
type EvaluationContext interface {
|
|
// UserDisplayName returns the current user's display name.
|
|
UserDisplayName() string
|
|
|
|
// RoomMemberCount returns the number of members in the room of
|
|
// the current event.
|
|
RoomMemberCount() (int, error)
|
|
|
|
// HasPowerLevel returns whether the user has at least the given
|
|
// power in the room of the current event.
|
|
HasPowerLevel(userID, levelKey string) (bool, error)
|
|
}
|
|
|
|
// A kindAndRules is just here to simplify iteration of the (ordered)
|
|
// kinds of rules.
|
|
type kindAndRules struct {
|
|
Kind Kind
|
|
Rules []*Rule
|
|
}
|
|
|
|
// NewRuleSetEvaluator creates a new evaluator for the given rule set.
|
|
func NewRuleSetEvaluator(ec EvaluationContext, ruleSet *RuleSet) *RuleSetEvaluator {
|
|
return &RuleSetEvaluator{
|
|
ec: ec,
|
|
ruleSet: []kindAndRules{
|
|
{OverrideKind, ruleSet.Override},
|
|
{ContentKind, ruleSet.Content},
|
|
{RoomKind, ruleSet.Room},
|
|
{SenderKind, ruleSet.Sender},
|
|
{UnderrideKind, ruleSet.Underride},
|
|
},
|
|
}
|
|
}
|
|
|
|
// MatchEvent returns the first matching rule. Returns nil if there
|
|
// was no match rule.
|
|
func (rse *RuleSetEvaluator) MatchEvent(event *gomatrixserverlib.Event) (*Rule, error) {
|
|
// TODO: server-default rules have lower priority than user rules,
|
|
// but they are stored together with the user rules. It's a bit
|
|
// unclear what the specification (11.14.1.4 Predefined rules)
|
|
// means the ordering should be.
|
|
//
|
|
// The most reasonable interpretation is that default overrides
|
|
// still have lower priority than user content rules, so we
|
|
// iterate twice.
|
|
for _, rsat := range rse.ruleSet {
|
|
for _, defRules := range []bool{false, true} {
|
|
for _, rule := range rsat.Rules {
|
|
if rule.Default != defRules {
|
|
continue
|
|
}
|
|
ok, err := ruleMatches(rule, rsat.Kind, event, rse.ec)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if ok {
|
|
return rule, nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// No matching rule.
|
|
return nil, nil
|
|
}
|
|
|
|
func ruleMatches(rule *Rule, kind Kind, event *gomatrixserverlib.Event, ec EvaluationContext) (bool, error) {
|
|
if !rule.Enabled {
|
|
return false, nil
|
|
}
|
|
|
|
switch kind {
|
|
case OverrideKind, UnderrideKind:
|
|
for _, cond := range rule.Conditions {
|
|
ok, err := conditionMatches(cond, event, ec)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
}
|
|
return true, nil
|
|
|
|
case ContentKind:
|
|
// TODO: "These configure behaviour for (unencrypted) messages
|
|
// that match certain patterns." - Does that mean "content.body"?
|
|
return patternMatches("content.body", rule.Pattern, event)
|
|
|
|
case RoomKind:
|
|
return rule.RuleID == event.RoomID(), nil
|
|
|
|
case SenderKind:
|
|
return rule.RuleID == event.Sender(), nil
|
|
|
|
default:
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
func conditionMatches(cond *Condition, event *gomatrixserverlib.Event, ec EvaluationContext) (bool, error) {
|
|
switch cond.Kind {
|
|
case EventMatchCondition:
|
|
return patternMatches(cond.Key, cond.Pattern, event)
|
|
|
|
case ContainsDisplayNameCondition:
|
|
return patternMatches("content.body", ec.UserDisplayName(), event)
|
|
|
|
case RoomMemberCountCondition:
|
|
cmp, err := parseRoomMemberCountCondition(cond.Is)
|
|
if err != nil {
|
|
return false, fmt.Errorf("parsing room_member_count condition: %w", err)
|
|
}
|
|
n, err := ec.RoomMemberCount()
|
|
if err != nil {
|
|
return false, fmt.Errorf("RoomMemberCount failed: %w", err)
|
|
}
|
|
return cmp(n), nil
|
|
|
|
case SenderNotificationPermissionCondition:
|
|
return ec.HasPowerLevel(event.Sender(), cond.Key)
|
|
|
|
default:
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
func patternMatches(key, pattern string, event *gomatrixserverlib.Event) (bool, error) {
|
|
re, err := globToRegexp(pattern)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
var eventMap map[string]interface{}
|
|
if err = json.Unmarshal(event.JSON(), &eventMap); err != nil {
|
|
return false, fmt.Errorf("parsing event: %w", err)
|
|
}
|
|
v, err := lookupMapPath(strings.Split(key, "."), eventMap)
|
|
if err != nil {
|
|
// An unknown path is a benign error that shouldn't stop rule
|
|
// processing. It's just a non-match.
|
|
return false, nil
|
|
}
|
|
|
|
return re.MatchString(fmt.Sprint(v)), nil
|
|
}
|