Adding ability to substract karma and a test to control a bug where
karma was not modified in the method karma from user.
This commit is contained in:
parent
567fbd5375
commit
883f16d368
@ -59,6 +59,49 @@ sub process_message_with_text {
|
||||
if ( $text eq '+1' && defined $reply_to_message ) {
|
||||
add_karma_to_replied_message_user($message);
|
||||
}
|
||||
if ($text eq '-1' && defined $reply_to_message) {
|
||||
substract_karma_to_replied_message_user ($message);
|
||||
}
|
||||
}
|
||||
|
||||
sub substract_karma_to_replied_message_user {
|
||||
my $message = shift;
|
||||
my $chat_id = $message->{chat}{id};
|
||||
|
||||
my $reply_to_message = $message->{reply_to_message};
|
||||
my $user_giving_id = $message->{from}{id};
|
||||
my $user_receiving_id = $reply_to_message->{from}{id};
|
||||
|
||||
my $user_dao = DoctorKarma::DAO::User->new( dbh => $db );
|
||||
my $sending_karma_user = $user_dao->recover_id( id => $user_giving_id );
|
||||
|
||||
if ( $user_giving_id == $user_receiving_id ) {
|
||||
$logger->log_info(<<"EOF");
|
||||
User @{[$sending_karma_user->first_name]}:@{[$sending_karma_user->username]}:@{[$sending_karma_user->id_user]} tried to substract karma to itself, refusing.
|
||||
EOF
|
||||
$telegram->send_message(
|
||||
chat_id => $chat_id,
|
||||
text => 'You cannot substract karma to yourself'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
my $receiving_karma_user =
|
||||
$user_dao->recover_id( id => $user_receiving_id );
|
||||
if ( !defined $receiving_karma_user ) {
|
||||
update_user($reply_to_message);
|
||||
$receiving_karma_user =
|
||||
$user_dao->recover_id( id => $user_receiving_id );
|
||||
}
|
||||
$user_dao->substract_1_karma( user => $receiving_karma_user );
|
||||
my $id_receptor = $receiving_karma_user->id_user;
|
||||
my $receptor_telegram_url = Mojo::URL->new('tg://user');
|
||||
$receptor_telegram_url->query( id => $id_receptor );
|
||||
$telegram->send_message(
|
||||
chat_id => $chat_id,
|
||||
text =>
|
||||
"[@{[$receiving_karma_user->first_name]}]($receptor_telegram_url) has now @{[$receiving_karma_user->karma]} of karma\\."
|
||||
);
|
||||
}
|
||||
|
||||
sub add_karma_to_replied_message_user {
|
||||
|
@ -98,6 +98,37 @@ EOF
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
my $validator = validation_for(
|
||||
params => {
|
||||
user => { type => InstanceOf ['DoctorKarma::Model::User'] },
|
||||
}
|
||||
);
|
||||
|
||||
sub substract_1_karma {
|
||||
my $self = shift;
|
||||
my %params = $validator->(@_);
|
||||
|
||||
my $user = $params{user};
|
||||
|
||||
my $db = $self->_db;
|
||||
my $logger = $self->_logger;
|
||||
|
||||
$db->do( <<'EOF', {}, $user->id_user );
|
||||
UPDATE users SET karma=karma-1 WHERE id = ?;
|
||||
EOF
|
||||
my $user_with_new_karma = $self->recover_id( id => $user->id_user );
|
||||
$user->karma( $user_with_new_karma->karma );
|
||||
$logger->log_info( 'User '
|
||||
. $user->first_name . ':'
|
||||
. $user->username . ':'
|
||||
. $user->id_user
|
||||
. ' has now '
|
||||
. $user->karma
|
||||
. ' of karma.' );
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
my $validator = validation_for(
|
||||
params => {
|
||||
@ -129,6 +160,7 @@ EOF
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
my $validator = validation_for(
|
||||
params => {
|
||||
|
@ -5,7 +5,7 @@ use v5.30.0;
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Test::Most tests => 6;
|
||||
use Test::Most tests => 7;
|
||||
use Test::MockModule;
|
||||
use Test::MockObject;
|
||||
|
||||
@ -36,3 +36,24 @@ BEGIN {
|
||||
is $user->first_name, $expected_firstname, 'Firstname matchs.';
|
||||
is $user->id_user, $expected_id_user, 'Id_User matchs.';
|
||||
}
|
||||
|
||||
{
|
||||
## GIVEN
|
||||
my $expected_username = 'sergiotarxz';
|
||||
my $expected_firstname = 'sergiotarxz';
|
||||
my $expected_karma = 100;
|
||||
my $expected_id_user = 100;
|
||||
|
||||
my $user = DoctorKarma::Model::User->new(
|
||||
id_user => $expected_id_user,
|
||||
username => $expected_username,
|
||||
first_name => $expected_firstname,
|
||||
karma => 0,
|
||||
);
|
||||
|
||||
## WHEN
|
||||
$user->karma($expected_karma);
|
||||
|
||||
## THEN
|
||||
is $user->karma, $expected_karma, 'The karma is the actually expected after edition.';
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user