1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
package internal
import (
"context"
"fmt"
"math"
"testing"
rsapi "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/roomserver/types"
"github.com/matrix-org/dendrite/syncapi/storage"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/gomatrixserverlib/spec"
"gotest.tools/v3/assert"
)
type mockHisVisRoomserverAPI struct {
rsapi.RoomserverInternalAPI
events []*types.HeaderedEvent
roomID string
}
func (s *mockHisVisRoomserverAPI) QueryMembershipAtEvent(ctx context.Context, roomID spec.RoomID, eventIDs []string, senderID spec.SenderID) (map[string]*types.HeaderedEvent, error) {
if roomID.String() == s.roomID {
membershipMap := map[string]*types.HeaderedEvent{}
for _, queriedEventID := range eventIDs {
for _, event := range s.events {
if event.EventID() == queriedEventID {
membershipMap[queriedEventID] = event
}
}
}
return membershipMap, nil
} else {
return nil, fmt.Errorf("room not found: \"%v\"", roomID)
}
}
func (s *mockHisVisRoomserverAPI) QuerySenderIDForUser(ctx context.Context, roomID spec.RoomID, userID spec.UserID) (*spec.SenderID, error) {
senderID := spec.SenderIDFromUserID(userID)
return &senderID, nil
}
func (s *mockHisVisRoomserverAPI) QueryUserIDForSender(ctx context.Context, roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
userID := senderID.ToUserID()
if userID == nil {
return nil, fmt.Errorf("sender ID not user ID")
}
return userID, nil
}
type mockDB struct {
storage.DatabaseTransaction
// user ID -> membership (i.e. 'join', 'leave', etc.)
currentMembership map[string]string
roomID string
}
func (s *mockDB) SelectMembershipForUser(ctx context.Context, roomID string, userID string, pos int64) (string, int64, error) {
if roomID == s.roomID {
membership, ok := s.currentMembership[userID]
if !ok {
return spec.Leave, math.MaxInt64, nil
}
return membership, math.MaxInt64, nil
}
return "", 0, fmt.Errorf("room not found: \"%v\"", roomID)
}
// Tests logic around history visibility boundaries
//
// Specifically that if a room's history visibility before or after a particular history visibility event
// allows them to see events (a boundary), then the history visibility event itself should be shown
// ( spec: https://spec.matrix.org/v1.8/client-server-api/#server-behaviour-5 )
//
// This also aims to emulate "Only see history_visibility changes on bounadries" in sytest/tests/30rooms/30history-visibility.pl
func Test_ApplyHistoryVisbility_Boundaries(t *testing.T) {
ctx := context.Background()
roomID := "!roomid:domain"
creatorUserID := spec.NewUserIDOrPanic("@creator:domain", false)
otherUserID := spec.NewUserIDOrPanic("@other:domain", false)
roomVersion := gomatrixserverlib.RoomVersionV10
roomVerImpl := gomatrixserverlib.MustGetRoomVersion(roomVersion)
eventsJSON := []struct {
id string
json string
}{
{id: "$create-event", json: fmt.Sprintf(`{
"type": "m.room.create", "state_key": "",
"room_id": "%v", "sender": "%v",
"content": {"creator": "%v", "room_version": "%v"}
}`, roomID, creatorUserID.String(), creatorUserID.String(), roomVersion)},
{id: "$creator-joined", json: fmt.Sprintf(`{
"type": "m.room.member", "state_key": "%v",
"room_id": "%v", "sender": "%v",
"content": {"membership": "join"}
}`, creatorUserID.String(), roomID, creatorUserID.String())},
{id: "$hisvis-1", json: fmt.Sprintf(`{
"type": "m.room.history_visibility", "state_key": "",
"room_id": "%v", "sender": "%v",
"content": {"history_visibility": "shared"}
}`, roomID, creatorUserID.String())},
{id: "$msg-1", json: fmt.Sprintf(`{
"type": "m.room.message",
"room_id": "%v", "sender": "%v",
"content": {"body": "1"}
}`, roomID, creatorUserID.String())},
{id: "$hisvis-2", json: fmt.Sprintf(`{
"type": "m.room.history_visibility", "state_key": "",
"room_id": "%v", "sender": "%v",
"content": {"history_visibility": "joined"},
"unsigned": {"prev_content": {"history_visibility": "shared"}}
}`, roomID, creatorUserID.String())},
{id: "$msg-2", json: fmt.Sprintf(`{
"type": "m.room.message",
"room_id": "%v", "sender": "%v",
"content": {"body": "1"}
}`, roomID, creatorUserID.String())},
{id: "$hisvis-3", json: fmt.Sprintf(`{
"type": "m.room.history_visibility", "state_key": "",
"room_id": "%v", "sender": "%v",
"content": {"history_visibility": "invited"},
"unsigned": {"prev_content": {"history_visibility": "joined"}}
}`, roomID, creatorUserID.String())},
{id: "$msg-3", json: fmt.Sprintf(`{
"type": "m.room.message",
"room_id": "%v", "sender": "%v",
"content": {"body": "2"}
}`, roomID, creatorUserID.String())},
{id: "$hisvis-4", json: fmt.Sprintf(`{
"type": "m.room.history_visibility", "state_key": "",
"room_id": "%v", "sender": "%v",
"content": {"history_visibility": "shared"},
"unsigned": {"prev_content": {"history_visibility": "invited"}}
}`, roomID, creatorUserID.String())},
{id: "$msg-4", json: fmt.Sprintf(`{
"type": "m.room.message",
"room_id": "%v", "sender": "%v",
"content": {"body": "3"}
}`, roomID, creatorUserID.String())},
{id: "$other-joined", json: fmt.Sprintf(`{
"type": "m.room.member", "state_key": "%v",
"room_id": "%v", "sender": "%v",
"content": {"membership": "join"}
}`, otherUserID.String(), roomID, otherUserID.String())},
}
events := make([]*types.HeaderedEvent, len(eventsJSON))
hisVis := gomatrixserverlib.HistoryVisibilityShared
for i, eventJSON := range eventsJSON {
pdu, err := roomVerImpl.NewEventFromTrustedJSONWithEventID(eventJSON.id, []byte(eventJSON.json), false)
if err != nil {
t.Fatalf("failed to prepare event %s for test: %s", eventJSON.id, err.Error())
}
events[i] = &types.HeaderedEvent{PDU: pdu}
// 'Visibility' should be the visibility of the room just before this event was sent
// (according to processRoomEvent in roomserver/internal/input/input_events.go)
events[i].Visibility = hisVis
if pdu.Type() == spec.MRoomHistoryVisibility {
newHisVis, err := pdu.HistoryVisibility()
if err != nil {
t.Fatalf("failed to prepare history visibility event: %s", err.Error())
}
hisVis = newHisVis
}
}
rsAPI := &mockHisVisRoomserverAPI{
events: events,
roomID: roomID,
}
syncDB := &mockDB{
roomID: roomID,
currentMembership: map[string]string{
creatorUserID.String(): spec.Join,
otherUserID.String(): spec.Join,
},
}
filteredEvents, err := ApplyHistoryVisibilityFilter(ctx, syncDB, rsAPI, events, nil, otherUserID, "hisVisTest")
if err != nil {
t.Fatalf("ApplyHistoryVisibility returned non-nil error: %s", err.Error())
}
filteredEventIDs := make([]string, len(filteredEvents))
for i, event := range filteredEvents {
filteredEventIDs[i] = event.EventID()
}
assert.DeepEqual(t,
[]string{
"$create-event", // Always see m.room.create
"$creator-joined", // Always see membership
"$hisvis-1", // Sets room to shared (technically the room is already shared since shared is default)
"$msg-1", // Room currently 'shared'
"$hisvis-2", // Room changed from 'shared' to 'joined', so boundary event and should be shared
// Other events hidden, as other is not joined yet
// hisvis-3 is also hidden, as it changes from joined to invited, neither of which is visible to other
"$hisvis-4", // Changes from 'invited' to 'shared', so is a boundary event and visible
"$msg-4", // Room is 'shared', so visible
"$other-joined", // other's membership
},
filteredEventIDs,
)
}
|