Skip to content
Snippets Groups Projects
Verified Commit bc532022 authored by Andrej Ramašeuski's avatar Andrej Ramašeuski
Browse files

Lepsi pocitani, prepnuti na Mojo::Pg

parent 65f42b64
No related branches found
No related tags found
No related merge requests found
...@@ -8,13 +8,12 @@ use POSIX qw(ceil); ...@@ -8,13 +8,12 @@ use POSIX qw(ceil);
use feature 'signatures'; use feature 'signatures';
no warnings qw{ experimental::signatures }; no warnings qw{ experimental::signatures };
use constant SOCKET_INACTIVITY_TIMEOUT => 300; use constant SOCKET_INACTIVITY_TIMEOUT => 180;
sub main { sub main {
my $c = shift; my $c = shift;
my $ip = $c->tx->remote_address; my $ip = $c->tx->remote_address;
my $key = $c->req->headers->header('Sec-WebSocket-Key');
my $user;
$c->inactivity_timeout(SOCKET_INACTIVITY_TIMEOUT); $c->inactivity_timeout(SOCKET_INACTIVITY_TIMEOUT);
...@@ -25,29 +24,53 @@ sub main { ...@@ -25,29 +24,53 @@ sub main {
}); });
$c->on(json => sub( $c, $message ) { $c->on(json => sub( $c, $message ) {
if ( $message->{event} eq 'KEEPALIVE' ) { if ( $message->{event} eq 'KEEPALIVE' ) {
if ($message->{payload} =~ /^(\d+)$/) { my $user;
# TODO: check signtaure
if ($message->{payload} =~ /^\d+$/) {
$user = $c->pg->db->select('users',
[qw(id username secret jitsi_allowed roles banned_until)],
{ id => $message->{payload}}
)->hash;
}
$user //= $c->schema->resultset('User')->find( if ( $user ) {
{ id => $1 } my $sig = hmac_sha1_hex($message->{payload}, $user->{secret} );
if ( $sig ne $message->{sig} ) {
$c->app->log->warn(
"Invalid signature for " . $user->{username}
); );
$user = undef;
} }
}
eval {
my $tx = $c->pg->db->begin;
$c->schema->resultset('Socket')->update_or_create({ $c->pg->db->delete('sockets', [
id => $c->req->headers->header('Sec-WebSocket-Key'), {id => $key},
{user_id => $message->{payload}},
]);
$c->pg->db->insert('sockets', {
id => $key,
ip => $ip, ip => $ip,
keepalive => \'now()', keepalive => \'now()',
user_id => $user ? $user->id : undef, user_id => $user ? $user->{id} : undef,
}, { key => $user ? 'user':'primary'} ); });
my $all = $c->schema->resultset('Socket_view')->count(); $tx->commit;
};
my $members = $c->schema->resultset('Socket_view')->count( my $all = $c->pg->db->query(
{ is_member => 't' } 'select count(*) from sockets_view'
); )->hash->{count};
my $members = $c->pg->db->query(
'select count(*) from sockets_view where is_member'
)->hash->{count};
my $group_size = $c->_member_group_size($members); my $group_size = $c->_member_group_size($members);
...@@ -59,15 +82,14 @@ sub main { ...@@ -59,15 +82,14 @@ sub main {
}}}); }}});
if ( $user ) { if ( $user ) {
my $jitsi = $user->jitsi_allowed || $user->roles =~ /chairman/; my $jitsi = $user->{jitsi_allowed} || $user->{roles} =~ /chairman|jitsi/;
$c->send({json => { event => 'user_status', payload => { $c->send({json => { event => 'user_status', payload => {
jitsi_allowed => $jitsi ? \1:\0, jitsi_allowed => $jitsi ? \1:\0,
is_banned => $user->banned_until ? \1:\0, is_banned => $user->{banned_until} ? \1:\0,
}}}); }}});
} }
} }
}); });
$c->on(finish => sub ($c, $code, $reason = undef) { $c->on(finish => sub ($c, $code, $reason = undef) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment