line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Notifications; |
2
|
10
|
|
|
10
|
|
114286
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
10
|
|
|
|
|
29
|
|
|
10
|
|
|
|
|
70
|
|
3
|
10
|
|
|
10
|
|
2283
|
use Mojo::Collection qw/c/; |
|
10
|
|
|
|
|
25
|
|
|
10
|
|
|
|
|
582
|
|
4
|
10
|
|
|
10
|
|
5615
|
use Mojolicious::Plugin::Notifications::Assets; |
|
10
|
|
|
|
|
33
|
|
|
10
|
|
|
|
|
362
|
|
5
|
10
|
|
|
10
|
|
83
|
use Mojo::Util qw/camelize/; |
|
10
|
|
|
|
|
29
|
|
|
10
|
|
|
|
|
537
|
|
6
|
10
|
|
|
10
|
|
66
|
use Scalar::Util qw/blessed/; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
12535
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $TYPE_RE = qr/^[-a-zA-Z_]+$/; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '1.07'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# TODO: |
13
|
|
|
|
|
|
|
# Maybe revert to tx-handler and use session instead of flash! |
14
|
|
|
|
|
|
|
# TODO: |
15
|
|
|
|
|
|
|
# Support Multiple Times Loading |
16
|
|
|
|
|
|
|
# TODO: |
17
|
|
|
|
|
|
|
# Subroutines for Engines should be given directly |
18
|
|
|
|
|
|
|
# TODO: |
19
|
|
|
|
|
|
|
# Engines may use hooks in addition |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# Register plugin |
22
|
|
|
|
|
|
|
sub register { |
23
|
10
|
|
|
10
|
1
|
527
|
my ($plugin, $app, $param) = @_; |
24
|
|
|
|
|
|
|
|
25
|
10
|
|
50
|
|
|
49
|
$param ||= {}; |
26
|
|
|
|
|
|
|
|
27
|
10
|
50
|
|
|
|
111
|
my $debug = $app->mode eq 'development' ? 1 : 0; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Load parameter from Config file |
30
|
10
|
50
|
|
|
|
251
|
if (my $config_param = $app->config('Notifications')) { |
31
|
0
|
|
|
|
|
0
|
$param = { %$param, %$config_param }; |
32
|
|
|
|
|
|
|
}; |
33
|
|
|
|
|
|
|
|
34
|
10
|
100
|
|
|
|
224
|
$param->{HTML} = 1 unless keys %$param; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Add engines from configuration |
37
|
10
|
|
|
|
|
25
|
my %engine; |
38
|
10
|
|
|
|
|
35
|
foreach my $name (keys %$param) { |
39
|
18
|
|
|
|
|
78
|
my $engine = camelize $name; |
40
|
18
|
100
|
|
|
|
197
|
if (index($engine,'::') < 0) { |
41
|
16
|
|
|
|
|
54
|
$engine = __PACKAGE__ . '::' . $engine; |
42
|
|
|
|
|
|
|
}; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Load engine |
45
|
18
|
|
|
|
|
71
|
my $e = $app->plugins->load_plugin($engine); |
46
|
18
|
100
|
|
|
|
4092
|
$e->register($app, ref $param->{$name} ? $param->{$name} : undef); |
47
|
18
|
|
|
|
|
572
|
$engine{lc $name} = $e; |
48
|
|
|
|
|
|
|
}; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# Create asset object |
51
|
10
|
|
|
|
|
85
|
my $asset = Mojolicious::Plugin::Notifications::Assets->new; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Set assets |
54
|
10
|
|
|
|
|
40
|
foreach (values %engine) { |
55
|
18
|
50
|
|
|
|
212
|
$asset->styles($_->styles) if $_->can('styles'); |
56
|
18
|
50
|
|
|
|
126
|
$asset->scripts($_->scripts) if $_->can('scripts'); |
57
|
|
|
|
|
|
|
}; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# Add notifications |
61
|
|
|
|
|
|
|
$app->helper( |
62
|
|
|
|
|
|
|
notify => sub { |
63
|
77
|
|
|
77
|
|
677733
|
my $c = shift; |
64
|
77
|
|
|
|
|
172
|
my $type = shift; |
65
|
77
|
|
|
|
|
207
|
my @msg = @_; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Ignore debug messages in production |
68
|
77
|
50
|
33
|
|
|
895
|
return if $type !~ $TYPE_RE || (!$debug && $type eq 'debug'); |
|
|
|
33
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
77
|
|
|
|
|
166
|
my $notes; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# Notifications already set |
73
|
77
|
100
|
|
|
|
270
|
if ($notes = $c->stash('notify.array')) { |
74
|
26
|
|
|
|
|
351
|
push @$notes, [$type => @msg]; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# New notifications as a Mojo::Collection |
78
|
|
|
|
|
|
|
else { |
79
|
51
|
|
|
|
|
923
|
$c->stash('notify.array' => c([$type => @msg])); |
80
|
|
|
|
|
|
|
}; |
81
|
|
|
|
|
|
|
} |
82
|
10
|
|
|
|
|
121
|
); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Embed notification display |
86
|
|
|
|
|
|
|
$app->helper( |
87
|
|
|
|
|
|
|
notifications => sub { |
88
|
71
|
|
|
71
|
|
256393
|
my $c = shift; |
89
|
|
|
|
|
|
|
|
90
|
71
|
100
|
|
|
|
328
|
return $asset unless @_; |
91
|
|
|
|
|
|
|
|
92
|
55
|
|
|
|
|
158
|
my $e_type = lc shift; |
93
|
55
|
|
|
|
|
126
|
my @param = @_; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# Get stash notifications |
96
|
55
|
|
66
|
|
|
171
|
my $notes = ($c->stash->{'notify.array'} //= c()); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# Get flash notifications |
99
|
55
|
|
|
|
|
1113
|
my $flash = $c->flash('n!.a'); |
100
|
|
|
|
|
|
|
|
101
|
55
|
100
|
66
|
|
|
18491
|
if ($flash && ref $flash eq 'ARRAY') { |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Ensure that no harmful types are injected |
104
|
11
|
|
|
|
|
45
|
unshift @$notes, grep { $_->[0] =~ $TYPE_RE } @$flash; |
|
21
|
|
|
|
|
202
|
|
105
|
|
|
|
|
|
|
}; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# Emit hook for further flexibility |
108
|
55
|
|
|
|
|
273
|
$app->plugins->emit_hook( |
109
|
|
|
|
|
|
|
before_notifications => $c, $notes |
110
|
|
|
|
|
|
|
); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# Delete from stash |
113
|
55
|
|
|
|
|
946
|
delete $c->stash->{'notify.array'}; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# Nothing to do |
116
|
55
|
100
|
100
|
|
|
628
|
return '' unless $notes->size || @_; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# Forward messages to notification center |
119
|
46
|
50
|
|
|
|
472
|
if (exists $engine{$e_type}) { |
120
|
|
|
|
|
|
|
|
121
|
46
|
|
|
|
|
87
|
my %rule; |
122
|
46
|
|
100
|
|
|
328
|
while ($param[-1] && index($param[-1], '-') == 0) { |
123
|
1
|
|
|
|
|
7
|
$rule{lc(substr(pop @param, 1))} = 1; |
124
|
|
|
|
|
|
|
}; |
125
|
|
|
|
|
|
|
|
126
|
46
|
|
|
|
|
284
|
return $engine{$e_type}->notifications($c, $notes, \%rule, @param); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
else { |
129
|
0
|
|
|
|
|
0
|
$c->app->log->error(qq{Unknown notification engine "$e_type"}); |
130
|
0
|
|
|
|
|
0
|
return; |
131
|
|
|
|
|
|
|
}; |
132
|
|
|
|
|
|
|
} |
133
|
10
|
|
|
|
|
1633
|
); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# Add hook for redirects |
137
|
|
|
|
|
|
|
$app->hook( |
138
|
|
|
|
|
|
|
after_dispatch => sub { |
139
|
61
|
|
|
61
|
|
37624
|
my $c = shift; |
140
|
|
|
|
|
|
|
|
141
|
61
|
100
|
|
|
|
269
|
if ($c->res->is_redirect) { |
142
|
|
|
|
|
|
|
# Get notify array from stash |
143
|
21
|
|
|
|
|
569
|
my $notes = delete $c->stash->{'notify.array'}; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# Check flash notes |
146
|
21
|
100
|
|
|
|
265
|
if ($c->flash('n!.a')) { |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# Merge notes with flash |
149
|
10
|
50
|
|
|
|
5209
|
if ($notes) { |
150
|
10
|
|
|
|
|
30
|
unshift @$notes, @{$c->flash('n!.a')}; |
|
10
|
|
|
|
|
34
|
|
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
# Get notes from flash |
154
|
|
|
|
|
|
|
else { |
155
|
0
|
|
|
|
|
0
|
$notes = $c->flash('n!.a'); |
156
|
|
|
|
|
|
|
}; |
157
|
|
|
|
|
|
|
}; |
158
|
|
|
|
|
|
|
|
159
|
21
|
50
|
|
|
|
3500
|
if ($notes) { |
160
|
21
|
|
|
|
|
111
|
$c->flash('n!.a' => $notes); |
161
|
|
|
|
|
|
|
}; |
162
|
|
|
|
|
|
|
}; |
163
|
10
|
|
|
|
|
952
|
}); |
164
|
|
|
|
|
|
|
}; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
1; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
__END__ |