Slack/Socket.pm

filter: Statementstmt 69.6% 372 / 534 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 79.7% 122 / 153 Podpod 21.7% 5 / 23 totaltotal 61.4% 499 / 812 CC 40 SCAR 28.3
CC
40
Cov
60%
on_frame
182.0
link_shared
56.0
connect
20.0
CRAP 16.9
« Slack.pm ↑ Summary Slack/EventType.pm »
1 package Net::Async::Slack::Socket;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.015'; # VERSION
7 2 our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
8  
9 2 ▶use parent qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Net::Async::Slack::Socket - socket-mode notifications for L<https://slack.com>
14  
15 =head1 DESCRIPTION
16  
17 This is a basic wrapper for Slack's socket-mode features.
18  
19 See L<https://api.slack.com/apis/connections/socket> for some background on using this feature.
20  
21 This provides an event stream using websockets.
22  
23 For a full list of events, see L<https://api.slack.com/events>.
24  
25 =cut
26  
27 2 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶no indirect qw(fatal);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 2 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use Dir::Self;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use URI::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use JSON::MaybeUTF8 qw(:v2);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 2 ▶use IO::Async::Timer::Countdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use Net::Async::WebSocket::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # We have a long list of events, these are all autogenerated - the
44 # idea being that you should be able to `->isa()` or check `->type`
45 # to filter on the features you are interested in.
46 2 ▶use Net::Async::Slack::Event::AccountsChanged;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2 ▶use Net::Async::Slack::Event::AppHomeOpened;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use Net::Async::Slack::Event::AppMention;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use Net::Async::Slack::Event::AppRateLimited;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use Net::Async::Slack::Event::AppUninstalled;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use Net::Async::Slack::Event::BlockActions;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 2 ▶use Net::Async::Slack::Event::BotAdded;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 ▶use Net::Async::Slack::Event::BotChanged;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶use Net::Async::Slack::Event::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use Net::Async::Slack::Event::ChannelArchive;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use Net::Async::Slack::Event::ChannelCreated;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 2 ▶use Net::Async::Slack::Event::ChannelDeleted;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 2 ▶use Net::Async::Slack::Event::ChannelHistoryChanged;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use Net::Async::Slack::Event::ChannelJoined;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 ▶use Net::Async::Slack::Event::ChannelLeft;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶use Net::Async::Slack::Event::ChannelMarked;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use Net::Async::Slack::Event::Channel;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶use Net::Async::Slack::Event::ChannelRename;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶use Net::Async::Slack::Event::ChannelUnarchive;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 2 ▶use Net::Async::Slack::Event::CommandsChanged;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 ▶use Net::Async::Slack::Event::DndUpdated;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 2 ▶use Net::Async::Slack::Event::DndUpdatedUser;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 2 ▶use Net::Async::Slack::Event::EmailDomainChanged;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 ▶use Net::Async::Slack::Event::EmojiChanged;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶use Net::Async::Slack::Event::EventCallback;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 2 ▶use Net::Async::Slack::Event::FileChange;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 2 ▶use Net::Async::Slack::Event::FileCommentAdded;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 2 ▶use Net::Async::Slack::Event::FileCommentDeleted;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 2 ▶use Net::Async::Slack::Event::FileCommentEdited;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 2 ▶use Net::Async::Slack::Event::FileCreated;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 2 ▶use Net::Async::Slack::Event::FileDeleted;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 2 ▶use Net::Async::Slack::Event::FilePublic;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 2 ▶use Net::Async::Slack::Event::FileShared;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 2 ▶use Net::Async::Slack::Event::FileUnshared;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 2 ▶use Net::Async::Slack::Event::Goodbye;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 ▶use Net::Async::Slack::Event::GridMigrationFinished;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 2 ▶use Net::Async::Slack::Event::GridMigrationStarted;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶use Net::Async::Slack::Event::GroupArchive;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 2 ▶use Net::Async::Slack::Event::GroupClose;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 2 ▶use Net::Async::Slack::Event::GroupDeleted;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 2 ▶use Net::Async::Slack::Event::GroupHistoryChanged;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 ▶use Net::Async::Slack::Event::GroupJoined;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 ▶use Net::Async::Slack::Event::GroupLeft;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 2 ▶use Net::Async::Slack::Event::GroupMarked;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 2 ▶use Net::Async::Slack::Event::GroupOpen;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 2 ▶use Net::Async::Slack::Event::GroupRename;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 2 ▶use Net::Async::Slack::Event::GroupUnarchive;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 2 ▶use Net::Async::Slack::Event::Hello;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 2 ▶use Net::Async::Slack::Event::ImClose;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 2 ▶use Net::Async::Slack::Event::ImCreated;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 2 ▶use Net::Async::Slack::Event::ImHistoryChanged;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 2 ▶use Net::Async::Slack::Event::ImMarked;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 2 ▶use Net::Async::Slack::Event::ImOpen;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 2 ▶use Net::Async::Slack::Event::LinkShared;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 2 ▶use Net::Async::Slack::Event::ManualPresenceChange;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 2 ▶use Net::Async::Slack::Event::MemberJoinedChannel;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 2 ▶use Net::Async::Slack::Event::MemberLeftChannel;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 2 ▶use Net::Async::Slack::Event::MessageAction;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 2 ▶use Net::Async::Slack::Event::MessageAppHome;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 2 ▶use Net::Async::Slack::Event::MessageChannels;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 2 ▶use Net::Async::Slack::Event::MessageGroups;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 2 ▶use Net::Async::Slack::Event::MessageIm;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 2 ▶use Net::Async::Slack::Event::MessageMpim;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 2 ▶use Net::Async::Slack::Event::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 2 ▶use Net::Async::Slack::Event::PinAdded;
Subroutine BEGIN: called CC=1 SCAR=0.0
111 2 ▶use Net::Async::Slack::Event::PinRemoved;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 2 ▶use Net::Async::Slack::Event::PrefChange;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 2 ▶use Net::Async::Slack::Event::PresenceChange;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 2 ▶use Net::Async::Slack::Event::PresenceQuery;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 2 ▶use Net::Async::Slack::Event::PresenceSub;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 2 ▶use Net::Async::Slack::Event::ReactionAdded;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 2 ▶use Net::Async::Slack::Event::ReactionRemoved;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 2 ▶use Net::Async::Slack::Event::ReconnectURL;
Subroutine BEGIN: called CC=1 SCAR=0.0
119 2 ▶use Net::Async::Slack::Event::ResourcesAdded;
Subroutine BEGIN: called CC=1 SCAR=0.0
120 2 ▶use Net::Async::Slack::Event::ResourcesRemoved;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 2 ▶use Net::Async::Slack::Event::ScopeDenied;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 2 ▶use Net::Async::Slack::Event::ScopeGranted;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 2 ▶use Net::Async::Slack::Event::Shortcut;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 2 ▶use Net::Async::Slack::Event::SlashCommands;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 2 ▶use Net::Async::Slack::Event::StarAdded;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 2 ▶use Net::Async::Slack::Event::StarRemoved;
Subroutine BEGIN: called CC=1 SCAR=0.0
127 2 ▶use Net::Async::Slack::Event::SubteamCreated;
Subroutine BEGIN: called CC=1 SCAR=0.0
128 2 ▶use Net::Async::Slack::Event::SubteamMembersChanged;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 2 ▶use Net::Async::Slack::Event::SubteamSelfAdded;
Subroutine BEGIN: called CC=1 SCAR=0.0
130 2 ▶use Net::Async::Slack::Event::SubteamSelfRemoved;
Subroutine BEGIN: called CC=1 SCAR=0.0
131 2 ▶use Net::Async::Slack::Event::SubteamUpdated;
Subroutine BEGIN: called CC=1 SCAR=0.0
132 2 ▶use Net::Async::Slack::Event::TeamDomainChange;
Subroutine BEGIN: called CC=1 SCAR=0.0
133 2 ▶use Net::Async::Slack::Event::TeamJoin;
Subroutine BEGIN: called CC=1 SCAR=0.0
134 2 ▶use Net::Async::Slack::Event::TeamMigrationStarted;
Subroutine BEGIN: called CC=1 SCAR=0.0
135 2 ▶use Net::Async::Slack::Event::TeamPlanChange;
Subroutine BEGIN: called CC=1 SCAR=0.0
136 2 ▶use Net::Async::Slack::Event::TeamPrefChange;
Subroutine BEGIN: called CC=1 SCAR=0.0
137 2 ▶use Net::Async::Slack::Event::TeamProfileChange;
Subroutine BEGIN: called CC=1 SCAR=0.0
138 2 ▶use Net::Async::Slack::Event::TeamProfileDelete;
Subroutine BEGIN: called CC=1 SCAR=0.0
139 2 ▶use Net::Async::Slack::Event::TeamProfileReorder;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 2 ▶use Net::Async::Slack::Event::TeamRename;
Subroutine BEGIN: called CC=1 SCAR=0.0
141 2 ▶use Net::Async::Slack::Event::TokensRevoked;
Subroutine BEGIN: called CC=1 SCAR=0.0
142 2 ▶use Net::Async::Slack::Event::URLVerification;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 2 ▶use Net::Async::Slack::Event::UserChange;
Subroutine BEGIN: called CC=1 SCAR=0.0
144 2 ▶use Net::Async::Slack::Event::UserResourceDenied;
Subroutine BEGIN: called CC=1 SCAR=0.0
145 2 ▶use Net::Async::Slack::Event::UserResourceGranted;
Subroutine BEGIN: called CC=1 SCAR=0.0
146 2 ▶use Net::Async::Slack::Event::UserResourceRemoved;
Subroutine BEGIN: called CC=1 SCAR=0.0
147 2 ▶use Net::Async::Slack::Event::UserTyping;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 2 ▶use Net::Async::Slack::Event::ViewSubmission;
Subroutine BEGIN: called CC=1 SCAR=0.0
149 2 ▶use Net::Async::Slack::Event::WorkflowStepEdit;
Subroutine BEGIN: called CC=1 SCAR=0.0
150  
151 2 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
152 2 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
153  
154 =head1 METHODS
155  
156 =head2 events
157  
158 This is the stream of events, as a L<Ryu::Source>.
159  
160 Example usage:
161  
162 $rtm->events
163 ->filter(type => 'message')
164 ->sprintf_methods('> %s', $_->text)
165 ->say
166 ->await;
167  
168 =cut
169  
170 0 ▶sub events {
Subroutine events: not called CC=2 SCAR=17.9
Pod documented
171 0 my ($self) = @_;
172 0 ▶ $self->{events} //= do {
Condition0%0/3
$self->{"events"} //= do { $self->ryu->source }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"events"} //= do { $self->ryu->source }
$self->{"events"} do { $self->ryu->source }
Truth table0%0/3
A //= B
A= $self->{"events"}B= do { $self->ryu->source }
ABresult
1X1
011
000
173 0 $self->ryu->source
174 }
175 }
176  
177 =head2 handle_unfurl_domain
178  
179 Registers a handler for URLs.
180  
181 Takes the following named parameters:
182  
183 =over 4
184  
185 =item * C<domain> - which host/domain to respond to, e.g. C<google.com> for L<https://google.com>
186  
187 =item * C<handler> - a callback, expected to take a L<URI> instance and return a L<Future> with a Slack message
188  
189 =back
190  
191 Example usage:
192  
193 $sock->handle_unfurl_domain(
194 domain => 'service.local',
195 handler => async sub ($uri) {
196 my ($id) = $uri->path =~ m{/id/([0-9]+)}
197 or return undef;
198 return +{
199 blocks => [ {
200 "type" => "section",
201 "text" => {
202 "type" => "mrkdwn",
203 "text" => "Request with ID `$id`",
204 },
205 } ]
206 };
207 }
208 );
209  
210 Returns the L<Net::Async::Slack::Socket> instance to allow chaining.
211  
212 =cut
213  
214 0 ▶sub handle_unfurl_domain {
Subroutine handle_unfurl_domain: not called CC=3 SCAR=24.8
Pod documented
215 0 my ($self, %args) = @_;
216 $self->{unfurl_domain}{
217 delete $args{domain} || die 'need a domain'
218 } = $args{handler}
219 0 ▶ or die 'need a handler';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"unfurl_domain"}{delete $args{"domain"} or die "need a domain"} = $args{"handler"} or die "need a handler" 0 0 0
Condition0%0/2
delete $args{'domain'} || die('need a domain')
l!l
00
MC/DC0%0/1
delete $args{'domain'} || die('need a domain')
delete $args{'domain'}
Truth table0%0/2
A
A= delete $args{'domain'}
Aresult
11
00
220 0 return $self;
221 }
222  
223 =head2 last_frame_epoch
224  
225 Returns the floating-point timestamp for the last frame we received. Will be
226 C<undef> if we have no frames yet.
227  
228 =cut
229  
230 0 ▶sub last_frame_epoch {
Subroutine last_frame_epoch: not called CC=1 SCAR=6.9
Pod documented
231 0 my ($self) = @_;
232 0 return $self->{last_frame_epoch};
233 }
234  
235 =head1 METHODS - Internal
236  
237 You may not need to call these directly. If I'm wrong and you find yourself having
238 to do that, please complain via the usual channels.
239  
240 =head2 connect
241  
242 Establishes the connection. Called by the top-level L<Net::Async::Slack> instance.
243  
244 =cut
245  
246 0 ▶async sub connect {
Subroutine connect: not called CC=4 SCAR=30.0
Pod documented
247 0 my ($self, %args) = @_;
248 0 ▶ my $uri = delete($args{uri}) // $self->wss_uri or die 'no websocket URI available';
Branch: 1 branch
BranchTrueFalseTotal
my $uri = delete $args{"uri"} // $self->wss_uri or die "no websocket URI available" 0 0 0
Condition0%0/3
delete $args{"uri"} // $self->wss_uri
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $args{"uri"} // $self->wss_uri
delete $args{"uri"} $self->wss_uri
Truth table0%0/3
A // B
A= delete $args{"uri"}B= $self->wss_uri
ABresult
1X1
011
000
249 0 my $prev = delete $self->{ws};
250 $self->add_child(
251 0 $self->{ws} = Net::Async::WebSocket::Client->new(
252 on_frame => $self->curry::weak::on_frame,
253 on_ping_frame => $self->curry::weak::on_ping_frame,
254 on_close_frame => $self->curry::weak::on_close_frame,
255 )
256 );
257 0 $log->tracef('URL for websockets will be %s', "$uri");
258 my $res = await $self->{ws}->connect(
259 0 url => "$uri",
260 );
261 0 ▶ if($prev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev) 0 0 0
262 0 $log->tracef('Closing previous websocket connection');
263 try {
264 0 ▶ $prev->send_close_frame('')->then(async sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
265 0 $prev->close_now;
266 0 ▶ $self->remove_child($prev) if $prev->parent;
Branch: 1 branch
BranchTrueFalseTotal
if $prev->parent 0 0 0
267 0 })->retain;
268 0 } catch($e) {
269 0 $log->errorf('Unable to clean up previous connection: %s', $e);
270 }
271 }
272 0 $self->event_mangler;
273 0 return $res;
274 2 }
275  
276 0 ▶sub on_ping_frame {
Subroutine on_ping_frame: not called CC=1 SCAR=6.9
Pod undocumented
277 0 my ($self, $ws, $bytes) = @_;
278 0 $self->connection_watchdog_nudge;
279 0 $ws->send_pong_frame('');
280 }
281  
282 0 ▶sub on_close_frame {
Subroutine on_close_frame: not called CC=1 SCAR=6.9
Pod undocumented
283 0 my ($self) = @_;
284 0 $log->debugf('Received close frame');
285 0 $self->trigger_reconnect_if_needed
286 }
287  
288 0 ▶async sub reconnect {
Subroutine reconnect: not called CC=2 SCAR=17.9
Pod undocumented
289 0 my ($self) = @_;
290 0 my $sleep = 0;
291 0 my $count = 0;
292 0 while(1) {
293 0 ++$count;
294 try {
295 0 $log->debugf('Attempting reconnect, try %d', $count);
296 0 my ($uri) = await Future->wait_any(
297 $self->slack->socket,
298 $self->loop->timeout_future(after => 30),
299 );
300 0 await Future->wait_any(
301 $self->connect(
302 uri => $uri
303 ),
304 $self->loop->timeout_future(after => 30),
305 );
306 0 return;
307 0 } catch($e) {
308 0 ▶ $sleep = min(30.0, ($sleep || 0.008) * 2);
Condition0%0/2
$sleep || 0.008
l!l
00
MC/DC0%0/1
$sleep || 0.008
$sleep
Truth table0%0/2
A
A= $sleep
Aresult
11
00
309 0 $log->errorf('Failed to reconnect for socket mode, will try again in %.3fs: %s', $sleep, $e);
310 0 await $self->loop->delay_future(after => $sleep);
311 }
312 }
313 2 }
314  
315 0 ▶sub on_close {
Subroutine on_close: not called CC=1 SCAR=6.9
Pod undocumented
316 0 my ($self) = @_;
317 0 $self->trigger_reconnect_if_needed;
318 }
319  
320 0 ▶sub trigger_reconnect_if_needed {
Subroutine trigger_reconnect_if_needed: not called CC=2 SCAR=17.9
Pod undocumented
321 0 my ($self) = @_;
322 0 $log->tracef('trigger_reconnect_if_needed');
323 0 $self->connection_watchdog->stop;
324 0 ▶ return $self->{reconnecting} ||= $self->reconnect->on_ready(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
325 delete $self->{reconnecting}
326 0 ▶ });
Condition0%0/3
$self->{"reconnecting"} ||= $self->reconnect->on_ready(sub { delete $self->{"reconnecting"}; } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"reconnecting"} ||= $self->reconnect->on_ready(sub { delete $self->{"reconnecting"}; } )
$self->{"reconnecting"} $self->reconnect->on_ready(sub { delete $self->{"reconnecting"}; } )
Truth table0%0/3
A ||= B
A= $self->{"reconnecting"}B= $self->reconnect->on_ready(sub { delete $self->{"reconnecting"}; } )
ABresult
1X1
011
000
327 }
328  
329 0 ▶sub connection_watchdog {
Subroutine connection_watchdog: not called CC=2 SCAR=17.9
Pod undocumented
330 0 my ($self) = @_;
331 0 ▶ $self->{connection_watchdog} ||= do {
Condition0%0/3
$self->{"connection_watchdog"} ||= do { $self->add_child(my $timer = "IO::Async::Timer::Countdown"->new("delay", 30, "on_expire", $self->$curry::weak(sub { my($self) = @_; $self->trigger_reconnect_if_needed; } ))); $timer->start; $timer }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"connection_watchdog"} ||= do { $self->add_child(my $timer = "IO::Async::Timer::Countdown"->new("delay", 30, "on_expire", $self->$curry::weak(sub { my($self) = @_; $self->trigger_reconnect_if_needed; } ))); $timer->start; $timer }
$self->{"connection_watchdog"} do { $self->add_child(my $timer = "IO::Async::Timer::Countdown"->new("delay", 30, "on_expire", $self->$curry::weak(sub { my($self) = @_; $self->trigger_reconnect_if_needed; } ))); $timer->start; $timer }
Truth table0%0/3
A ||= B
A= $self->{"connection_watchdog"}B= do { $self->add_child(my $timer = "IO::Async::Timer::Countdown"->new("delay", 30, "on_expire", $self->$curry::weak(sub { my($self) = @_; $self->trigger_reconnect_if_needed; } ))); $timer->start; $timer }
ABresult
1X1
011
000
332 $self->add_child(
333 my $timer = IO::Async::Timer::Countdown->new(
334 delay => 30,
335 0 ▶ on_expire => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
336 0 my ($self) = @_;
337 0 $self->trigger_reconnect_if_needed
338 0 }),
339 )
340 );
341 0 $timer->start;
342 0 $timer
343 };
344 }
345  
346 0 ▶sub connection_watchdog_nudge {
Subroutine connection_watchdog_nudge: not called CC=2 SCAR=17.9
Pod undocumented
347 0 my ($self) = @_;
348 0 my $timer = $self->connection_watchdog;
349 0 $timer->reset;
350 0 ▶ $timer->start if $timer->is_expired;
Branch: 1 branch
BranchTrueFalseTotal
if $timer->is_expired 0 0 0
351 0 $self->{last_frame_epoch} = $self->loop->time;
352 0 $timer
353 }
354  
355 0 ▶sub on_frame {
Subroutine on_frame: not called CC=13 SCAR=52.0
Pod undocumented
356 0 my ($self, $ws, $bytes) = @_;
357 0 $self->connection_watchdog_nudge;
358  
359 # Empty frame is used for PING, send a response back
360 0 ▶ if(!length($bytes)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $bytes) 0 0 0
361 0 $ws->send_frame('');
362 0 return;
363 }
364  
365 0 ▶ my $text = eval { Encode::decode_utf8($bytes) } // do {
Condition0%0/3
eval { do { &Encode::decode_utf8($bytes) } } // do { $Net::Async::Slack::Socket::log->errorf("Invalid UTF8 received from Slack: %v02x", $bytes); return }
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { &Encode::decode_utf8($bytes) } } // do { $Net::Async::Slack::Socket::log->errorf("Invalid UTF8 received from Slack: %v02x", $bytes); return }
eval { do { &Encode::decode_utf8($bytes) } } do { $Net::Async::Slack::Socket::log->errorf("Invalid UTF8 received from Slack: %v02x", $bytes); return }
Truth table0%0/3
A // B
A= eval { do { &Encode::decode_utf8($bytes) } }B= do { $Net::Async::Slack::Socket::log->errorf("Invalid UTF8 received from Slack: %v02x", $bytes); return }
ABresult
1X1
011
000
366 0 $log->errorf('Invalid UTF8 received from Slack: %v02x', $bytes);
367 0 return;
368 };
369 try {
370 0 $log->tracef("<< %s", $text);
371 0 my $data = decode_json_text($text);
372 0 ▶ if($data->{type} eq 'disconnect') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"type"} eq "disconnect") 0 0 0
373 0 $log->debugf('Received disconnection notification, reason: %s (debug info: %s)', $data->{reason}, $data->{debug_info});
374 0 $self->trigger_reconnect_if_needed;
375 }
376  
377 0 my $pending;
378  
379 0 my $env_id = $data->{envelope_id};
380 0 ▶ if($env_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env_id) 0 0 0
381 0 ▶ if($data->{accepts_response_payload}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'accepts_response_payload'}) { } 0 0 0
382  
383 # If the caller marks our future as done, send the result over as a payload
384 0 ▶ $pending = $self->loop->new_future->on_done($self->$curry::weak(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
385 0 my ($self, $payload) = @_;
386 0 $self->send_response($env_id, $payload)->retain;
387 0 return;
388 0 }));
389 # ... but auto-acknowledge before the deadline if they don't, for back-compatibility
390 my $f = $self->loop->delay_future(
391 after => 1.5
392 0 ▶ )->on_done($self->$curry::weak(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
393 0 my ($self) = @_;
394 0 $self->send_response($env_id)->retain;
395 0 return;
396 0 }));
397 # Make sure only one of the actions completes
398 0 Future->wait_any($pending, $f)->retain;
399 } else {
400 0 $self->send_response($env_id)->retain;
401 }
402 }
403  
404 0 ▶ if(my $type = $data->{payload}{type}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $type = $data->{'payload'}{'type'}) { } 0 0 0
elsif ($type = $data->{'type'}) { } 0 0 0
405 0 ▶ if($type eq 'event_callback') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'event_callback') { } 0 0 0
406 my $ev = Net::Async::Slack::EventType->from_json(
407 $data->{payload}{event}
408 0 );
409 0 $ev->{envelope_id} = $env_id;
410 0 ▶ $ev->{response_future} = $pending if $pending;
Branch: 1 branch
BranchTrueFalseTotal
if $pending 0 0 0
411 0 $log->tracef("Have event [%s], emitting", $ev->type);
412 0 $self->events->emit($ev);
413 } else {
414 0 ▶ if(my $ev = Net::Async::Slack::EventType->from_json(
Branch: 1 branch
BranchTrueFalseTotal
if (my $ev = 'Net::Async::Slack::EventType'->from_json($data->{'payload'})) { } 0 0 0
415 $data->{payload}
416 )) {
417 0 $ev->{envelope_id} = $env_id;
418 0 ▶ $ev->{response_future} = $pending if $pending;
Branch: 1 branch
BranchTrueFalseTotal
if $pending 0 0 0
419 0 $log->tracef("Have event [%s], emitting", $ev->type);
420 0 $self->events->emit($ev);
421 } else {
422 0 $log->errorf('Failed to locate event type from payload %s', $data->{payload});
423 }
424 }
425 } elsif($type = $data->{type}) {
426 0 $log->tracef("Have generic/unknown event [%s], emitting", $data);
427 0 ▶ if(my $ev = Net::Async::Slack::EventType->from_json(
Branch: 1 branch
BranchTrueFalseTotal
if (my $ev = 'Net::Async::Slack::EventType'->from_json($data)) { } 0 0 0
428 $data
429 )) {
430 0 $self->events->emit($ev);
431 } else {
432 0 $log->errorf('Unable to find event type from %s', $data);
433 }
434 }
435 0 } catch ($e) {
436 0 $log->errorf("Exception in websocket raw frame handling: %s (original text %s)", $e, $text);
437 }
438 }
439  
440 0 ▶sub send_response {
Subroutine send_response: not called CC=3 SCAR=24.8
Pod undocumented
441 0 my ($self, $env_id, $payload) = @_;
442 0 ▶ die 'need env_id' unless $env_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $env_id 0 0 0
443 0 ▶ my $data = encode_json_utf8({
Branch: 1 branch
BranchTrueFalseTotal
$payload ? : 0 0 0
444 envelope_id => $env_id,
445 ($payload ? (payload => $payload) : ()),
446 });
447 0 $log->tracef(">> %s", $data);
448 0 return $self->ws->send_frame(
449 buffer => $data,
450 masked => 1
451 );
452 }
453  
454 0 ▶sub next_id {
Subroutine next_id: not called CC=2 SCAR=17.9
Pod undocumented
455 0 my ($self, $id) = @_;
456 0 ▶ $self->{last_id} = $id // ++$self->{last_id};
Condition0%0/2
$id // ++$self->{"last_id"}
l!l
00
MC/DC0%0/1
$id // ++$self->{"last_id"}
$id
Truth table0%0/2
A
A= $id
Aresult
11
00
457 }
458  
459 0 ▶sub configure {
Subroutine configure: not called CC=3 SCAR=24.8
Pod documented
460 0 my ($self, %args) = @_;
461 0 for my $k (qw(slack wss_uri)) {
462 0 ▶ $self->{$k} = delete $args{$k} if exists $args{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$k} 0 0 0
463 }
464 0 $self->next::method(%args);
465 }
466  
467 0 ▶sub ping_timer {
Subroutine ping_timer: not called CC=2 SCAR=17.9
Pod undocumented
468 0 my ($self) = @_;
469 0 ▶ $self->{ping_timer} ||= do {
Condition0%0/3
$self->{"ping_timer"} ||= do { $self->add_child(my $timer = "IO::Async::Timer::Countdown"->new("delay", 10, "on_expire", $self->$curry::weak(sub { (shift())->trigger_ping; } ))); $timer }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"ping_timer"} ||= do { $self->add_child(my $timer = "IO::Async::Timer::Countdown"->new("delay", 10, "on_expire", $self->$curry::weak(sub { (shift())->trigger_ping; } ))); $timer }
$self->{"ping_timer"} do { $self->add_child(my $timer = "IO::Async::Timer::Countdown"->new("delay", 10, "on_expire", $self->$curry::weak(sub { (shift())->trigger_ping; } ))); $timer }
Truth table0%0/3
A ||= B
A= $self->{"ping_timer"}B= do { $self->add_child(my $timer = "IO::Async::Timer::Countdown"->new("delay", 10, "on_expire", $self->$curry::weak(sub { (shift())->trigger_ping; } ))); $timer }
ABresult
1X1
011
000
470 $self->add_child(
471 my $timer = IO::Async::Timer::Countdown->new(
472 delay => 10,
473 0 ▶ on_expire => $self->$curry::weak(sub { shift->trigger_ping }),
Subroutine __ANON__: not called CC=1 SCAR=6.9
474 )
475 );
476 0 $timer
477 }
478 }
479  
480 0 ▶sub event_mangler {
Subroutine event_mangler: not called CC=2 SCAR=17.9
Pod undocumented
481 0 my ($self) = @_;
482 0 ▶ $self->{event_handling} //= $self->events->map($self->$curry::weak(async sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
483 0 my ($self, $ev) = @_;
484 try {
485 0 ▶ if(my $code = $self->can($ev->type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->can($ev->type)) { } 0 0 0
486 0 await $self->$code($ev);
487 } else {
488 0 $log->tracef('Ignoring event %s', $ev->type);
489 }
490 0 } catch ($e) {
491 0 $log->errorf('Event handling on %s failed: %s', $ev, $e);
492 }
493 0 ▶ }))->ordered_futures(
Condition0%0/3
$self->{"event_handling"} //= $self->events->map($self->$curry::weak(sub { XXX; eval { do { my($self, $ev) = @_; do { <custom op catch> } } }; <custom op leaveasync>; } ))->ordered_futures("low", 16, "high", 100)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"event_handling"} //= $self->events->map($self->$curry::weak(sub { XXX; eval { do { my($self, $ev) = @_; do { <custom op catch> } } }; <custom op leaveasync>; } ))->ordered_futures("low", 16, "high", 100)
$self->{"event_handling"} $self->events->map($self->$curry::weak(sub { XXX; eval { do { my($self, $ev) = @_; do { <custom op catch> } } }; <custom op leaveasync>; } ))->ordered_futures("low", 16, "high", 100)
Truth table0%0/3
A //= B
A= $self->{"event_handling"}B= $self->events->map($self->$curry::weak(sub { XXX; eval { do { my($self, $ev) = @_; do { <custom op catch> } } }; <custom op leaveasync>; } ))->ordered_futures("low", 16, "high", 100)
ABresult
1X1
011
000
494 low => 16,
495 high => 100,
496 );
497 }
498  
499 0 ▶async sub link_shared {
Subroutine link_shared: not called CC=7 SCAR=40.3
Pod undocumented
500 0 my ($self, $ev) = @_;
501 0 my %uri_map;
502 0 for my $link ($ev->{links}->@*) {
503 0 ▶ if(my $handler = $self->{unfurl_domain}{$link->{domain}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handler = $self->{"unfurl_domain"}{$link->{"domain"}}) 0 0 0
504 0 my $uri = URI->new($link->{url});
505 0 $log->tracef('Unfurling URL %s', $uri);
506 0 my $unfurled = await $handler->($uri);
507 0 ▶ $uri_map{$uri} = $unfurled if $unfurled;
Branch: 1 branch
BranchTrueFalseTotal
if $unfurled 0 0 0
508 }
509 }
510 0 ▶ return unless keys %uri_map;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %uri_map 0 0 0
511 my $res = await $self->slack->chat_unfurl(
512 channel => $ev->{channel_id} // $ev->{channel}->id,
513 ts => $ev->{message_ts},
514 0 ▶ unfurls => \%uri_map,
Condition0%0/3
$ev->{"channel_id"} // $ev->{"channel"}->id
l!l&&r!l&&!r
000
MC/DC0%0/2
$ev->{"channel_id"} // $ev->{"channel"}->id
$ev->{"channel_id"} $ev->{"channel"}->id
Truth table0%0/3
A // B
A= $ev->{"channel_id"}B= $ev->{"channel"}->id
ABresult
1X1
011
000
515 );
516 0 ▶ die 'invalid URI unfurling' unless $res->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"ok"} 0 0 0
517 0 return;
518 2 }
519  
520 0 ▶sub trigger_ping {
Subroutine trigger_ping: not called CC=2 SCAR=17.9
Pod undocumented
521 0 my ($self, %args) = @_;
522 0 my $id = $self->next_id($args{id});
523 0 $self->ws->send_frame(
524 buffer => encode_json_utf8({
525 type => 'ping',
526 id => $id,
527 }),
528 masked => 1
529 );
530 0 $self->ping_timer->reset;
531 0 ▶ $self->ping_timer->start if $self->ping_timer->is_expired;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ping_timer->is_expired 0 0 0
532 }
533  
534 0 ▶sub _add_to_loop {
Subroutine _add_to_loop: not called CC=2 SCAR=17.9
535 0 my ($self, $loop) = @_;
536 $self->add_child(
537 0 $self->{ryu} = Ryu::Async->new
538 );
539 # $self->ping_timer->start;
540 0 ▶ $self->{last_id} //= 0;
Condition0%0/2
$self->{"last_id"} //= 0
l!l
00
MC/DC0%0/1
$self->{"last_id"} //= 0
$self->{"last_id"}
Truth table0%0/2
A
A= $self->{"last_id"}
Aresult
11
00
541 }
542  
543 0 ▶sub slack { shift->{slack} }
Subroutine slack: not called CC=1 SCAR=6.9
Pod undocumented
544  
545 0 ▶sub wss_uri { shift->{wss_uri} }
Subroutine wss_uri: not called CC=1 SCAR=6.9
Pod undocumented
546  
547 0 ▶sub ws { shift->{ws} }
Subroutine ws: not called CC=1 SCAR=6.9
Pod undocumented
548  
549 0 ▶sub ryu { shift->{ryu} }
Subroutine ryu: not called CC=1 SCAR=6.9
Pod undocumented
550  
551 2 1;
552  
553 =head1 AUTHOR
554  
555 Tom Molesworth <TEAM@cpan.org>
556  
557 =head1 LICENSE
558  
559 Copyright Tom Molesworth 2016-2024. Licensed under the same terms as Perl itself.
560  
« Slack.pm ↑ Summary Slack/EventType.pm »