App-SpreadRevolutionaryDate

 view release on metacpan or  search on metacpan

lib/App/SpreadRevolutionaryDate/Target/Mastodon.pm  view on Meta::CPAN

  my ($self, $msg, $test, $img) = @_;
  $test //= 0;

  # Multiline message
  $msg =~ s/\\n/\n/g;

  if ($test) {
    $msg = __("Spread on Mastodon: ") . $msg;

    if ($img) {
      $msg .= " with image path: " . $img->{path} . " , alt: " . $img->{alt};
    }

    use open qw(:std :encoding(UTF-8));
    use IO::Handle;
    my $io = IO::Handle->new;
    $io->fdopen(fileno(STDOUT), "w");

    my @msgs = $self->_split_msg($msg, $self->max_lenght);

    for (my $i = 0; $i < scalar @msgs; $i++) {
      my $msg = "Message " . ($i+1) . ": " . $msgs[$i];
      $msg = encode('UTF-8', $msg) if is_utf8($msg);
      $io->say($msg);
    }
  } else {
    my @msgs = $self->_split_msg($msg, $self->max_lenght);
    my $last_status_id;

    foreach my $msg (@msgs) {
      my $params = {};

      if (!$last_status_id) {
        # First post
        if ($img) {
          $img = {path => $img} unless ref($img) && ref($img) eq 'HASH' && $img->{path};
          my $img_alt = $img->{alt} // ucfirst(fileparse($img->{path}, qr/\.[^.]*/));
          $img_alt = encode('UTF-8', $img_alt) if is_utf8($img_alt);
          my $resp_img = $self->obj->upload_media($img->{path}, {description => $img_alt});
          $params->{media_ids} = [$resp_img->{id}] if $resp_img->{id};
        }

        my $status = $self->obj->post_status($msg, $params);
        $last_status_id = $status->{id} if ($status && ref($status) eq 'HASH' && $status->{id});
      } else {
        # Next posts with reply_to
        $params->{in_reply_to_id} = $last_status_id;
        my $status = $self->obj->post_status($msg, $params);
        $last_status_id = $status->{id} if ($status && ref($status) eq 'HASH' && $status->{id});
      }
    }
  }
}


no Moose;
__PACKAGE__->meta->make_immutable;

# A module must return a true value. Traditionally, a module returns 1.
# But this module is a revolutionary one, so it discards all old traditions.
# Idea borrowed from Jean Forget's DateTime::Calendar::FrenchRevolutionary.
"Quand le gouvernement viole les droits du peuple,
l'insurrection est pour le peuple le plus sacré
et le plus indispensable des devoirs";

__END__

=pod

=encoding UTF-8

=head1 NAME

App::SpreadRevolutionaryDate::Target::Mastodon - Target class for L<App::SpreadRevolutionaryDate> to handle spreading on Mastodon.

=head1 VERSION

version 0.54

=head1 METHODS

=head2 new

Constructor class method. Takes a hash argument with the following mandatory keys: C<instance>, C<client_id>, C<client_secret>, and C<access_token>, with all values being strings. Authentifies to Mastodon and returns an C<App::SpreadRevolutionaryDate...

=head2 spread

Spreads a message to Mastodon. Takes one mandatory argument: C<$msg> which should be the message to spread as a characters string; and one optional argument: C<test>, which defaults to C<false>, and if C<true> prints the message on standard output in...

=head1 SEE ALSO

=over

=item L<spread-revolutionary-date>

=item L<App::SpreadRevolutionaryDate>

=item L<App::SpreadRevolutionaryDate::Config>

=item L<App::SpreadRevolutionaryDate::BlueskyLite>

=item L<App::SpreadRevolutionaryDate::Target>

=item L<App::SpreadRevolutionaryDate::Target::Bluesky>

=item L<App::SpreadRevolutionaryDate::Target::Twitter>

=item L<App::SpreadRevolutionaryDate::Target::Freenode>

=item L<App::SpreadRevolutionaryDate::Target::Freenode::Bot>

=item L<App::SpreadRevolutionaryDate::Target::Liberachat>

=item L<App::SpreadRevolutionaryDate::Target::Liberachat::Bot>

=item L<App::SpreadRevolutionaryDate::MsgMaker>

=item L<App::SpreadRevolutionaryDate::MsgMaker::RevolutionaryDate>

=item L<App::SpreadRevolutionaryDate::MsgMaker::RevolutionaryDate::Calendar>



( run in 0.904 second using v1.01-cache-2.11-cpan-5a3173703d6 )