<commit_before>use List::Util qw( first );

test "User sees their own presence in a sync",
   requires => [qw( first_api_client can_sync )],

   check => sub {
      my ( $http ) = @_;

      my ( $user, $filter_id );

      matrix_register_user_with_filter( $http, {} )->then( sub {
         ( $user, $filter_id ) = @_;

         matrix_sync( $user, filter => $filter_id );
      })->then( sub {
         my ( $body ) = @_;

         my $events = $body->{presence}{events};
         my $presence = first { $_->{type} eq "m.presence" } @$events
            or die "Expected to see our own presence";

         $presence->{sender} eq $user->user_id or die "Unexpected sender";
         $presence->{content}{presence} eq "online"
            or die "Expected to be online";

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


test "User is offline if they set_presence=offline in their sync",
   requires => [qw( first_api_client can_sync )],

   check => sub {
      my ( $http ) = @_;

      my ( $user, $filter_id );

      matrix_register_user_with_filter( $http, {} )->then( sub {
         ( $user, $filter_id ) = @_;

         matrix_sync( $user, filter => $filter_id, set_presence => "offline" );
      })->then( sub {
         my ( $body ) = @_;

         my $events = $body->{presence}{events};
         my $presence = first { $_->{type} eq "m.presence" } @$events
            or die "Expected to see our own presence";

         $presence->{sender} eq $user->user_id or die "Unexpected sender";
         $presence->{content}{presence} eq "offline"
            or die "Expected to be offline";

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


test "User sees updates to presence from other users in the incremental sync.",
   requires => [qw( first_api_client can_sync )],

   check => sub {
      my ( $http ) = @_;

      my ( $user_a, $user_b, $filter_id_a, $filter_id_b, $next_a );

      Future->needs_all(
         matrix_register_user_with_filter( $http, {} ),
         matrix_register_user_with_filter( $http, {} ),
      )->then( sub {
         ( $user_a, $filter_id_a, $user_b, $filter_id_b ) = @_;

         # We can't use matrix_create_and_join since that polls the event
         # stream to check that the user has joined.
         matrix_create_room( $user_a )->then( sub {
            my ( $room_id ) = @_;

            matrix_join_room( $user_b, $room_id );
         });
      })->then( sub {
         matrix_sync( $user_a, filter => $filter_id_a );
      })->then( sub {
         my ( $body ) = @_;

         $next_a = $body->{next_batch};
         # Set user B's presence to online by syncing.
         matrix_sync( $user_b, filter => $filter_id_b );
      })->then( sub {
         matrix_sync( $user_a, filter => $filter_id_a, since => $next_a );
      })->then( sub {
         my ( $body ) = @_;

         my $events = $body->{presence}{events};
         my $presence = first { $_->{type} eq "m.presence" } @$events
            or die "Expected to see B's presence";

         $presence->{sender} eq $user_b->user_id or die "Unexpected sender";
         $presence->{content}{presence} eq "online"
            or die "Expected B to be online";

         Future->done(1);
      })
   };
<commit_msg>Filter the v2 sync result to include only m.presence events<commit_after>use List::Util qw( first );

test "User sees their own presence in a sync",
   requires => [qw( first_api_client can_sync )],

   check => sub {
      my ( $http ) = @_;

      my ( $user, $filter_id );

      my $filter = { presence => { types => [ "m.presence" ] } };

      matrix_register_user_with_filter( $http, $filter )->then( sub {
         ( $user, $filter_id ) = @_;

         matrix_sync( $user, filter => $filter_id );
      })->then( sub {
         my ( $body ) = @_;

         my $events = $body->{presence}{events};
         my $presence = first { $_->{type} eq "m.presence" } @$events
            or die "Expected to see our own presence";

         $presence->{sender} eq $user->user_id or die "Unexpected sender";
         $presence->{content}{presence} eq "online"
            or die "Expected to be online";

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


test "User is offline if they set_presence=offline in their sync",
   requires => [qw( first_api_client can_sync )],

   check => sub {
      my ( $http ) = @_;

      my ( $user, $filter_id );

      my $filter = { presence => { types => [ "m.presence" ] } };

      matrix_register_user_with_filter( $http, $filter )->then( sub {
         ( $user, $filter_id ) = @_;

         matrix_sync( $user, filter => $filter_id, set_presence => "offline" );
      })->then( sub {
         my ( $body ) = @_;

         my $events = $body->{presence}{events};
         my $presence = first { $_->{type} eq "m.presence" } @$events
            or die "Expected to see our own presence";

         $presence->{sender} eq $user->user_id or die "Unexpected sender";
         $presence->{content}{presence} eq "offline"
            or die "Expected to be offline";

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


test "User sees updates to presence from other users in the incremental sync.",
   requires => [qw( first_api_client can_sync )],

   check => sub {
      my ( $http ) = @_;

      my ( $user_a, $user_b, $filter_id_a, $filter_id_b, $next_a );

      my $filter = { presence => { types => [ "m.presence" ] } };

      Future->needs_all(
         matrix_register_user_with_filter( $http, $filter ),
         matrix_register_user_with_filter( $http, $filter ),
      )->then( sub {
         ( $user_a, $filter_id_a, $user_b, $filter_id_b ) = @_;

         # We can't use matrix_create_and_join since that polls the event
         # stream to check that the user has joined.
         matrix_create_room( $user_a )->then( sub {
            my ( $room_id ) = @_;

            matrix_join_room( $user_b, $room_id );
         });
      })->then( sub {
         matrix_sync( $user_a, filter => $filter_id_a );
      })->then( sub {
         my ( $body ) = @_;

         $next_a = $body->{next_batch};
         # Set user B's presence to online by syncing.
         matrix_sync( $user_b, filter => $filter_id_b );
      })->then( sub {
         matrix_sync( $user_a, filter => $filter_id_a, since => $next_a );
      })->then( sub {
         my ( $body ) = @_;

         my $events = $body->{presence}{events};
         my $presence = first { $_->{type} eq "m.presence" } @$events
            or die "Expected to see B's presence";

         $presence->{sender} eq $user_b->user_id or die "Unexpected sender";
         $presence->{content}{presence} eq "online"
            or die "Expected B to be online";

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

{'fraction_non_alphanumeric': 0.15169630847406965, 'fraction_numerical': 0.0010461814377522045, 'mean_word_length': 2.1881848499285375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1523322', 'n_tokens_mistral': 2219, 'n_tokens_neox': 2147, 'n_words': 588}