跳到正文
DEV Community· Aditya Pandey·· 1 小時前AI 評分47

Kahan Chalein?:以連結作資料庫的免後端羣組投票

The link is the database: group voting with no backend

AI 導讀

Kahan Chalein? 以 URL 傳遞羣組投票狀態,讓用戶毋須伺服器、資料庫或登入,便可共同挑選行程。每組投票涵蓋 10 條行程,最多容納 15 名投票者;每次轉發連結都會帶上完整狀態,開啟最新連結即可查看投票結果。

正文

Aditya Pandey

Every Friday the same question shows up in Indian group chats: kahan chalein? (where do we go?). Five people, five opinions, and the plan dies in the chat.

I had 72 hours for a build sprint, so I built Kahan Chalein?: pick your city, and it shows weekend trips with this weekend's live weather, a rough cost and an honest "skip it if". The part I want to write about is the group mode: everyone swipes the same 10 trips, and the app shows where the group matches. There is no server, no database and no login.

The trick: the plan lives in the URL

A group plan is a tiny object:

// c = city, d = days, k = the 10 trip ids, v = votes per person (one bit per trip)
{ c: "blr", d: 2, k: ["hampi", "gokarna", "coorg", ...], v: { "Aditya": "1011000110" } }

That object is JSON, base64url-encoded, and put in ?g=:

const b64 = s => btoa(String.fromCharCode(...new TextEncoder().encode(s)))
  .replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
const link = plan => `${location.origin}${location.pathname}?g=${b64(JSON.stringify(plan))}`;

The flow:

  1. I swipe my 10 cards. My votes become a bit string like "1011000110".
  2. I send the link to the group chat.
  3. A friend opens it, sees "Aditya is planning a weekend from Bengaluru", and swipes the same 10 cards.
  4. Their votes are added to the object, the URL is rewritten with history.replaceState, and they send the new link back.
  5. Anyone who opens the latest link sees the tally: the top match, who voted for what, and the trips nobody picked.

The link is the database. Each forward carries the whole state.

Don't trust the link

Anyone can edit a URL, so decoding validates everything before using it (slightly simplified):

function decode(code) {
  try {
    const o = JSON.parse(unb64(code));
    if (!CITIES[o.c] || ![1, 2, 3].includes(+o.d) || !Array.isArray(o.k)) return null;
    const k = o.k.filter(id => byId(id) && byId(id).from[o.c]).slice(0, 10);
    if (k.length < 2 || k.length !== o.k.length) return null;
    const v = {};
    for (const [name, bits] of Object.entries(o.v || {}).slice(0, 15)) {
      const n = String(name).trim().slice(0, 20);
      if (n && /^[01]+$/.test(bits) && bits.length === k.length) v[n] = bits;
    }
    return { c: o.c, d: +o.d, k, v };
  } catch { return null; }
}

Unknown trip ids, wrong-length bit strings, long names and too many voters are all dropped, and names are only ever rendered with textContent.

Trade-offs

  • Concurrent votes can fork. If two friends vote on the same old link at once, there are two "latest" links. In a group chat this is fine in practice: people reply to the newest message. A server would fix it, but then it's no longer free and instant.
  • URL length. 10 trips and up to 15 voters keep the link short enough for WhatsApp.
  • No privacy problem to manage. There's no stored data at all; the votes exist only in the messages people send each other.

The rest of the stack

  • Static HTML, CSS and vanilla JS on GitHub Pages. No framework, no build step.
  • Weather: Open-Meteo accepts comma-separated coordinates, so the forecast for about 20 places is a single request, cached for the session.
  • Postcard art is SVG generated from a seeded random generator, so each place always gets the same picture and there are no stock photos.
  • Story images for Instagram and WhatsApp are drawn on a 1080×1920 canvas and shared with the Web Share API.
  • Analytics with GoatCounter (no cookies), and the numbers are public: live numbers.

I built it with an AI coding assistant, and I can walk through every line. The code is on GitHub.

Try it, and if you're in India, send the group swipe to your friends this weekend: https://itzaditya43.github.io/kahan-chalein/?ref=devto

Feedback very welcome, especially on the "link as database" idea.

來源:DEV Community · dev.to