diff --git a/shared/src/collection/collection.schema.spec.ts b/shared/src/collection/collection.schema.spec.ts index 6df258f66..8da942184 100644 --- a/shared/src/collection/collection.schema.spec.ts +++ b/shared/src/collection/collection.schema.spec.ts @@ -184,6 +184,15 @@ describe('collectionCopyToTripRequestSchema', () => { false, ); }); + + // PLC-21: a list's tags (its labels) travel the same way, by their own ids. + it('takes the list tags to bring, and refuses a fractional one or more than 50', () => { + const body = { trip_id: 3, place_ids: [1], label_ids: [4, 5] }; + expect(collectionCopyToTripRequestSchema.parse(body)).toEqual(body); + expect(collectionCopyToTripRequestSchema.safeParse({ ...body, label_ids: [1.5] }).success).toBe(false); + const many = Array.from({ length: 51 }, (_, i) => i + 1); + expect(collectionCopyToTripRequestSchema.safeParse({ ...body, label_ids: many }).success).toBe(false); + }); }); describe('collectionPlacesReorderRequestSchema', () => { diff --git a/shared/src/collection/collection.schema.ts b/shared/src/collection/collection.schema.ts index 57c920fc6..5f4d6d7d5 100644 --- a/shared/src/collection/collection.schema.ts +++ b/shared/src/collection/collection.schema.ts @@ -2,6 +2,7 @@ import { idSchema } from '../common/primitives.schema'; import { placeCategorySchema, placeImageUrlSchema, + placeRatingToolFieldSchema, placeRatingVoteSchema, placeWebsiteSchema, } from '../place/place.schema'; @@ -96,6 +97,26 @@ export const collectionPlaceSchema = z.object({ }); export type CollectionPlace = z.infer; +/** rate_collection_place: the acting user's vote on a saved place. */ +export const collectionPlaceRateToolInputSchema = z.object({ + placeId: idSchema, + rating: placeRatingToolFieldSchema, +}); +export type CollectionPlaceRateToolInput = z.infer; + +/** + * What a vote on a saved place answers: the saved place as its list shows it. + * Only the fields the vote is about are pinned here; the place carries all its others. + */ +export const collectionPlaceRateResponseSchema = collectionPlaceSchema.pick({ + id: true, + collection_id: true, + name: true, + ratings: true, + rating_avg: true, + rating_count: true, +}); + /** Member of a shared list (mirrors vacay person rows). */ export const collectionMemberSchema = z.object({ user_id: z.number(), @@ -293,12 +314,12 @@ export const collectionCopyToTripRequestSchema = z.object({ .describe( 'Ids of tags the saved places carry that should come along. Each becomes a tag of the trip, reusing a trip tag of the same name; ids a place does not carry are ignored. Left out, no tags are copied', ), - personal_tag_ids: z + label_ids: z .array(z.number().int()) .max(50) .optional() .describe( - "PLC-21: ids of the caller's own personal tags to share with the trip. Each becomes a tag of the trip on every copied place, reusing a trip tag of the same name; ids that are not the caller's personal tags are ignored. Left out, personal tags stay private", + "PLC-21: ids of the list's tags (its labels) the saved places carry that should come along. Each becomes a tag of the trip, reusing a trip tag of the same name; ids a place does not carry are ignored. Left out, no list tags are copied", ), }); export type CollectionCopyToTripRequest = z.infer; diff --git a/shared/src/index.ts b/shared/src/index.ts index 6ecc7e0c5..987140868 100644 --- a/shared/src/index.ts +++ b/shared/src/index.ts @@ -130,8 +130,6 @@ export * from './places-library/place-visits.schema'; export * from './places-library/place-history.schema'; // PLC-18: the bookings and tours a place is about. export * from './places-library/place-activities.schema'; -// PLC-21: the user's personal tags on the entries of All places. -export * from './places-library/place-tags.schema'; // PLC-15/PLC-16: the lists a trip draws on and their saved places on the trip map. export * from './trip-collections/trip-collections.schema'; export * from './trip/trip.schema'; diff --git a/shared/src/place/place.schema.ts b/shared/src/place/place.schema.ts index e72619ca4..0ce40da3c 100644 --- a/shared/src/place/place.schema.ts +++ b/shared/src/place/place.schema.ts @@ -286,6 +286,40 @@ export const placeRatingRequestSchema = z.object({ }); export type PlaceRatingRequest = z.infer; +/** The stars of the vote tools: 1 to 5, or null or left out to take the vote back. */ +export const placeRatingToolFieldSchema = z + .number() + .int() + .min(1) + .max(5) + .nullable() + .optional() + .describe('1-5 stars; null/omitted clears the vote'); + +/** rate_place: the acting user's vote on a trip place. */ +export const placeRateToolInputSchema = z.object({ + tripId: idSchema, + placeId: idSchema, + rating: placeRatingToolFieldSchema, +}); +export type PlaceRateToolInput = z.infer; + +/** + * What a vote on a trip place answers: the place as the trip lists it. Only the + * fields the vote is about are pinned here; the place carries all its others. + */ +export const placeRateResponseSchema = z.object({ + place: placeSchema.pick({ + id: true, + trip_id: true, + name: true, + ratings: true, + rating_avg: true, + rating_count: true, + }), +}); +export type PlaceRateResponse = z.infer; + export const placeBulkDeleteRequestSchema = z.object({ ids: z.array(z.number().int()), }); diff --git a/shared/src/places-library/place-tags.schema.spec.ts b/shared/src/places-library/place-tags.schema.spec.ts deleted file mode 100644 index da84fb5e5..000000000 --- a/shared/src/places-library/place-tags.schema.spec.ts +++ /dev/null @@ -1,87 +0,0 @@ -import { collectionCopyToTripRequestSchema } from '../collection/collection.schema'; -import { - PLACES_LIBRARY_TAG_KEYS_MAX, - placesLibraryBulkTagsRequestSchema, - placesLibraryEntryTagsRequestSchema, -} from './place-tags.schema'; -import { PLACES_LIBRARY_TAGS_MAX, placesLibraryEntrySchema, placesLibraryQuerySchema } from './places-library.schema'; - -import { describe, expect, it } from 'vitest'; - -describe('PLC-21 personal place tags contracts', () => { - it('reads tag_ids from a query string or a list, and refuses what is no id', () => { - expect(placesLibraryQuerySchema.parse({ tag_ids: '3' }).tag_ids).toEqual([3]); - expect(placesLibraryQuerySchema.parse({ tag_ids: '3,7' }).tag_ids).toEqual([3, 7]); - expect(placesLibraryQuerySchema.parse({ tag_ids: [4, 5] }).tag_ids).toEqual([4, 5]); - for (const bad of ['', '0x5', '1,,2', '1.5', ' 1', '+1', '0', '9007199254740993', [], [1.5], [0]]) { - expect(placesLibraryQuerySchema.safeParse({ tag_ids: bad }).success).toBe(false); - } - const many = Array.from({ length: PLACES_LIBRARY_TAGS_MAX + 1 }, (_, index) => index + 1); - expect(placesLibraryQuerySchema.safeParse({ tag_ids: many.join(',') }).success).toBe(false); - }); - - it('sets the tags of one entry by its key, an empty list taking all off', () => { - expect(placesLibraryEntryTagsRequestSchema.parse({ key: 'own:2', tag_ids: [] })).toEqual({ - key: 'own:2', - tag_ids: [], - }); - for (const bad of [ - { key: 'tour:1', tag_ids: [] }, - { key: 'trip:1', tag_ids: [1.5] }, - { key: 'trip:1' }, - { key: 'trip:1', tag_ids: [], extra: 1 }, - ]) { - expect(placesLibraryEntryTagsRequestSchema.safeParse(bad).success).toBe(false); - } - }); - - it('changes several entries at once, with something to add or remove and never both for one tag', () => { - expect(placesLibraryBulkTagsRequestSchema.safeParse({ keys: ['trip:1', 'list:2'], add: [3] }).success).toBe(true); - expect(placesLibraryBulkTagsRequestSchema.safeParse({ keys: ['trip:1'], remove: [3] }).success).toBe(true); - const keys = Array.from({ length: PLACES_LIBRARY_TAG_KEYS_MAX + 1 }, (_, index) => `trip:${index + 1}`); - for (const bad of [ - { keys: ['trip:1'] }, - { keys: ['trip:1'], add: [], remove: [] }, - { keys: [], add: [1] }, - { keys, add: [1] }, - { keys: ['trip:1'], add: [1], remove: [1] }, - ]) { - expect(placesLibraryBulkTagsRequestSchema.safeParse(bad).success).toBe(false); - } - }); - - it('an entry may carry its tags, and a copy may share personal tags', () => { - const entry = { - key: 'trip:1', - name: 'Café', - address: null, - lat: null, - lng: null, - category: null, - image_url: null, - updated_at: null, - origins: [ - { - kind: 'trip', - trip_id: 1, - trip_title: 'Alps', - trip_start: null, - trip_end: null, - place_id: 1, - can_edit: true, - read_only: false, - }, - ], - }; - expect(placesLibraryEntrySchema.safeParse(entry).success).toBe(true); - expect(placesLibraryEntrySchema.safeParse({ ...entry, tags: [{ id: 1, name: 'Wine', color: null }] }).success).toBe( - true, - ); - expect( - collectionCopyToTripRequestSchema.safeParse({ trip_id: 1, place_ids: [2], personal_tag_ids: [3] }).success, - ).toBe(true); - expect( - collectionCopyToTripRequestSchema.safeParse({ trip_id: 1, place_ids: [2], personal_tag_ids: [1.5] }).success, - ).toBe(false); - }); -}); diff --git a/shared/src/places-library/place-tags.schema.ts b/shared/src/places-library/place-tags.schema.ts deleted file mode 100644 index 24ba32285..000000000 --- a/shared/src/places-library/place-tags.schema.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { PLACES_LIBRARY_TAGS_MAX, placesLibraryEntrySchema, placesLibraryKeySchema } from './places-library.schema'; - -import { z } from 'zod'; - -/** - * PLC-21: a user's personal tags on the entries of "All places". The tags - * themselves are the user's personal tags of /api/tags (`list_tags`, - * `create_tag`, ...); here they are put on entries and taken off again. Every - * assignment is the user's own: nobody else on a trip or a list sees it, and - * it changes no trip place and no saved place. - * - * A tag sits on the entry: on the user's own place where the entry is one - * (`own:`), else on the trip place or saved place it stands for. A tag - * that sits on one of an entry's uses counts for the entry too, so tags - * survive putting places together and taking them apart again. - */ - -/** The most entries one bulk change names. */ -export const PLACES_LIBRARY_TAG_KEYS_MAX = 200; - -const tagIdsSchema = z - .array(z.number().int().positive()) - .max(PLACES_LIBRARY_TAGS_MAX) - .describe('Ids of personal tags of the user (list_tags)'); - -/** The personal tags an entry carries from now on, all of them: an empty list takes every tag off. */ -export const placesLibraryEntryTagsRequestSchema = z - .object({ - key: placesLibraryKeySchema, - tag_ids: tagIdsSchema.describe('Every personal tag the entry carries from now on; an empty list takes all off'), - }) - .strict(); -export type PlacesLibraryEntryTagsRequest = z.infer; - -/** Put tags on several entries and take others off, in one go. */ -export const placesLibraryBulkTagsRequestSchema = z - .object({ - keys: z - .array(placesLibraryKeySchema) - .min(1) - .max(PLACES_LIBRARY_TAG_KEYS_MAX) - .describe('The entries to change, by their key from list_all_places'), - add: tagIdsSchema.optional().describe('Personal tags to put on every one of the entries'), - remove: tagIdsSchema.optional().describe('Personal tags to take off every one of the entries'), - }) - .strict() - .refine((body) => (body.add?.length ?? 0) + (body.remove?.length ?? 0) > 0, { - message: 'Name tags to add or to remove', - path: ['add'], - }) - .refine((body) => !(body.add ?? []).some((id) => (body.remove ?? []).includes(id)), { - message: 'A tag cannot be added and removed at once', - path: ['remove'], - }); -export type PlacesLibraryBulkTagsRequest = z.infer; - -/** The entry after the change, null when the user does not reach it any more. */ -export const placesLibraryEntryTagsResponseSchema = z.object({ - entry: placesLibraryEntrySchema.nullable(), -}); -export type PlacesLibraryEntryTagsResponse = z.infer; - -/** The entries after a bulk change, in the order the request named them. */ -export const placesLibraryBulkTagsResponseSchema = z.object({ - entries: z.array(placesLibraryEntrySchema), -}); -export type PlacesLibraryBulkTagsResponse = z.infer; diff --git a/shared/src/places-library/places-library.schema.spec.ts b/shared/src/places-library/places-library.schema.spec.ts index 2f5211a6f..77fa7483f 100644 --- a/shared/src/places-library/places-library.schema.spec.ts +++ b/shared/src/places-library/places-library.schema.spec.ts @@ -1,9 +1,16 @@ import { PLACES_LIBRARY_PAGE_MAX, + PLACES_LIBRARY_TAG_NAME_MAX, + PLACES_LIBRARY_TAGS_MAX, + joinLibraryTagNames, placesLibraryDetailRequestSchema, placesLibraryDetailResponseSchema, placesLibraryPageResponseSchema, placesLibraryQuerySchema, + placesLibraryRateRequestSchema, + placesLibraryRateResponseSchema, + placesLibraryRateToolInputSchema, + placesLibraryUnrateQuerySchema, } from './places-library.schema'; import { describe, expect, it } from 'vitest'; @@ -36,6 +43,10 @@ const entry = { image_url: null, updated_at: '2026-11-01 10:00:00', origins: [tripOrigin, listOrigin], + tags: [ + { name: 'Coffee', color: '#111111' }, + { name: 'View', color: null }, + ], }; describe('placesLibraryQuerySchema', () => { @@ -67,6 +78,39 @@ describe('placesLibraryQuerySchema', () => { expect(placesLibraryQuerySchema.safeParse({ status: 'maybe' }).success).toBe(false); expect(placesLibraryQuerySchema.safeParse({ notes: 'x' }).success).toBe(false); }); + + it('sorts by the votes and filters by the fewest stars, 1 to 5, from a query string or a number', () => { + expect(placesLibraryQuerySchema.parse({ sort: 'rating' })).toEqual({ sort: 'rating' }); + expect(placesLibraryQuerySchema.parse({ min_rating: '4' })).toEqual({ min_rating: 4 }); + expect(placesLibraryQuerySchema.parse({ min_rating: 1 })).toEqual({ min_rating: 1 }); + for (const min_rating of ['0', '6', 2.5, '3.5', ' 3', 'x', -1, '']) { + expect(placesLibraryQuerySchema.safeParse({ min_rating }).success).toBe(false); + } + }); + + // PLC-21: the tag filter names the tags of the lists and trips, so it takes names, not ids. + it('reads tag names from a list or from a query string joined by commas, trimmed', () => { + expect(placesLibraryQuerySchema.parse({ tags: [' Coffee ', 'View'] })).toEqual({ tags: ['Coffee', 'View'] }); + expect(placesLibraryQuerySchema.parse({ tags: 'Coffee, View,,' })).toEqual({ tags: ['Coffee', 'View'] }); + expect(placesLibraryQuerySchema.safeParse({ tag_ids: '3' }).success).toBe(false); + }); + + it('keeps a comma or a backslash inside a name it was joined with', () => { + const names = ['Food, drinks', 'a\\b', 'View']; + const joined = joinLibraryTagNames(names); + expect(joined).toBe('Food\\, drinks,a\\\\b,View'); + expect(placesLibraryQuerySchema.parse({ tags: joined })).toEqual({ tags: names }); + }); + + it('refuses a filter naming no tag, too many or a name past the longest a tag has', () => { + expect(placesLibraryQuerySchema.safeParse({ tags: '' }).success).toBe(false); + expect(placesLibraryQuerySchema.safeParse({ tags: [' '] }).success).toBe(false); + const many = Array.from({ length: PLACES_LIBRARY_TAGS_MAX + 1 }, (_, i) => `t${i}`); + expect(placesLibraryQuerySchema.safeParse({ tags: many }).success).toBe(false); + expect(placesLibraryQuerySchema.safeParse({ tags: 'x'.repeat(PLACES_LIBRARY_TAG_NAME_MAX + 1) }).success).toBe( + false, + ); + }); }); describe('placesLibraryDetailRequestSchema', () => { @@ -119,14 +163,97 @@ describe('placesLibraryDetailResponseSchema', () => { place_id: 12, info, notes: 'Book ahead', + tags: [{ id: 7, name: 'View', color: null }], + available_tags: [ + { id: 7, name: 'View', color: null }, + { id: 8, name: 'Museum', color: '#222222' }, + ], + can_edit_tags: true, rating, days: [{ day_id: 1, day_number: 1, date: '2026-07-01', title: null, time: '09:00', end_time: null }], has_accommodation: false, has_reservation: true, }, - { kind: 'list', collection_place_id: 40, info, notes: null, labels: [], rating }, + { + kind: 'list', + collection_place_id: 40, + info, + notes: null, + labels: [{ id: 3, name: 'Coffee', color: '#111111' }], + tags: [{ id: 3, name: 'Coffee', color: '#111111' }], + available_tags: [{ id: 3, name: 'Coffee', color: '#111111' }], + can_edit_tags: false, + rating, + }, ], }; expect(placesLibraryDetailResponseSchema.parse(detail)).toEqual(detail); }); + + it('refuses an origin detail without its tags or whether they may be changed', () => { + const info = { + name: 'x', + address: null, + lat: null, + lng: null, + description: null, + website: null, + phone: null, + image_url: null, + category: null, + }; + const rating = { average: null, count: 0, mine: null }; + const list = { kind: 'list', collection_place_id: 40, info, notes: null, labels: [], rating }; + expect(placesLibraryDetailResponseSchema.safeParse({ entry, details: [list] }).success).toBe(false); + const complete = { ...list, tags: [], available_tags: [], can_edit_tags: true }; + expect(placesLibraryDetailResponseSchema.safeParse({ entry, details: [complete] }).success).toBe(true); + }); +}); + +describe('the vote on an entry', () => { + it('takes an entry key and 1 to 5 whole stars', () => { + expect(placesLibraryRateRequestSchema.safeParse({ key: 'trip:12', rating: 5 }).success).toBe(true); + expect(placesLibraryRateRequestSchema.safeParse({ key: 'own:3', rating: 1 }).success).toBe(true); + for (const rating of [0, 6, 2.5, '3', null]) { + expect(placesLibraryRateRequestSchema.safeParse({ key: 'trip:12', rating }).success).toBe(false); + } + for (const key of ['trip:0', 'trip:abc', 'place:1', '', 'trip:12 ']) { + expect(placesLibraryRateRequestSchema.safeParse({ key, rating: 3 }).success).toBe(false); + } + }); + + it('takes the vote back by key alone, and the tool takes null or nothing for that', () => { + expect(placesLibraryUnrateQuerySchema.safeParse({ key: 'list:4' }).success).toBe(true); + expect(placesLibraryUnrateQuerySchema.safeParse({}).success).toBe(false); + expect(placesLibraryRateToolInputSchema.safeParse({ key: 'list:4' }).success).toBe(true); + expect(placesLibraryRateToolInputSchema.safeParse({ key: 'list:4', rating: null }).success).toBe(true); + expect(placesLibraryRateToolInputSchema.safeParse({ key: 'list:4', rating: 7 }).success).toBe(false); + }); + + it('answers the entry rating', () => { + expect( + placesLibraryRateResponseSchema.safeParse({ key: 'trip:1', rating: { average: 3.5, count: 2, mine: 4 } }).success, + ).toBe(true); + expect( + placesLibraryRateResponseSchema.safeParse({ key: 'trip:1', rating: { average: null, count: -1, mine: null } }) + .success, + ).toBe(false); + }); + + it('names the people a rating counts, with their picture, one vote each', () => { + const votes = [ + { user_id: 2, username: 'ana', avatar_url: '/uploads/avatars/a.png', rating: 5 }, + { user_id: 3, username: 'ben', avatar_url: null, rating: 3 }, + ]; + expect( + placesLibraryRateResponseSchema.safeParse({ key: 'trip:1', rating: { average: 4, count: 2, mine: 5, votes } }) + .success, + ).toBe(true); + expect( + placesLibraryRateResponseSchema.safeParse({ + key: 'trip:1', + rating: { average: 4, count: 1, mine: null, votes: [{ user_id: 2, username: 'ana', rating: 4 }] }, + }).success, + ).toBe(false); + }); }); diff --git a/shared/src/places-library/places-library.schema.ts b/shared/src/places-library/places-library.schema.ts index 3ed27c3f1..696bb47fb 100644 --- a/shared/src/places-library/places-library.schema.ts +++ b/shared/src/places-library/places-library.schema.ts @@ -1,5 +1,6 @@ import { COLLECTION_STATUSES } from '../collection/collection.schema'; import { idParamSchema, idSchema } from '../common/primitives.schema'; +import { placeRatingToolFieldSchema } from '../place/place.schema'; import { z } from 'zod'; @@ -19,29 +20,54 @@ export const PLACES_LIBRARY_PAGE_MAX = 200; export const PLACES_LIBRARY_KINDS = ['trip', 'list'] as const; export type PlacesLibraryKind = (typeof PLACES_LIBRARY_KINDS)[number]; -/** By name (A to Z) or by the last change (newest first). */ -export const PLACES_LIBRARY_SORTS = ['name', 'updated'] as const; +/** By name (A to Z), by the last change (newest first) or by the votes (highest average first, unrated last). */ +export const PLACES_LIBRARY_SORTS = ['name', 'updated', 'rating'] as const; export type PlacesLibrarySort = (typeof PLACES_LIBRARY_SORTS)[number]; -/** PLC-21: the most personal tags one filter or one assignment names. */ +/** PLC-21: the most tag names one filter names. */ export const PLACES_LIBRARY_TAGS_MAX = 50; +/** The longest tag name a filter takes, the longest a tag of a list or a trip can have. */ +export const PLACES_LIBRARY_TAG_NAME_MAX = 100; + /** - * Personal tag ids as a filter takes them: a list of ids (MCP, JSON), or the - * ids joined by commas as a query string carries them (`tag_ids=3,7`). Each - * one is decimal digits naming a positive safe integer. + * Tag names joined for a query string: a comma between two names, and a comma + * or a backslash inside a name written with a backslash before it (`\,`, `\\`). */ -export const placesLibraryTagIdsSchema = z - .union([ - z.array(z.number().int().positive()), - z - .string() - .regex(/^\d+(,\d+)*$/, 'Invalid id') - .transform((joined) => joined.split(',').map(Number)), - ]) - .pipe( - z.array(z.number().int().positive().refine(Number.isSafeInteger, 'Invalid id')).min(1).max(PLACES_LIBRARY_TAGS_MAX), - ); +export function joinLibraryTagNames(names: readonly string[]): string { + return names.map((name) => name.replace(/[\\,]/g, (char) => `\\${char}`)).join(','); +} + +/** The names `joinLibraryTagNames` joined, read back. */ +export function splitLibraryTagNames(joined: string): string[] { + const names: string[] = []; + let current = ''; + for (let index = 0; index < joined.length; index += 1) { + const char = joined[index]; + if (char === '\\' && index + 1 < joined.length) { + index += 1; + current += joined[index]; + } else if (char === ',') { + names.push(current); + current = ''; + } else { + current += char; + } + } + names.push(current); + return names; +} + +/** + * Tag names as a filter takes them: a list of names (MCP, JSON), or the names + * joined by `joinLibraryTagNames` as a query string carries them + * (`tags=Coffee,View`). Each name is trimmed and an empty one left out; a + * filter that names none is refused. + */ +export const placesLibraryTagNamesSchema = z + .union([z.array(z.string()).max(PLACES_LIBRARY_TAGS_MAX * 2), z.string().max(10_000).transform(splitLibraryTagNames)]) + .transform((names) => names.map((name) => name.trim()).filter((name) => name !== '')) + .pipe(z.array(z.string().max(PLACES_LIBRARY_TAG_NAME_MAX)).min(1).max(PLACES_LIBRARY_TAGS_MAX)); /** A count from a query string: its digits, or a number from a client that sends one. */ const pageLimitSchema = z @@ -49,6 +75,12 @@ const pageLimitSchema = z .transform(Number) .pipe(z.number().int().min(1).max(PLACES_LIBRARY_PAGE_MAX)); +/** The fewest stars an entry's average may have, 1 to 5: its digits from a query string, or a number. */ +const minRatingSchema = z + .union([z.number().int(), z.string().regex(/^\d+$/)]) + .transform(Number) + .pipe(z.number().int().min(1).max(5)); + export const placesLibraryQuerySchema = z .object({ q: z.string().trim().max(100).optional().describe('A part of the name or the address, case ignored'), @@ -60,15 +92,22 @@ export const placesLibraryQuerySchema = z collection_id: idParamSchema.optional().describe('Only entries in this list'), trip_id: idParamSchema.optional().describe('Only entries in this trip'), status: z.enum(COLLECTION_STATUSES).optional().describe('Only entries with this status in one of the lists'), - tag_ids: placesLibraryTagIdsSchema + tags: placesLibraryTagNamesSchema .optional() .describe( - 'Only entries carrying at least one of these personal tags of the user (ids from list_tags; in a query string joined by commas)', + 'Only entries carrying a tag of one of these names, case ignored: a tag of one of its lists or of one of its trips (in a query string joined by commas, a comma inside a name written as \\,)', + ), + min_rating: minRatingSchema + .optional() + .describe( + 'Only entries whose average vote (every person once, over the copies the user reaches) is at least this many stars, 1 to 5; entries without votes are left out', ), sort: z .enum(PLACES_LIBRARY_SORTS) .optional() - .describe('name (A to Z, the default) or updated (last changed first)'), + .describe( + 'name (A to Z, the default), updated (last changed first) or rating (highest average vote first, entries without votes last)', + ), cursor: z .string() // A cursor carries the sort value of the last entry, a place name among them. @@ -94,7 +133,11 @@ export const placesLibraryDetailRequestSchema = z .strict(); export type PlacesLibraryDetailRequest = z.infer; -/** PLC-21: a personal tag of the user on an entry. Nobody else sees it. */ +/** + * PLC-21: a tag of a list or a trip as one origin has it. A list's tags are its + * labels, a trip's tags the trip's own; every member of the list or the trip + * sees them. + */ export const placesLibraryTagSchema = z.object({ id: z.number().int(), name: z.string(), @@ -102,6 +145,13 @@ export const placesLibraryTagSchema = z.object({ }); export type PlacesLibraryTag = z.infer; +/** PLC-21: a tag of an entry by name, gathered from the lists and trips it is in. */ +export const placesLibraryEntryTagSchema = z.object({ + name: z.string(), + color: z.string().nullable(), +}); +export type PlacesLibraryEntryTag = z.infer; + export const placesLibraryCategorySchema = z.object({ id: z.number().int(), name: z.string(), @@ -156,6 +206,28 @@ export const placesLibraryKeySchema = z .regex(PLACES_LIBRARY_KEY_PATTERN, 'Invalid key') .describe('An entry key of list_all_places: own:, trip: or list:'); +/** One person's vote as a place shows it beside its stars: who, with their picture, and how many stars. */ +export const placesLibraryVoteSchema = z.object({ + user_id: z.number().int(), + username: z.string(), + avatar_url: z.string().nullable(), + rating: z.number().int(), +}); +export type PlacesLibraryVote = z.infer; + +/** + * The votes on a place where it is: their mean, how many, the user's own, and + * the people counted with their vote (`votes`, one per person, the same people + * the mean counts: the members of the lists and trips the user reaches). + */ +export const placesLibraryRatingSchema = z.object({ + average: z.number().nullable(), + count: z.number().int().nonnegative(), + mine: z.number().int().nullable(), + votes: z.array(placesLibraryVoteSchema).optional(), +}); +export type PlacesLibraryRating = z.infer; + /** * One place of "All places". `key` stays the same across pages and sorts: * `own:` for uses the user put together as one place of their @@ -184,11 +256,20 @@ export const placesLibraryEntrySchema = z.object({ visit_count: z.number().int().nonnegative().optional(), last_visit: z.string().nullable().optional(), /** - * PLC-21: the user's own personal tags on this entry, by name: the ones on - * its own place (and on one merged into it) and on any of its uses. Private: - * only ever the asking user's. + * PLC-21: the tags of the lists and trips this entry is in, of every one of + * them the user reaches: each list's labels on its saved place and each trip's + * tags on its trip place. A name stands once, whatever its case (with the + * colour of the first one read), sorted by name. They are changed in the list + * or the trip they belong to; the detail names them per origin. */ - tags: z.array(placesLibraryTagSchema).optional(), + tags: z.array(placesLibraryEntryTagSchema).optional(), + /** + * The votes on this place as one: every person's one vote, read from the + * copies of it the user reaches (each list and trip shows its own members' + * votes), each person counted once, and the user's own. A person with + * different votes on two copies counts with the one cast last. + */ + rating: placesLibraryRatingSchema.optional(), }); export type PlacesLibraryEntry = z.infer; @@ -204,7 +285,7 @@ export const placesLibraryPageResponseSchema = z.object({ entries: z.array(placesLibraryEntrySchema), /** Entries every filter of the query matches, on every page. */ total: z.number().int().nonnegative(), - /** Entries `q`, `category_id` and `tag_ids` match, whatever the other filters say, for the kind tabs. */ + /** Entries `q`, `category_id`, `tags` and `min_rating` match, whatever the other filters say, for the kind tabs. */ counts: placesLibraryCountsSchema, next_cursor: z.string().nullable(), }); @@ -224,14 +305,6 @@ export const placesLibraryInfoSchema = z.object({ }); export type PlacesLibraryInfo = z.infer; -/** The votes on a place where it is: their mean, how many, and the user's own. */ -export const placesLibraryRatingSchema = z.object({ - average: z.number().nullable(), - count: z.number().int().nonnegative(), - mine: z.number().int().nullable(), -}); -export type PlacesLibraryRating = z.infer; - export const placesLibraryLabelSchema = z.object({ id: z.number().int(), name: z.string(), @@ -249,23 +322,42 @@ export const placesLibraryDaySchema = z.object({ }); export type PlacesLibraryDay = z.infer; -/** What a list keeps of the place: its facts, the list's notes and labels, and the members' votes. */ +/** + * What a list keeps of the place: its facts, the list's notes and tags, and the + * members' votes. `labels` and `tags` hold the same list tags (the list's labels + * on the saved place; `labels` is their older name). `available_tags` are all + * the list's tags, and `can_edit_tags` says whether the user may put them on, + * take them off and add one (any list role but viewer). + */ export const placesLibraryListDetailSchema = z.object({ kind: z.literal('list'), collection_place_id: z.number().int(), info: placesLibraryInfoSchema, notes: z.string().nullable(), labels: z.array(placesLibraryLabelSchema), + tags: z.array(placesLibraryTagSchema), + available_tags: z.array(placesLibraryTagSchema), + can_edit_tags: z.boolean(), rating: placesLibraryRatingSchema, }); export type PlacesLibraryListDetail = z.infer; -/** What a trip keeps of the place: its facts, the trip's notes and votes, the days, and whether a stay or booking uses it. */ +/** + * What a trip keeps of the place: its facts, the trip's notes, tags and votes, + * the days, and whether a stay or booking uses it. `tags` are the tags on the + * trip place, `available_tags` all the trip's own tags followed by any other + * tag the place carries (an older personal tag of someone on the trip), and + * `can_edit_tags` whether the user may put them on, take them off and add one + * (the trip's `place_edit`, the same value as the origin's `can_edit`). + */ export const placesLibraryTripDetailSchema = z.object({ kind: z.literal('trip'), place_id: z.number().int(), info: placesLibraryInfoSchema, notes: z.string().nullable(), + tags: z.array(placesLibraryTagSchema), + available_tags: z.array(placesLibraryTagSchema), + can_edit_tags: z.boolean(), rating: placesLibraryRatingSchema, days: z.array(placesLibraryDaySchema), has_accommodation: z.boolean(), @@ -285,3 +377,34 @@ export const placesLibraryDetailResponseSchema = z.object({ details: z.array(placesLibraryDetailSchema), }); export type PlacesLibraryDetailResponse = z.infer; + +/** + * PUT /api/addons/collections/library/places/rating: the user's vote on an entry + * of All places, set on every copy of it they may vote on (each list they belong + * to, each trip they are on) in one go. + */ +export const placesLibraryRateRequestSchema = z.object({ + key: placesLibraryKeySchema, + rating: z.number().int().min(1).max(5), +}); +export type PlacesLibraryRateRequest = z.infer; + +/** DELETE /api/addons/collections/library/places/rating?key=: the user's vote taken back from every copy. */ +export const placesLibraryUnrateQuerySchema = z.object({ + key: placesLibraryKeySchema, +}); +export type PlacesLibraryUnrateQuery = z.infer; + +/** rate_library_place: the vote of the route, null or left out takes it back. */ +export const placesLibraryRateToolInputSchema = z.object({ + key: placesLibraryKeySchema, + rating: placeRatingToolFieldSchema, +}); +export type PlacesLibraryRateToolInput = z.infer; + +/** The entry's votes after the change, as the entry carries them. */ +export const placesLibraryRateResponseSchema = z.object({ + key: z.string(), + rating: placesLibraryRatingSchema, +}); +export type PlacesLibraryRateResponse = z.infer;