Diff -- /var/www/vhosts/webstls.com/httpdocs/messages_pending.cgi
Diff

/var/www/vhosts/webstls.com/httpdocs/messages_pending.cgi

added on WebSTLs (webstls.com) at 2026-07-01 22:26:35

Added
+69
lines
Removed
-0
lines
Context
0
unchanged
Blobs
from
to 3233d91f5213
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# 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#======================================================================
15use strict;
16use warnings;
17
18use lib '/var/www/vhosts/webstls.com/httpdocs';
19use CGI;
20use MODS::Login;
21use MODS::DBConnect;
22use MODS::WebSTLs::Config;
23
24my $auth = MODS::Login->new;
25my $userinfo = $auth->login_verify();
26
27my $n = 0;
28if ($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
66print "Content-Type: application/json; charset=utf-8\n";
67print "Cache-Control: no-store, max-age=0\n";
68print "\n";
69print qq~{"n":$n}\n~;
Keyboard: j next diff k previous diff g top G bottom r restore c compile-check ? help