places: let an admin share the details users typed with the TREK Places API

Every shareable field of a place now records where its value came from: typed
by a user, filled by a provider, written by the assistant, a plugin or an
import. Only values a user typed are ever sent, and only after an admin agreed
to the current terms for the whole installation, field by field.

The new place-contrib domain keeps the decision in app settings, queues values
in an outbox a cron job sends from, logs what the service made of each one and
can withdraw everything this installation sent. Development, test and demo
instances, an own TREK_PLACES_URL, TREK_SERVICES=off and
TREK_PLACES_CONTRIBUTE=off never send anything. Index results carry the
community version, so the details caches refresh when it changes.
This commit is contained in:
Maurice
2026-10-30 18:15:24 +01:00
parent f56f6ae11e
commit 182ed56fa4
69 changed files with 5758 additions and 8 deletions
+1
View File
@@ -116,6 +116,7 @@ export * from './collection/collection-file.schema';
export * from './trip/trip.schema';
export * from './place/place-hours';
export * from './place/place-kind';
export * from './place/place-contrib.schema';
export * from './trip/day-grid';
export * from './trip-invite/trip-invite.schema';
export * from './collab/collab.schema';
@@ -0,0 +1,163 @@
import {
PLACE_CLIENT_ORIGINS,
PLACE_FIELD_ORIGINS,
placeContribDecisionRequestSchema,
placeContribErrorSchema,
placeContribImageAnswerSchema,
placeContribLogQuerySchema,
placeContribPlaceAnswerSchema,
placeContribPlaceBodySchema,
placeContribStateSchema,
placeContribWithdrawRequestSchema,
placeFieldOriginsSchema,
} from './place-contrib.schema';
import { placeCreateRequestSchema, placeUpdateRequestSchema } from './place.schema';
import { describe, expect, it } from 'vitest';
const SWITCHES = { website: true, phone: true, email: false, opening_hours: true, description: true, image: false };
describe('placeFieldOriginsSchema', () => {
it('takes a partial map of the client origins', () => {
expect(placeFieldOriginsSchema.safeParse({}).success).toBe(true);
expect(
placeFieldOriginsSchema.safeParse({ website: 'user', phone: 'google', description: 'enrichment' }).success,
).toBe(true);
});
it('refuses the origins only the server decides', () => {
for (const origin of ['agent', 'plugin', 'import', 'attachment', 'unknown']) {
expect(placeFieldOriginsSchema.safeParse({ website: origin }).success, origin).toBe(false);
}
});
it('keeps the client list a subset of the stored one', () => {
for (const origin of PLACE_CLIENT_ORIGINS) expect(PLACE_FIELD_ORIGINS).toContain(origin);
});
it('rides on the place create and update contracts and is checked there', () => {
expect(placeCreateRequestSchema.safeParse({ name: 'Café', fieldOrigins: { website: 'user' } }).success).toBe(true);
expect(placeUpdateRequestSchema.safeParse({ fieldOrigins: { phone: 'osm' } }).success).toBe(true);
expect(placeUpdateRequestSchema.safeParse({ fieldOrigins: { phone: 'agent' } }).success).toBe(false);
expect(placeUpdateRequestSchema.safeParse({ fieldOrigins: 'user' }).success).toBe(false);
});
});
describe('placeContribDecisionRequestSchema', () => {
it('takes each action with what it needs', () => {
expect(
placeContribDecisionRequestSchema.safeParse({ action: 'agree', termsVersion: 1, fields: SWITCHES }).success,
).toBe(true);
expect(placeContribDecisionRequestSchema.safeParse({ action: 'snooze' }).success).toBe(true);
expect(placeContribDecisionRequestSchema.safeParse({ action: 'decline', termsVersion: 1 }).success).toBe(true);
expect(
placeContribDecisionRequestSchema.safeParse({ action: 'fields', fields: SWITCHES, withdraw: true }).success,
).toBe(true);
expect(placeContribDecisionRequestSchema.safeParse({ action: 'off' }).success).toBe(true);
});
it('refuses an agreement without terms or with a partial set of switches', () => {
expect(placeContribDecisionRequestSchema.safeParse({ action: 'agree', fields: SWITCHES }).success).toBe(false);
expect(
placeContribDecisionRequestSchema.safeParse({ action: 'agree', termsVersion: 1, fields: { website: true } })
.success,
).toBe(false);
expect(placeContribDecisionRequestSchema.safeParse({ action: 'maybe' }).success).toBe(false);
});
});
describe('placeContribLogQuerySchema', () => {
it('coerces the query strings and defaults the page', () => {
expect(placeContribLogQuerySchema.parse({})).toEqual({ page: 1, perPage: 50 });
expect(placeContribLogQuerySchema.parse({ page: '2', perPage: '10', result: 'withheld', field: 'phone' })).toEqual({
page: 2,
perPage: 10,
result: 'withheld',
field: 'phone',
});
expect(placeContribLogQuerySchema.safeParse({ result: 'sent' }).success).toBe(false);
});
});
describe('placeContribWithdrawRequestSchema', () => {
it('needs the explicit confirmation', () => {
expect(placeContribWithdrawRequestSchema.safeParse({ confirm: true }).success).toBe(true);
expect(placeContribWithdrawRequestSchema.safeParse({}).success).toBe(false);
expect(placeContribWithdrawRequestSchema.safeParse({ confirm: 'yes' }).success).toBe(false);
});
});
describe('placeContribStateSchema', () => {
it('describes an undecided, eligible installation', () => {
const state = {
termsVersion: 1,
ceiling: 'admin',
eligible: true,
ineligibleReason: null,
consent: {
enabled: false,
fields: SWITCHES,
termsVersion: null,
decision: null,
decidedBy: null,
decidedAt: null,
},
showPrompt: true,
snoozedUntil: null,
active: false,
counts: { accepted: 0, duplicate: 0, rejected: 0, withheld: 0, retracted: 0 },
lastSuccessDay: null,
queue: { size: 0, cap: 5000, full: false },
pauseReason: null,
registrationOpen: false,
};
expect(placeContribStateSchema.safeParse(state).success).toBe(true);
});
});
describe('the central wire format', () => {
it('builds a place body of at most the shareable text fields', () => {
const body = {
v: 1,
termsVersion: 1,
cid: 'c0ffee-1234',
key: 'gers:08f2a1b3c4d5e6f7a8b9c0d1e2f3a4b5',
fields: { website: { value: 'https://cafe-luna.de/', baseHash: 'a91e3f00' } },
place: { name: 'Café Luna', lat: 54.08, lng: 12.13, country: 'DE' },
};
expect(placeContribPlaceBodySchema.safeParse(body).success).toBe(true);
expect(placeContribPlaceBodySchema.safeParse({ ...body, cid: 'short' }).success).toBe(false);
expect(
placeContribPlaceBodySchema.safeParse({ ...body, fields: { image: { value: 'x', baseHash: null } } }).success,
).toBe(false);
});
it('reads the answers the service gives', () => {
expect(
placeContribPlaceAnswerSchema.safeParse({
ok: true,
cid: 'c0ffee-1234',
duplicate: false,
key: 'osm:node/42',
community: { version: 3 },
results: { website: { result: 'published', reason: null, flags: [] }, phone: { result: 'review' } },
}).success,
).toBe(true);
expect(
placeContribPlaceAnswerSchema.safeParse({
ok: true,
cid: 'x',
duplicate: false,
key: 'k',
results: { website: { result: 'sent' } },
}).success,
).toBe(false);
expect(
placeContribImageAnswerSchema.safeParse({
ok: true,
result: { result: 'review', image: '0123456789abcdef0123456789abcdef', sameAs: false },
}).success,
).toBe(true);
expect(placeContribErrorSchema.safeParse({ error: 'field_not_accepted:email' }).success).toBe(true);
});
});
+320
View File
@@ -0,0 +1,320 @@
import { paginationQuerySchema } from '../common/pagination.schema';
import { z } from 'zod';
/**
* Community enrichment of place details: an installation sends the details its
* users typed in (never what a provider, the assistant, a plugin or an import
* filled) to the TREK Places API, so every installation gets better details
* over time. Off until an admin agrees, for the whole installation.
*
* Three groups live here: where a place's value came from (origin tracking,
* which the place write contracts carry), the admin surface under
* /api/admin/places-contribute, and the wire format of the central service,
* which the server validates every answer against.
*/
/** The place details that can be shared. `image` is the place's own picture, the `image_url` column. */
export const PLACE_CONTRIB_FIELDS = ['website', 'phone', 'email', 'opening_hours', 'description', 'image'] as const;
export const placeContribFieldSchema = z.enum(PLACE_CONTRIB_FIELDS);
export type PlaceContribField = z.infer<typeof placeContribFieldSchema>;
/**
* Where the current value of a field came from. Only `user` is ever shared, and
* only while the stored hash still matches the value. The server decides
* `agent` (MCP), `plugin` (plugin RPC), `import` (file and list imports) and
* `attachment` (a trip file made the picture); `unknown` is a changed value
* nobody vouched for.
*/
export const PLACE_FIELD_ORIGINS = [
'user',
'attachment',
'agent',
'plugin',
'import',
'google',
'amap',
'osm',
'trek',
'wikimedia',
'website',
'unsplash',
'enrichment',
'unknown',
] as const;
export const placeFieldOriginSchema = z.enum(PLACE_FIELD_ORIGINS);
export type PlaceFieldOrigin = z.infer<typeof placeFieldOriginSchema>;
/** The origins a client may report: the user, or the provider a search pick or a details lookup came from. */
export const PLACE_CLIENT_ORIGINS = [
'user',
'google',
'amap',
'osm',
'trek',
'wikimedia',
'website',
'unsplash',
'enrichment',
] as const;
export const placeClientOriginSchema = z.enum(PLACE_CLIENT_ORIGINS);
export type PlaceClientOrigin = z.infer<typeof placeClientOriginSchema>;
/**
* `fieldOrigins` on a place create or update: which fields of this write the
* user typed and which a provider filled. Optional and partial; a changed field
* without an entry is stored as `unknown`, and the server keeps the provider's
* origin when a value claimed as typed equals what a provider supplied.
*/
export const placeFieldOriginsSchema = z.object({
website: placeClientOriginSchema.optional(),
phone: placeClientOriginSchema.optional(),
email: placeClientOriginSchema.optional(),
opening_hours: placeClientOriginSchema.optional(),
description: placeClientOriginSchema.optional(),
image: placeClientOriginSchema.optional(),
});
export type PlaceFieldOrigins = z.infer<typeof placeFieldOriginsSchema>;
// ── Consent and the admin surface ───────────────────────────────────────────
/**
* The version of the contribution terms the admin agrees to. Raised when the
* text or the set of fields changes; sending stops until an admin agrees again.
*/
export const PLACE_CONTRIB_TERMS_VERSION = 1;
/** One switch per field. */
export const placeContribFieldSwitchesSchema = z.object({
website: z.boolean(),
phone: z.boolean(),
email: z.boolean(),
opening_hours: z.boolean(),
description: z.boolean(),
image: z.boolean(),
});
export type PlaceContribFieldSwitches = z.infer<typeof placeContribFieldSwitchesSchema>;
/**
* The installation's decision, as the `places_contribute` app setting holds it
* and the admin state answers it. `decision` is null until an admin chose,
* `declined` keeps the prompt away until the terms change, `off` is an
* agreement taken back.
*/
export const placeContribConsentSchema = z.object({
enabled: z.boolean(),
fields: placeContribFieldSwitchesSchema,
termsVersion: z.number().int().nonnegative().nullable(),
decision: z.enum(['agreed', 'declined', 'off']).nullable(),
decidedBy: z.number().int().positive().nullable(),
decidedAt: z.string().nullable(),
});
export type PlaceContribConsent = z.infer<typeof placeContribConsentSchema>;
/**
* Why this installation cannot send, checked in this order: the environment
* ceiling, a non-production or demo instance, the index switched off, an own
* TREK_PLACES_URL, TREK_SERVICES=off, no installation key (yet).
*/
export const placeContribIneligibleReasonSchema = z.enum([
'env_off',
'not_production',
'demo',
'index_off',
'custom_places_url',
'services_off',
'no_key',
]);
export type PlaceContribIneligibleReason = z.infer<typeof placeContribIneligibleReasonSchema>;
/** Why sending waits although the admin agreed: a refused key, or the service not taking contributions now. */
export const placeContribPauseReasonSchema = z.enum(['key_invalid', 'key_refused', 'not_accepting']);
export type PlaceContribPauseReason = z.infer<typeof placeContribPauseReasonSchema>;
/** What became of one field, as the transmission log keeps it. */
export const placeContribLogResultSchema = z.enum(['accepted', 'duplicate', 'rejected', 'withheld', 'retracted']);
export type PlaceContribLogResult = z.infer<typeof placeContribLogResultSchema>;
const count = z.number().int().nonnegative();
/** GET /api/admin/places-contribute */
export const placeContribStateSchema = z.object({
/** The terms an agreement has to name. */
termsVersion: z.number().int().positive(),
/** TREK_PLACES_CONTRIBUTE. */
ceiling: z.enum(['off', 'admin']),
eligible: z.boolean(),
ineligibleReason: placeContribIneligibleReasonSchema.nullable(),
consent: placeContribConsentSchema,
/** Whether this admin gets the prompt now: eligible, undecided for these terms, not snoozed. */
showPrompt: z.boolean(),
/** When this admin's "not now" ends (ISO), or null. */
snoozedUntil: z.string().nullable(),
/** Whether sending is on right now: eligible, agreed to the current terms, at least one field. */
active: z.boolean(),
/** Log rows of the last 30 days, per result. */
counts: z.object({ accepted: count, duplicate: count, rejected: count, withheld: count, retracted: count }),
/** The day (YYYY-MM-DD) of the last accepted field, or null. */
lastSuccessDay: z.string().nullable(),
queue: z.object({ size: count, cap: count, full: z.boolean() }),
pauseReason: placeContribPauseReasonSchema.nullable(),
/** Open registration: anyone can make an account and type details, which the prompt warns about. */
registrationOpen: z.boolean(),
});
export type PlaceContribState = z.infer<typeof placeContribStateSchema>;
/**
* PUT /api/admin/places-contribute/decision
*
* - `agree`: the prompt's "Agree", with the field switches it showed and the
* terms version it was built for (a stale dialog answers 409).
* - `snooze`: "Not now", for this admin, 14 days.
* - `decline`: "No"; the prompt comes back only with new terms.
* - `fields`: change the switches of an agreement; a field switched off drops
* its queued values, and with `withdraw` also what was sent of it.
* - `off`: the master switch off; the queue is emptied, and with `withdraw`
* everything this installation sent is withdrawn.
*/
export const placeContribDecisionRequestSchema = z.discriminatedUnion('action', [
z.object({
action: z.literal('agree'),
termsVersion: z.number().int().positive(),
fields: placeContribFieldSwitchesSchema,
}),
z.object({ action: z.literal('snooze') }),
z.object({ action: z.literal('decline'), termsVersion: z.number().int().positive() }),
z.object({
action: z.literal('fields'),
fields: placeContribFieldSwitchesSchema,
withdraw: z.boolean().optional(),
}),
z.object({ action: z.literal('off'), withdraw: z.boolean().optional() }),
]);
export type PlaceContribDecisionRequest = z.infer<typeof placeContribDecisionRequestSchema>;
/** GET /api/admin/places-contribute/log?page=&perPage=&result=&field= */
export const placeContribLogQuerySchema = paginationQuerySchema.extend({
result: placeContribLogResultSchema.optional(),
field: placeContribFieldSchema.optional(),
});
export type PlaceContribLogQuery = z.infer<typeof placeContribLogQuerySchema>;
/** One line of the transmission log. No user, trip or row ids: the place's key and name, the field, what it said. */
export const placeContribLogEntrySchema = z.object({
id: z.number().int().positive(),
day: z.string(),
key: z.string(),
placeName: z.string(),
field: placeContribFieldSchema,
valueSummary: z.string().nullable(),
result: placeContribLogResultSchema,
reason: z.string().nullable(),
});
export type PlaceContribLogEntry = z.infer<typeof placeContribLogEntrySchema>;
export const placeContribLogResponseSchema = z.object({
entries: z.array(placeContribLogEntrySchema),
total: count,
page: z.number().int().positive(),
perPage: z.number().int().positive(),
});
export type PlaceContribLogResponse = z.infer<typeof placeContribLogResponseSchema>;
/** POST /api/admin/places-contribute/withdraw: the confirmation the button sends. */
export const placeContribWithdrawRequestSchema = z.object({ confirm: z.literal(true) });
export type PlaceContribWithdrawRequest = z.infer<typeof placeContribWithdrawRequestSchema>;
/** What the service withdrew, per kind as it counts them, and how many queued values were dropped here. */
export const placeContribWithdrawResponseSchema = z.object({
withdrawn: z.record(z.string(), count),
cleared: count,
});
export type PlaceContribWithdrawResponse = z.infer<typeof placeContribWithdrawResponseSchema>;
// ── The central service's wire format ───────────────────────────────────────
/** What the service made of one field. */
export const placeContribFieldResultSchema = z.enum([
'published',
'checking',
'review',
'unchanged',
'rejected',
'quota',
]);
export type PlaceContribFieldResult = z.infer<typeof placeContribFieldResultSchema>;
/** The idempotency id of one request. */
export const placeContribCidSchema = z.string().regex(/^[A-Za-z0-9_-]{8,64}$/);
const shortText = z.string().max(200);
/** POST /v1/contrib/place: one place, the fields of it this installation's users typed in. */
export const placeContribPlaceBodySchema = z.object({
v: z.literal(1),
termsVersion: z.number().int().positive(),
cid: placeContribCidSchema,
key: z.string().max(80),
fields: z.partialRecord(
placeContribFieldSchema.exclude(['image']),
z.object({
value: z.string().min(1).max(4000),
baseHash: z
.string()
.regex(/^[0-9a-f]{8,64}$/)
.nullable(),
}),
),
place: z.object({
name: z.string().max(200),
lat: z.number().nullable(),
lng: z.number().nullable(),
country: z.string().length(2).nullable(),
}),
});
export type PlaceContribPlaceBody = z.infer<typeof placeContribPlaceBodySchema>;
/** Its 200 answer. */
export const placeContribPlaceAnswerSchema = z.object({
ok: z.literal(true),
cid: z.string().max(64),
duplicate: z.boolean(),
key: z.string().max(80),
community: z.object({ version: z.number().int().nonnegative() }).nullable().optional(),
results: z.partialRecord(
placeContribFieldSchema,
z.object({
result: placeContribFieldResultSchema,
reason: shortText.nullable().optional(),
flags: z.array(z.string().max(64)).max(32).optional(),
}),
),
});
export type PlaceContribPlaceAnswer = z.infer<typeof placeContribPlaceAnswerSchema>;
/** POST /v1/contrib/image's 200 answer. */
export const placeContribImageAnswerSchema = z.object({
ok: z.literal(true),
result: z.object({
result: z.enum(['review', 'published', 'rejected', 'quota']),
image: z
.string()
.regex(/^[0-9a-f]{32}$/)
.nullable()
.optional(),
sameAs: z.boolean().optional(),
reason: shortText.nullable().optional(),
}),
});
export type PlaceContribImageAnswer = z.infer<typeof placeContribImageAnswerSchema>;
/** DELETE /v1/contrib/install's 200 answer. */
export const placeContribWithdrawAnswerSchema = z.object({
ok: z.literal(true),
withdrawn: z.record(z.string(), z.unknown()).optional(),
});
export type PlaceContribWithdrawAnswer = z.infer<typeof placeContribWithdrawAnswerSchema>;
/** Every refusal: `{ error: "<code>" }`, `field_not_accepted:<name>` naming the field. */
export const placeContribErrorSchema = z.object({ error: shortText });
export type PlaceContribError = z.infer<typeof placeContribErrorSchema>;
+4
View File
@@ -1,4 +1,5 @@
import { tagSchema } from '../tag/tag.schema';
import { placeFieldOriginsSchema } from './place-contrib.schema';
import { placeEmailField, placeOpeningHoursField } from './place-hours';
import { PLACE_WEBSITE_MAX_LENGTH, normalizePlaceWebsite } from './place-website';
@@ -267,6 +268,9 @@ const stopTypeField = z.object({
// outside it on purpose — a stop that fills nothing is not a stop, and letting one
// through would leave a range budget that never resets.
fill_percent: fillPercentSchema,
// Which fields of this write the user typed and which a provider filled, for the
// community enrichment (place-contrib.schema.ts). Never stored on the place itself.
fieldOrigins: placeFieldOriginsSchema.optional(),
});
export const placeCreateRequestSchema = open.and(z.object({ name: z.string().min(1) })).and(stopTypeField);