Something went wrong. Try again.
Schedule posts and reposts to Bluesky (and other PDSes) with Cloudflare workers. skyscheduler.work
scheduling social-media cloudflare bsky service cloudflare-workers bsky-tool bluesky
Something went wrong. Try again.
1.8 kB · 68 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869import { USE_INVITE_KEYS } from "../config";import type { AllContext } from "../types";import { has } from "./helpers";
export const isUsingInviteKeys = (c: AllContext): boolean => { return has(c.env, "INVITE_POOL") && USE_INVITE_KEYS;};
export const doesInviteKeyHaveValues = async (c: AllContext, inviteKey: string | undefined): Promise<boolean> => { if (isUsingInviteKeys(c)) { if (inviteKey === undefined) return false;
const value = await c.env.INVITE_POOL.get(inviteKey); // Key does not exist if (value === null) return false;
// check the amount we have const amount: number = parseInt(value);
// nans are failure if (isNaN(amount)) return false;
// -1 means infinite invites if (amount === -1 || amount >= 1) return true; else return false; } return true;};
export const consumeInviteKey = async (c: AllContext, inviteKey: string | undefined) => { if (isUsingInviteKeys(c)) { if (inviteKey === undefined) return;
const value = await c.env.INVITE_POOL.get(inviteKey); if (value === null) { console.error(`attempted to use invite key ${inviteKey} but is invalid`); return; } // check the amount we have const amount: number = parseInt(value);
// handle NaN if (isNaN(amount)) { console.warn(`${inviteKey} has the value of ${value} which triggers NaN.`); return; }
// -1 has infinite invites if (amount === -1) return;
const newValue: number = amount - 1; // Delete any keys that fall to 0, they should be removed from the db if (newValue <= 0) { await c.env.INVITE_POOL.delete(inviteKey); return; }
// put the new value on the stack await c.env.INVITE_POOL.put(inviteKey, newValue.toString()); }};