EV-Telegram-TDLib

 view release on metacpan or  search on metacpan

lib/EV/Telegram/TDLib/Connection.pm  view on Meta::CPAN

package EV::Telegram::TDLib::Connection;

use strict;
use warnings;

our $VERSION = '0.04';

=head1 NAME

EV::Telegram::TDLib::Connection - connection methods for EV::Telegram::TDLib

=head1 DESCRIPTION

One of the mixins L<EV::Telegram::TDLib> inherits from. It has no
interface of its own and is not meant to be used directly: loading the
main module loads this one, and its methods are called on a client.

They are documented together with the rest of the API, under
L<EV::Telegram::TDLib/"Connection mixin">.

=cut

sub CLONE_SKIP { 1 }


our %UPDATES = (
    updateConnectionState => \&update_connection_state,
    updateOption          => \&update_option,
);

# TDLib pushes its options as updates, my_id among them right after login,
# so the account's own id is known without a getMe round trip
sub update_option {
    my ($self, $obj) = @_;
    my $name = $obj->{name};
    return unless defined $name;
    my $v = $obj->{value} // {};
    my $type = $v->{'@type'} // '';
    $self->{cache}{options}{$name} =
          $type eq 'optionValueEmpty' ? undef
        : $type eq 'optionValueBoolean' ? ($v->{value} ? 1 : 0)
        : $v->{value};
}

sub option {
    my ($self, $name) = @_;
    # same as the other cache readers: undef in, undef out, and no warning
    # from inside the module for the lookup
    return undef unless defined $name;
    return $self->{cache}{options}{$name};
}

sub my_id { $_[0]{cache}{options}{my_id} }

sub update_connection_state {
    my ($self, $obj) = @_;
    my $state = $obj->{state} or return;
    my $type = $state->{'@type'} // '';
    return unless length $type;
    $self->{cache}{connection_state} = $type;
    if (my $cb = $self->{on_connection_state}) { $cb->($type) }
}

sub connection_state { $_[0]{cache}{connection_state} }

sub on_connection_state {
    my ($self, $cb) = @_;
    $self->{on_connection_state} = $cb if @_ > 1;
    return $self->{on_connection_state};
}

sub sessions {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    no_extra('sessions', 0, \@args);
    $self->send({ '@type' => 'getActiveSessions' }, $cb);
    return;
}

# session ids are TL int64 and must not cross as numbers
sub terminate_session {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    no_extra('terminate_session', 1, \@args);
    my ($session_id) = @args;
    need('session_id', $session_id);
    $self->send({ '@type' => 'terminateSession',
                  session_id => plain_text('a session id', $session_id) }, $cb);
    return;
}

sub terminate_other_sessions {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    no_extra('terminate_other_sessions', 0, \@args);
    $self->send({ '@type' => 'terminateAllOtherSessions' }, $cb);
    return;
}

sub set_session_ttl {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    no_extra('set_session_ttl', 1, \@args);
    my ($days) = @args;
    need('inactive_session_ttl_days', $days);
    $self->send({ '@type' => 'setInactiveSessionTtl',
                  inactive_session_ttl_days => 0 + $days }, $cb);
    return;
}


my %PROXY_TYPE = (
    socks5  => 'proxyTypeSocks5',
    http    => 'proxyTypeHttp',
    mtproto => 'proxyTypeMtproto',



( run in 0.407 second using v1.01-cache-2.11-cpan-57ff5998125 )