@magpie-community/opencode-qoder-auth 0.2.16 → 0.2.17

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.
Files changed (3) hide show
  1. package/README.md +4 -1
  2. package/index.mjs +68 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -201,7 +201,10 @@ The hosts and reply shapes come from a real VPC account's report
201
201
  Qoder gives credits for a daily claim (the Qoder client's campaign
202
202
  `CLAIM_BENEFIT`, 100 Credits a day as reported), on both sites. magpie can
203
203
  claim it for each account when its Qoder check-in switch is on
204
- (Settings). It asks these pages through this plugin's fetch (since 0.2.7),
204
+ (Settings), through `auth.checkin`: an already claimed campaign is `done`,
205
+ a campaign not available is `inactive`, and a claimable one is `claimed`
206
+ with its benefit amount. It asks these pages through this plugin's own
207
+ campaigns path (since 0.2.7),
205
208
  which sends them as the account, on the device token (`Bearer`,
206
209
  `Cosy-ClientType: 10`) as usage is read; a refused device token is
207
210
  rotated once and the page asked again.
package/index.mjs CHANGED
@@ -1618,6 +1618,73 @@ const makePlugin = (site) => async ({ client }) => {
1618
1618
  }
1619
1619
  }
1620
1620
 
1621
+ // checkin claims the daily credits through the same campaigns page as
1622
+ // Qoder's client. A campaign already claimed is done; one not available
1623
+ // is inactive, never an error that magpie would retry all day.
1624
+ const checkin = async (getAuth) => {
1625
+ let cred
1626
+ try {
1627
+ cred = await fresh(getAuth)
1628
+ } catch (e) {
1629
+ return { outcome: "failed", message: e?.message ?? String(e), ...(e?.expired ? { signIn: "expired" } : {}) }
1630
+ }
1631
+ const base = campaignTarget(site, siteOf(site, cred), site.openapi + CAMPAIGNS)
1632
+ if (!base) return { outcome: "inactive", message: "no campaigns page for this account" }
1633
+ try {
1634
+ const machine = site.id === "qoder" ? await machineForCampaign(cred.uid) : {}
1635
+ const call = async (url, method, body) => {
1636
+ let res = await campaignCall(url, method, body, cred.deviceToken, machine)
1637
+ if (res.status === 401 || res.status === 403) {
1638
+ const token = await deviceToken(getAuth, cred.deviceToken)
1639
+ const current = latest(await getAuth())
1640
+ cred = { ...current, deviceToken: token }
1641
+ res = await campaignCall(url, method, body, token, machine)
1642
+ }
1643
+ return res
1644
+ }
1645
+ const listRes = await call(base, "GET")
1646
+ let list = {}
1647
+ let listRaw = ""
1648
+ try {
1649
+ listRaw = await listRes.text()
1650
+ list = JSON.parse(listRaw) ?? {}
1651
+ } catch {
1652
+ return { outcome: "failed", message: `campaigns: unreadable reply ${listRaw.trim().slice(0, 120) || "(empty)"}` }
1653
+ }
1654
+ if (listRes.status !== 200) return { outcome: "failed", message: `campaigns: HTTP ${listRes.status}` }
1655
+ const now = Date.now()
1656
+ const active = (c) => (!c.startAt || c.startAt * 1000 <= now) && (!c.endAt || c.endAt * 1000 > now)
1657
+ const campaigns = (Array.isArray(list.campaigns) ? list.campaigns : []).filter((x) => x?.actionType === "CLAIM_BENEFIT")
1658
+ const campaign = campaigns.find((x) => x.claimStatus === "CLAIMABLE")
1659
+ if (!campaign) {
1660
+ const done = campaigns.find((x) => x.claimStatus === "CLAIMED" && active(x))
1661
+ if (done) {
1662
+ const amount = Number(done.benefit?.amount)
1663
+ return { outcome: "done", ...(Number.isFinite(amount) && amount > 0 ? { credit: amount } : {}) }
1664
+ }
1665
+ return campaigns.length ? { outcome: "failed", message: "campaigns: today's credits aren't offered yet" } : { outcome: "inactive" }
1666
+ }
1667
+ const claimRes = await call(base.replace(/\/$/, "") + "/" + encodeURIComponent(campaign.campaignId) + "/claim", "POST", "{}")
1668
+ let claim = {}
1669
+ let claimRaw = ""
1670
+ try {
1671
+ claimRaw = await claimRes.text()
1672
+ claim = JSON.parse(claimRaw) ?? {}
1673
+ } catch {
1674
+ return { outcome: "failed", message: `claim: unreadable reply ${claimRaw.trim().slice(0, 120) || "(empty)"}` }
1675
+ }
1676
+ const said = claim?.status ?? claim?.data?.status
1677
+ if (claimRes.status !== 200) return { outcome: "failed", message: `claim: HTTP ${claimRes.status}` }
1678
+ if (said === "CLAIMED") {
1679
+ const amount = Number(claim?.benefit?.amount ?? claim?.data?.benefit?.amount ?? campaign.benefit?.amount)
1680
+ return { outcome: "claimed", ...(Number.isFinite(amount) && amount > 0 ? { credit: amount } : {}) }
1681
+ }
1682
+ return { outcome: "failed", message: `claim: ${said || "unknown answer"}` }
1683
+ } catch (e) {
1684
+ return { outcome: "failed", message: e?.message ?? String(e), ...(e?.expired ? { signIn: "expired" } : {}) }
1685
+ }
1686
+ }
1687
+
1621
1688
  const signedInError = (e) =>
1622
1689
  errorResponse({
1623
1690
  status: e instanceof SignInGone ? 401 : 502,
@@ -1774,6 +1841,7 @@ const makePlugin = (site) => async ({ client }) => {
1774
1841
  : []),
1775
1842
  ],
1776
1843
  usage,
1844
+ checkin,
1777
1845
  },
1778
1846
  async config(config) {
1779
1847
  config.provider ??= {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-qoder-auth",
3
- "version": "0.2.16",
3
+ "version": "0.2.17",
4
4
  "description": "Qoder subscriptions in OpenCode and magpie: Qoder (qoder.com) and Qoder CN (qoder.cn)",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",