# ctx.pub

Public JSON other people using your app can read back — leaderboards, streaks, things people hide for each other.

pub

## Calls

`ctx.pub`

```ts
await ctx.pub.get();              // your value, or undefined
await ctx.pub.set(value);         // replaces your whole value — any JSON value but null, up to 4 KB
await ctx.pub.delete();
await ctx.pub.list(query);        // everyone's values for this app
await ctx.pub.rank(query);        // your place in that list — { rank, total } or null. The query needs an order
```

One value per person per app. Only its owner writes it; everyone who uses the same app can read it. Store one object and keep what you need inside it — a best score, this week’s run, a few things you hid.

**Reading works without an account.** `list()` answers even when nobody’s signed in, or when the person hasn’t added your app yet — other people’s rows, with `initial` and `color` as always. Everything else — `get()`, `set()`, `delete()`, `rank()`, `report()` — needs an account, and `ctx.can('pub')` tells you whether you have one. So show the board to everyone and only ask people to sign in when they want on it.

## A weekly board

`src/index.ts`

```ts
Fave1.defineApp({
  async onLaunch(ctx) {
    const week = isoWeek(new Date()); // '2026-W41' — in UTC, so everyone shares one week
    const mine = ctx.can('pub'); // signed in and not switched off — only then can you post

    // Call this when a round ends. The first post on a site asks once — Not now is a normal answer.
    const post = (best: number) => ctx.pub.set({ week, best }).catch((e) => {
      if (e.code !== 'cancelled') ctx.ui.toast('Couldn’t post your score', 'error');
    });

    // The board shows for everyone, signed in or not. Rows never carry a name — just an initial and a color.
    const top = await ctx.pub.list({ where: { week }, order: '-best', limit: 10 });
    const me = mine ? await ctx.pub.rank({ where: { week }, order: '-best' }) : null;
    ctx.ui.render([
      ctx.ui.heading('This week'),
      ...top.map((r, i) => ctx.ui.text(`${i + 1}. ${r.me ? 'You' : r.initial} — ${r.value.best}`)),
      me ? ctx.ui.muted(`You're #${me.rank} of ${me.total}`)
        : ctx.ui.muted(mine ? 'Play a round to get on it.' : 'Sign in to Fave1 to get on the board.'),
    ]);
  },
});

// The ISO week, in UTC — e.g. '2026-W41'.
function isoWeek(d: Date) {
  const t = new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()));
  t.setUTCDate(t.getUTCDate() + 4 - (t.getUTCDay() || 7));
  const jan1 = new Date(Date.UTC(t.getUTCFullYear(), 0, 1));
  const n = Math.ceil(((t.getTime() - jan1.getTime()) / 86400000 + 1) / 7);
  return `${t.getUTCFullYear()}-W${String(n).padStart(2, '0')}`;
}
```

## Asking for the right rows

| Field | What it does | Default |
| --- | --- | --- |
| `where` | Rows whose value **contains** this JSON. `{ week: '2026-W40' }` matches that week. For arrays, `{ hides: [{ site: 'amazon.com' }] }` matches anyone with at least one hide on amazon.com. Up to 2 KB. | every row |
| `order` | A field to sort by: `'best'`, `'-best'` for largest first, `'stats.best'` to go deeper. When a key has a dot in it, pass a path: `['runs', 'amazon.com', 'ms']`. Up to 6 levels deep, each part 1–64 characters. A tie goes to whoever wrote first. | newest first |
| `desc` | Largest first — `'-best'` already says it. | false |
| `limit` | 1 to 100 rows. | 50 |

## What a row looks like

`a row`

```ts
{
  id: 'f3a91c0d2b7e44a1c9d0e2b7', // opaque — the same person has another id in another app
  initial: 'M',                   // one letter for an avatar — yours included. Rows never carry a name
  color: 'linear-gradient(…)',    // their avatar color, ready for CSS background
  avatar: 'data:image/svg+xml,…', // the avatar they picked in Fave1 — put it in <img src>
  avatarStill: 'data:image/svg+xml,…', // the same avatar, standing still — for lists and small circles
  value: { week: '2026-W40', best: 18420 },
  updatedAt: '2026-09-28T14:02:11.000Z',
  me: false,
}
```

**No row has a name.** What people post is public, so Fave1 shows it with a first letter and an avatar only — the same promise people read in Fave1’s Privacy Policy. Label rows by what they posted (the site, the score, the collection), and show “You” on your own.

**Show the avatar they picked.** People choose a shape or a letter, a color and a pattern for their Fave1 avatar — never a photo. `avatar` is that picture, drawn by Fave1, so an `<img src={row.avatar} alt="">` shows it, and it looks the same in your app as everywhere else in Fave1. On someone else’s site the page’s own rules can block `data:` pictures — so lay the image over a circle painted with `color` and the first letter, the way Fave1’s own apps do, and that circle is what people see when the picture can’t load.

**Small circles stand still.** Fave1 Plus members can give their avatar a moving effect — a shine, hearts, a rainbow ring. `avatar` moves; `avatarStill` is the same picture standing still. Use `avatarStill` in lists and anywhere the circle is under about 40px — a leaderboard of moving avatars is busy to look at and heavy to draw. Save `avatar` for the one big circle, like the person a page is about.

`a person, picture over letter`

```
<span class="av" style="background:${row.color}">${row.initial}<img src="${row.avatarStill}" alt=""></span>

