Skip to content

Commit

Permalink
Implement MSC2625 (unread counters) (#890)
Browse files Browse the repository at this point in the history
Test for matrix-org/synapse#7673

This is basically hacking `Messages that notify from another user increment unread notification count` so it also runs for the action specified in MSC2625.
  • Loading branch information
babolivier authored Jun 17, 2020
1 parent 4e1aa9d commit 47533ca
Showing 1 changed file with 104 additions and 60 deletions.
164 changes: 104 additions & 60 deletions tests/61push/03_unread_count.pl
Original file line number Diff line number Diff line change
@@ -1,64 +1,108 @@
test "Messages that notify from another user increment unread notification count",
requires => [ local_user_fixture( with_events => 0 ),
local_user_fixture( with_events => 0 ),
qw( can_sync ) ],

check => sub {
my ( $user1, $user2 ) = @_;

my $room_id;

matrix_add_push_rule( $user1, 'global', 'content', 'anything', {
pattern => "*",
actions => [ "notify" ]
})->then( sub {
matrix_create_room( $user1 );
})->then( sub {
( $room_id ) = @_;

matrix_join_room( $user2, $room_id );
})->then( sub {
matrix_send_room_text_message( $user2, $room_id,
body => "Test message 1",
);
})->then( sub {
my ( $event_id ) = @_;

matrix_advance_room_receipt_synced( $user1, $room_id, "m.read" => $event_id );
})->then( sub {
matrix_sync( $user1 );
})->then( sub {
my ( $body ) = @_;

my $room = $body->{rooms}{join}{$room_id};

assert_json_keys( $room, "unread_notifications" );
my $unread = $room->{unread_notifications};
assert_json_keys( $unread, "notification_count" );

$unread->{notification_count} == 0
or die "Expected unread notification count to be 0";

matrix_send_room_text_message_synced( $user2, $room_id,
body => "Test message 2",
# fixture which registers a user, and sets up a push rule for them
# the push rule matches any content and performs the given action.
#
# returns the user id.
sub user_with_push_rule_fixture
{
my( $action, $skip_on_fail ) = @_;

return fixture(
name => "user_with_push_rule_fixture($action)",
requires => [ local_user_fixture() ],
setup => sub {
my ( $user ) = @_;

matrix_add_push_rule( $user, 'global', 'content', 'anything', {
pattern => "*",
actions => [ $action ]
})->then_with_f(
sub { # done
Future->done( $user );
},
http => sub { # catch http
my ( $f, undef, undef, $response ) = @_;
log_if_fail "push rule fail", $response;
if ( $skip_on_fail && $response && $response->code == 400 ) {
# assume the server doesn't support this
die "SKIP: server does not support $action action";
}
# otherwise propagate the original result
return $f;
},
);
})->then( sub {
matrix_sync( $user1 );
})->then( sub {
my ( $body ) = @_;

my $room = $body->{rooms}{join}{$room_id};

assert_json_keys( $room, "unread_notifications" );
my $unread = $room->{unread_notifications};
assert_json_keys( $unread, "notification_count" );

$unread->{notification_count} == 1
or die "Expected unread notification count to be 1";

Future->done(1);
})
};
},
);
}

foreach my $action_and_counter (
[ "notify", "notification_count" ],
[ "org.matrix.msc2625.mark_unread", "org.matrix.msc2625.unread_count" ]
) {
my ( $action, $counter ) = @$action_and_counter;
test "Messages that $action from another user increment $counter",
requires => [
user_with_push_rule_fixture(
$action,
$action eq "org.matrix.msc2625.mark_unread",
),
local_user_fixture(),
qw( can_sync )
],

check => sub {
my ( $user1, $user2 ) = @_;

my $room_id;

matrix_create_room( $user1 )
->then( sub {
($room_id) = @_;

matrix_join_room( $user2, $room_id );
})->then( sub {
matrix_send_room_text_message( $user2, $room_id,
body => "Test message 1",
);
})->then( sub {
my ( $event_id ) = @_;

matrix_advance_room_receipt_synced( $user1, $room_id, "m.read" => $event_id );
})->then( sub {
matrix_sync( $user1 );
})->then( sub {
my ( $body ) = @_;

my $room = $body->{rooms}{join}{$room_id};
log_if_fail "room in sync response", $room;

assert_json_keys( $room, "unread_notifications" );
my $unread = $room->{unread_notifications};
assert_json_keys( $unread, $counter );

$unread->{$counter} == 0
or die "Expected $counter to be 0";

matrix_send_room_text_message_synced( $user2, $room_id,
body => "Test message 2",
);
})->then( sub {
matrix_sync( $user1 );
})->then( sub {
my ( $body ) = @_;

my $room = $body->{rooms}{join}{$room_id};

assert_json_keys( $room, "unread_notifications" );
my $unread = $room->{unread_notifications};
assert_json_keys( $unread, $counter );

$unread->{$counter} == 1
or die "Expected $counter to be 1";

Future->done(1);
});
};
}

test "Messages that highlight from another user increment unread highlight count",
requires => [ local_user_fixture( with_events => 0 ),
Expand Down

0 comments on commit 47533ca

Please sign in to comment.