File: Util.pm

package info (click to toggle)
libnet-async-matrix-perl 0.19-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 264 kB
  • sloc: perl: 2,595; makefile: 2
file content (136 lines) | stat: -rw-r--r-- 2,598 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package t::Util;

use strict;
use warnings;

use Carp;

use Exporter 'import';
our @EXPORT = qw(
   respond_json
   next_pending_not_sync
   next_pending_sync
   send_sync

   matrix_login
   matrix_join_room
);

use HTTP::Response;
use JSON::MaybeXS qw( encode_json );

use constant SYNC_PATH => "/_matrix/client/r0/sync";

sub respond_json
{
   my ( $p, $content ) = @_;

   ref $content or
      croak "respond_json() called with non-reference";

   $p->respond(
      HTTP::Response->new( 200, "OK", [ "Content-Type" => "application/json" ],
         encode_json $content
      )
   );
}

my $sync_p;

sub next_pending_not_sync
{
   my ( $ua ) = @_;

   while(1) {
      my $p = $ua->next_pending or return;
      my $req = $p->request;

      return $p if $req->method ne "GET" or
                   $req->uri->path ne SYNC_PATH;

      die "Received a second /sync request before the first finished" if $sync_p;
      $sync_p = $p;
      $sync_p->response->on_cancel( sub { undef $sync_p } );
   }
}

sub next_pending_sync
{
   my ( $ua ) = @_;

   if( $sync_p ) {
      my $p = $sync_p; undef $sync_p;
      return $p;
   }

   my $p = $ua->next_pending;
   my $req = $p->request;

   return $p if $req->method eq "GET" and
                $req->uri->path eq SYNC_PATH;

   die "Received a different request while waiting for an /sync request";
}

my $next_event_token = 0;

sub send_sync
{
   my ( $ua, %fields ) = @_;

   respond_json( next_pending_sync( $ua ), {
      next_batch => $next_event_token,
      %fields,
   });

   $next_event_token++;
}

sub matrix_login
{
   my ( $matrix, $ua ) = @_;

   my $login_f = $matrix->login(
      user_id => '@my-test-user:localserver.test',
      access_token => "0123456789ABCDEF",
   );

   # respond to initial /sync request
   if( my $p = $ua->next_pending ) {
      respond_json( $p, {
         next_batch => "next_token_here",
         presence   => { events => [] },
         rooms      => {},
      });
   }

   $login_f->get;
}

sub matrix_join_room
{
   my ( $matrix, $ua, @initial_state ) = @_;

   my $join_f = $matrix->join_room( "!room:localserver.test" );

   my $p = next_pending_not_sync( $ua );
   respond_json( $p, { room_id => "!room:localserver.test" } );

   # Server sends new room initial state in the next /sync response
   send_sync( $ua,
      rooms => {
         join => {
            "!room:localserver.test" => {
               timeline => {},
               state => {
                  events => [ @initial_state ],
               },
            },
         },
      },
   );

   return $join_f->get;
}

0x55AA;