2022-09-09 12:56:33 +01:00
|
|
|
package consumers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-02 10:18:11 +00:00
|
|
|
"reflect"
|
|
|
|
"sync"
|
2022-09-09 12:56:33 +01:00
|
|
|
"testing"
|
2022-11-02 10:18:11 +00:00
|
|
|
"time"
|
2022-09-09 12:56:33 +01:00
|
|
|
|
2023-03-17 11:09:45 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2023-04-27 12:54:20 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2022-09-09 12:56:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-09-09 12:56:33 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/internal/pushrules"
|
2023-06-06 21:55:18 +01:00
|
|
|
rsapi "github.com/matrix-org/dendrite/roomserver/api"
|
2022-09-09 12:56:33 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
|
|
"github.com/matrix-org/dendrite/test"
|
|
|
|
"github.com/matrix-org/dendrite/userapi/storage"
|
2022-11-02 10:18:11 +00:00
|
|
|
userAPITypes "github.com/matrix-org/dendrite/userapi/types"
|
2022-09-09 12:56:33 +01:00
|
|
|
)
|
|
|
|
|
2023-02-20 13:58:03 +00:00
|
|
|
func mustCreateDatabase(t *testing.T, dbType test.DBType) (storage.UserDatabase, func()) {
|
2022-09-09 12:56:33 +01:00
|
|
|
t.Helper()
|
|
|
|
connStr, close := test.PrepareDBConnectionString(t, dbType)
|
2023-03-22 08:21:32 +00:00
|
|
|
cm := sqlutil.NewConnectionManager(nil, config.DatabaseOptions{})
|
2023-03-17 11:09:45 +00:00
|
|
|
db, err := storage.NewUserDatabase(context.Background(), cm, &config.DatabaseOptions{
|
2022-09-09 12:56:33 +01:00
|
|
|
ConnectionString: config.DataSource(connStr),
|
|
|
|
}, "", 4, 0, 0, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create new user db: %v", err)
|
|
|
|
}
|
2022-10-18 15:59:08 +01:00
|
|
|
return db, func() {
|
|
|
|
close()
|
|
|
|
}
|
2022-09-09 12:56:33 +01:00
|
|
|
}
|
|
|
|
|
2023-04-27 12:54:20 +01:00
|
|
|
func mustCreateEvent(t *testing.T, content string) *types.HeaderedEvent {
|
2022-09-09 12:56:33 +01:00
|
|
|
t.Helper()
|
2023-04-21 17:06:29 +01:00
|
|
|
ev, err := gomatrixserverlib.MustGetRoomVersion(gomatrixserverlib.RoomVersionV10).NewEventFromTrustedJSON([]byte(content), false)
|
2022-09-09 12:56:33 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create event: %v", err)
|
|
|
|
}
|
2023-05-02 15:03:16 +01:00
|
|
|
return &types.HeaderedEvent{PDU: ev}
|
2022-09-09 12:56:33 +01:00
|
|
|
}
|
|
|
|
|
2023-06-06 21:55:18 +01:00
|
|
|
type FakeUserRoomserverAPI struct{ rsapi.UserRoomserverAPI }
|
|
|
|
|
2023-06-14 15:23:46 +01:00
|
|
|
func (f *FakeUserRoomserverAPI) QueryUserIDForSender(ctx context.Context, roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-07 18:14:35 +01:00
|
|
|
return spec.NewUserID(string(senderID), true)
|
2023-06-06 21:55:18 +01:00
|
|
|
}
|
|
|
|
|
2022-09-09 12:56:33 +01:00
|
|
|
func Test_evaluatePushRules(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
|
|
db, close := mustCreateDatabase(t, dbType)
|
|
|
|
defer close()
|
2023-06-06 21:55:18 +01:00
|
|
|
consumer := OutputRoomEventConsumer{db: db, rsAPI: &FakeUserRoomserverAPI{}}
|
2022-09-09 12:56:33 +01:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
eventContent string
|
|
|
|
wantAction pushrules.ActionKind
|
|
|
|
wantActions []*pushrules.Action
|
|
|
|
wantNotify bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "m.receipt doesn't notify",
|
2023-06-14 15:23:46 +01:00
|
|
|
eventContent: `{"type":"m.receipt","room_id":"!room:example.com"}`,
|
2022-09-09 12:56:33 +01:00
|
|
|
wantAction: pushrules.UnknownAction,
|
|
|
|
wantActions: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "m.reaction doesn't notify",
|
2023-06-14 15:23:46 +01:00
|
|
|
eventContent: `{"type":"m.reaction","room_id":"!room:example.com"}`,
|
2022-09-09 12:56:33 +01:00
|
|
|
wantAction: pushrules.DontNotifyAction,
|
|
|
|
wantActions: []*pushrules.Action{
|
|
|
|
{
|
|
|
|
Kind: pushrules.DontNotifyAction,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "m.room.message notifies",
|
2023-06-14 15:23:46 +01:00
|
|
|
eventContent: `{"type":"m.room.message","room_id":"!room:example.com"}`,
|
2022-09-09 12:56:33 +01:00
|
|
|
wantNotify: true,
|
|
|
|
wantAction: pushrules.NotifyAction,
|
|
|
|
wantActions: []*pushrules.Action{
|
|
|
|
{Kind: pushrules.NotifyAction},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "m.room.message highlights",
|
2023-06-14 15:23:46 +01:00
|
|
|
eventContent: `{"type":"m.room.message", "content": {"body": "test"},"room_id":"!room:example.com"}`,
|
2022-09-09 12:56:33 +01:00
|
|
|
wantNotify: true,
|
|
|
|
wantAction: pushrules.NotifyAction,
|
|
|
|
wantActions: []*pushrules.Action{
|
|
|
|
{Kind: pushrules.NotifyAction},
|
|
|
|
{
|
|
|
|
Kind: pushrules.SetTweakAction,
|
|
|
|
Tweak: pushrules.SoundTweak,
|
|
|
|
Value: "default",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Kind: pushrules.SetTweakAction,
|
|
|
|
Tweak: pushrules.HighlightTweak,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
actions, err := consumer.evaluatePushRules(ctx, mustCreateEvent(t, tc.eventContent), &localMembership{
|
|
|
|
UserID: "@test:localhost",
|
|
|
|
Localpart: "test",
|
|
|
|
Domain: "localhost",
|
|
|
|
}, 10)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to evaluate push rules: %v", err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, tc.wantActions, actions)
|
|
|
|
gotAction, _, err := pushrules.ActionsToTweaks(actions)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get actions: %v", err)
|
|
|
|
}
|
|
|
|
if gotAction != tc.wantAction {
|
|
|
|
t.Fatalf("expected action to be '%s', got '%s'", tc.wantAction, gotAction)
|
|
|
|
}
|
|
|
|
// this is taken from `notifyLocal`
|
|
|
|
if tc.wantNotify && gotAction != pushrules.NotifyAction && gotAction != pushrules.CoalesceAction {
|
|
|
|
t.Fatalf("expected to notify but didn't")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-11-02 10:18:11 +00:00
|
|
|
|
|
|
|
func TestMessageStats(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
eventType string
|
|
|
|
eventSender string
|
|
|
|
roomID string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2023-04-19 15:50:33 +01:00
|
|
|
ourServer spec.ServerName
|
2022-11-02 10:18:11 +00:00
|
|
|
lastUpdate time.Time
|
2023-04-19 15:50:33 +01:00
|
|
|
initRoomCounts map[spec.ServerName]map[string]bool
|
2022-11-02 10:18:11 +00:00
|
|
|
wantStats userAPITypes.MessageStats
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "m.room.create does not count as a message",
|
|
|
|
ourServer: "localhost",
|
|
|
|
args: args{
|
|
|
|
eventType: "m.room.create",
|
|
|
|
eventSender: "@alice:localhost",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "our server - message",
|
|
|
|
ourServer: "localhost",
|
|
|
|
args: args{
|
|
|
|
eventType: "m.room.message",
|
|
|
|
eventSender: "@alice:localhost",
|
|
|
|
roomID: "normalRoom",
|
|
|
|
},
|
|
|
|
wantStats: userAPITypes.MessageStats{Messages: 1, SentMessages: 1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "our server - E2EE message",
|
|
|
|
ourServer: "localhost",
|
|
|
|
args: args{
|
|
|
|
eventType: "m.room.encrypted",
|
|
|
|
eventSender: "@alice:localhost",
|
|
|
|
roomID: "encryptedRoom",
|
|
|
|
},
|
|
|
|
wantStats: userAPITypes.MessageStats{Messages: 1, SentMessages: 1, MessagesE2EE: 1, SentMessagesE2EE: 1},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "remote server - message",
|
|
|
|
ourServer: "localhost",
|
|
|
|
args: args{
|
|
|
|
eventType: "m.room.message",
|
|
|
|
eventSender: "@alice:remote",
|
|
|
|
roomID: "normalRoom",
|
|
|
|
},
|
|
|
|
wantStats: userAPITypes.MessageStats{Messages: 2, SentMessages: 1, MessagesE2EE: 1, SentMessagesE2EE: 1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "remote server - E2EE message",
|
|
|
|
ourServer: "localhost",
|
|
|
|
args: args{
|
|
|
|
eventType: "m.room.encrypted",
|
|
|
|
eventSender: "@alice:remote",
|
|
|
|
roomID: "encryptedRoom",
|
|
|
|
},
|
|
|
|
wantStats: userAPITypes.MessageStats{Messages: 2, SentMessages: 1, MessagesE2EE: 2, SentMessagesE2EE: 1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "day change creates a new room map",
|
|
|
|
ourServer: "localhost",
|
|
|
|
lastUpdate: time.Now().Add(-time.Hour * 24),
|
2023-04-19 15:50:33 +01:00
|
|
|
initRoomCounts: map[spec.ServerName]map[string]bool{
|
2022-11-02 10:18:11 +00:00
|
|
|
"localhost": {"encryptedRoom": true},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
eventType: "m.room.encrypted",
|
|
|
|
eventSender: "@alice:remote",
|
|
|
|
roomID: "someOtherRoom",
|
|
|
|
},
|
|
|
|
wantStats: userAPITypes.MessageStats{Messages: 2, SentMessages: 1, MessagesE2EE: 3, SentMessagesE2EE: 1},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
|
|
db, close := mustCreateDatabase(t, dbType)
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if tt.lastUpdate.IsZero() {
|
|
|
|
tt.lastUpdate = time.Now()
|
|
|
|
}
|
|
|
|
if tt.initRoomCounts == nil {
|
2023-04-19 15:50:33 +01:00
|
|
|
tt.initRoomCounts = map[spec.ServerName]map[string]bool{}
|
2022-11-02 10:18:11 +00:00
|
|
|
}
|
|
|
|
s := &OutputRoomEventConsumer{
|
|
|
|
db: db,
|
2023-04-19 15:50:33 +01:00
|
|
|
msgCounts: map[spec.ServerName]userAPITypes.MessageStats{},
|
2022-11-02 10:18:11 +00:00
|
|
|
roomCounts: tt.initRoomCounts,
|
|
|
|
countsLock: sync.Mutex{},
|
|
|
|
lastUpdate: tt.lastUpdate,
|
|
|
|
serverName: tt.ourServer,
|
|
|
|
}
|
|
|
|
s.storeMessageStats(context.Background(), tt.args.eventType, tt.args.eventSender, tt.args.roomID)
|
|
|
|
t.Logf("%+v", s.roomCounts)
|
|
|
|
gotStats, activeRooms, activeE2EERooms, err := db.DailyRoomsMessages(context.Background(), tt.ourServer)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(gotStats, tt.wantStats) {
|
|
|
|
t.Fatalf("expected %+v, got %+v", tt.wantStats, gotStats)
|
|
|
|
}
|
|
|
|
if tt.args.eventType == "m.room.encrypted" && activeE2EERooms != 1 {
|
|
|
|
t.Fatalf("expected room to be activeE2EE")
|
|
|
|
}
|
|
|
|
if tt.args.eventType == "m.room.message" && activeRooms != 1 {
|
|
|
|
t.Fatalf("expected room to be active")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|