lib/PAGI/App/WebSocket/Chat.pm

filter: Statementstmt 76.8% 83 / 108 Branchbran 40.4% 17 / 42 Conditioncond 29.4% 10 / 34 MC/DCmcdc 5.0% 1 / 20 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 2 totaltotal 56.2% 122 / 217 CC 40 SCAR 45.2
CC
40
Cov
60%
__ANON__
154.9
_broadcast_to_room
10.4
new
3.3
CRAP 91.9
« blib/lib/PAGI/App/File.pm ↑ Summary blib/lib/PAGI/App/Proxy.pm »
1 package PAGI::App::WebSocket::Chat;
2 1 $PAGI::App::WebSocket::Chat::VERSION = '0.002002';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 PAGI::App::WebSocket::Chat - Multi-room chat application
11  
12 =head1 SYNOPSIS
13  
14 use PAGI::App::WebSocket::Chat;
15  
16 my $app = PAGI::App::WebSocket::Chat->new->to_app;
17  
18 =cut
19  
20 # Shared state
21 1 my %rooms; # room => { users => { id => { send => cb, name => str } } }
22 my %user_rooms; # user_id => { room => 1 }
23 1 my $next_id = 1;
24  
25 3 ▶sub new {
Subroutine new: called CC=3 SCAR=12.0
Pod undocumented
26 3 my ($class, %args) = @_;
27  
28 return bless {
29 default_room => $args{default_room} // 'lobby',
30 3 ▶ max_rooms => $args{max_rooms} // 100,
Condition50%2/4
$args{"default_room"} // "lobby"
l!l
03
$args{"max_rooms"} // 100
l!l
03
MC/DC0%0/1
$args{"default_room"} // "lobby"
$args{"default_room"}
MC/DC0%0/1
$args{"max_rooms"} // 100
$args{"max_rooms"}
Truth table50%1/2
A
A= $args{"default_room"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"max_rooms"}
Aresult
11
00
31 }, $class;
32 }
33  
34 3 ▶sub to_app {
Subroutine to_app: called CC=1 SCAR=1.3
Pod undocumented
35 3 my ($self) = @_;
36  
37 3 my $default_room = $self->{default_room};
38 3 my $max_rooms = $self->{max_rooms};
39  
40 3 ▶ return async sub {
Subroutine __ANON__: called CC=29 SCAR=50.4
41 3 my ($scope, $receive, $send) = @_;
42 3 ▶ die "Unsupported scope type: $scope->{type}" if $scope->{type} ne 'websocket';
Branch: 1 branch
BranchTrueFalseTotal
if $scope->{"type"} ne "websocket" 0 3 3
43  
44 3 await $send->({ type => 'websocket.accept' });
45  
46 3 my $user_id = $next_id++;
47 3 my $username = "user_$user_id";
48 3 $user_rooms{$user_id} = {};
49  
50 # Join default room
51 3 _join_room($user_id, $username, $send, $default_room);
52  
53 # Send welcome message
54 3 await $send->({
55 type => 'websocket.send',
56 text => _encode({
57 type => 'welcome',
58 user_id => $user_id,
59 username => $username,
60 room => $default_room,
61 }),
62 });
63  
64 3 eval {
65 3 while (1) {
66 5 my $event = await $receive->();
67  
68 5 ▶ if ($event->{type} eq 'websocket.receive') {
Branch: 2 branches
BranchTrueFalseTotal
if ($event->{'type'} eq 'websocket.receive') { } 2 3 5
elsif ($event->{'type'} eq 'websocket.disconnect') { } 3 0 3
69 2 ▶ my $data = eval { JSON::MaybeXS::decode_json($event->{text} // '{}') } // {};
Condition50%2/4
eval { do { &JSON::MaybeXS::decode_json(scalar($event->{"text"} // "{}")) } } // {}
l!l
20
$event->{"text"} // "{}"
l!l
20
MC/DC0%0/1
eval { do { &JSON::MaybeXS::decode_json(scalar($event->{"text"} // "{}")) } } // {}
eval { do { &JSON::MaybeXS::decode_json(scalar($event->{"text"} // "{}")) } }
MC/DC0%0/1
$event->{"text"} // "{}"
$event->{"text"}
Truth table50%1/2
A
A= eval { do { &JSON::MaybeXS::decode_json(scalar($event->{"text"} // "{}")) } }
Aresult
11
00
Truth table50%1/2
A
A= $event->{"text"}
Aresult
11
00
70 2 ▶ my $cmd = $data->{type} // 'message';
Condition50%1/2
$data->{"type"} // "message"
l!l
20
MC/DC0%0/1
$data->{"type"} // "message"
$data->{"type"}
Truth table50%1/2
A
A= $data->{"type"}
Aresult
11
00
71  
72 2 ▶ if ($cmd eq 'message') {
Branch: 6 branches
BranchTrueFalseTotal
if ($cmd eq 'message') { } 0 2 2
elsif ($cmd eq 'join') { } 0 2 2
elsif ($cmd eq 'leave') { } 0 2 2
elsif ($cmd eq 'nick') { } 1 1 2
elsif ($cmd eq 'list') { } 0 1 1
elsif ($cmd eq 'rooms') { } 1 0 1
73 # Broadcast to current room(s)
74 0 ▶ my $msg = $data->{message} // '';
Condition0%0/2
$data->{"message"} // ""
l!l
00
MC/DC0%0/1
$data->{"message"} // ""
$data->{"message"}
Truth table0%0/2
A
A= $data->{"message"}
Aresult
11
00
75 0 my $target_room = $data->{room};
76  
77 my @target_rooms = $target_room
78 ? ($target_room)
79 0 ▶ : keys %{$user_rooms{$user_id}};
Branch: 1 branch
BranchTrueFalseTotal
$target_room ? : 0 0 0
80  
81 0 for my $room (@target_rooms) {
82 0 ▶ next unless $user_rooms{$user_id}{$room};
Branch: 1 branch
BranchTrueFalseTotal
unless $user_rooms{$user_id}{$room} 0 0 0
83 0 await _broadcast_to_room($room, {
84 type => 'message',
85 room => $room,
86 user_id => $user_id,
87 username => $username,
88 message => $msg,
89 timestamp => time(),
90 }, $user_id);
91 }
92 } elsif ($cmd eq 'join') {
93 0 ▶ my $room = $data->{room} // $default_room;
Condition0%0/3
$data->{"room"} // $default_room
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{"room"} // $default_room
$data->{"room"} $default_room
Truth table0%0/3
A // B
A= $data->{"room"}B= $default_room
ABresult
1X1
011
000
94 0 ▶ if (keys(%rooms) >= $max_rooms && !$rooms{$room}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %rooms >= $max_rooms and not $rooms{$room}) { } 0 0 0
Condition0%0/3
keys %rooms >= $max_rooms and not $rooms{$room}
!ll&&!rl&&r
000
MC/DC0%0/2
keys %rooms >= $max_rooms and not $rooms{$room}
keys %rooms >= $max_rooms not $rooms{$room}
Truth table0%0/3
A and not(B)
A= keys %rooms >= $max_roomsB= not $rooms{$room}
ABresult
0X0
100
111
95 0 await $send->({
96 type => 'websocket.send',
97 text => _encode({ type => 'error', message => 'Max rooms reached' }),
98 });
99 } else {
100 0 _join_room($user_id, $username, $send, $room);
101 0 await $send->({
102 type => 'websocket.send',
103 text => _encode({ type => 'joined', room => $room }),
104 });
105 }
106 } elsif ($cmd eq 'leave') {
107 0 my $room = $data->{room};
108 0 ▶ if ($room && $user_rooms{$user_id}{$room}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($room and $user_rooms{$user_id}{$room}) 0 0 0
Condition0%0/3
$room and $user_rooms{$user_id}{$room}
!ll&&!rl&&r
000
MC/DC0%0/2
$room and $user_rooms{$user_id}{$room}
$room $user_rooms{$user_id}{$room}
Truth table0%0/3
A and B
A= $roomB= $user_rooms{$user_id}{$room}
ABresult
0X0
100
111
109 0 await _leave_room($user_id, $username, $room);
110 0 await $send->({
111 type => 'websocket.send',
112 text => _encode({ type => 'left', room => $room }),
113 });
114 }
115 } elsif ($cmd eq 'nick') {
116 1 ▶ my $new_name = $data->{username} // $username;
Condition33%1/3
$data->{"username"} // $username
l!l&&r!l&&!r
100
MC/DC0%0/2
$data->{"username"} // $username
$data->{"username"} $username
Truth table33%1/3
A // B
A= $data->{"username"}B= $username
ABresult
1X1
011
000
117 1 $new_name =~ s/[^\w\-]//g;
118 1 ▶ $new_name = substr($new_name, 0, 20) if length($new_name) > 20;
Branch: 1 branch
BranchTrueFalseTotal
if length $new_name > 20 0 1 1
119  
120 # Update in all rooms
121 1 for my $room (keys %{$user_rooms{$user_id}}) {
122 1 ▶ if ($rooms{$room}{users}{$user_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rooms{$room}{"users"}{$user_id}) 1 0 1
123 1 $rooms{$room}{users}{$user_id}{name} = $new_name;
124 }
125 }
126 1 $username = $new_name;
127 1 await $send->({
128 type => 'websocket.send',
129 text => _encode({ type => 'nick', username => $username }),
130 });
131 } elsif ($cmd eq 'list') {
132 0 my $room = $data->{room};
133 0 ▶ if ($room && $rooms{$room}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($room and $rooms{$room}) 0 0 0
Condition0%0/3
$room and $rooms{$room}
!ll&&!rl&&r
000
MC/DC0%0/2
$room and $rooms{$room}
$room $rooms{$room}
Truth table0%0/3
A and B
A= $roomB= $rooms{$room}
ABresult
0X0
100
111
134 0 my @users = map { $_->{name} } values %{$rooms{$room}{users}};
135 0 await $send->({
136 type => 'websocket.send',
137 text => _encode({ type => 'users', room => $room, users => \@users }),
138 });
139 }
140 } elsif ($cmd eq 'rooms') {
141 my @room_list = map {
142 1 { name => $_, count => scalar keys %{$rooms{$_}{users}} }
143 } keys %rooms;
144 1 await $send->({
145 type => 'websocket.send',
146 text => _encode({ type => 'rooms', rooms => \@room_list }),
147 });
148 }
149 } elsif ($event->{type} eq 'websocket.disconnect') {
150 3 last;
151 }
152 }
153 };
154  
155 # Cleanup - leave all rooms
156 3 for my $room (keys %{$user_rooms{$user_id}}) {
157 3 eval { _leave_room($user_id, $username, $room) };
158 }
159 3 delete $user_rooms{$user_id};
160 3 };
161 }
162  
163 11 ▶sub _encode {
Subroutine _encode: called CC=1 SCAR=0.0
164 11 my ($data) = @_;
165  
166 11 return JSON::MaybeXS::encode_json($data);
167 }
168  
169 3 ▶sub _join_room {
Subroutine _join_room: called CC=2 SCAR=7.0
170 3 my ($user_id, $username, $send, $room) = @_;
171  
172 3 ▶ $rooms{$room} //= { users => {} };
Condition50%1/2
$rooms{$room} //= {"users", {}}
l!l
03
MC/DC0%0/1
$rooms{$room} //= {"users", {}}
$rooms{$room}
Truth table50%1/2
A
A= $rooms{$room}
Aresult
11
00
173 3 $rooms{$room}{users}{$user_id} = { send => $send, name => $username };
174 3 $user_rooms{$user_id}{$room} = 1;
175  
176 # Notify others
177 3 _broadcast_to_room($room, {
178 type => 'user_joined',
179 room => $room,
180 user_id => $user_id,
181 username => $username,
182 }, $user_id);
183 }
184  
185 3 ▶async sub _leave_room {
Subroutine _leave_room: called CC=3 SCAR=11.1
186 3 my ($user_id, $username, $room) = @_;
187  
188 3 ▶ return unless $rooms{$room};
Branch: 1 branch
BranchTrueFalseTotal
unless $rooms{$room} 0 3 3
189  
190 3 delete $rooms{$room}{users}{$user_id};
191 3 delete $user_rooms{$user_id}{$room};
192  
193 # Notify others
194 3 await _broadcast_to_room($room, {
195 type => 'user_left',
196 room => $room,
197 user_id => $user_id,
198 username => $username,
199 });
200  
201 # Cleanup empty room
202 3 ▶ delete $rooms{$room} if !keys %{$rooms{$room}{users}};
Branch: 1 branch
BranchTrueFalseTotal
if not keys %{$rooms{$room}{"users"};} 3 0 3
203 1 }
204  
205 6 ▶async sub _broadcast_to_room {
Subroutine _broadcast_to_room: called CC=7 SCAR=23.4
206 6 my ($room, $data, $exclude_id) = @_;
207 6 ▶ $exclude_id //= undef;
Condition100%2/2
$exclude_id //= undef
l!l
33
MC/DC100%1/1
$exclude_id //= undef
$exclude_id
Truth table100%2/2
A
A= $exclude_id
Aresult
11
00
208  
209 6 ▶ return unless $rooms{$room};
Branch: 1 branch
BranchTrueFalseTotal
unless $rooms{$room} 0 6 6
210  
211 6 my $json = _encode($data);
212 6 my $users = $rooms{$room}{users};
213  
214 6 for my $id (keys %$users) {
215 3 ▶ next if defined $exclude_id && $id eq $exclude_id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $exclude_id and $id eq $exclude_id 3 0 3
Condition33%1/3
defined $exclude_id and $id eq $exclude_id
!ll&&!rl&&r
003
MC/DC0%0/2
defined $exclude_id and $id eq $exclude_id
defined $exclude_id $id eq $exclude_id
Truth table33%1/3
A and B
A= defined $exclude_idB= $id eq $exclude_id
ABresult
0X0
100
111
216 0 eval {
217 0 await $users->{$id}{send}->({
218 type => 'websocket.send',
219 text => $json,
220 });
221 };
222 0 ▶ delete $users->{$id} if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
223 }
224 1 }
225  
226 1 1;
227  
228 __END__
« blib/lib/PAGI/App/File.pm ↑ Summary blib/lib/PAGI/App/Proxy.pm »