Adding support for reporting the current karma.

This commit is contained in:
sergiotarxz 2022-02-07 22:31:09 +01:00
parent 0132d56c96
commit 567fbd5375
3 changed files with 71 additions and 21 deletions

View File

@ -8,6 +8,8 @@ use warnings;
use Data::Dumper; use Data::Dumper;
use JSON; use JSON;
use Mojo::URL;
use DoctorKarma::Config; use DoctorKarma::Config;
use DoctorKarma::Telegram; use DoctorKarma::Telegram;
use DoctorKarma::Logger; use DoctorKarma::Logger;
@ -61,6 +63,7 @@ sub process_message_with_text {
sub add_karma_to_replied_message_user { sub add_karma_to_replied_message_user {
my $message = shift; my $message = shift;
my $chat_id = $message->{chat}{id};
my $reply_to_message = $message->{reply_to_message}; my $reply_to_message = $message->{reply_to_message};
my $user_giving_id = $message->{from}{id}; my $user_giving_id = $message->{from}{id};
@ -73,6 +76,10 @@ sub add_karma_to_replied_message_user {
$logger->log_info(<<"EOF"); $logger->log_info(<<"EOF");
User @{[$sending_karma_user->first_name]}:@{[$sending_karma_user->username]}:@{[$sending_karma_user->id_user]} tried to give karma to itself, refusing. User @{[$sending_karma_user->first_name]}:@{[$sending_karma_user->username]}:@{[$sending_karma_user->id_user]} tried to give karma to itself, refusing.
EOF EOF
$telegram->send_message(
chat_id => $chat_id,
text => 'You cannot give karma to yourself'
);
return; return;
} }
@ -80,10 +87,18 @@ EOF
$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 ) {
update_user($reply_to_message); update_user($reply_to_message);
$receiving_karma_user = $user_dao->recover_id (id => $user_receiving_id); $receiving_karma_user =
$user_dao->recover_id( id => $user_receiving_id );
} }
$user_dao->add_1_karma( user => $receiving_karma_user ); $user_dao->add_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 update_user { sub update_user {
@ -104,6 +119,14 @@ sub update_user {
$user_dao->store( user => $user ); $user_dao->store( user => $user );
} }
if ( ( $user->username // '' ) ne ( $username // '' ) ) { if ( ( $user->username // '' ) ne ( $username // '' ) ) {
if ( defined $username ) {
my $user_already_with_username =
$user_dao->recover_username( username => $username );
$user_dao->update_username(
user => $user_already_with_username,
username => undef,
) if defined $user_already_with_username;
}
$user_dao->update_username( $user_dao->update_username(
user => $user, user => $user,
username => $username username => $username

View File

@ -71,7 +71,7 @@ sub last_karma_given_date {
my $self = shift; my $self = shift;
my ($new_karma) = $validator->(@_); my ($new_karma) = $validator->(@_);
if ( defined $new_karma ) { if ( defined $new_karma ) {
$self->{new_karma} = $new_karma; $self->{karma} = $new_karma;
} }
return $self->{karma}; return $self->{karma};
} }

View File

@ -62,8 +62,9 @@ sub _user_agent {
my $ua = $self->_user_agent; my $ua = $self->_user_agent;
my $logger = $self->_logger; my $logger = $self->_logger;
my $url = $self->_generate_url( method => $method ); my $url = $self->_generate_url( method => $method );
my $response = decode_json( my $response =
$ua->post( $url => {} => json => $body )->result->body ); decode_json( $ua->post( $url => {} => json => $body )->result->body );
unless ( $response->{ok} ) { unless ( $response->{ok} ) {
$logger->log_critical( $response->{description} ); $logger->log_critical( $response->{description} );
} }
@ -71,6 +72,32 @@ sub _user_agent {
} }
} }
{
my $validator = validation_for(
params => {
chat_id => { type => Int },
text => { type => Str },
}
);
sub send_message {
my $self = shift;
my %params = $validator->(@_);
my $chat_id = $params{chat_id};
my $text = $params{text};
my $response = $self->_request(
method => q/sendMessage/,
body => {
chat_id => $chat_id,
text => $text,
parse_mode => 'MarkdownV2',
}
);
return $response->{result};
}
}
sub get_updates { sub get_updates {
my $self = shift; my $self = shift;
my $last_update = $self->_last_update; my $last_update = $self->_last_update;