Skip to content

Commit ed7b3d1

Browse files
committed
Implement .public Model endpoint
That delays publicizing new alerts (to let us fix typos, undo it, etc)
1 parent bc2ef45 commit ed7b3d1

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

bin/server.p6

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ my $Alerts := P6lert::Model::Alerts.new;
99
sub MAIN (Str:D :$host = 'localhost', UInt:D :$port = 10000) {
1010
my $application = route {
1111
get -> {
12-
content 'text/html', html-render-alerts $Alerts.all
12+
content 'text/html', html-render-alerts $Alerts.public
1313
}
1414
get -> 'alert', UInt $id {
1515
content 'text/html', html-render-alerts $Alerts.get: $id
1616
}
1717

1818
get -> 'api', 'v1', 'all' {
1919
content 'application/json', to-json {
20-
alerts => $Alerts.all».TO-JSON,
20+
alerts => $Alerts.public».TO-JSON,
2121
};
2222
}
2323

lib/P6lert/Model/Alerts.pm6

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use DBIish;
33
use P6lert::Alert;
44

55
has IO::Path:D $.db-file = 'alerts.sqlite.db'.IO;
6+
has UInt $.public-delay = 60*10; # delay before making messages public
67
has $!dbh;
78

89
submethod TWEAK {
@@ -50,6 +51,16 @@ method all {
5051
}
5152
}
5253

54+
method public {
55+
given $!dbh.prepare:
56+
SELECT * FROM alerts WHERE time < ? ORDER BY time DESC
57+
{
58+
LEAVE .finish;
59+
.execute: time - $!public-delay;
60+
eager .allrows(:array-of-hash).map: { P6lert::Alert.new: |$_ }
61+
}
62+
}
63+
5364
method get (UInt:D $id) {
5465
given $!dbh.prepare: SELECT * FROM alerts where id = ? {
5566
LEAVE .finish;

t/model/00-basic.t

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
use lib <lib>;
22
use Testo;
3-
use Temp::Path;
4-
use DBIish;
53

4+
plan 21;
5+
6+
use Temp::Path;
67
use P6lert::Model::Alerts;
78

9+
my $public-delay := 4;
810
my $db-file := make-temp-path.extension: 'sqlite', :parts(0..*);
9-
my $alerts := P6lert::Model::Alerts.new: :$db-file;
11+
my $alerts := P6lert::Model::Alerts.new: :$db-file, :$public-delay;
1012
is $alerts, P6lert::Model::Alerts, '.new constructs right object';
1113
is $db-file, *.e, ".new creates db file $db-file";
1214

1315
is (my $id = $alerts.add: 'test♥1'), UInt:D, .add returns new alert's ID;
14-
16+
is $alerts.public, 0, '.public gives 0 alerts';
1517
is $alerts.all, 1, 'have total 1 alert in db';
1618
group '.all.head object' => 7 => {
1719
with $alerts.all.head -> \a {
@@ -27,12 +29,14 @@ group '.all.head object' => 7 => {
2729

2830
is $alerts.delete($id), *, 'deleting alert';
2931
is $alerts.all, 0, 'no alerts in db any more';
32+
is $alerts.public, 0, '.public still gives 0 alerts';
3033

3134
is $alerts.add('test♥2'), UInt:D, re-added first message;
3235
is $alerts.add(
3336
'meow2', :creator<Zof>, :affects('2017.12 and earlier'), :severity<critical>,
3437
), UInt:D, added second message;
3538
is $alerts.all, 2, 'two messages in db';
39+
is $alerts.public, 0, '.public still gives 0 alerts';
3640

3741
group 'first message' => 7 => {
3842
with $alerts.all.head -> \a {
@@ -57,3 +61,34 @@ group 'second message' => 7 => {
5761
is a.affects, '2017.12 and earlier', '.affects';
5862
}
5963
}
64+
65+
is *, *, 'waiting for public delay expire';
66+
sleep $public-delay+1;
67+
68+
is $alerts.add('unpublic'), UInt:D, added third, not-yet public message;
69+
is $alerts.all, 3, 'have 3 total alerts in db';
70+
is $alerts.public, 2, 'have 2 total public alerts in db';
71+
72+
group 'first public message' => 7 => {
73+
with $alerts.public.head -> \a {
74+
is a, P6lert::Alert, 'righ type';
75+
is a.id, 2, '.id';
76+
is a.alert, 'test♥2', '.alert';
77+
is a.time, /^\d**9..11$/, '.time looks approximately right';
78+
is a.creator, 'Anonymous', '.creator';
79+
is a.severity, 'normal', '.severity';
80+
is a.affects, '', '.affects';
81+
}
82+
}
83+
84+
group 'second public message' => 7 => {
85+
with $alerts.public.tail -> \a {
86+
is a, P6lert::Alert, 'righ type';
87+
is a.id, 3, '.id';
88+
is a.alert, 'meow2', '.alert';
89+
is a.time, /^\d**9..11$/, '.time looks approximately right';
90+
is a.creator, 'Zof', '.creator';
91+
is a.severity, 'critical', '.severity';
92+
is a.affects, '2017.12 and earlier', '.affects';
93+
}
94+
}

0 commit comments

Comments
 (0)