added on local at 2026-07-01 21:47:08
| 1 | #!/usr/bin/perl | |
| 2 | #====================================================================== | |
| 3 | # RePricer -- topbar Messages pill live-poll data feed. | |
| 4 | # | |
| 5 | # Returns JSON { "n": <count> } | |
| 6 | # - For admins: total support_threads with admin_unread > 0. | |
| 7 | # - For regular users: their own support_threads with | |
| 8 | # customer_unread > 0. | |
| 9 | # | |
| 10 | # (Until 2026-06-20 this also added cross-platform external_comments | |
| 11 | # counts -- that whole inbox feature was removed in the WebSTLs-fork | |
| 12 | # cleanup pass; only support_threads remain.) | |
| 13 | # | |
| 14 | # Polled every ~20s by the wrapper template so the pill blinks | |
| 15 | # without a page refresh. | |
| 16 | #====================================================================== | |
| 17 | use strict; | |
| 18 | use warnings; | |
| 19 | ||
| 20 | use lib '/var/www/vhosts/3dshawn.com/repricer.3dshawn.com'; | |
| 21 | use CGI; | |
| 22 | use MODS::Login; | |
| 23 | use MODS::DBConnect; | |
| 24 | use MODS::RePricer::Config; | |
| 25 | ||
| 26 | my $auth = MODS::Login->new; | |
| 27 | my $userinfo = $auth->login_verify(); | |
| 28 | ||
| 29 | my $n = 0; | |
| 30 | if ($userinfo && $userinfo->{user_id}) { | |
| 31 | my $uid = $userinfo->{user_id}; | |
| 32 | $uid =~ s/[^0-9]//g; | |
| 33 | my $is_admin = $userinfo->{is_admin} || $userinfo->{_admin_is_admin} || 0; | |
| 34 | my $db = MODS::DBConnect->new; | |
| 35 | my $cfg = MODS::RePricer::Config->new; | |
| 36 | my $DB = $cfg->settings('database_name'); | |
| 37 | my $dbh = $db->db_connect(); | |
| 38 | if ($dbh) { | |
| 39 | # support_threads | |
| 40 | my $have_st = $db->db_readwrite($dbh, qq~ | |
| 41 | SELECT COUNT(*) AS n FROM information_schema.tables | |
| 42 | WHERE table_schema='$DB' AND table_name='support_threads' | |
| 43 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 44 | if ($have_st && $have_st->{n}) { | |
| 45 | my $sql = $is_admin | |
| 46 | ? qq~SELECT COUNT(*) AS n FROM `${DB}`.support_threads WHERE admin_unread > 0~ | |
| 47 | : qq~SELECT COUNT(*) AS n FROM `${DB}`.support_threads WHERE customer_user_id='$uid' AND customer_unread > 0~; | |
| 48 | my $r = $db->db_readwrite($dbh, $sql, $ENV{SCRIPT_NAME}, __LINE__); | |
| 49 | $n += ($r && $r->{n}) ? $r->{n} : 0; | |
| 50 | } | |
| 51 | $db->db_disconnect($dbh); | |
| 52 | } | |
| 53 | } | |
| 54 | ||
| 55 | print "Content-Type: application/json; charset=utf-8\n"; | |
| 56 | print "Cache-Control: no-store, max-age=0\n"; | |
| 57 | print "\n"; | |
| 58 | print qq~{"n":$n}\n~; |