JOHN SILLVESTOR commited on
Commit
2ecf90c
·
verified ·
1 Parent(s): 9dbbe1d

VIP accessLevel: longer Firebase lookup, do not cache missed VIP as registered

Browse files
Files changed (1) hide show
  1. server.js +22 -12
server.js CHANGED
@@ -442,15 +442,23 @@ async function rtdb(path, method, body) {
442
  return t ? JSON.parse(t) : null;
443
  }
444
 
 
 
445
  function vipActive(sub) {
 
446
  if (!sub || typeof sub !== "object") return false;
447
  const now = Math.floor(Date.now() / 1000);
448
- const exp = Number(sub.expiry || sub.expiresAt || 0);
449
- if (exp > 1e11) {
450
- if (exp <= Date.now()) return false;
451
- } else if (exp && exp <= now) return false;
 
 
 
 
452
  if (sub.status === "cancelled" || sub.status === "expired" || sub.status === "inactive") return false;
453
- return sub.status === "active" || sub.status === "subscribed" || sub.status === "vip" || sub.active === true || !!exp;
 
454
  }
455
 
456
  async function accessLevel(uid) {
@@ -458,11 +466,14 @@ async function accessLevel(uid) {
458
  const hit = lvlCache.get(uid);
459
  if (hit && Date.now() - hit.ts < LVL_TTL) return hit.lvl;
460
  let lvl = "registered";
 
461
  try {
462
- const sub = await withTime(rtdb("users/" + uid + "/subscription", "GET"), 900, null);
 
 
463
  if (vipActive(sub)) lvl = "subscribed";
464
  } catch {}
465
- lvlCache.set(uid, { lvl, ts: Date.now() });
466
  return lvl;
467
  }
468
 
@@ -787,7 +798,6 @@ function mediaHeaders(type, msgId, size, hash, rng) {
787
  return headers;
788
  }
789
 
790
-
791
  function pipeBudget(start, end) {
792
  const n = Math.max(1, end - start + 1);
793
  return Math.min(PIPE_MAX_MS, Math.max(PIPE_MS, Math.ceil(n / (128 * 1024)) * 1000));
@@ -1022,7 +1032,7 @@ async function handleGenerate(req, res, url) {
1022
  let uid = null;
1023
  let lvl = "anonymous";
1024
  try {
1025
- const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 1200, null);
1026
  if (idp) {
1027
  uid = idp.sub;
1028
  lvl = await accessLevel(uid);
@@ -1189,7 +1199,7 @@ async function handleVipStatus(req, res) {
1189
  if (denyHot(req, res)) return;
1190
  let lvl = "anonymous";
1191
  try {
1192
- const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 1200, null);
1193
  if (idp) {
1194
  lvl = await accessLevel(idp.sub);
1195
  let sub = null;
@@ -1205,7 +1215,7 @@ async function handleVipStart(req, res) {
1205
  if (!STRIPE_KEY) return json(res, 503, { error: "stripe_not_configured" });
1206
  if (!destOk(req, ["empty"])) return json(res, 403, { error: "forbidden" });
1207
  if (denyHot(req, res)) return;
1208
- const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 1200, null);
1209
  if (!idp) return json(res, 401, { error: "login" });
1210
  if (payLimited(idp.sub, clientIp(req))) {
1211
  res.setHeader("Retry-After", "30");
@@ -1254,7 +1264,7 @@ async function handleVipGo(req, res) {
1254
  if (!STRIPE_KEY) return json(res, 503, { error: "stripe_not_configured" });
1255
  if (!destOk(req, ["empty"])) return json(res, 403, { error: "forbidden" });
1256
  if (denyHot(req, res)) return;
1257
- const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 1200, null);
1258
  if (!idp) return json(res, 401, { error: "login" });
1259
  let body;
1260
  try {
 
442
  return t ? JSON.parse(t) : null;
443
  }
444
 
445
+ const LVL_MISS = { miss: 1 };
446
+
447
  function vipActive(sub) {
448
+ if (sub === true || sub === "active" || sub === "subscribed" || sub === "vip") return true;
449
  if (!sub || typeof sub !== "object") return false;
450
  const now = Math.floor(Date.now() / 1000);
451
+ const nowMs = Date.now();
452
+ const exp = Number(sub.expiry || sub.expiresAt || sub.expires || sub.expiration || sub.exp || 0);
453
+ const hasExp = Number.isFinite(exp) && exp > 0;
454
+ if (hasExp) {
455
+ if (exp > 1e11) {
456
+ if (exp <= nowMs) return false;
457
+ } else if (exp <= now) return false;
458
+ }
459
  if (sub.status === "cancelled" || sub.status === "expired" || sub.status === "inactive") return false;
460
+ if (sub.status === "active" || sub.status === "subscribed" || sub.status === "vip" || sub.active === true || sub.isVip === true || sub.vip === true || sub.subscribed === true) return true;
461
+ return hasExp;
462
  }
463
 
464
  async function accessLevel(uid) {
 
466
  const hit = lvlCache.get(uid);
467
  if (hit && Date.now() - hit.ts < LVL_TTL) return hit.lvl;
468
  let lvl = "registered";
469
+ let got = false;
470
  try {
471
+ const sub = await withTime(rtdb("users/" + uid + "/subscription", "GET"), 3500, LVL_MISS);
472
+ if (sub === LVL_MISS) return "registered";
473
+ got = true;
474
  if (vipActive(sub)) lvl = "subscribed";
475
  } catch {}
476
+ if (got) lvlCache.set(uid, { lvl, ts: Date.now() });
477
  return lvl;
478
  }
479
 
 
798
  return headers;
799
  }
800
 
 
801
  function pipeBudget(start, end) {
802
  const n = Math.max(1, end - start + 1);
803
  return Math.min(PIPE_MAX_MS, Math.max(PIPE_MS, Math.ceil(n / (128 * 1024)) * 1000));
 
1032
  let uid = null;
1033
  let lvl = "anonymous";
1034
  try {
1035
+ const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 4000, null);
1036
  if (idp) {
1037
  uid = idp.sub;
1038
  lvl = await accessLevel(uid);
 
1199
  if (denyHot(req, res)) return;
1200
  let lvl = "anonymous";
1201
  try {
1202
+ const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 4000, null);
1203
  if (idp) {
1204
  lvl = await accessLevel(idp.sub);
1205
  let sub = null;
 
1215
  if (!STRIPE_KEY) return json(res, 503, { error: "stripe_not_configured" });
1216
  if (!destOk(req, ["empty"])) return json(res, 403, { error: "forbidden" });
1217
  if (denyHot(req, res)) return;
1218
+ const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 4000, null);
1219
  if (!idp) return json(res, 401, { error: "login" });
1220
  if (payLimited(idp.sub, clientIp(req))) {
1221
  res.setHeader("Retry-After", "30");
 
1264
  if (!STRIPE_KEY) return json(res, 503, { error: "stripe_not_configured" });
1265
  if (!destOk(req, ["empty"])) return json(res, 403, { error: "forbidden" });
1266
  if (denyHot(req, res)) return;
1267
+ const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 4000, null);
1268
  if (!idp) return json(res, 401, { error: "login" });
1269
  let body;
1270
  try {