.av { position: relative; overflow: hidden; width: 28px; height: 28px; border-radius: 50%;
      display: grid; place-items: center; color: #fff; font-weight: 800; }
.av > img { position: absolute; inset: 0; width: 100%; height: 100%; }
```

**Some sites come back with emoji in them.** If a string in a row is just a site or a web address from a site we keep off public posts (adult sites, for one), it arrives as that site’s host with the blocked part and the ending swapped for emoji — `sitename.com` turns into something like `🦄🍭✨🌈🫧🌸🍬🎀.🙈🍦🧁`, and the path is dropped. That happens on everyone’s rows, yours too. A sentence that mentions a site is left alone. Your own `get()` is never changed, so what you write back stays as it was.

**On a site that hasn’t said yes yet, your own row looks like anyone else’s.** Until this site allows your app — the first post asks — `list()` still has your row but with `me: false`, and `rank()` gives `null`, so the page can’t pick out your id. Check [ctx.user.granted](https://developers.fave1.com/docs/ctx-user.md#granted) before you tell someone they’re not on the board. In app mode and on sites that said yes, it’s all there.

## The first post from a site

The first time your app writes from a site, Fave1 asks once, right in your panel: **Post to \<your app’s name>?** The button is Fave1’s own — the page can’t press it for them, and it can’t post in their name. Every write after that, on that site, goes straight through, and the same answer covers [ctx.storage](https://developers.fave1.com/docs/ctx-storage.md), [ctx.files](https://developers.fave1.com/docs/ctx-files.md), [ctx.shared](https://developers.fave1.com/docs/ctx-shared.md) and a [ctx.live](https://developers.fave1.com/docs/ctx-live.md) room (whichever asked first, that answer covers the rest). On your app’s own page (`<id>.fave1.app`) it never asks.

So `set()` can take as long as a person takes to decide. Show that you’re posting, and treat `cancelled` as a normal answer.

## Limits

- **Per value** (4 KB) — Measured as JSON text. It’s copied to everyone who opens your app, so it’s small on purpose.
- **Writes in a row** (merged) — Call `set()` as often as you like — calls close together are merged and only the last value is sent.

## Errors

| Code | What happened | Try |
| --- | --- | --- |
| `signed-out` | Nobody’s signed in to Fave1 here | fold the feature |
| `denied` | `pub` isn’t in your manifest, or they switched it off | ctx.perms.open() |
| `not-installed` | They haven’t added your app — a sideloaded run | say so |
| `cancelled` | They chose Not now, or closed the app while asked | leave it |
| `too-large` | The value is over 4 KB | trim it |
| `invalid` | Not JSON, or a query we can’t read | fix it |
| `unreachable` | This page blocks Fave1 from reaching their account | say so |
| `rate` | Writes closer together than Fave1 takes them — the runtime already retried once. Or, for someone signed out (or who hasn’t added your app), more than 120 `list()` calls a minute from one network | wait, then retry |
| `timeout` | No answer in 15 seconds | retry |
| `server` | Fave1 had a problem on its side | retry later |

## While you build

> **DEV builds start in a sandbox.** Run from `fave1 dev` or a review link, `ctx.pub` keeps your value in this browser and lists only your own row — a build in progress never lands on a real board. With Data on [My account](https://developers.fave1.com/docs/quickstart.md#dev-account) it writes to the DEV board, which only your team’s DEV builds share — never the live one.

## When it goes away

Switching `pub` off or removing the app clears that person’s value, and closes the door to [ctx.live](https://developers.fave1.com/docs/ctx-live.md) rooms — the same switch. Your store listing has to say that others can see what they post. Anyone can write anything into their own value — boards are for bragging, not prizes.

## Reporting

If your app shows what other people posted — a board, a note left on a site — give them a way to report it. One call; Fave1 looks at every report, and whoever posted it is never told who reported.

`src/index.ts`

```ts
await ctx.pub.report(row.id, 'inappropriate');           // reasons: inappropriate · scam · exposes_personal_data · other — anything else is sent as other
await ctx.pub.report(row.id, 'other', 'Says it is a giveaway');   // an optional note, up to 500 chars
```

On a third-party site the first report asks once in your panel (**Report this on …?**), like posting does. Reviewers check for this on apps that render other people’s text.
