Group photos by room with your own AI
Group a listing's photos by room with your own AI, through the enrichment hook and your own photo field, and show them as tabs in your gallery.
Ruuster doesn't label photos. With the extension points turned on for your account, your own AI can:
we send your hook each new or changed listing's photos, your code labels each photo's room, and we
return those labels to your site under custom. Your AI runs on your key, so you choose the model
and pay for it directly. Build on our data
explains each piece.
1. Define the field
Add a photo field to your account (with the extension points turned on): key room, type enum,
values kitchen, living_room, dining_room, bedroom, bathroom, exterior and other:
npx @ruuster/idx setup field room --target photo --type enum --values kitchen,living_room,dining_room,bedroom,bathroom,exterior,other.
Then register your hook with npx @ruuster/idx setup hook https://your-server/hook --write .env.local. A label your hook
answers outside that list is refused, so a model's typo never reaches your page.
2. The hook route
Deploy this at a public https address and give us the URL. It checks the signature, labels each
photo with your model and answers the labels.
packages/ruuster-idx-recipes/snippets/photos-by-room-hook.ts
// app/api/ruuster-enrich/route.ts: your enrichment hook. Ruuster POSTs a new or changed listing's
// photos here; your own AI labels each photo's room and you answer the labels. Ruuster stores them
// as your account's fields and returns them under `custom` on that listing (only to your account).
//
// Set RUUSTER_HOOK_SECRET to the hook's signing secret from the console. The AI call runs on YOUR key:
// swap labelRoom for your own model call (vision models take the photo's URL).
import { createHmac, timingSafeEqual } from "node:crypto";
type EnrichRequest = {
id: string;
kind: "listing.enrich";
reason: string;
listing: { id: string; mlsId: string; status: string | null; photos: { id: string; url: string; order: number }[] };
fields: { key: string; target: "listing" | "photo"; type: string; values?: string[] }[];
};
const ROOMS = ["kitchen", "living_room", "dining_room", "bedroom", "bathroom", "exterior", "other"] as const;
/** Ruuster-Signature: t=<unix>,v1=<hex> (HMAC-SHA256 of "<t>.<raw body>"), at most 5 minutes old. */
function verified(raw: string, header: string | null, secret: string): boolean {
const parts = new Map((header ?? "").split(",").map((p) => p.split("=", 2) as [string, string]));
const t = Number(parts.get("t"));
const sigs = (header ?? "").split(",").filter((p) => p.startsWith("v1=")).map((p) => p.slice(3));
if (!Number.isFinite(t) || Math.abs(Date.now() / 1000 - t) > 300 || !sigs.length) return false;
const want = Buffer.from(createHmac("sha256", secret).update(`${t}.${raw}`).digest("hex"));
return sigs.some((s) => s.length === want.length && timingSafeEqual(Buffer.from(s), want));
}
/** Your own AI. This placeholder answers "other"; call your vision model here instead. */
async function labelRoom(photoUrl: string): Promise<(typeof ROOMS)[number]> {
void photoUrl;
return "other";
}
export async function POST(req: Request): Promise<Response> {
const raw = await req.text();
if (!verified(raw, req.headers.get("ruuster-signature"), process.env.RUUSTER_HOOK_SECRET ?? "")) {
return new Response("bad signature", { status: 401 });
}
const body = JSON.parse(raw) as EnrichRequest;
// Only answer fields you defined: here a photo field "room" (enum) in the console.
if (!body.fields.some((f) => f.key === "room" && f.target === "photo")) return Response.json({});
const photos: Record<string, { room: string }> = {};
// Ruuster waits 8 seconds: label a handful at a time, or answer fast and label the rest next time.
for (const p of body.listing.photos.slice(0, 40)) photos[p.id] = { room: await labelRoom(p.url) };
return Response.json({ photos });
}
Each call has 8 seconds. A listing with many photos can be labelled a batch at a time: answer what you have, and the next change of the listing sends the rest.
3. The gallery
The listing answer carries the photos with their ids and your labels under custom.photos. This
component groups them into sections, with the photos you haven't labelled under "More photos".
packages/ruuster-idx-recipes/snippets/gallery-by-room.tsx
// components/gallery-by-room.tsx: the listing's photos grouped by one of your own photo fields
// ("room", set by your enrichment hook), as tabs. Photos without the field go under "More photos".
// Works on GET /v1/listings/{id} (data.photos and data.custom) and on /photos the same way.
import type { ListingCustomFields, ListingPhoto } from "@ruuster/idx";
const LABELS: Record<string, string> = {
kitchen: "Kitchen",
living_room: "Living room",
dining_room: "Dining room",
bedroom: "Bedrooms",
bathroom: "Bathrooms",
exterior: "Outside",
};
export function groupPhotosBy(photos: readonly ListingPhoto[], custom: ListingCustomFields | undefined, key: string): { group: string; photos: ListingPhoto[] }[] {
const valueOf = new Map((custom?.photos ?? []).filter((f) => f.key === key).map((f) => [f.photoId, String(f.value)] as const));
const groups = new Map<string, ListingPhoto[]>();
for (const p of photos) {
const g = (p.id && valueOf.get(p.id)) || "more";
groups.set(g, [...(groups.get(g) ?? []), p]);
}
// Your order: the labels above first, then anything else, "More photos" last.
const order = [...Object.keys(LABELS), ...Array.from(groups.keys()).filter((g) => !(g in LABELS) && g !== "more"), "more"];
return order.filter((g) => groups.has(g)).map((g) => ({ group: g, photos: groups.get(g) ?? [] }));
}
export function GalleryByRoom({ photos, custom }: { photos: readonly ListingPhoto[]; custom?: ListingCustomFields }) {
const groups = groupPhotosBy(photos, custom, "room");
return (
<div>
{groups.map(({ group, photos: list }) => (
<section key={group} aria-label={LABELS[group] ?? (group === "more" ? "More photos" : group)}>
<h3>
{LABELS[group] ?? (group === "more" ? "More photos" : group)} ({list.length})
</h3>
{list.map((p) => (
<img key={p.id ?? p.url} src={p.url} srcSet={p.sizes?.map((s) => `${s.url} ${s.width}w`).join(", ") || undefined} sizes="(min-width: 768px) 33vw, 100vw" alt={p.alt} loading="lazy" width={p.width ?? 800} height={p.height ?? 600} />
))}
</section>
))}
</div>
);
}
What to know
- The photos and their order come from the MLS and never change. Your labels sit beside them.
- When a listing's photos change, your hook is called again (with photo change events on), and its answer replaces the labels it gave before.
- Your labels are your account's alone. Show them as your own work, not as the MLS's.