This commit is contained in:
Kegan Dougal 2020-07-24 15:47:39 +01:00
parent 8a75ff1029
commit cc7ff3815b
2 changed files with 12 additions and 14 deletions

View file

@ -161,6 +161,7 @@ func (s *OutputKeyChangeEventConsumer) OnLeaveEvent(ev *gomatrixserverlib.Header
} }
// nolint:gocyclo
func (s *OutputKeyChangeEventConsumer) trackChangedUsers( func (s *OutputKeyChangeEventConsumer) trackChangedUsers(
ctx context.Context, userID string, newlyJoinedRooms, newlyLeftRooms []string, ctx context.Context, userID string, newlyJoinedRooms, newlyLeftRooms []string,
) (changed, left []string, err error) { ) (changed, left []string, err error) {
@ -195,7 +196,7 @@ func (s *OutputKeyChangeEventConsumer) trackChangedUsers(
} }
for _, state := range stateRes.Rooms { for _, state := range stateRes.Rooms {
for tuple, membership := range state { for tuple, membership := range state {
if membership != "join" { if membership != gomatrixserverlib.Join {
continue continue
} }
queryRes.UserIDsToCount[tuple.StateKey]-- queryRes.UserIDsToCount[tuple.StateKey]--
@ -235,7 +236,7 @@ func (s *OutputKeyChangeEventConsumer) trackChangedUsers(
} }
for _, state := range stateRes.Rooms { for _, state := range stateRes.Rooms {
for tuple, membership := range state { for tuple, membership := range state {
if membership != "join" { if membership != gomatrixserverlib.Join {
continue continue
} }
// new user who we weren't previously sharing rooms with // new user who we weren't previously sharing rooms with

View file

@ -11,6 +11,10 @@ import (
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
) )
const (
syncingUser = "@alice:localhost"
)
type mockCurrentStateAPI struct { type mockCurrentStateAPI struct {
roomIDToJoinedMembers map[string][]string roomIDToJoinedMembers map[string][]string
} }
@ -134,9 +138,8 @@ func leaveResponseWithRooms(syncResponse *types.Response, userID string, roomIDs
// tests that joining a room which results in sharing a new user includes that user in `changed` // tests that joining a room which results in sharing a new user includes that user in `changed`
func TestKeyChangeCatchupOnJoinShareNewUser(t *testing.T) { func TestKeyChangeCatchupOnJoinShareNewUser(t *testing.T) {
syncingUser := "@alice:localhost"
newShareUser := "@bob:localhost" newShareUser := "@bob:localhost"
newlyJoinedRoom := "!foo:bar" newlyJoinedRoom := "!TestKeyChangeCatchupOnJoinShareNewUser:bar"
consumer := NewOutputKeyChangeEventConsumer(gomatrixserverlib.ServerName("localhost"), "some_topic", nil, &mockCurrentStateAPI{ consumer := NewOutputKeyChangeEventConsumer(gomatrixserverlib.ServerName("localhost"), "some_topic", nil, &mockCurrentStateAPI{
roomIDToJoinedMembers: map[string][]string{ roomIDToJoinedMembers: map[string][]string{
newlyJoinedRoom: {syncingUser, newShareUser}, newlyJoinedRoom: {syncingUser, newShareUser},
@ -158,9 +161,8 @@ func TestKeyChangeCatchupOnJoinShareNewUser(t *testing.T) {
// tests that leaving a room which results in sharing no rooms with a user includes that user in `left` // tests that leaving a room which results in sharing no rooms with a user includes that user in `left`
func TestKeyChangeCatchupOnLeaveShareLeftUser(t *testing.T) { func TestKeyChangeCatchupOnLeaveShareLeftUser(t *testing.T) {
syncingUser := "@alice:localhost"
removeUser := "@bob:localhost" removeUser := "@bob:localhost"
newlyLeftRoom := "!foo:bar" newlyLeftRoom := "!TestKeyChangeCatchupOnLeaveShareLeftUser:bar"
consumer := NewOutputKeyChangeEventConsumer(gomatrixserverlib.ServerName("localhost"), "some_topic", nil, &mockCurrentStateAPI{ consumer := NewOutputKeyChangeEventConsumer(gomatrixserverlib.ServerName("localhost"), "some_topic", nil, &mockCurrentStateAPI{
roomIDToJoinedMembers: map[string][]string{ roomIDToJoinedMembers: map[string][]string{
newlyLeftRoom: {removeUser}, newlyLeftRoom: {removeUser},
@ -182,9 +184,8 @@ func TestKeyChangeCatchupOnLeaveShareLeftUser(t *testing.T) {
// tests that joining a room which doesn't result in sharing a new user results in no changes. // tests that joining a room which doesn't result in sharing a new user results in no changes.
func TestKeyChangeCatchupOnJoinShareNoNewUsers(t *testing.T) { func TestKeyChangeCatchupOnJoinShareNoNewUsers(t *testing.T) {
syncingUser := "@alice:localhost"
existingUser := "@bob:localhost" existingUser := "@bob:localhost"
newlyJoinedRoom := "!foo:bar" newlyJoinedRoom := "!TestKeyChangeCatchupOnJoinShareNoNewUsers:bar"
consumer := NewOutputKeyChangeEventConsumer(gomatrixserverlib.ServerName("localhost"), "some_topic", nil, &mockCurrentStateAPI{ consumer := NewOutputKeyChangeEventConsumer(gomatrixserverlib.ServerName("localhost"), "some_topic", nil, &mockCurrentStateAPI{
roomIDToJoinedMembers: map[string][]string{ roomIDToJoinedMembers: map[string][]string{
newlyJoinedRoom: {syncingUser, existingUser}, newlyJoinedRoom: {syncingUser, existingUser},
@ -205,9 +206,8 @@ func TestKeyChangeCatchupOnJoinShareNoNewUsers(t *testing.T) {
// tests that leaving a room which doesn't result in sharing no rooms with a user results in no changes. // tests that leaving a room which doesn't result in sharing no rooms with a user results in no changes.
func TestKeyChangeCatchupOnLeaveShareNoUsers(t *testing.T) { func TestKeyChangeCatchupOnLeaveShareNoUsers(t *testing.T) {
syncingUser := "@alice:localhost"
existingUser := "@bob:localhost" existingUser := "@bob:localhost"
newlyLeftRoom := "!foo:bar" newlyLeftRoom := "!TestKeyChangeCatchupOnLeaveShareNoUsers:bar"
consumer := NewOutputKeyChangeEventConsumer(gomatrixserverlib.ServerName("localhost"), "some_topic", nil, &mockCurrentStateAPI{ consumer := NewOutputKeyChangeEventConsumer(gomatrixserverlib.ServerName("localhost"), "some_topic", nil, &mockCurrentStateAPI{
roomIDToJoinedMembers: map[string][]string{ roomIDToJoinedMembers: map[string][]string{
newlyLeftRoom: {existingUser}, newlyLeftRoom: {existingUser},
@ -228,9 +228,8 @@ func TestKeyChangeCatchupOnLeaveShareNoUsers(t *testing.T) {
// tests that not joining any rooms (but having messages in the response) do not result in changes. // tests that not joining any rooms (but having messages in the response) do not result in changes.
func TestKeyChangeCatchupNoNewJoinsButMessages(t *testing.T) { func TestKeyChangeCatchupNoNewJoinsButMessages(t *testing.T) {
syncingUser := "@alice:localhost"
existingUser := "@bob:localhost" existingUser := "@bob:localhost"
roomID := "!foo:bar" roomID := "!TestKeyChangeCatchupNoNewJoinsButMessages:bar"
consumer := NewOutputKeyChangeEventConsumer(gomatrixserverlib.ServerName("localhost"), "some_topic", nil, &mockCurrentStateAPI{ consumer := NewOutputKeyChangeEventConsumer(gomatrixserverlib.ServerName("localhost"), "some_topic", nil, &mockCurrentStateAPI{
roomIDToJoinedMembers: map[string][]string{ roomIDToJoinedMembers: map[string][]string{
roomID: {syncingUser, existingUser}, roomID: {syncingUser, existingUser},
@ -288,7 +287,6 @@ func TestKeyChangeCatchupNoNewJoinsButMessages(t *testing.T) {
// tests that joining/leaving multiple rooms can result in both `changed` and `left` and they are not duplicated. // tests that joining/leaving multiple rooms can result in both `changed` and `left` and they are not duplicated.
func TestKeyChangeCatchupChangeAndLeft(t *testing.T) { func TestKeyChangeCatchupChangeAndLeft(t *testing.T) {
syncingUser := "@alice:localhost"
newShareUser := "@berta:localhost" newShareUser := "@berta:localhost"
newShareUser2 := "@bob:localhost" newShareUser2 := "@bob:localhost"
newlyLeftUser := "@charlie:localhost" newlyLeftUser := "@charlie:localhost"
@ -328,7 +326,6 @@ func TestKeyChangeCatchupChangeAndLeft(t *testing.T) {
// doesn't share any rooms with him. // doesn't share any rooms with him.
// Ergo, we put them in `left` as it is simpler. // Ergo, we put them in `left` as it is simpler.
func TestKeyChangeCatchupChangeAndLeftSameRoom(t *testing.T) { func TestKeyChangeCatchupChangeAndLeftSameRoom(t *testing.T) {
syncingUser := "@alice:localhost"
newShareUser := "@berta:localhost" newShareUser := "@berta:localhost"
newShareUser2 := "@bob:localhost" newShareUser2 := "@bob:localhost"
roomID := "!join:bar" roomID := "!join:bar"