@lynn123411/dsh-a6api 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -892,6 +892,66 @@ async function fetchChannelDetails(channelId, userId, sessionCookie, targetModel
892
892
  }
893
893
  return null;
894
894
  }
895
+ async function fetchPriceFluctuation(userId, sessionCookie, accessToken) {
896
+ const token = (accessToken || sessionCookie || "").trim();
897
+ const uid = (userId || "").trim();
898
+ if (!uid && !token) {
899
+ return { pendingCount: 0, unseenCount: 0, totalCount: 0, authError: false };
900
+ }
901
+ const headers = buildWebHeaders(uid || void 0, token || void 0);
902
+ const url = "https://a6api.com/api/marketplace/price-notices";
903
+ try {
904
+ const res = await fetch(url, { headers, signal: AbortSignal.timeout(8e3) });
905
+ if (res.status === 401 || res.status === 403) {
906
+ console.warn("[dsh-a6api] fetchPriceFluctuation auth failed", res.status);
907
+ return { pendingCount: 0, unseenCount: 0, totalCount: 0, authError: true };
908
+ }
909
+ if (!res.ok) {
910
+ console.warn("[dsh-a6api] fetchPriceFluctuation HTTP", res.status);
911
+ return { pendingCount: 0, unseenCount: 0, totalCount: 0 };
912
+ }
913
+ const json = await res.json().catch(() => null);
914
+ if (!json) return { pendingCount: 0, unseenCount: 0, totalCount: 0 };
915
+ if (json.success === false) return { pendingCount: 0, unseenCount: 0, totalCount: 0 };
916
+ let arr = [];
917
+ if (Array.isArray(json)) arr = json;
918
+ else if (Array.isArray(json.data)) arr = json.data;
919
+ else if (Array.isArray(json.data?.notices)) arr = json.data.notices;
920
+ else if (Array.isArray(json.data?.items)) arr = json.data.items;
921
+ else if (Array.isArray(json.notices)) arr = json.notices;
922
+ else if (Array.isArray(json.items)) arr = json.items;
923
+ const pickWithPresent = (keys) => {
924
+ for (const k of keys) {
925
+ const v = json?.data?.[k] ?? json?.[k];
926
+ if (v !== void 0 && v !== null) {
927
+ const n = Number(v);
928
+ if (!Number.isNaN(n)) return { value: n, present: true };
929
+ }
930
+ }
931
+ return { value: 0, present: false };
932
+ };
933
+ const pendingPick = pickWithPresent(["pendingCount", "pending_count", "pending", "openCount"]);
934
+ const unseenPick = pickWithPresent(["unseenCount", "unseen_count", "unseen", "has_unseen_count"]);
935
+ let pending = pendingPick.value;
936
+ let unseen = unseenPick.value;
937
+ const total = arr.length;
938
+ if (!pendingPick.present && arr.length > 0) {
939
+ const counted = arr.filter((n) => {
940
+ const s = String(n.state || n.status || "").toLowerCase();
941
+ return s === "open" || s === "pending" || n.pending === true;
942
+ }).length;
943
+ const hasState = arr.some((n) => n.state !== void 0 || n.status !== void 0);
944
+ if (hasState) pending = counted;
945
+ }
946
+ if (!unseenPick.present && arr.length > 0) {
947
+ unseen = arr.filter((n) => n.has_unseen === true || n.hasUnseen === true || n.unseen === true || n.is_unread === true).length;
948
+ }
949
+ return { pendingCount: pending, unseenCount: unseen, totalCount: total, notices: arr };
950
+ } catch (err) {
951
+ console.warn("[dsh-a6api] fetchPriceFluctuation error", err);
952
+ return { pendingCount: 0, unseenCount: 0, totalCount: 0 };
953
+ }
954
+ }
895
955
 
896
956
  // src/server/probe.ts
897
957
  var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
@@ -1494,6 +1554,17 @@ function apply(ctx) {
1494
1554
  const dshConfiguredModels = await getDshConfiguredModels();
1495
1555
  return sendJson(res, 200, { ok: true, dshConfiguredModels });
1496
1556
  }
1557
+ if (pathname === "/price-fluctuation" && (req.method === "GET" || req.method === "HEAD")) {
1558
+ const config = await readPluginConfig();
1559
+ const token = config.accessToken || config.sessionCookie || "";
1560
+ if (!token || !config.userId) {
1561
+ return sendJson(res, 200, { ok: true, data: { pendingCount: 0, unseenCount: 0, totalCount: 0, hasAuth: false, authError: false, updatedAt: Date.now() } });
1562
+ }
1563
+ const result = await fetchPriceFluctuation(config.userId, token, token);
1564
+ const { notices, ...counts } = result;
1565
+ const hasAuth = !counts.authError;
1566
+ return sendJson(res, 200, { ok: true, data: { pendingCount: counts.pendingCount, unseenCount: counts.unseenCount, totalCount: counts.totalCount, hasAuth, authError: Boolean(counts.authError), updatedAt: Date.now() } });
1567
+ }
1497
1568
  return sendJson(res, 404, { ok: false, error: "Not found" });
1498
1569
  } catch (err) {
1499
1570
  console.error("[dsh-a6api] API error:", err);