Debian Codegraph @

filter.go

raw
1 // Copyright 2017 Jan Christian Grünhage
2
3 package mautrix
4
5 import (
6 "errors"
7
8 "maunium.net/go/mautrix/event"
9 "maunium.net/go/mautrix/id"
10 )
11
12 type EventFormat string
13
14 const (
15 EventFormatClient EventFormat = "client"
16 EventFormatFederation EventFormat = "federation"
17 )
18
19 // Filter is used by clients to specify how the server should filter responses to e.g. sync requests
20 // Specified by: https://spec.matrix.org/v1.2/client-server-api/#filtering
21 type Filter struct {
22 AccountData FilterPart `json:"account_data,omitempty"`
23 EventFields []string `json:"event_fields,omitempty"`
24 EventFormat EventFormat `json:"event_format,omitempty"`
25 Presence FilterPart `json:"presence,omitempty"`
26 Room RoomFilter `json:"room,omitempty"`
27 }
28
29 // RoomFilter is used to define filtering rules for room events
30 type RoomFilter struct {
31 AccountData FilterPart `json:"account_data,omitempty"`
32 Ephemeral FilterPart `json:"ephemeral,omitempty"`
33 IncludeLeave bool `json:"include_leave,omitempty"`
34 NotRooms []id.RoomID `json:"not_rooms,omitempty"`
35 Rooms []id.RoomID `json:"rooms,omitempty"`
36 State FilterPart `json:"state,omitempty"`
37 Timeline FilterPart `json:"timeline,omitempty"`
38 }
39
40 // FilterPart is used to define filtering rules for specific categories of events
41 type FilterPart struct {
42 NotRooms []id.RoomID `json:"not_rooms,omitempty"`
43 Rooms []id.RoomID `json:"rooms,omitempty"`
44 Limit int `json:"limit,omitempty"`
45 NotSenders []id.UserID `json:"not_senders,omitempty"`
46 NotTypes []event.Type `json:"not_types,omitempty"`
47 Senders []id.UserID `json:"senders,omitempty"`
48 Types []event.Type `json:"types,omitempty"`
49 ContainsURL *bool `json:"contains_url,omitempty"`
50
51 LazyLoadMembers bool `json:"lazy_load_members,omitempty"`
52 IncludeRedundantMembers bool `json:"include_redundant_members,omitempty"`
53 }
54
55 // Validate checks if the filter contains valid property values
56 func (filter *Filter) Validate() error {
57 if filter.EventFormat != EventFormatClient && filter.EventFormat != EventFormatFederation {
58 return errors.New("Bad event_format value. Must be one of [\"client\", \"federation\"]")
59 }
60 return nil
61 }
62
63 // DefaultFilter returns the default filter used by the Matrix server if no filter is provided in the request
64 func DefaultFilter() Filter {
65 return Filter{
66 AccountData: DefaultFilterPart(),
67 EventFields: nil,
68 EventFormat: "client",
69 Presence: DefaultFilterPart(),
70 Room: RoomFilter{
71 AccountData: DefaultFilterPart(),
72 Ephemeral: DefaultFilterPart(),
73 IncludeLeave: false,
74 NotRooms: nil,
75 Rooms: nil,
76 State: DefaultFilterPart(),
77 Timeline: DefaultFilterPart(),
78 },
79 }
80 }
81
82 // DefaultFilterPart returns the default filter part used by the Matrix server if no filter is provided in the request
83 func DefaultFilterPart() FilterPart {
84 return FilterPart{
85 NotRooms: nil,
86 Rooms: nil,
87 Limit: 20,
88 NotSenders: nil,
89 NotTypes: nil,
90 Senders: nil,
91 Types: nil,
92 }
93 }