Adding timeout to karma give.

This commit is contained in:
sergiotarxz 2022-02-09 23:35:40 +01:00
parent 9bc1419231
commit c20aad1b95
4 changed files with 90 additions and 10 deletions

View File

@ -26,5 +26,6 @@ WriteMakefile(
'v5.30.0' => '0', 'v5.30.0' => '0',
'warnings' => '0', 'warnings' => '0',
'DBD::SQLite' => 0, 'DBD::SQLite' => 0,
'DateTime::Format::ISO8601' => 0,
}, },
); );

View File

@ -129,13 +129,36 @@ sub process_message_with_text {
} }
} }
sub check_can_modify_karma {
my $message = shift;
my $user_id = $message->{from}{id};
my $chat_id = $message->{chat}{id};
my $user_dao = DoctorKarma::DAO::User->new( dbh => $db );
my $user = $user_dao->recover_id( id => $user_id );
if ( $user->can_modify_karma ) {
$user_dao->update_last_karma_given_date(
user => $user,
last_karma_given_date => DateTime->now
);
return 1;
}
$telegram->send_message(
chat_id => $chat_id,
text => "You cannot give karma yet wait a bit.",
);
return 0;
}
sub show_help { sub show_help {
my $message = shift; my $message = shift;
my $chat_id = $message->{chat}{id}; my $chat_id = $message->{chat}{id};
my $html = "<b><u>List of commands</u></b>\n\n"; my $html = "<b><u>List of commands</u></b>\n\n";
for my $command (@commands) { for my $command (@commands) {
$html .= "<b>@{[xml_escape('/'.$command->{command})]}</b> @{[xml_escape($command->{description})]}\n"; $html .=
"<b>@{[xml_escape('/'.$command->{command})]}</b> @{[xml_escape($command->{description})]}\n";
} }
$telegram->send_message( $telegram->send_message(
text => $html, text => $html,
@ -272,7 +295,7 @@ EOF
); );
return; return;
} }
return unless check_can_modify_karma($message);
my $receiving_karma_user = my $receiving_karma_user =
$user_dao->recover_id( id => $user_receiving_id ); $user_dao->recover_id( id => $user_receiving_id );
if ( !defined $receiving_karma_user ) { if ( !defined $receiving_karma_user ) {
@ -315,6 +338,7 @@ EOF
return; return;
} }
return unless check_can_modify_karma($message);
my $receiving_karma_user = my $receiving_karma_user =
$user_dao->recover_id( id => $user_receiving_id ); $user_dao->recover_id( id => $user_receiving_id );
if ( !defined $receiving_karma_user ) { if ( !defined $receiving_karma_user ) {

View File

@ -8,6 +8,8 @@ use warnings;
use Types::Standard qw/Str Int InstanceOf ArrayRef Maybe HasMethods/; use Types::Standard qw/Str Int InstanceOf ArrayRef Maybe HasMethods/;
use Params::ValidationCompiler qw(validation_for); use Params::ValidationCompiler qw(validation_for);
use DateTime::Format::ISO8601;
{ {
my $validator = validation_for( my $validator = validation_for(
params => { params => {
@ -58,7 +60,7 @@ EOF
my $username = "\@@{[$user->username]}:" // ''; my $username = "\@@{[$user->username]}:" // '';
my $user_id = $user->id_user; my $user_id = $user->id_user;
$logger->log_info("Trying to register ${username}${user_id}."); $logger->log_info("Trying to register ${username}${user_id}.");
my $success = 0 + my $success = 0 +
$db->do( $insert, {}, $user->id_user, $user->username, $user->karma ); $db->do( $insert, {}, $user->id_user, $user->username, $user->karma );
if ($success) { if ($success) {
$logger->log_info("${username}${user_id} registered."); $logger->log_info("${username}${user_id} registered.");
@ -153,7 +155,7 @@ EOF
$user->karma( $user_with_new_karma->karma ); $user->karma( $user_with_new_karma->karma );
$logger->log_info( 'User ' $logger->log_info( 'User '
. $user->first_name . ':' . $user->first_name . ':'
. ($user->username // '') . ':' . ( $user->username // '' ) . ':'
. $user->id_user . $user->id_user
. ' has now ' . ' has now '
. $user->karma . $user->karma
@ -161,6 +163,32 @@ EOF
} }
} }
{
my $validator = validation_for(
params => {
user => { type => InstanceOf ['DoctorKarma::Model::User'] },
last_karma_given_date =>
{ type => InstanceOf ['DateTime'], optional => 1 },
}
);
sub update_last_karma_given_date {
my $self = shift;
my %params = $validator->(@_);
my $db = $self->_db;
my $last_karma_given_date = $params{last_karma_given_date};
my $user = $params{user};
$db->do( <<'EOF', {}, $last_karma_given_date . '', $user->id_user );
UPDATE users SET last_karma_given_date=? WHERE id = ?;
EOF
my $user_with_new_last_karma =
$self->recover_id( id => $user->id_user );
$user->last_karma_given_date( $user->last_karma_given_date );
}
}
{ {
my $validator = validation_for( my $validator = validation_for(
params => { params => {
@ -306,6 +334,11 @@ EOF
for my $key_field ( keys %$user_db ) { for my $key_field ( keys %$user_db ) {
$user_db->{$key_field} // delete $user_db->{$key_field}; $user_db->{$key_field} // delete $user_db->{$key_field};
} }
if ( defined $user_db->{last_karma_given_date} ) {
my $iso8601 = DateTime::Format::ISO8601->new;
$user_db->{last_karma_given_date} =
$iso8601->parse_datetime( $user_db->{last_karma_given_date} );
}
return DoctorKarma::Model::User->new(%$user_db); return DoctorKarma::Model::User->new(%$user_db);
} }
return undef; return undef;

View File

@ -8,6 +8,8 @@ use warnings;
use Types::Standard qw/Str Int InstanceOf/; use Types::Standard qw/Str Int InstanceOf/;
use Params::ValidationCompiler qw(validation_for); use Params::ValidationCompiler qw(validation_for);
use DateTime;
{ {
my $validator = validation_for( my $validator = validation_for(
params => { params => {
@ -15,7 +17,8 @@ use Params::ValidationCompiler qw(validation_for);
username => { type => Str, optional => 1 }, username => { type => Str, optional => 1 },
first_name => { type => Str, optional => 1 }, first_name => { type => Str, optional => 1 },
karma => { type => Int }, karma => { type => Int },
last_karma_given_date => { type => InstanceOf ['DateTime'], optional => 1 }, last_karma_given_date =>
{ type => InstanceOf ['DateTime'], optional => 1 },
} }
); );
@ -27,10 +30,19 @@ use Params::ValidationCompiler qw(validation_for);
} }
} }
sub can_modify_karma {
my $self = shift;
my $last_karma_given_date = $self->last_karma_given_date;
if ( !defined $last_karma_given_date ) {
return 1;
}
return DateTime->now >
$self->last_karma_given_date->clone->add( minutes => 1 );
}
sub first_name { sub first_name {
my $self = shift; my $self = shift;
if (exists $self->{first_name}) { if ( exists $self->{first_name} ) {
return $self->{first_name}; return $self->{first_name};
} }
return undef; return undef;
@ -58,9 +70,19 @@ sub id_user {
} }
} }
sub last_karma_given_date { {
my $self = shift; my $validator =
return $self->{last_karma_given_date}; validation_for(
params => [ { type => InstanceOf ['DateTime'], optional => 1} ] );
sub last_karma_given_date {
my $self = shift;
my ($last_karma_given_date) = $validator->(@_);
if ( defined $last_karma_given_date ) {
$self->{last_karma_given_date} = $last_karma_given_date;
}
return $self->{last_karma_given_date};
}
} }
{ {