pushrules

package
v0.19.0-mod-2 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: MPL-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package pushrules contains utilities to parse push notification rules.

Index

Constants

This section is empty.

Variables

View Source
var DefaultPushActions = PushActionArray{&PushAction{Action: ActionDontNotify}}

DefaultPushActions is the value returned if none of the rule collections in a Ruleset match the event given to GetActions()

View Source
var MemberCountFilterRegex = regexp.MustCompile("^(==|[<>]=?)?([0-9]+)$")

MemberCountFilterRegex is the regular expression to parse the MemberCountCondition of PushConditions.

Functions

This section is empty.

Types

type EventContent

type EventContent struct {
	Ruleset *PushRuleset `json:"global"`
}

EventContent represents the content of a m.push_rules account data event. https://spec.matrix.org/v1.2/client-server-api/#mpush_rules

type EventfulRoom

type EventfulRoom interface {
	Room
	GetEvent(id.EventID) *event.Event
}

EventfulRoom is an extension of Room to support MSC3664.

type PushAction

type PushAction struct {
	Action PushActionType
	Tweak  PushActionTweak
	Value  interface{}
}

PushAction is a single action that should be triggered when receiving a message.

func (*PushAction) MarshalJSON

func (action *PushAction) MarshalJSON() (raw []byte, err error)

MarshalJSON is the reverse of UnmarshalJSON()

func (*PushAction) UnmarshalJSON

func (action *PushAction) UnmarshalJSON(raw []byte) error

UnmarshalJSON parses JSON into this PushAction.

  • If the JSON is a single string, the value is stored in the Action field.
  • If the JSON is an object with the set_tweak field, Action will be set to "set_tweak", Tweak will be set to the value of the set_tweak field and and Value will be set to the value of the value field.
  • In any other case, the function does nothing.

type PushActionArray

type PushActionArray []*PushAction

PushActionArray is an array of PushActions.

func (PushActionArray) Should

func (actions PushActionArray) Should() (should PushActionArrayShould)

Should parses this push action array and returns the relevant details wrapped in a PushActionArrayShould struct.

type PushActionArrayShould

type PushActionArrayShould struct {
	// Whether the array contained a Notify, DontNotify or Coalesce action type.
	// Deprecated: an empty array should be treated as no notification, so there's no reason to check this field.
	NotifySpecified bool
	// Whether the event in question should trigger a notification.
	Notify bool
	// Whether the event in question should be highlighted.
	Highlight bool

	// Whether the event in question should trigger a sound alert.
	PlaySound bool
	// The name of the sound to play if PlaySound is true.
	SoundName string
}

PushActionArrayShould contains the important information parsed from a PushActionArray.

type PushActionTweak

type PushActionTweak string

PushActionTweak is the type of the tweak in SetTweak push actions.

const (
	TweakSound     PushActionTweak = "sound"
	TweakHighlight PushActionTweak = "highlight"
)

The allowed tweak types as specified in spec section 11.12.1.4.1.1.

type PushActionType

type PushActionType string

PushActionType is the type of a PushAction

const (
	ActionNotify     PushActionType = "notify"
	ActionDontNotify PushActionType = "dont_notify"
	ActionCoalesce   PushActionType = "coalesce"
	ActionSetTweak   PushActionType = "set_tweak"
)

The allowed push action types as specified in spec section 11.12.1.4.1.

type PushCondKind

type PushCondKind string

PushCondKind is the type of a push condition.

const (
	KindEventMatch            PushCondKind = "event_match"
	KindContainsDisplayName   PushCondKind = "contains_display_name"
	KindRoomMemberCount       PushCondKind = "room_member_count"
	KindEventPropertyIs       PushCondKind = "event_property_is"
	KindEventPropertyContains PushCondKind = "event_property_contains"

	KindRelatedEventMatch         PushCondKind = "related_event_match"
	KindUnstableRelatedEventMatch PushCondKind = "im.nheko.msc3664.related_event_match"
)

The allowed push condition kinds as specified in https://spec.matrix.org/v1.2/client-server-api/#conditions-1

type PushCondition

type PushCondition struct {
	// The type of the condition.
	Kind PushCondKind `json:"kind"`
	// The dot-separated field of the event to match. Only applicable if kind is EventMatch.
	Key string `json:"key,omitempty"`
	// The glob-style pattern to match the field against. Only applicable if kind is EventMatch.
	Pattern string `json:"pattern,omitempty"`
	// The exact value to match the field against. Only applicable if kind is EventPropertyIs or EventPropertyContains.
	Value any `json:"value,omitempty"`
	// The condition that needs to be fulfilled for RoomMemberCount-type conditions.
	// A decimal integer optionally prefixed by ==, <, >, >= or <=. Prefix "==" is assumed if no prefix found.
	MemberCountCondition string `json:"is,omitempty"`

	// The relation type for related_event_match from MSC3664
	RelType event.RelationType `json:"rel_type,omitempty"`
}

PushCondition wraps a condition that is required for a specific PushRule to be used.

func (*PushCondition) Match

func (cond *PushCondition) Match(room Room, evt *event.Event) bool

Match checks if this condition is fulfilled for the given event in the given room.

type PushRule

type PushRule struct {
	// The type of this rule.
	Type PushRuleType `json:"-"`
	// The ID of this rule.
	// For room-specific rules and user-specific rules, this is the room or user ID (respectively)
	// For other types of rules, this doesn't affect anything.
	RuleID string `json:"rule_id"`
	// The actions this rule should trigger when matched.
	Actions PushActionArray `json:"actions"`
	// Whether this is a default rule, or has been set explicitly.
	Default bool `json:"default"`
	// Whether or not this push rule is enabled.
	Enabled bool `json:"enabled"`
	// The conditions to match in order to trigger this rule.
	// Only applicable to generic underride/override rules.
	Conditions []*PushCondition `json:"conditions,omitempty"`
	// Pattern for content-specific push rules
	Pattern string `json:"pattern,omitempty"`
}

func (*PushRule) GetActions

func (rule *PushRule) GetActions() PushActionArray

func (*PushRule) Match

func (rule *PushRule) Match(room Room, evt *event.Event) bool

type PushRuleArray

type PushRuleArray []*PushRule

func (PushRuleArray) GetActions

func (rules PushRuleArray) GetActions(room Room, evt *event.Event) PushActionArray

func (PushRuleArray) GetMatchingRule

func (rules PushRuleArray) GetMatchingRule(room Room, evt *event.Event) *PushRule

func (PushRuleArray) SetType

func (rules PushRuleArray) SetType(typ PushRuleType) PushRuleArray

func (PushRuleArray) SetTypeAndMap

func (rules PushRuleArray) SetTypeAndMap(typ PushRuleType) PushRuleMap

type PushRuleCollection

type PushRuleCollection interface {
	GetMatchingRule(room Room, evt *event.Event) *PushRule
	GetActions(room Room, evt *event.Event) PushActionArray
}

type PushRuleMap

type PushRuleMap struct {
	Map  map[string]*PushRule
	Type PushRuleType
}

func (PushRuleMap) GetActions

func (ruleMap PushRuleMap) GetActions(room Room, evt *event.Event) PushActionArray

func (PushRuleMap) GetMatchingRule

func (ruleMap PushRuleMap) GetMatchingRule(room Room, evt *event.Event) *PushRule

func (PushRuleMap) Unmap

func (ruleMap PushRuleMap) Unmap() PushRuleArray

type PushRuleType

type PushRuleType string
const (
	OverrideRule  PushRuleType = "override"
	ContentRule   PushRuleType = "content"
	RoomRule      PushRuleType = "room"
	SenderRule    PushRuleType = "sender"
	UnderrideRule PushRuleType = "underride"
)

type PushRuleset

type PushRuleset struct {
	Override  PushRuleArray
	Content   PushRuleArray
	Room      PushRuleMap
	Sender    PushRuleMap
	Underride PushRuleArray
}

func EventToPushRules

func EventToPushRules(evt *event.Event) (*PushRuleset, error)

EventToPushRules converts a m.push_rules event to a PushRuleset by passing the data through JSON.

func (*PushRuleset) GetActions

func (rs *PushRuleset) GetActions(room Room, evt *event.Event) (match PushActionArray)

GetActions matches the given event against all of the push rule collections in this push ruleset in the order of priority as specified in spec section 11.12.1.4.

func (*PushRuleset) GetMatchingRule

func (rs *PushRuleset) GetMatchingRule(room Room, evt *event.Event) (rule *PushRule)

func (*PushRuleset) MarshalJSON

func (rs *PushRuleset) MarshalJSON() ([]byte, error)

MarshalJSON is the reverse of UnmarshalJSON()

func (*PushRuleset) UnmarshalJSON

func (rs *PushRuleset) UnmarshalJSON(raw []byte) (err error)

UnmarshalJSON parses JSON into this PushRuleset.

For override, sender and underride push rule arrays, the type is added to each PushRule and the array is used as-is.

For room and sender push rule arrays, the type is added to each PushRule and the array is converted to a map with the rule ID as the key and the PushRule as the value.

type Room

type Room interface {
	GetOwnDisplayname() string
	GetMemberCount() int
}

Room is an interface with the functions that are needed for processing room-specific push conditions

Directories

Path Synopsis
Package glob provides objects for matching strings with globs
Package glob provides objects for matching strings with globs

Jump to

Keyboard shortcuts

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