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

Pocitani aktivnich uzivatelu, ban, unban, userinfo, autorizace v socketu

parent 4b3b035b
No related branches found
No related tags found
No related merge requests found
package CF::Controller::Users;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Pg::PubSub;
use feature 'signatures';
no warnings qw{ experimental::signatures };
sub me ($c){
my $user = $c->_get( $c->user->{id} ) // return;
$c->render(openapi => $c->spec_filter($user->formatted, 'User'));
}
sub ban ($c){
my $user = $c->_get( $c->stash->{id} ) // return;
my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
my $guard = $c->schema->txn_scope_guard;
$user->update({ banned_until => \"now()+'8 hour'", });
#TODO: ANN
$pubsub->json('notify')->notify( notify => {
event => 'user_banned',
payload => $c->spec_filter($user->formatted, 'User'),
});
$guard->commit;
$c->render(status => 204, text => '');
}
sub unban ($c){
my $user = $c->_get( $c->stash->{id} ) // return;
my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
my $guard = $c->schema->txn_scope_guard;
$user->update({ banned_until => undef });
#TODO: ANN
$pubsub->json('notify')->notify( notify => {
event => 'user_unbanned',
payload => $c->spec_filter($user->formatted, 'User'),
});
$guard->commit;
$c->render(status => 204, text => '');
}
sub _get ($c, $id ) {
my $user;
if ( $id =~ /^\d+$/) {
$user = $c->schema->resultset('User')->find( { id => $id } );
}
else {
$user = $c->schema->resultset('User')->find( { uuid => $id } );
}
return $c->error(404, 'User not found') if ! $user;
return $user;
}
1;
...@@ -2,13 +2,32 @@ package CF::Controller::Websockets; ...@@ -2,13 +2,32 @@ package CF::Controller::Websockets;
use Mojo::Base 'Mojolicious::Controller'; use Mojo::Base 'Mojolicious::Controller';
use Mojo::Pg::PubSub; use Mojo::Pg::PubSub;
use JSON;
use feature 'signatures'; use feature 'signatures';
no warnings qw{ experimental::signatures }; no warnings qw{ experimental::signatures };
use constant SOCKET_INACTIVITY_TIMEOUT => 300;
use constant USER_ALIVE_TIMEOUT => 100;
sub main { sub main {
my $c = shift; my $c = shift;
$c->inactivity_timeout(600); $c->inactivity_timeout(SOCKET_INACTIVITY_TIMEOUT);
my $user;
if ( my $key = $c->req->headers->authorization ) {
if ( $key =~ s/Bearer\s+//i ) {
$c->oauth_token($key);
if ( $c->user ) {
$user = $c->schema->resultset('User')->find_or_create(
$c->user, { key => 'uuid'}
);
}
}
}
my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg); my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
...@@ -16,6 +35,20 @@ sub main { ...@@ -16,6 +35,20 @@ sub main {
$c->send($payload); $c->send($payload);
}); });
$c->on(message => sub( $c, $message ) {
$user->update({keepalive => \'now()'}) if $user;
my $min_alive_time = "now() - '" . USER_ALIVE_TIMEOUT. " s'::interval";
my $alive = $c->schema->resultset('User')->count(
{
keepalive => { '>' => \$min_alive_time },
}
);
$c->send(to_json({ online_users => $alive }));
});
$c->on(finish => sub ($c, $code, $reason = undef) { $c->on(finish => sub ($c, $code, $reason = undef) {
$pubsub->unlisten('notify'); $pubsub->unlisten('notify');
$c->app->log->debug("WebSocket closed with status $code"); $c->app->log->debug("WebSocket closed with status $code");
......
...@@ -29,7 +29,6 @@ __PACKAGE__->add_columns( ...@@ -29,7 +29,6 @@ __PACKAGE__->add_columns(
__PACKAGE__->set_primary_key('id'); __PACKAGE__->set_primary_key('id');
1;
sub format { sub format {
my $self = shift; my $self = shift;
...@@ -46,3 +45,5 @@ sub format { ...@@ -46,3 +45,5 @@ sub format {
return $announcement; return $announcement;
} }
1;
...@@ -22,6 +22,8 @@ __PACKAGE__->add_columns( ...@@ -22,6 +22,8 @@ __PACKAGE__->add_columns(
username username
name name
main_group_name main_group_name
keepalive
banned_until
), ),
); );
...@@ -36,4 +38,17 @@ __PACKAGE__->has_many( ...@@ -36,4 +38,17 @@ __PACKAGE__->has_many(
{ 'foreign.user_id' => 'self.id', }, { 'foreign.user_id' => 'self.id', },
); );
sub formatted {
my $self = shift;
my $user = {
$self->get_columns,
group => $self->main_group_name,
is_banned => $self->banned_until ? 1:0,
};
return $user;
}
1; 1;
...@@ -92,7 +92,7 @@ components: ...@@ -92,7 +92,7 @@ components:
type: string type: string
originator: originator:
type: string type: string
Author: User:
type: object type: object
properties: properties:
id: id:
...@@ -103,6 +103,8 @@ components: ...@@ -103,6 +103,8 @@ components:
type: string type: string
username: username:
type: string type: string
is_banned:
type: boolean
Announcement: Announcement:
type: object type: object
description: Notifikace description: Notifikace
...@@ -144,7 +146,7 @@ components: ...@@ -144,7 +146,7 @@ components:
is_archived: is_archived:
type: boolean type: boolean
author: author:
$ref: '#/components/schemas/Author' $ref: '#/components/schemas/User'
ranking: ranking:
$ref: '#/components/schemas/Ranking' $ref: '#/components/schemas/Ranking'
history_log: history_log:
...@@ -419,6 +421,18 @@ paths: ...@@ -419,6 +421,18 @@ paths:
204: 204:
description: Post updated description: Post updated
delete:
x-mojo-to: posts#delete
security:
- Bearer: ['chairman']
tags:
- posts
summary: "Smazat zpravu"
operationId: deletePost
responses:
204:
description: Post deleted
/posts/{id}/like: /posts/{id}/like:
patch: patch:
security: security:
...@@ -460,3 +474,46 @@ paths: ...@@ -460,3 +474,46 @@ paths:
responses: responses:
204: 204:
description: Post disliked description: Post disliked
/users/me:
get:
x-mojo-to: users#me
security:
- Bearer: []
tags:
- users
summary: "Detail uzivatele"
operationId: getCurrentUser
responses:
200:
description: User
content:
application/json:
schema:
$ref: '#/components/schemas/User'
/users/{id}/ban:
patch:
security:
- Bearer: ['chairman']
x-mojo-to: users#ban
tags:
- users
summary: Ban user
operationId: banUser
responses:
204:
description: User banned
/users/{id}/unban:
patch:
security:
- Bearer: ['chairman']
x-mojo-to: users#unban
tags:
- users
summary: Unban user
operationId: UnbanUser
responses:
204:
description: User banned
...@@ -21,6 +21,8 @@ create table "users" ( ...@@ -21,6 +21,8 @@ create table "users" (
"username" text, "username" text,
"name" text, "name" text,
"main_group_name" text, "main_group_name" text,
"keepalive" timestamp,
"banned_until" timestamp,
primary key("id"), primary key("id"),
unique("uuid") unique("uuid")
); );
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment