Diff -- /var/www/vhosts/3dshawn.com/abforge.3dshawn.com/admin_chat_pending.cgi

O Operator
Diff

/var/www/vhosts/3dshawn.com/abforge.3dshawn.com/admin_chat_pending.cgi

added on local at 2026-07-01 16:00:51

Added
+62
lines
Removed
-0
lines
Context
0
unchanged
Blobs
from
to 7b53f3f1dc73
Restore this content →
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# ABForge -- 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 abforge_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#======================================================================
17use strict;
18use warnings;
19
20use lib '/var/www/vhosts/abforge.com/httpdocs';
21use CGI;
22use MODS::Login;
23use MODS::DBConnect;
24use MODS::ABForge::Config;
25
26my $auth = MODS::Login->new;
27my $userinfo = $auth->login_verify();
28
29my $n = 0;
30my $first_id = 0;
31if ($userinfo && $userinfo->{user_id}
32 && ($userinfo->{is_admin} || $userinfo->{_admin_is_admin})) {
33 my $db = MODS::DBConnect->new;
34 my $cfg = MODS::ABForge::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
59print "Content-Type: application/json; charset=utf-8\n";
60print "Cache-Control: no-store, max-age=0\n";
61print "\n";
62print qq~{"n":$n,"first_id":$first_id}\n~;