Diff -- /var/www/vhosts/3dshawn.com/admin.3dshawn.com/MODS/MetaAdmin/AutoAck.pm
Diff
/var/www/vhosts/3dshawn.com/admin.3dshawn.com/MODS/MetaAdmin/AutoAck.pm
added on local at 2026-07-10 14:13:06
Added
+0
lines
Removed
-0
lines
Context
537
unchanged
Blobs
from 0cb9fe0eed3d
to 0cb9fe0eed3d
to 0cb9fe0eed3d
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.| 1 | 1 | package MODS::MetaAdmin::AutoAck; |
| 2 | 2 | #====================================================================== |
| 3 | 3 | # Meta-Admin -- auto-acknowledge scheduler for Support Inbox. |
| 4 | 4 | # |
| 5 | 5 | # Design: |
| 6 | 6 | # * discover() -- scan all supported sites for eligible threads, |
| 7 | 7 | # match against active rules, insert scheduled fires |
| 8 | 8 | # into auto_ack_queue (with random delay per rule). |
| 9 | 9 | # * fire_ready() -- process queued rows whose fire_at <= NOW(). Re-runs |
| 10 | 10 | # eligibility checks at fire time (a human may have |
| 11 | 11 | # replied in the meantime; the customer may have |
| 12 | 12 | # closed the thread; global kill switch may be off). |
| 13 | 13 | # |
| 14 | 14 | # Both intended to be called from auto_ack_tick.pl, running every minute |
| 15 | 15 | # from /etc/cron.d. |
| 16 | 16 | # |
| 17 | 17 | # Ethical guardrail: the templates themselves are enforced-ack-only by |
| 18 | 18 | # operator discipline (see the /canned.cgi KPI tile). This module just |
| 19 | 19 | # fires whatever's in the pool -- so keep the pool clean. |
| 20 | 20 | #====================================================================== |
| 21 | 21 | use strict; |
| 22 | 22 | use warnings; |
| 23 | 23 | use POSIX (); |
| 24 | 24 | use MODS::DBConnect; |
| 25 | 25 | use MODS::MetaAdmin::SiteConn; |
| 26 | 26 | use MODS::MetaAdmin::SupportInbox; |
| 27 | 27 | use MODS::MetaAdmin::CannedReplies; |
| 28 | 28 | |
| 29 | 29 | sub new { |
| 30 | 30 | my ($class) = @_; |
| 31 | 31 | return bless({ |
| 32 | 32 | db => MODS::DBConnect->new, |
| 33 | 33 | sc => MODS::MetaAdmin::SiteConn->new, |
| 34 | 34 | inbox => MODS::MetaAdmin::SupportInbox->new, |
| 35 | 35 | canned => MODS::MetaAdmin::CannedReplies->new, |
| 36 | 36 | log => [], |
| 37 | 37 | }, $class); |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | sub log_lines { return $_[0]->{log} } |
| 41 | 41 | |
| 42 | 42 | sub _log { push @{$_[0]->{log}}, $_[1] } |
| 43 | 43 | |
| 44 | 44 | #--------------------------------------------------------------------- |
| 45 | 45 | # is_globally_enabled -> 0/1. The single kill switch. |
| 46 | 46 | #--------------------------------------------------------------------- |
| 47 | 47 | sub is_globally_enabled { |
| 48 | 48 | my $self = shift; |
| 49 | 49 | my $dbh = $self->{db}->db_connect or return 0; |
| 50 | 50 | my $r = $self->{db}->db_readwrite($dbh, |
| 51 | 51 | "SELECT setting_value AS v FROM auto_ack_settings WHERE setting_key='global_enabled' LIMIT 1", |
| 52 | 52 | __FILE__, __LINE__); |
| 53 | 53 | $self->{db}->db_disconnect($dbh); |
| 54 | 54 | return ($r && $r->{v} && $r->{v} eq '1') ? 1 : 0; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | sub set_global_enabled { |
| 58 | 58 | my ($self, $v) = @_; |
| 59 | 59 | $v = $v ? '1' : '0'; |
| 60 | 60 | my $dbh = $self->{db}->db_connect or return 0; |
| 61 | 61 | $self->{db}->db_readwrite($dbh, qq~ |
| 62 | 62 | UPDATE auto_ack_settings SET setting_value='$v' |
| 63 | 63 | WHERE setting_key='global_enabled' |
| 64 | 64 | ~, __FILE__, __LINE__); |
| 65 | 65 | $self->{db}->db_disconnect($dbh); |
| 66 | 66 | return 1; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | #--------------------------------------------------------------------- |
| 70 | 70 | # is_business_hours($rule) -> 0/1 |
| 71 | 71 | # Compares current time (Mountain Time) against rule's start/end hours. |
| 72 | 72 | #--------------------------------------------------------------------- |
| 73 | 73 | sub is_business_hours { |
| 74 | 74 | my ($self, $rule) = @_; |
| 75 | 75 | return 1 unless $rule->{business_hours_only}; |
| 76 | 76 | local $ENV{TZ} = 'America/Denver'; |
| 77 | 77 | POSIX::tzset(); |
| 78 | 78 | my @lt = localtime(time); |
| 79 | 79 | my $hour = $lt[2]; |
| 80 | 80 | my $s = int($rule->{business_hours_start} || 0); |
| 81 | 81 | my $e = int($rule->{business_hours_end} || 23); |
| 82 | 82 | if ($s <= $e) { return ($hour >= $s && $hour <= $e) ? 1 : 0; } |
| 83 | 83 | # Wrap-around window (e.g. 22..6) |
| 84 | 84 | return ($hour >= $s || $hour <= $e) ? 1 : 0; |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | #--------------------------------------------------------------------- |
| 88 | 88 | # list_rules({ active_only }) -> arrayref of rule hashrefs |
| 89 | 89 | #--------------------------------------------------------------------- |
| 90 | 90 | sub list_rules { |
| 91 | 91 | my ($self, %a) = @_; |
| 92 | 92 | my $active = exists $a{active_only} ? $a{active_only} : 0; |
| 93 | 93 | my $where = $active ? 'WHERE is_active=1' : ''; |
| 94 | 94 | my $dbh = $self->{db}->db_connect or return []; |
| 95 | 95 | my @rows = $self->{db}->db_readwrite_multiple($dbh, qq~ |
| 96 | 96 | SELECT * FROM auto_ack_rules $where ORDER BY priority, id |
| 97 | 97 | ~, __FILE__, __LINE__); |
| 98 | 98 | $self->{db}->db_disconnect($dbh); |
| 99 | 99 | return \@rows; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | sub get_rule { |
| 103 | 103 | my ($self, $id) = @_; |
| 104 | 104 | $id = int($id || 0); return undef unless $id > 0; |
| 105 | 105 | my $dbh = $self->{db}->db_connect or return undef; |
| 106 | 106 | my $r = $self->{db}->db_readwrite($dbh, |
| 107 | 107 | "SELECT * FROM auto_ack_rules WHERE id=$id LIMIT 1", __FILE__, __LINE__); |
| 108 | 108 | $self->{db}->db_disconnect($dbh); |
| 109 | 109 | return $r; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | sub save_rule { |
| 113 | 113 | my ($self, %a) = @_; |
| 114 | 114 | my $id = int($a{id} || 0); |
| 115 | 115 | my $dbh = $self->{db}->db_connect or return undef; |
| 116 | 116 | my $name_q = $dbh->quote($a{name} // ''); |
| 117 | 117 | my $desc_q = $dbh->quote($a{description} // ''); |
| 118 | 118 | my $ss_q = $dbh->quote($a{site_scope} // 'all'); |
| 119 | 119 | my $tpl = int($a{template_id} || 0); |
| 120 | 120 | my $mn = int($a{min_delay_seconds} || 120); |
| 121 | 121 | my $mx = int($a{max_delay_seconds} || 480); |
| 122 | 122 | my $mno = ($a{match_new_only}) ? 1 : 0; |
| 123 | 123 | my $mnar = ($a{match_no_admin_reply}) ? 1 : 0; |
| 124 | 124 | my $motmin = int($a{match_customer_other_threads_min} || 0); |
| 125 | 125 | my $motmax = int($a{match_customer_other_threads_max} || 999); |
| 126 | 126 | my $bho = ($a{business_hours_only}) ? 1 : 0; |
| 127 | 127 | my $bhs = int($a{business_hours_start} // 8); |
| 128 | 128 | my $bhe = int($a{business_hours_end} // 23); |
| 129 | 129 | my $pri = int($a{priority} || 100); |
| 130 | 130 | my $act = ($a{is_active} // 1) ? 1 : 0; |
| 131 | 131 | |
| 132 | 132 | if ($id > 0) { |
| 133 | 133 | $self->{db}->db_readwrite($dbh, qq~ |
| 134 | 134 | UPDATE auto_ack_rules SET |
| 135 | 135 | name=$name_q, description=$desc_q, template_id=$tpl, |
| 136 | 136 | site_scope=$ss_q, min_delay_seconds=$mn, max_delay_seconds=$mx, |
| 137 | 137 | match_new_only=$mno, match_no_admin_reply=$mnar, |
| 138 | 138 | match_customer_other_threads_min=$motmin, |
| 139 | 139 | match_customer_other_threads_max=$motmax, |
| 140 | 140 | business_hours_only=$bho, business_hours_start=$bhs, |
| 141 | 141 | business_hours_end=$bhe, priority=$pri, is_active=$act |
| 142 | 142 | WHERE id=$id LIMIT 1 |
| 143 | 143 | ~, __FILE__, __LINE__); |
| 144 | 144 | } else { |
| 145 | 145 | $self->{db}->db_readwrite($dbh, qq~ |
| 146 | 146 | INSERT INTO auto_ack_rules |
| 147 | 147 | (name, description, template_id, site_scope, |
| 148 | 148 | min_delay_seconds, max_delay_seconds, |
| 149 | 149 | match_new_only, match_no_admin_reply, |
| 150 | 150 | match_customer_other_threads_min, match_customer_other_threads_max, |
| 151 | 151 | business_hours_only, business_hours_start, business_hours_end, |
| 152 | 152 | priority, is_active, created_at) |
| 153 | 153 | VALUES |
| 154 | 154 | ($name_q, $desc_q, $tpl, $ss_q, |
| 155 | 155 | $mn, $mx, $mno, $mnar, $motmin, $motmax, |
| 156 | 156 | $bho, $bhs, $bhe, $pri, $act, NOW()) |
| 157 | 157 | ~, __FILE__, __LINE__); |
| 158 | 158 | my $r = $self->{db}->db_readwrite($dbh, "SELECT LAST_INSERT_ID() AS id", |
| 159 | 159 | __FILE__, __LINE__); |
| 160 | 160 | $id = $r && $r->{id} ? $r->{id} : 0; |
| 161 | 161 | } |
| 162 | 162 | $self->{db}->db_disconnect($dbh); |
| 163 | 163 | return $id; |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | sub toggle_rule { |
| 167 | 167 | my ($self, $id) = @_; |
| 168 | 168 | $id = int($id || 0); return 0 unless $id > 0; |
| 169 | 169 | my $dbh = $self->{db}->db_connect or return 0; |
| 170 | 170 | $self->{db}->db_readwrite($dbh, |
| 171 | 171 | "UPDATE auto_ack_rules SET is_active=1-is_active WHERE id=$id LIMIT 1", |
| 172 | 172 | __FILE__, __LINE__); |
| 173 | 173 | $self->{db}->db_disconnect($dbh); |
| 174 | 174 | return 1; |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | #--------------------------------------------------------------------- |
| 178 | 178 | # discover() -- for each active rule, scan supported sites for eligible |
| 179 | 179 | # threads, insert into auto_ack_queue. Returns count inserted. |
| 180 | 180 | #--------------------------------------------------------------------- |
| 181 | 181 | sub discover { |
| 182 | 182 | my $self = shift; |
| 183 | 183 | return 0 unless $self->is_globally_enabled; |
| 184 | 184 | |
| 185 | 185 | my $rules = $self->list_rules(active_only => 1); |
| 186 | 186 | return 0 unless @$rules; |
| 187 | 187 | |
| 188 | 188 | my $supported = $self->{inbox}->supported_slugs; |
| 189 | 189 | my %slug_ok = map { $_ => 1 } @$supported; |
| 190 | 190 | |
| 191 | 191 | my $inserted = 0; |
| 192 | 192 | foreach my $rule (@$rules) { |
| 193 | 193 | # Business hours short-circuit avoids per-site query cost outside window. |
| 194 | 194 | next unless $self->is_business_hours($rule); |
| 195 | 195 | |
| 196 | 196 | my @scope_slugs; |
| 197 | 197 | if (($rule->{site_scope} || 'all') eq 'all') { |
| 198 | 198 | @scope_slugs = @$supported; |
| 199 | 199 | } else { |
| 200 | 200 | @scope_slugs = grep { $slug_ok{$_} } |
| 201 | 201 | split(/\s*,\s*/, $rule->{site_scope}); |
| 202 | 202 | } |
| 203 | 203 | next unless @scope_slugs; |
| 204 | 204 | |
| 205 | 205 | foreach my $slug (@scope_slugs) { |
| 206 | 206 | my $candidates = $self->_find_candidates($slug, $rule); |
| 207 | 207 | foreach my $cand (@$candidates) { |
| 208 | 208 | my $queued = $self->_enqueue($slug, $cand, $rule); |
| 209 | 209 | $inserted++ if $queued; |
| 210 | 210 | } |
| 211 | 211 | } |
| 212 | 212 | } |
| 213 | 213 | return $inserted; |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | 216 | #--------------------------------------------------------------------- |
| 217 | 217 | # _find_candidates($slug, $rule) -> arrayref of {thread_id, customer_user_id} |
| 218 | 218 | # |
| 219 | 219 | # Runs per-site via SiteConn. Handles PT column names via SupportInbox's |
| 220 | 220 | # adapter (fetch adapter, use its column names in the SQL). |
| 221 | 221 | #--------------------------------------------------------------------- |
| 222 | 222 | sub _find_candidates { |
| 223 | 223 | my ($self, $slug, $rule) = @_; |
| 224 | 224 | # Adapter columns for THIS site |
| 225 | 225 | my $adapter = MODS::MetaAdmin::SupportInbox->adapter_for($slug); |
| 226 | 226 | return [] unless $adapter; |
| 227 | 227 | my $cust_col = $adapter->{thread_customer_col}; |
| 228 | 228 | |
| 229 | 229 | # Only look at recent (72h) threads to keep the scan bounded. |
| 230 | 230 | # "new_only" = last message from customer/user (not admin) AND thread status |
| 231 | 231 | # is one of the "waiting for us" family (native to each site). |
| 232 | 232 | my @native_open_statuses; |
| 233 | 233 | push @native_open_statuses, $adapter->{status_reverse}{open}; |
| 234 | 234 | push @native_open_statuses, $adapter->{status_reverse}{waiting_admin}; |
| 235 | 235 | my %uniq; @native_open_statuses = grep { !$uniq{$_}++ } @native_open_statuses; |
| 236 | 236 | my $status_in = join(',', map { "'$_'" } @native_open_statuses); |
| 237 | 237 | |
| 238 | 238 | my $admin_reply_clause = ''; |
| 239 | 239 | if ($rule->{match_no_admin_reply}) { |
| 240 | 240 | $admin_reply_clause = qq~AND NOT EXISTS ( |
| 241 | 241 | SELECT 1 FROM support_messages m |
| 242 | 242 | WHERE m.thread_id = t.id |
| 243 | 243 | AND m.$adapter->{message_sender_col} = '$adapter->{admin_sender_value}' |
| 244 | 244 | )~; |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | my $sql = qq~ |
| 248 | 248 | SELECT t.id AS thread_id, t.$cust_col AS customer_user_id |
| 249 | 249 | FROM support_threads t |
| 250 | 250 | WHERE t.created_at >= DATE_SUB(NOW(), INTERVAL 72 HOUR) |
| 251 | 251 | AND t.$adapter->{thread_status_col} IN ($status_in) |
| 252 | 252 | $admin_reply_clause |
| 253 | 253 | ORDER BY t.id ASC |
| 254 | 254 | LIMIT 50 |
| 255 | 255 | ~; |
| 256 | 256 | my $rows = $self->{sc}->query_many($slug, $sql) || []; |
| 257 | 257 | |
| 258 | 258 | # Filter by "other open threads by same customer" bracket + by not-already-queued. |
| 259 | 259 | my $dbh = $self->{db}->db_connect; |
| 260 | 260 | my @keep; |
| 261 | 261 | foreach my $r (@$rows) { |
| 262 | 262 | my $tid = int($r->{thread_id} || 0); |
| 263 | 263 | my $cust = int($r->{customer_user_id} || 0); |
| 264 | 264 | |
| 265 | 265 | # Dedup: already queued/sent for this (site, thread, template)? |
| 266 | 266 | my $already = $self->{db}->db_readwrite($dbh, qq~ |
| 267 | 267 | SELECT id FROM auto_ack_queue |
| 268 | 268 | WHERE site='$slug' AND thread_id=$tid AND template_id=$rule->{template_id} |
| 269 | 269 | LIMIT 1 |
| 270 | 270 | ~, __FILE__, __LINE__); |
| 271 | 271 | next if $already && $already->{id}; |
| 272 | 272 | |
| 273 | 273 | # Dedup: same template already sent to same customer on this site? |
| 274 | 274 | if ($cust) { |
| 275 | 275 | my $prev = $self->{db}->db_readwrite($dbh, qq~ |
| 276 | 276 | SELECT id FROM auto_ack_queue |
| 277 | 277 | WHERE site='$slug' AND customer_user_id=$cust |
| 278 | 278 | AND template_id=$rule->{template_id} |
| 279 | 279 | AND status IN ('sent','queued') |
| 280 | 280 | LIMIT 1 |
| 281 | 281 | ~, __FILE__, __LINE__); |
| 282 | 282 | next if $prev && $prev->{id}; |
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | # Other-open-threads-by-same-customer bracket |
| 286 | 286 | if ($cust) { |
| 287 | 287 | my $other_count = $self->_count_other_open_threads($slug, $cust, $tid, $adapter); |
| 288 | 288 | next if $other_count < ($rule->{match_customer_other_threads_min} || 0); |
| 289 | 289 | next if $other_count > ($rule->{match_customer_other_threads_max} || 999); |
| 290 | 290 | $r->{other_open} = $other_count; |
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | push @keep, $r; |
| 294 | 294 | } |
| 295 | 295 | $self->{db}->db_disconnect($dbh); |
| 296 | 296 | return \@keep; |
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | sub _count_other_open_threads { |
| 300 | 300 | my ($self, $slug, $cust, $tid, $adapter) = @_; |
| 301 | 301 | my $cust_col = $adapter->{thread_customer_col}; |
| 302 | 302 | my @open_statuses; |
| 303 | 303 | push @open_statuses, $adapter->{status_reverse}{open}; |
| 304 | 304 | push @open_statuses, $adapter->{status_reverse}{waiting_admin}; |
| 305 | 305 | push @open_statuses, $adapter->{status_reverse}{waiting_customer}; |
| 306 | 306 | my %uniq; @open_statuses = grep { !$uniq{$_}++ } @open_statuses; |
| 307 | 307 | my $status_in = join(',', map { "'$_'" } @open_statuses); |
| 308 | 308 | my $r = $self->{sc}->query_one($slug, qq~ |
| 309 | 309 | SELECT COUNT(*) AS n FROM support_threads |
| 310 | 310 | WHERE $cust_col=$cust AND id != $tid |
| 311 | 311 | AND $adapter->{thread_status_col} IN ($status_in) |
| 312 | 312 | ~); |
| 313 | 313 | return $r ? int($r->{n} || 0) : 0; |
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | sub _enqueue { |
| 317 | 317 | my ($self, $slug, $cand, $rule) = @_; |
| 318 | 318 | my $tid = int($cand->{thread_id}); |
| 319 | 319 | my $cust = int($cand->{customer_user_id} || 0); |
| 320 | 320 | my $tpl = int($rule->{template_id}); |
| 321 | 321 | my $rid = int($rule->{id}); |
| 322 | 322 | |
| 323 | 323 | # Random delay within rule window |
| 324 | 324 | my $mn = int($rule->{min_delay_seconds} || 60); |
| 325 | 325 | my $mx = int($rule->{max_delay_seconds} || 300); |
| 326 | 326 | $mx = $mn if $mx < $mn; |
| 327 | 327 | my $delay = $mn + int(rand($mx - $mn + 1)); |
| 328 | 328 | |
| 329 | 329 | my $dbh = $self->{db}->db_connect or return 0; |
| 330 | 330 | my $ok = eval { |
| 331 | 331 | $self->{db}->db_readwrite($dbh, qq~ |
| 332 | 332 | INSERT INTO auto_ack_queue |
| 333 | 333 | (site, thread_id, customer_user_id, template_id, rule_id, |
| 334 | 334 | discovered_at, fire_at, status) |
| 335 | 335 | VALUES |
| 336 | 336 | ('$slug', $tid, $cust, $tpl, $rid, |
| 337 | 337 | NOW(), DATE_ADD(NOW(), INTERVAL $delay SECOND), 'queued') |
| 338 | 338 | ~, __FILE__, __LINE__); |
| 339 | 339 | 1; |
| 340 | 340 | }; |
| 341 | 341 | $self->{db}->db_disconnect($dbh); |
| 342 | 342 | if (!$ok) { |
| 343 | 343 | $self->_log("enqueue $slug/$tid tpl=$tpl failed: $@"); |
| 344 | 344 | return 0; |
| 345 | 345 | } |
| 346 | 346 | $self->_log("queued $slug/$tid tpl=$tpl rule=$rid delay=${delay}s"); |
| 347 | 347 | return 1; |
| 348 | 348 | } |
| 349 | 349 | |
| 350 | 350 | #--------------------------------------------------------------------- |
| 351 | 351 | # fire_ready() -- process queued rows whose fire_at <= NOW(). Returns |
| 352 | 352 | # arrayref of results. |
| 353 | 353 | #--------------------------------------------------------------------- |
| 354 | 354 | sub fire_ready { |
| 355 | 355 | my $self = shift; |
| 356 | 356 | unless ($self->is_globally_enabled) { |
| 357 | 357 | $self->_log("global kill switch is OFF, skipping fire_ready"); |
| 358 | 358 | return []; |
| 359 | 359 | } |
| 360 | 360 | my $dbh = $self->{db}->db_connect or return []; |
| 361 | 361 | my @due = $self->{db}->db_readwrite_multiple($dbh, qq~ |
| 362 | 362 | SELECT * FROM auto_ack_queue |
| 363 | 363 | WHERE status='queued' AND fire_at <= NOW() |
| 364 | 364 | ORDER BY fire_at ASC |
| 365 | 365 | LIMIT 20 |
| 366 | 366 | ~, __FILE__, __LINE__); |
| 367 | 367 | $self->{db}->db_disconnect($dbh); |
| 368 | 368 | |
| 369 | 369 | my @results; |
| 370 | 370 | foreach my $q (@due) { |
| 371 | 371 | push @results, $self->_fire_one($q); |
| 372 | 372 | } |
| 373 | 373 | return \@results; |
| 374 | 374 | } |
| 375 | 375 | |
| 376 | 376 | sub _fire_one { |
| 377 | 377 | my ($self, $q) = @_; |
| 378 | 378 | my $qid = int($q->{id}); |
| 379 | 379 | my $slug = $q->{site}; |
| 380 | 380 | my $tid = int($q->{thread_id}); |
| 381 | 381 | my $tpl = int($q->{template_id}); |
| 382 | 382 | my $rule = $self->get_rule($q->{rule_id}); |
| 383 | 383 | my $tpl_row = $self->{canned}->get($tpl); |
| 384 | 384 | |
| 385 | 385 | # Re-verify eligibility -- state may have shifted since queueing. |
| 386 | 386 | my $verdict = $self->_reverify($q, $rule); |
| 387 | 387 | if ($verdict->{skip}) { |
| 388 | 388 | $self->_mark_skipped($qid, $verdict->{reason}); |
| 389 | 389 | $self->_log("skip q=$qid $slug/$tid: $verdict->{reason}"); |
| 390 | 390 | return { id => $qid, action => 'skipped', reason => $verdict->{reason} }; |
| 391 | 391 | } |
| 392 | 392 | unless ($tpl_row) { |
| 393 | 393 | $self->_mark_skipped($qid, 'template_deleted'); |
| 394 | 394 | return { id => $qid, action => 'skipped', reason => 'template_deleted' }; |
| 395 | 395 | } |
| 396 | 396 | unless ($tpl_row->{is_active}) { |
| 397 | 397 | $self->_mark_skipped($qid, 'template_inactive'); |
| 398 | 398 | return { id => $qid, action => 'skipped', reason => 'template_inactive' }; |
| 399 | 399 | } |
| 400 | 400 | |
| 401 | 401 | # Expand template body against live thread context |
| 402 | 402 | my $thread_res = $self->{inbox}->get_thread($slug, $tid); |
| 403 | 403 | unless ($thread_res && $thread_res->{thread}) { |
| 404 | 404 | $self->_mark_skipped($qid, 'thread_missing'); |
| 405 | 405 | return { id => $qid, action => 'skipped', reason => 'thread_missing' }; |
| 406 | 406 | } |
| 407 | 407 | my $ctx = $self->{canned}->thread_context($thread_res->{thread}); |
| 408 | 408 | my $body = $self->{canned}->expand($tpl_row->{body}, $ctx); |
| 409 | 409 | |
| 410 | 410 | my ($ok, $ret) = $self->{inbox}->post_reply($slug, $tid, $body); |
| 411 | 411 | if ($ok) { |
| 412 | 412 | $self->_mark_sent($qid, $ret); |
| 413 | 413 | $self->_log("SENT q=$qid $slug/$tid tpl=$tpl mid=$ret"); |
| 414 | 414 | return { id => $qid, action => 'sent', message_id => $ret }; |
| 415 | 415 | } else { |
| 416 | 416 | $self->_mark_failed($qid, $ret); |
| 417 | 417 | $self->_log("FAIL q=$qid $slug/$tid: $ret"); |
| 418 | 418 | return { id => $qid, action => 'failed', error => $ret }; |
| 419 | 419 | } |
| 420 | 420 | } |
| 421 | 421 | |
| 422 | 422 | #--------------------------------------------------------------------- |
| 423 | 423 | # _reverify -- last-mile guards. Runs immediately before firing. |
| 424 | 424 | # Returns { skip => 0|1, reason => '...' }. |
| 425 | 425 | #--------------------------------------------------------------------- |
| 426 | 426 | sub _reverify { |
| 427 | 427 | my ($self, $q, $rule) = @_; |
| 428 | 428 | return { skip => 1, reason => 'rule_deleted' } unless $rule; |
| 429 | 429 | return { skip => 1, reason => 'rule_disabled' } unless $rule->{is_active}; |
| 430 | 430 | return { skip => 1, reason => 'outside_hours' } unless $self->is_business_hours($rule); |
| 431 | 431 | |
| 432 | 432 | my $slug = $q->{site}; |
| 433 | 433 | my $tid = int($q->{thread_id}); |
| 434 | 434 | my $adapter = MODS::MetaAdmin::SupportInbox->adapter_for($slug); |
| 435 | 435 | return { skip => 1, reason => 'unsupported_site' } unless $adapter; |
| 436 | 436 | |
| 437 | 437 | # Fetch current thread state |
| 438 | 438 | my $t = $self->{sc}->query_one($slug, qq~ |
| 439 | 439 | SELECT id, $adapter->{thread_status_col} AS status, |
| 440 | 440 | $adapter->{thread_customer_col} AS customer_user_id |
| 441 | 441 | FROM support_threads WHERE id = $tid LIMIT 1 |
| 442 | 442 | ~); |
| 443 | 443 | return { skip => 1, reason => 'thread_gone' } unless $t; |
| 444 | 444 | |
| 445 | 445 | # If the thread has since been closed/resolved, skip. |
| 446 | 446 | my $native_status = $t->{status} || ''; |
| 447 | 447 | my $normalized = $adapter->{status_map}{$native_status} || $native_status; |
| 448 | 448 | if ($normalized eq 'closed' || $normalized eq 'resolved') { |
| 449 | 449 | return { skip => 1, reason => 'thread_closed' }; |
| 450 | 450 | } |
| 451 | 451 | |
| 452 | 452 | # If any admin message has been posted since queueing, skip -- a human |
| 453 | 453 | # got there first, don't step on their reply. |
| 454 | 454 | if ($rule->{match_no_admin_reply}) { |
| 455 | 455 | my $adm = $self->{sc}->query_one($slug, qq~ |
| 456 | 456 | SELECT COUNT(*) AS n FROM support_messages |
| 457 | 457 | WHERE thread_id = $tid |
| 458 | 458 | AND $adapter->{message_sender_col} = '$adapter->{admin_sender_value}' |
| 459 | 459 | ~); |
| 460 | 460 | return { skip => 1, reason => 'admin_replied_first' } |
| 461 | 461 | if $adm && $adm->{n}; |
| 462 | 462 | } |
| 463 | 463 | |
| 464 | 464 | return { skip => 0 }; |
| 465 | 465 | } |
| 466 | 466 | |
| 467 | 467 | sub _mark_sent { |
| 468 | 468 | my ($self, $qid, $mid) = @_; |
| 469 | 469 | $mid = int($mid || 0); |
| 470 | 470 | my $dbh = $self->{db}->db_connect or return; |
| 471 | 471 | $self->{db}->db_readwrite($dbh, qq~ |
| 472 | 472 | UPDATE auto_ack_queue |
| 473 | 473 | SET status='sent', sent_at=NOW(), message_id=$mid |
| 474 | 474 | WHERE id=$qid LIMIT 1 |
| 475 | 475 | ~, __FILE__, __LINE__); |
| 476 | 476 | $self->{db}->db_disconnect($dbh); |
| 477 | 477 | } |
| 478 | 478 | |
| 479 | 479 | sub _mark_skipped { |
| 480 | 480 | my ($self, $qid, $reason) = @_; |
| 481 | 481 | my $dbh = $self->{db}->db_connect or return; |
| 482 | 482 | my $r_q = $dbh->quote($reason || ''); |
| 483 | 483 | $self->{db}->db_readwrite($dbh, qq~ |
| 484 | 484 | UPDATE auto_ack_queue |
| 485 | 485 | SET status='skipped', sent_at=NOW(), skip_reason=$r_q |
| 486 | 486 | WHERE id=$qid LIMIT 1 |
| 487 | 487 | ~, __FILE__, __LINE__); |
| 488 | 488 | $self->{db}->db_disconnect($dbh); |
| 489 | 489 | } |
| 490 | 490 | |
| 491 | 491 | sub _mark_failed { |
| 492 | 492 | my ($self, $qid, $err) = @_; |
| 493 | 493 | my $dbh = $self->{db}->db_connect or return; |
| 494 | 494 | my $e_q = $dbh->quote($err || ''); |
| 495 | 495 | $self->{db}->db_readwrite($dbh, qq~ |
| 496 | 496 | UPDATE auto_ack_queue |
| 497 | 497 | SET status='failed', sent_at=NOW(), error_message=$e_q |
| 498 | 498 | WHERE id=$qid LIMIT 1 |
| 499 | 499 | ~, __FILE__, __LINE__); |
| 500 | 500 | $self->{db}->db_disconnect($dbh); |
| 501 | 501 | } |
| 502 | 502 | |
| 503 | 503 | sub list_queued { |
| 504 | 504 | my ($self, %a) = @_; |
| 505 | 505 | my $limit = int($a{limit} || 30); |
| 506 | 506 | my $dbh = $self->{db}->db_connect or return []; |
| 507 | 507 | my @rows = $self->{db}->db_readwrite_multiple($dbh, qq~ |
| 508 | 508 | SELECT q.*, t.name AS template_name, r.name AS rule_name |
| 509 | 509 | FROM auto_ack_queue q |
| 510 | 510 | LEFT JOIN canned_templates t ON t.id = q.template_id |
| 511 | 511 | LEFT JOIN auto_ack_rules r ON r.id = q.rule_id |
| 512 | 512 | WHERE q.status='queued' |
| 513 | 513 | ORDER BY q.fire_at ASC |
| 514 | 514 | LIMIT $limit |
| 515 | 515 | ~, __FILE__, __LINE__); |
| 516 | 516 | $self->{db}->db_disconnect($dbh); |
| 517 | 517 | return \@rows; |
| 518 | 518 | } |
| 519 | 519 | |
| 520 | 520 | sub list_history { |
| 521 | 521 | my ($self, %a) = @_; |
| 522 | 522 | my $limit = int($a{limit} || 50); |
| 523 | 523 | my $dbh = $self->{db}->db_connect or return []; |
| 524 | 524 | my @rows = $self->{db}->db_readwrite_multiple($dbh, qq~ |
| 525 | 525 | SELECT q.*, t.name AS template_name, r.name AS rule_name |
| 526 | 526 | FROM auto_ack_queue q |
| 527 | 527 | LEFT JOIN canned_templates t ON t.id = q.template_id |
| 528 | 528 | LEFT JOIN auto_ack_rules r ON r.id = q.rule_id |
| 529 | 529 | WHERE q.status IN ('sent','skipped','failed') |
| 530 | 530 | ORDER BY COALESCE(q.sent_at, q.discovered_at) DESC |
| 531 | 531 | LIMIT $limit |
| 532 | 532 | ~, __FILE__, __LINE__); |
| 533 | 533 | $self->{db}->db_disconnect($dbh); |
| 534 | 534 | return \@rows; |
| 535 | 535 | } |
| 536 | 536 | |
| 537 | 537 | 1; |