line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Notifications::JSON; |
2
|
5
|
|
|
5
|
|
4852
|
use Mojo::Base 'Mojolicious::Plugin::Notifications::Engine'; |
|
5
|
|
|
|
|
64
|
|
|
5
|
|
|
|
|
35
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# TODO: |
5
|
|
|
|
|
|
|
# Probably introduce a notify_json function |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has key => 'notifications'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# Nothing to register |
10
|
|
|
|
|
|
|
sub register { |
11
|
5
|
|
|
5
|
1
|
18
|
my ($plugin, $app, $param) = @_; |
12
|
5
|
50
|
|
|
|
27
|
$plugin->key($param->{key}) if $param->{key}; |
13
|
|
|
|
|
|
|
}; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# Notification method |
17
|
|
|
|
|
|
|
sub notifications { |
18
|
15
|
|
|
15
|
1
|
68
|
my ($self, $c, $notify_array, $rule, $json, %param) = @_; |
19
|
|
|
|
|
|
|
|
20
|
15
|
|
33
|
|
|
118
|
my $key = $param{key} // $self->key; |
21
|
|
|
|
|
|
|
|
22
|
15
|
100
|
|
|
|
196
|
return $json unless @$notify_array; |
23
|
|
|
|
|
|
|
|
24
|
13
|
50
|
66
|
|
|
76
|
if (!$json || ref $json) { |
25
|
13
|
|
|
|
|
31
|
my @msgs; |
26
|
13
|
|
|
|
|
39
|
foreach (@$notify_array) { |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Confirmation message |
29
|
22
|
100
|
100
|
|
|
124
|
if (ref $_->[1] && ref $_->[1] eq 'HASH' && |
|
|
|
33
|
|
|
|
|
|
|
|
66
|
|
|
|
|
30
|
|
|
|
|
|
|
($_->[1]->{ok} || $_->[1]->{cancel} || $_[1]->{confirm})) { |
31
|
4
|
|
|
|
|
57
|
my $param = $_->[1]; |
32
|
|
|
|
|
|
|
|
33
|
4
|
|
|
|
|
11
|
my $opt = {}; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Set confirmation path |
36
|
4
|
100
|
|
|
|
19
|
if ($param->{confirm}) { |
37
|
|
|
|
|
|
|
$opt->{$param->{confirm_label} // 'confirm'} = { |
38
|
|
|
|
|
|
|
method => 'GET', |
39
|
|
|
|
|
|
|
url => $param->{confirm} |
40
|
1
|
|
50
|
|
|
12
|
}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
else { |
44
|
|
|
|
|
|
|
# Set confirmation path |
45
|
3
|
100
|
|
|
|
9
|
if ($param->{ok}) { |
46
|
|
|
|
|
|
|
$opt->{$param->{ok_label} // 'ok'} = { |
47
|
|
|
|
|
|
|
method => 'POST', |
48
|
|
|
|
|
|
|
url => $param->{ok} |
49
|
2
|
|
100
|
|
|
30
|
}; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Set cancelation path |
53
|
3
|
50
|
|
|
|
11
|
if ($param->{cancel}) { |
54
|
|
|
|
|
|
|
$opt->{$param->{cancel_label} // 'cancel'} = { |
55
|
|
|
|
|
|
|
method => 'POST', |
56
|
|
|
|
|
|
|
url => $param->{cancel} |
57
|
3
|
|
100
|
|
|
38
|
}; |
58
|
|
|
|
|
|
|
}; |
59
|
|
|
|
|
|
|
}; |
60
|
|
|
|
|
|
|
|
61
|
4
|
|
|
|
|
26
|
push @msgs, [$_->[0], ''.$_->[-1], $opt]; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# Normal notification |
65
|
|
|
|
|
|
|
else { |
66
|
18
|
|
|
|
|
142
|
push(@msgs, [$_->[0], ''.$_->[-1]]); |
67
|
|
|
|
|
|
|
}; |
68
|
|
|
|
|
|
|
}; |
69
|
|
|
|
|
|
|
|
70
|
13
|
100
|
|
|
|
94
|
if (!$json) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
71
|
2
|
|
|
|
|
22
|
return { $key => \@msgs } |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Obect is an array |
75
|
|
|
|
|
|
|
elsif (index(ref $json, 'ARRAY') >= 0) { |
76
|
1
|
|
|
|
|
10
|
push(@$json, { $key => \@msgs }); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# Object is a hash |
80
|
|
|
|
|
|
|
elsif (index(ref $json, 'HASH') >= 0) { |
81
|
10
|
|
50
|
|
|
82
|
my $n = ($json->{$key} //= []); |
82
|
10
|
|
|
|
|
34
|
push(@$n, @msgs); |
83
|
|
|
|
|
|
|
}; |
84
|
|
|
|
|
|
|
}; |
85
|
|
|
|
|
|
|
|
86
|
11
|
|
|
|
|
117
|
return $json; |
87
|
|
|
|
|
|
|
}; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
1; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
__END__ |