| 1 | # | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # This file is part of App-SpreadRevolutionaryDate | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # This software is Copyright (c) 2019-2026 by Gérald Sédrati. | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | package App::SpreadRevolutionaryDate::Target::Mastodon; | ||||||||||||||||||||||||||||||||||||||||||
| 13 | 6 | $App::SpreadRevolutionaryDate::Target::Mastodon::VERSION = '0.54'; | |||||||||||||||||||||||||||||||||||||||||
| 14 | # ABSTRACT: Target class for L<App::SpreadRevolutionaryDate> to handle spreading on Mastodon. | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 6 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 6 | with 'App::SpreadRevolutionaryDate::Target' | |||||||||||||||||||||||||||||||||||||||||
| 18 | => {worker => 'Mastodon::Client'}; | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 6 | ▶ | use Mastodon::Client; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 6 | ▶ | use Encode qw(encode decode is_utf8); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 6 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 6 | ▶ | use Locale::TextDomain 'App-SpreadRevolutionaryDate'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 6 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 6 | has 'instance' => ( | |||||||||||||||||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 29 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 6 | has 'client_id' => ( | |||||||||||||||||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 35 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 6 | has 'client_secret' => ( | |||||||||||||||||||||||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 41 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 6 | has 'access_token' => ( | |||||||||||||||||||||||||||||||||||||||||
| 46 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 47 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 48 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 6 | has 'max_lenght' => ( | |||||||||||||||||||||||||||||||||||||||||
| 52 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 53 | isa => 'Int', | ||||||||||||||||||||||||||||||||||||||||||
| 54 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | default => 300, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 6 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 6 | my ($orig, $class) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 6 | my $args = $class->$orig(@_); | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | $args->{obj} = Mastodon::Client->new( | ||||||||||||||||||||||||||||||||||||||||||
| 66 | instance => $args->{instance}, | ||||||||||||||||||||||||||||||||||||||||||
| 67 | client_id => $args->{client_id}, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | client_secret => $args->{client_secret}, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | access_token => $args->{access_token}, | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 6 | name => 'RevolutionaryDate'); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 6 | return $args; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 6 | }; | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | ▶ | sub spread { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spread: called CC=21 SCAR=50.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | my ($self, $msg, $test, $img) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | ▶ | $test //= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$test //= 0
MC/DC0%0/1
$test //= 0
$test
Truth table50%1/2
A
A= $test
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | # Multiline message | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | $msg =~ s/\\n/\n/g; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 2 | ▶ | if ($test) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | $msg = __("Spread on Mastodon: ") . $msg; | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | ▶ | if ($img) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | $msg .= " with image path: " . $img->{path} . " , alt: " . $img->{alt}; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 6 | ▶ | use open qw(:std :encoding(UTF-8)); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 6 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 2 | my $io = IO::Handle->new; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 2 | $io->fdopen(fileno(STDOUT), "w"); | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 2 | my @msgs = $self->_split_msg($msg, $self->max_lenght); | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 2 | ▶ | for (my $i = 0; $i < scalar @msgs; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | my $msg = "Message " . ($i+1) . ": " . $msgs[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | ▶ | $msg = encode('UTF-8', $msg) if is_utf8($msg); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | $io->say($msg); | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my @msgs = $self->_split_msg($msg, $self->max_lenght); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | my $last_status_id; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | foreach my $msg (@msgs) { | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my $params = {}; | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if (!$last_status_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | # First post | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ($img) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | $img = {path => $img} unless ref($img) && ref($img) eq 'HASH' && $img->{path}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
ref $img and ref $img eq "HASH" and $img->{"path"}
ref $img and ref $img eq "HASH"
MC/DC0%0/3
ref $img and ref $img eq "HASH" and $img->{"path"}
ref $img
ref $img eq "HASH"
$img->{"path"}
Truth table0%0/4
A and B and C
A= ref $imgB= ref $img eq "HASH"C= $img->{"path"}
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my $img_alt = $img->{alt} // ucfirst(fileparse($img->{path}, qr/\.[^.]*/)); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$img->{"alt"} // ucfirst fileparse($img->{"path"}, qr/\.[^.]*/u)
MC/DC0%0/1
$img->{"alt"} // ucfirst fileparse($img->{"path"}, qr/\.[^.]*/u)
$img->{"alt"}
Truth table0%0/2
A
A= $img->{"alt"}
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | $img_alt = encode('UTF-8', $img_alt) if is_utf8($img_alt); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my $resp_img = $self->obj->upload_media($img->{path}, {description => $img_alt}); | |||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | $params->{media_ids} = [$resp_img->{id}] if $resp_img->{id}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my $status = $self->obj->post_status($msg, $params); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | $last_status_id = $status->{id} if ($status && ref($status) eq 'HASH' && $status->{id}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$status and ref $status eq "HASH" and $status->{"id"}
$status and ref $status eq "HASH"
MC/DC0%0/3
$status and ref $status eq "HASH" and $status->{"id"}
$status
ref $status eq "HASH"
$status->{"id"}
Truth table0%0/4
A and B and C
A= $statusB= ref $status eq "HASH"C= $status->{"id"}
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 121 | # Next posts with reply_to | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | $params->{in_reply_to_id} = $last_status_id; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | my $status = $self->obj->post_status($msg, $params); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | $last_status_id = $status->{id} if ($status && ref($status) eq 'HASH' && $status->{id}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$status and ref $status eq "HASH" and $status->{"id"}
$status and ref $status eq "HASH"
MC/DC0%0/3
$status and ref $status eq "HASH" and $status->{"id"}
$status
ref $status eq "HASH"
$status->{"id"}
Truth table0%0/4
A and B and C
A= $statusB= ref $status eq "HASH"C= $status->{"id"}
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 6 | ▶ | no Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | # A module must return a true value. Traditionally, a module returns 1. | ||||||||||||||||||||||||||||||||||||||||||
| 135 | # But this module is a revolutionary one, so it discards all old traditions. | ||||||||||||||||||||||||||||||||||||||||||
| 136 | # Idea borrowed from Jean Forget's DateTime::Calendar::FrenchRevolutionary. | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 6 | "Quand le gouvernement viole les droits du peuple, | |||||||||||||||||||||||||||||||||||||||||
| 138 | l'insurrection est pour le peuple le plus sacré | ||||||||||||||||||||||||||||||||||||||||||
| 139 | et le plus indispensable des devoirs"; | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||||||||||||||||||||||||||