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