added on WebSTLs (webstls.com) at 2026-07-01 22:26:35
| 1 | #!/usr/bin/perl | |
| 2 | #====================================================================== | |
| 3 | # WebSTLs -- topbar Messages pill live-poll data feed. | |
| 4 | # | |
| 5 | # Returns JSON { "n": <count> } | |
| 6 | # - For admins: total support_threads with admin_unread > 0 plus | |
| 7 | # external_comments (status='new', is_reply=0) on their own | |
| 8 | # storefront. | |
| 9 | # - For regular users: their own support_threads with | |
| 10 | # customer_unread > 0 plus external_comments tied to them. | |
| 11 | # | |
| 12 | # Polled every ~20s by the wrapper template so the pill blinks | |
| 13 | # without a page refresh. | |
| 14 | #====================================================================== | |
| 15 | use strict; | |
| 16 | use warnings; | |
| 17 | ||
| 18 | use lib '/var/www/vhosts/webstls.com/httpdocs'; | |
| 19 | use CGI; | |
| 20 | use MODS::Login; | |
| 21 | use MODS::DBConnect; | |
| 22 | use MODS::WebSTLs::Config; | |
| 23 | ||
| 24 | my $auth = MODS::Login->new; | |
| 25 | my $userinfo = $auth->login_verify(); | |
| 26 | ||
| 27 | my $n = 0; | |
| 28 | if ($userinfo && $userinfo->{user_id}) { | |
| 29 | my $uid = $userinfo->{user_id}; | |
| 30 | $uid =~ s/[^0-9]//g; | |
| 31 | my $is_admin = $userinfo->{is_admin} || $userinfo->{_admin_is_admin} || 0; | |
| 32 | my $db = MODS::DBConnect->new; | |
| 33 | my $cfg = MODS::WebSTLs::Config->new; | |
| 34 | my $DB = $cfg->settings('database_name'); | |
| 35 | my $dbh = $db->db_connect(); | |
| 36 | if ($dbh) { | |
| 37 | # 1) external_comments (guarded against pre-migration deploys) | |
| 38 | my $have_ec = $db->db_readwrite($dbh, qq~ | |
| 39 | SELECT COUNT(*) AS n FROM information_schema.tables | |
| 40 | WHERE table_schema='$DB' AND table_name='external_comments' | |
| 41 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 42 | if ($have_ec && $have_ec->{n}) { | |
| 43 | my $r = $db->db_readwrite($dbh, qq~ | |
| 44 | SELECT COUNT(*) AS n FROM `${DB}`.external_comments | |
| 45 | WHERE user_id='$uid' AND status='new' AND is_reply=0 | |
| 46 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 47 | $n += ($r && $r->{n}) ? $r->{n} : 0; | |
| 48 | } | |
| 49 | ||
| 50 | # 2) support_threads | |
| 51 | my $have_st = $db->db_readwrite($dbh, qq~ | |
| 52 | SELECT COUNT(*) AS n FROM information_schema.tables | |
| 53 | WHERE table_schema='$DB' AND table_name='support_threads' | |
| 54 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 55 | if ($have_st && $have_st->{n}) { | |
| 56 | my $sql = $is_admin | |
| 57 | ? qq~SELECT COUNT(*) AS n FROM `${DB}`.support_threads WHERE admin_unread > 0~ | |
| 58 | : qq~SELECT COUNT(*) AS n FROM `${DB}`.support_threads WHERE customer_user_id='$uid' AND customer_unread > 0~; | |
| 59 | my $r = $db->db_readwrite($dbh, $sql, $ENV{SCRIPT_NAME}, __LINE__); | |
| 60 | $n += ($r && $r->{n}) ? $r->{n} : 0; | |
| 61 | } | |
| 62 | $db->db_disconnect($dbh); | |
| 63 | } | |
| 64 | } | |
| 65 | ||
| 66 | print "Content-Type: application/json; charset=utf-8\n"; | |
| 67 | print "Cache-Control: no-store, max-age=0\n"; | |
| 68 | print "\n"; | |
| 69 | print qq~{"n":$n}\n~; |