diff --git a/Makefile.PL b/Makefile.PL
index 5106f9d..efe153d 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -26,5 +26,6 @@ WriteMakefile(
'v5.30.0' => '0',
'warnings' => '0',
'DBD::SQLite' => 0,
+ 'DateTime::Format::ISO8601' => 0,
},
);
diff --git a/bin/doctor_karma.pl b/bin/doctor_karma.pl
index e51f9cf..dd3b7a3 100755
--- a/bin/doctor_karma.pl
+++ b/bin/doctor_karma.pl
@@ -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 {
my $message = shift;
my $chat_id = $message->{chat}{id};
my $html = "List of commands\n\n";
for my $command (@commands) {
- $html .= "@{[xml_escape('/'.$command->{command})]} @{[xml_escape($command->{description})]}\n";
+ $html .=
+"@{[xml_escape('/'.$command->{command})]} @{[xml_escape($command->{description})]}\n";
}
$telegram->send_message(
text => $html,
@@ -272,7 +295,7 @@ EOF
);
return;
}
-
+ return unless check_can_modify_karma($message);
my $receiving_karma_user =
$user_dao->recover_id( id => $user_receiving_id );
if ( !defined $receiving_karma_user ) {
@@ -315,6 +338,7 @@ EOF
return;
}
+ return unless check_can_modify_karma($message);
my $receiving_karma_user =
$user_dao->recover_id( id => $user_receiving_id );
if ( !defined $receiving_karma_user ) {
diff --git a/lib/DoctorKarma/DAO/User.pm b/lib/DoctorKarma/DAO/User.pm
index bce012d..86934b0 100644
--- a/lib/DoctorKarma/DAO/User.pm
+++ b/lib/DoctorKarma/DAO/User.pm
@@ -8,6 +8,8 @@ use warnings;
use Types::Standard qw/Str Int InstanceOf ArrayRef Maybe HasMethods/;
use Params::ValidationCompiler qw(validation_for);
+use DateTime::Format::ISO8601;
+
{
my $validator = validation_for(
params => {
@@ -58,7 +60,7 @@ EOF
my $username = "\@@{[$user->username]}:" // '';
my $user_id = $user->id_user;
$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 );
if ($success) {
$logger->log_info("${username}${user_id} registered.");
@@ -153,7 +155,7 @@ EOF
$user->karma( $user_with_new_karma->karma );
$logger->log_info( 'User '
. $user->first_name . ':'
- . ($user->username // '') . ':'
+ . ( $user->username // '' ) . ':'
. $user->id_user
. ' has now '
. $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(
params => {
@@ -306,6 +334,11 @@ EOF
for my $key_field ( keys %$user_db ) {
$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 undef;
diff --git a/lib/DoctorKarma/Model/User.pm b/lib/DoctorKarma/Model/User.pm
index 44284b0..e266cd9 100644
--- a/lib/DoctorKarma/Model/User.pm
+++ b/lib/DoctorKarma/Model/User.pm
@@ -8,6 +8,8 @@ use warnings;
use Types::Standard qw/Str Int InstanceOf/;
use Params::ValidationCompiler qw(validation_for);
+use DateTime;
+
{
my $validator = validation_for(
params => {
@@ -15,7 +17,8 @@ use Params::ValidationCompiler qw(validation_for);
username => { type => Str, optional => 1 },
first_name => { type => Str, optional => 1 },
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 {
- my $self = shift;
- if (exists $self->{first_name}) {
+ my $self = shift;
+ if ( exists $self->{first_name} ) {
return $self->{first_name};
}
return undef;
@@ -58,9 +70,19 @@ sub id_user {
}
}
-sub last_karma_given_date {
- my $self = shift;
- return $self->{last_karma_given_date};
+{
+ my $validator =
+ 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};
+ }
}
{