Diff -- /var/www/vhosts/3dshawn.com/affiliate.3dshawn.com/admin_chat_pending.cgi
Diff
/var/www/vhosts/3dshawn.com/affiliate.3dshawn.com/admin_chat_pending.cgi
added on local at 2026-07-01 13:46:56
Added
+62
lines
Removed
-0
lines
Context
0
unchanged
Blobs
from
to 93c1a5b2c22c
to 93c1a5b2c22c
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.| 1 | #!/usr/bin/perl | |
| 2 | #====================================================================== | |
| 3 | # WebSTLs -- pulsing-pill data feed. | |
| 4 | # | |
| 5 | # Returns a tiny JSON payload: {"n": <count>} | |
| 6 | # | |
| 7 | # Where <count> is the number of OPEN support chats that have at | |
| 8 | # least one unread message for the admin. Polled every 20s from the | |
| 9 | # topbar pill (see webstls_wrapper.html) so admins see the count | |
| 10 | # update without refreshing whatever page they're on. | |
| 11 | # | |
| 12 | # Only admins get a real number. Anyone else (including unauth'd | |
| 13 | # requests) gets {"n":0} -- the pill is gated on $is_admin in the | |
| 14 | # wrapper so non-admins never render it anyway, but defence in depth | |
| 15 | # is free. | |
| 16 | #====================================================================== | |
| 17 | use strict; | |
| 18 | use warnings; | |
| 19 | ||
| 20 | use lib '/var/www/vhosts/3dshawn.com/affiliate.3dshawn.com'; | |
| 21 | use CGI; | |
| 22 | use MODS::Login; | |
| 23 | use MODS::DBConnect; | |
| 24 | use MODS::AffSoft::Config; | |
| 25 | ||
| 26 | my $auth = MODS::Login->new; | |
| 27 | my $userinfo = $auth->login_verify(); | |
| 28 | ||
| 29 | my $n = 0; | |
| 30 | my $first_id = 0; | |
| 31 | if ($userinfo && $userinfo->{user_id} | |
| 32 | && ($userinfo->{is_admin} || $userinfo->{_admin_is_admin})) { | |
| 33 | my $db = MODS::DBConnect->new; | |
| 34 | my $cfg = MODS::AffSoft::Config->new; | |
| 35 | my $DB = $cfg->settings('database_name'); | |
| 36 | my $dbh = $db->db_connect(); | |
| 37 | if ($dbh) { | |
| 38 | my $have = $db->db_readwrite($dbh, qq~ | |
| 39 | SELECT COUNT(*) AS n FROM information_schema.tables | |
| 40 | WHERE table_schema='$DB' AND table_name='support_chats' | |
| 41 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 42 | if ($have && $have->{n}) { | |
| 43 | # Single pass returns count + oldest waiting chat so the | |
| 44 | # pill can rewrite its href to land the admin directly on | |
| 45 | # the conversation that has been waiting longest. | |
| 46 | my @rows = $db->db_readwrite_multiple($dbh, qq~ | |
| 47 | SELECT id FROM `${DB}`.support_chats | |
| 48 | WHERE status = 'open' | |
| 49 | AND unread_for_admin > 0 | |
| 50 | ORDER BY last_message_at ASC | |
| 51 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 52 | $n = scalar(@rows); | |
| 53 | $first_id = $n ? ($rows[0]->{id} + 0) : 0; | |
| 54 | } | |
| 55 | $db->db_disconnect($dbh); | |
| 56 | } | |
| 57 | } | |
| 58 | ||
| 59 | print "Content-Type: application/json; charset=utf-8\n"; | |
| 60 | print "Cache-Control: no-store, max-age=0\n"; | |
| 61 | print "\n"; | |
| 62 | print qq~{"n":$n,"first_id":$first_id}\n~; |