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