Fix m.receipts causing notifications (#2893)

Fixes https://github.com/matrix-org/dendrite/issues/2353
This commit is contained in:
Till 2022-11-29 15:46:28 +01:00 committed by GitHub
parent 1990c154e9
commit f8d1dc521d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 108 additions and 2 deletions

View File

@ -87,8 +87,7 @@ func (p *ReceiptStreamProvider) IncrementalSync(
}
ev := gomatrixserverlib.ClientEvent{
Type: gomatrixserverlib.MReceipt,
RoomID: roomID,
Type: gomatrixserverlib.MReceipt,
}
content := make(map[string]ReceiptMRead)
for _, receipt := range receipts {

View File

@ -480,6 +480,13 @@ func (jr JoinResponse) MarshalJSON() ([]byte, error) {
if jr.Ephemeral != nil && len(jr.Ephemeral.Events) == 0 {
a.Ephemeral = nil
}
if jr.Ephemeral != nil {
// Remove the room_id from EDUs, as this seems to cause Element Web
// to trigger notifications - https://github.com/vector-im/element-web/issues/17263
for i := range jr.Ephemeral.Events {
jr.Ephemeral.Events[i].RoomID = ""
}
}
if jr.AccountData != nil && len(jr.AccountData.Events) == 0 {
a.AccountData = nil
}

View File

@ -2,6 +2,7 @@ package types
import (
"encoding/json"
"reflect"
"testing"
"github.com/matrix-org/gomatrixserverlib"
@ -63,3 +64,102 @@ func TestNewInviteResponse(t *testing.T) {
t.Fatalf("Invite response didn't contain correct info")
}
}
func TestJoinResponse_MarshalJSON(t *testing.T) {
type fields struct {
Summary *Summary
State *ClientEvents
Timeline *Timeline
Ephemeral *ClientEvents
AccountData *ClientEvents
UnreadNotifications *UnreadNotifications
}
tests := []struct {
name string
fields fields
want []byte
wantErr bool
}{
{
name: "empty state is removed",
fields: fields{
State: &ClientEvents{},
},
want: []byte("{}"),
},
{
name: "empty accountdata is removed",
fields: fields{
AccountData: &ClientEvents{},
},
want: []byte("{}"),
},
{
name: "empty ephemeral is removed",
fields: fields{
Ephemeral: &ClientEvents{},
},
want: []byte("{}"),
},
{
name: "empty timeline is removed",
fields: fields{
Timeline: &Timeline{},
},
want: []byte("{}"),
},
{
name: "empty summary is removed",
fields: fields{
Summary: &Summary{},
},
want: []byte("{}"),
},
{
name: "unread notifications are removed, if everything else is empty",
fields: fields{
UnreadNotifications: &UnreadNotifications{},
},
want: []byte("{}"),
},
{
name: "unread notifications are NOT removed, if state is set",
fields: fields{
State: &ClientEvents{Events: []gomatrixserverlib.ClientEvent{{Content: []byte("{}")}}},
UnreadNotifications: &UnreadNotifications{NotificationCount: 1},
},
want: []byte(`{"state":{"events":[{"content":{},"type":""}]},"unread_notifications":{"highlight_count":0,"notification_count":1}}`),
},
{
name: "roomID is removed from EDUs",
fields: fields{
Ephemeral: &ClientEvents{
Events: []gomatrixserverlib.ClientEvent{
{RoomID: "!someRandomRoomID:test", Content: []byte("{}")},
},
},
},
want: []byte(`{"ephemeral":{"events":[{"content":{},"type":""}]}}`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
jr := JoinResponse{
Summary: tt.fields.Summary,
State: tt.fields.State,
Timeline: tt.fields.Timeline,
Ephemeral: tt.fields.Ephemeral,
AccountData: tt.fields.AccountData,
UnreadNotifications: tt.fields.UnreadNotifications,
}
got, err := jr.MarshalJSON()
if (err != nil) != tt.wantErr {
t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MarshalJSON() got = %v, want %v", string(got), string(tt.want))
}
})
}
}