| 1 | package Monitoring::Spooler::Web::API; | ||
| 2 | $Monitoring::Spooler::Web::API::VERSION = '0.05'; | ||
| 3 | BEGIN { | ||
| 4 | $Monitoring::Spooler::Web::API::AUTHORITY = 'cpan:TEX'; | ||
| 5 | } | ||
| 6 | # ABSTRACT: the API endpoint for the spooler | ||
| 7 | |||
| 8 | use 5.010_000; | ||
| 9 | use mro 'c3'; | ||
| 10 | use feature ':5.10'; | ||
| 11 | |||
| 12 | use Moose; | ||
| 13 | use namespace::autoclean; | ||
| 14 | |||
| 15 | # use IO::Handle; | ||
| 16 | # use autodie; | ||
| 17 | # use MooseX::Params::Validate; | ||
| 18 | # use Carp; | ||
| 19 | # use English qw( -no_match_vars ); | ||
| 20 | use URI::Escape; | ||
| 21 | use Try::Tiny; | ||
| 22 | use JSON; | ||
| 23 | |||
| 24 | # extends ... | ||
| 25 | extends 'Monitoring::Spooler::Web'; | ||
| 26 | # has ... | ||
| 27 | has 'json' => ( | ||
| 28 | 'is' => 'ro', | ||
| 29 | 'isa' => 'JSON', | ||
| 30 | 'lazy' => 1, | ||
| 31 | 'builder' => '_init_json', | ||
| 32 | ); | ||
| 33 | # with ... | ||
| 34 | # initializers ... | ||
| 35 | sub _init_json { | ||
| 36 | my $self = shift; | ||
| 37 | |||
| 38 | my $JSON = JSON::->new()->utf8(); | ||
| 39 | |||
| 40 | return $JSON; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub _init_fields { | ||
| 44 | return [qw(mode group_id queue message)]; | ||
| 45 | } | ||
| 46 | |||
| 47 | # your code here ... | ||
| 48 | sub _handle_request { | ||
| 49 | my $self = shift; | ||
| 50 | my $request = shift; | ||
| 51 | |||
| 52 | my $mode = $request->{'mode'} || 'update_queue'; | ||
| 53 | |||
| 54 | if($mode eq 'update_queue') { | ||
| 55 | # set the new notification/escalation queue | ||
| 56 | if($self->_handle_update_queue($request)) { | ||
| 57 | return [ 200, ['Content-Type', 'text/plain'], ['OK']]; | ||
| 58 | } else { | ||
| 59 | return [ 400, ['Content-Type', 'text/plain'], ['Bad Request']]; | ||
| 60 | } | ||
| 61 | } elsif($mode eq 'send_text') { | ||
| 62 | # enqueue a single text message | ||
| 63 | if($self->_handle_send_text($request)) { | ||
| 64 | return [ 202, ['Content-Type', 'text/plain'], ['Your messages has been queued for deliery.']]; | ||
| 65 | } else { | ||
| 66 | return [ 400, ['Content-Type', 'text/plain'], ['Bad Request']]; | ||
| 67 | } | ||
| 68 | } else { | ||
| 69 | return [ 400, ['Content-Type', 'text/plain'], ['Bad Request']]; | ||
| 70 | } | ||
| 71 | |||
| 72 | return 1; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _handle_send_text { | ||
| 76 | my $self = shift; | ||
| 77 | my $request = shift; | ||
| 78 | |||
| 79 | # This is just an convenience method for sending text messages | ||
| 80 | # through the spooler. Uncommon but you should never stop | ||
| 81 | # people from doing it the wrong way ... 'uhm wait, did I get that right? ;) | ||
| 82 | |||
| 83 | my $group_id = $request->{'group_id'}; | ||
| 84 | my $message = $request->{'message'}; | ||
| 85 | |||
| 86 | return unless $group_id && $message; | ||
| 87 | |||
| 88 | $message = URI::Escape::uri_unescape($message); | ||
| 89 | |||
| 90 | my $sql = "INSERT INTO msg_queue (group_id,type,message) VALUES(?,'text',?)"; | ||
| 91 | my $sth = $self->dbh()->prepare($sql); | ||
| 92 | if(!$sth) { | ||
| 93 | $self->logger()->log( message => 'Failed to prepare statement: '.$self->dbh()->errstr, level => 'warning', ); | ||
| 94 | return; | ||
| 95 | } | ||
| 96 | if(!$sth->execute($group_id,$message)) { | ||
| 97 | $self->logger()->log( message => 'Failed to execute statement: '.$sth->errstr, level => 'warning', ); | ||
| 98 | return; | ||
| 99 | } | ||
| 100 | $sth->finish(); | ||
| 101 | |||
| 102 | return 1; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _handle_update_queue { | ||
| 106 | my $self = shift; | ||
| 107 | my $request = shift; | ||
| 108 | |||
| 109 | # This method is the primary purpose of this class: | ||
| 110 | # Update the notification queue for a given group | ||
| 111 | |||
| 112 | my $group_id = $request->{'group_id'}; | ||
| 113 | my $queue = $request->{'queue'}; | ||
| 114 | my $message = $request->{'message'} || 'You are now the primary contact for Monitoring Notifications'; | ||
| 115 | |||
| 116 | if(!$queue || !$group_id) { | ||
| 117 | $self->logger()->log( message => 'Missing queue or group_id. Aborting.', level => 'error', ); | ||
| 118 | return; | ||
| 119 | } | ||
| 120 | |||
| 121 | $queue = URI::Escape::uri_unescape($queue); | ||
| 122 | my $queue_ref = undef; | ||
| 123 | try { | ||
| 124 | $queue_ref = $self->json()->decode($queue); | ||
| 125 | } catch { | ||
| 126 | $self->logger()->log( message => 'Failed to decode JSON: '.$_, level => 'warning', ); | ||
| 127 | }; | ||
| 128 | $queue = undef; | ||
| 129 | |||
| 130 | # if json decoding above failed we must exit here, since we've got | ||
| 131 | # nothing to write in the DB. | ||
| 132 | if(!$queue_ref) { | ||
| 133 | $self->logger()->log( message => 'Queue is empty after decoding JSON. Aborting.', level => 'error', ); | ||
| 134 | return; | ||
| 135 | } | ||
| 136 | |||
| 137 | my $sql = 'SELECT COUNT(*) FROM groups WHERE id = ?'; | ||
| 138 | my $sth = $self->dbh()->prepare($sql); | ||
| 139 | $sth->execute($group_id); | ||
| 140 | my $has_group = $sth->fetchrow_array(); | ||
| 141 | $sth->finish(); | ||
| 142 | |||
| 143 | if(!$has_group) { | ||
| 144 | # the requested group doesn't exist | ||
| 145 | $self->logger()->log( message => 'The requested group does not exist. Aborting.', level => 'error', ); | ||
| 146 | return; | ||
| 147 | } | ||
| 148 | |||
| 149 | # before we start to modify the DB, we start a TX so we can | ||
| 150 | # rollback later | ||
| 151 | $self->dbh()->do('BEGIN TRANSACTION'); | ||
| 152 | |||
| 153 | # remove the old notification queue for this group | ||
| 154 | $sql = 'DELETE FROM notify_order WHERE group_id = ?'; | ||
| 155 | $sth = $self->dbh()->prepare($sql); | ||
| 156 | if(!$sth) { | ||
| 157 | $self->logger()->log( message => 'Failed to prepare statement: '.$self->dbh()->errstr, level => 'warning', ); | ||
| 158 | $self->dbh()->do('ROLLBACK'); | ||
| 159 | return; | ||
| 160 | } | ||
| 161 | if(!$sth->execute($group_id)) { | ||
| 162 | $self->logger()->log( message => 'Failed to execute statement: '.$sth->errstr, level => 'warning', ); | ||
| 163 | $self->dbh()->do('ROLLBACK'); | ||
| 164 | return; | ||
| 165 | } | ||
| 166 | $sth->finish(); | ||
| 167 | |||
| 168 | # insert each user of the new notification queue into the DB in the order | ||
| 169 | # that they were sent to us. | ||
| 170 | $sql = 'INSERT INTO notify_order (group_id,name,number) VALUES(?,?,?)'; | ||
| 171 | $sth = $self->dbh()->prepare($sql); | ||
| 172 | if(!$sth) { | ||
| 173 | $self->logger()->log( message => 'Failed to prepare statement: '.$self->dbh()->errstr, level => 'warning', ); | ||
| 174 | $self->dbh()->do('ROLLBACK'); | ||
| 175 | return; | ||
| 176 | } | ||
| 177 | |||
| 178 | foreach my $user (@$queue_ref) { | ||
| 179 | if(!$sth->execute($group_id,$user->{'name'},$user->{'cellphone'})) { | ||
| 180 | $self->logger()->log( message => 'Failed to execute statement: '.$sth->errstr, level => 'warning', ); | ||
| 181 | $self->dbh()->do('ROLLBACK'); | ||
| 182 | return; | ||
| 183 | } | ||
| 184 | # if this user has an alternated number defined insert it, too | ||
| 185 | if($user->{'phone_alt'}) { | ||
| 186 | if(!$sth->execute($group_id,$user->{'name'}.' (Fallback)',$user->{'phone_alt'})) { | ||
| 187 | $self->logger()->log( message => 'Failed to execute statement: '.$sth->errstr.'. Ignoring!', level => 'warning', ); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | } | ||
| 191 | $sth->finish(); | ||
| 192 | |||
| 193 | # send a welcome message to our new primary contact. | ||
| 194 | # this should be configurable on a per-group base in the db | ||
| 195 | $sql = "INSERT INTO msg_queue (group_id,type,message) VALUES(?,'text',?)"; | ||
| 196 | $sth = $self->dbh()->prepare($sql); | ||
| 197 | if(!$sth) { | ||
| 198 | $self->logger()->log( message => 'Failed to prepare statement: '.$self->dbh()->errstr, level => 'warning', ); | ||
| 199 | return; | ||
| 200 | } | ||
| 201 | if(!$sth->execute($group_id,$message)) { | ||
| 202 | $self->logger()->log( message => 'Failed to execute statement: '.$sth->errstr, level => 'warning', ); | ||
| 203 | return; | ||
| 204 | } | ||
| 205 | $sth->finish(); | ||
| 206 | |||
| 207 | # everything ok, make changes durable | ||
| 208 | if($self->dbh()->do('COMMIT')) { | ||
| 209 | $self->logger()->log( message => 'Successfully update queue for group '.$group_id, level => 'debug', ); | ||
| 210 | } | ||
| 211 | |||
| 212 | return 1; | ||
| 213 | } | ||
| 214 | |||
| 215 | no Moose; | ||
| 216 | __PACKAGE__->meta->make_immutable; | ||
| 217 | |||
| 218 | 1; | ||
| 219 | |||
| 220 | __END__ |