mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 18:54:17 +00:00
fix(places): undo, unlink and dismissals that hold after a key or access change
Review findings on the place identities and trip list links: - The assistant offers PlacesLibraryMcp, PlaceIdentitiesMcp and TripCollectionsMcp with the collections group (done with the CI fix). - Undoing a merge restores the entries as they were: the merge logs, in its moved_link_ids JSON, each link it moved and each link it made with the use that link names, and whether the merged side became an own place only for the merge. The undo deletes what the merge made and moves the rest back, and only touches a link still in the leading place naming the same use, so a link id SQLite hands out again never moves someone else's link. The sweep counts a merge as spent by the same rule. Same unreleased migration, no schema change. - A use can be taken out of an own place by the user's own link row also once its trip or list is out of reach; the answer then holds no entry, and the place left can be merged again. - A dismissed duplicate is kept by one use of each side (an own place by its first link) and matched by the uses each entry holds, so it stays out when an entry becomes an own place or a merge is undone. - link() checks the named uses with one targeted read instead of reading every use of the user, and the library reads its rows in blocks of 500 ids. - Saved place reorders and the trip tags of a copy run one statement after the other again; a copy takes the first spelling of a tag name, as before. - Unlinking a list from a trip takes place_edit alone, so a link to a list the writer cannot open can be taken off; a member leaving or taken off the trip takes the links whose list nobody left on it can open. - The linked lists and their map places sort by lower(name), then by id, the same on both engines. - list_all_places names the own:<id> keys and identity_id (MCP and wiki); the undo, unlink and trip unlink descriptions say what they do now; README counts 451 tools.
This commit is contained in:
@@ -617,9 +617,31 @@ export const MAIL_INGEST_ATTACHMENTS: JsonColumn<MailIngestAttachment[]> = {
|
||||
fallback: () => [],
|
||||
};
|
||||
|
||||
/** The links a merge of two own places moved (D6), for its undo. A broken row moves nothing back. */
|
||||
export const USER_PLACE_MERGE_MOVED_LINKS: JsonColumn<number[]> = {
|
||||
/**
|
||||
* A link a merge wrote or moved, with the use it named then: the undo only
|
||||
* acts on a link that still names that use, so an id SQLite handed out again
|
||||
* never moves someone else's link.
|
||||
*/
|
||||
const mergedLinkSchema = z.object({
|
||||
link: z.number().int().positive(),
|
||||
kind: z.enum(['trip', 'list']),
|
||||
id: z.number().int().positive(),
|
||||
});
|
||||
export type MergedLink = z.infer<typeof mergedLinkSchema>;
|
||||
|
||||
const userPlaceMergeLogSchema = z.object({
|
||||
/** The links that moved from the merged own place to the leading one. */
|
||||
moved: z.array(mergedLinkSchema),
|
||||
/** The links the merge made for the leading entry when it was no own place yet. */
|
||||
made: z.array(mergedLinkSchema),
|
||||
/** Whether the merged entry became an own place only for the merge. */
|
||||
merged_made: z.boolean(),
|
||||
});
|
||||
export type UserPlaceMergeLog = z.infer<typeof userPlaceMergeLogSchema>;
|
||||
|
||||
/** What a merge of two own places moved and made (D6), for its undo. A broken row moves nothing back. */
|
||||
export const USER_PLACE_MERGE_MOVED_LINKS: JsonColumn<UserPlaceMergeLog> = {
|
||||
column: 'user_place_merges.moved_link_ids',
|
||||
schema: idList,
|
||||
fallback: () => [],
|
||||
schema: userPlaceMergeLogSchema,
|
||||
fallback: () => ({ moved: [], made: [], merged_made: false }),
|
||||
};
|
||||
|
||||
+3
-3
@@ -16,9 +16,9 @@ import { Migration } from '@mikro-orm/migrations';
|
||||
* The links go with the use, so a deleted place or list drops them; an identity
|
||||
* left without links is removed by the service, not here.
|
||||
*
|
||||
* `user_place_merges` keeps what a merge moved (the link ids, JSON) so it can
|
||||
* be undone, and `user_place_merge_dismissals` the duplicate suggestions the
|
||||
* user turned down, by the two entry keys in order.
|
||||
* `user_place_merges` keeps what a merge moved and made (each link id with the
|
||||
* use it names, JSON) so it can be undone, and `user_place_merge_dismissals`
|
||||
* the duplicate suggestions the user turned down, by one use of each side in order.
|
||||
*/
|
||||
export class Migration20200101046530_a_user_keeps_their_own_place_identities extends Migration {
|
||||
override name = 'Migration20200101046530_a_user_keeps_their_own_place_identities';
|
||||
|
||||
@@ -151,16 +151,16 @@ export class CollectionPlacesRepository extends TrekRepository<CollectionPlaces>
|
||||
|
||||
/** `UPDATE collection_places SET sort_order=? WHERE id=? AND collection_id=?`, per place, inside the caller's transaction. */
|
||||
async setSortOrders(collectionId: number, orderedIds: number[]): Promise<void> {
|
||||
await Promise.all(
|
||||
orderedIds.map((id, position) =>
|
||||
this.db_()
|
||||
.updateTable('collection_places')
|
||||
.set({ sort_order: position })
|
||||
.where('id', '=', id)
|
||||
.where('collection_id', '=', collectionId)
|
||||
.execute(),
|
||||
),
|
||||
);
|
||||
// One statement after the other on the transaction's connection, so a failure stops the rest before the rollback.
|
||||
await orderedIds.reduce(async (previous, id, position) => {
|
||||
await previous;
|
||||
await this.db_()
|
||||
.updateTable('collection_places')
|
||||
.set({ sort_order: position })
|
||||
.where('id', '=', id)
|
||||
.where('collection_id', '=', collectionId)
|
||||
.execute();
|
||||
}, Promise.resolve());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
@@ -494,12 +494,23 @@ export class PlacesLibraryRepository {
|
||||
.orderBy('u.from_list')
|
||||
.orderBy('u.use_id')
|
||||
.execute();
|
||||
return rows.map((row) => ({
|
||||
anchor_key: Number(row.anchor_key),
|
||||
from_list: Number(row.from_list),
|
||||
use_id: Number(row.use_id),
|
||||
linked: Number(row.linked) === 1,
|
||||
}));
|
||||
return rows.map(useRowOf);
|
||||
}
|
||||
|
||||
/** Which of the uses (from_list 0: a trip place, 1: a saved place) the user reaches, each with its entry. */
|
||||
async reachedUses(arms: LibraryArms, uses: readonly { fromList: number; useId: number }[]): Promise<LibraryUseRow[]> {
|
||||
if (uses.length === 0) return [];
|
||||
const db = this.db();
|
||||
const rows = await db
|
||||
.selectFrom(this.entries(db, arms, NO_MATCH))
|
||||
.select(['u.anchor_key', 'u.from_list', 'u.use_id', 'u.linked'])
|
||||
.where((eb) =>
|
||||
eb.or(uses.map((use) => eb.and([eb('u.from_list', '=', use.fromList), eb('u.use_id', '=', use.useId)]))),
|
||||
)
|
||||
.orderBy('u.from_list')
|
||||
.orderBy('u.use_id')
|
||||
.execute();
|
||||
return rows.map(useRowOf);
|
||||
}
|
||||
|
||||
/** The entry the use stands for (from_list 0: a trip place, 1: a saved place), when the user reaches it. */
|
||||
@@ -720,3 +731,13 @@ export class PlacesLibraryRepository {
|
||||
return { stays: present(stays), bookings: present(bookings) };
|
||||
}
|
||||
}
|
||||
|
||||
/** A use as the union reads it, its numbers as numbers whatever the engine hands back. */
|
||||
function useRowOf(row: { anchor_key: unknown; from_list: unknown; use_id: unknown; linked: unknown }): LibraryUseRow {
|
||||
return {
|
||||
anchor_key: Number(row.anchor_key),
|
||||
from_list: Number(row.from_list),
|
||||
use_id: Number(row.use_id),
|
||||
linked: Number(row.linked) === 1,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ export class TripCollectionLinksRepository extends TrekRepository<TripCollection
|
||||
return this.kysely<TripCollectionLinksDB>();
|
||||
}
|
||||
|
||||
/** The lists linked to the trip, by list name. */
|
||||
/** The lists linked to the trip, by list name (case ignored, the same on both engines), then by id. */
|
||||
async listForTrip(tripId: number): Promise<TripCollectionLinkRow[]> {
|
||||
const rows = await this.db()
|
||||
.selectFrom('trip_collection_links as l')
|
||||
@@ -66,7 +66,7 @@ export class TripCollectionLinksRepository extends TrekRepository<TripCollection
|
||||
.as('place_count'),
|
||||
])
|
||||
.where('l.trip_id', '=', tripId)
|
||||
.orderBy('c.name')
|
||||
.orderBy((eb) => eb.fn<string>('lower', ['c.name']))
|
||||
.orderBy('l.collection_id')
|
||||
.execute();
|
||||
return rows.map((row) => ({ ...row, place_count: Number(row.place_count ?? 0) }));
|
||||
@@ -97,7 +97,7 @@ export class TripCollectionLinksRepository extends TrekRepository<TripCollection
|
||||
* with the trip place it came from: the same provider id, or the same name
|
||||
* (trimmed, case ignored) within `COORD_DEDUP_TOLERANCE` and no provider id
|
||||
* that differs. The stored source alone proves nothing, since any editor of
|
||||
* the list may have written it. By list, then by name.
|
||||
* the list may have written it. By list, then by name (case ignored), then by id.
|
||||
*/
|
||||
async layerPlaces(tripId: number, collectionIds: number[]): Promise<TripCollectionLayerRow[]> {
|
||||
const rows = await this.db()
|
||||
@@ -154,7 +154,7 @@ export class TripCollectionLinksRepository extends TrekRepository<TripCollection
|
||||
.where('cp.lat', 'is not', null)
|
||||
.where('cp.lng', 'is not', null)
|
||||
.orderBy('cp.collection_id')
|
||||
.orderBy('cp.name')
|
||||
.orderBy((eb) => eb.fn<string>('lower', ['cp.name']))
|
||||
.orderBy('cp.id')
|
||||
.execute();
|
||||
return rows.flatMap((row) =>
|
||||
|
||||
@@ -322,6 +322,71 @@ export class TripMembersRepository extends TrekRepository<TripMembers> {
|
||||
.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops the trip's list links (PLC-15) whose list no one left on the trip,
|
||||
* its owner or a member, can open: such a link shows to nobody, and would
|
||||
* come back unasked once the list is shared with one of them again. Runs
|
||||
* inside the caller's transaction, after the member is gone.
|
||||
*/
|
||||
async dropUnseenCollectionLinks(tripId: number): Promise<void> {
|
||||
const db =
|
||||
this.kysely<
|
||||
Pick<DB, 'trip_collection_links' | 'collections' | 'collection_members' | 'trips' | 'trip_members'>
|
||||
>();
|
||||
await db
|
||||
.deleteFrom('trip_collection_links')
|
||||
.where('trip_id', '=', tripId)
|
||||
.where((eb) =>
|
||||
eb.not(
|
||||
eb.exists(
|
||||
eb
|
||||
.selectFrom('collections as c')
|
||||
.select('c.id')
|
||||
.whereRef('c.id', '=', 'trip_collection_links.collection_id')
|
||||
.where((seen) =>
|
||||
seen.or([
|
||||
seen(
|
||||
'c.owner_id',
|
||||
'in',
|
||||
seen.selectFrom('trips as t').select('t.user_id').where('t.id', '=', tripId),
|
||||
),
|
||||
seen(
|
||||
'c.owner_id',
|
||||
'in',
|
||||
seen.selectFrom('trip_members as tm').select('tm.user_id').where('tm.trip_id', '=', tripId),
|
||||
),
|
||||
seen.exists(
|
||||
seen
|
||||
.selectFrom('collection_members as cm')
|
||||
.select('cm.id')
|
||||
.whereRef('cm.collection_id', '=', 'c.id')
|
||||
.where('cm.status', '=', 'accepted')
|
||||
.where((member) =>
|
||||
member.or([
|
||||
member(
|
||||
'cm.user_id',
|
||||
'in',
|
||||
member.selectFrom('trips as t2').select('t2.user_id').where('t2.id', '=', tripId),
|
||||
),
|
||||
member(
|
||||
'cm.user_id',
|
||||
'in',
|
||||
member
|
||||
.selectFrom('trip_members as tm2')
|
||||
.select('tm2.user_id')
|
||||
.where('tm2.trip_id', '=', tripId),
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
.execute();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Plan 3c Task 7 (`trips.rpc.ts::members`) — additive: no other repository
|
||||
// is rooted at `trip_members` joined to `users`, and RP3's shape is
|
||||
|
||||
@@ -16,6 +16,12 @@ interface UserPlaceLinkRef {
|
||||
user_place_id: number;
|
||||
}
|
||||
|
||||
/** A link with the own place it is in and the use it names. */
|
||||
export interface UserPlaceLinkUse extends LinkedUse {
|
||||
link: number;
|
||||
user_place_id: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* `user_place_links`: which use belongs to which own place of a person (D5).
|
||||
* A use belongs to one own place per person at most, which the unique keys on
|
||||
@@ -81,16 +87,28 @@ export class UserPlaceLinksRepository extends TrekRepository<UserPlaceLinks> {
|
||||
await this.db().deleteFrom('user_place_links').where('id', '=', id).where('user_id', '=', userId).execute();
|
||||
}
|
||||
|
||||
/** The ids of the links the own place holds. */
|
||||
async idsOf(userId: number, userPlaceId: number): Promise<number[]> {
|
||||
/** The links the own place holds, each with the use it names, in id order. */
|
||||
async linksOf(userId: number, userPlaceId: number): Promise<UserPlaceLinkUse[]> {
|
||||
const rows = await this.db()
|
||||
.selectFrom('user_place_links')
|
||||
.select('id')
|
||||
.select(['id', 'user_place_id', 'place_id', 'collection_place_id'])
|
||||
.where('user_id', '=', userId)
|
||||
.where('user_place_id', '=', userPlaceId)
|
||||
.orderBy('id')
|
||||
.execute();
|
||||
return rows.map((row) => row.id);
|
||||
return rows.map(linkUseOf);
|
||||
}
|
||||
|
||||
/** The links `ids` that still exist, each with the own place it is in and the use it names. */
|
||||
async byIds(ids: number[]): Promise<UserPlaceLinkUse[]> {
|
||||
if (ids.length === 0) return [];
|
||||
const rows = await this.db()
|
||||
.selectFrom('user_place_links')
|
||||
.select(['id', 'user_place_id', 'place_id', 'collection_place_id'])
|
||||
.where('id', 'in', ids)
|
||||
.orderBy('id')
|
||||
.execute();
|
||||
return rows.map(linkUseOf);
|
||||
}
|
||||
|
||||
/** Moves the user's links `ids` to the own place `userPlaceId`; a link gone by now is skipped. */
|
||||
@@ -104,10 +122,24 @@ export class UserPlaceLinksRepository extends TrekRepository<UserPlaceLinks> {
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** Which of the links `ids` still exist. */
|
||||
async existing(ids: number[]): Promise<number[]> {
|
||||
if (ids.length === 0) return [];
|
||||
const rows = await this.db().selectFrom('user_place_links').select('id').where('id', 'in', ids).execute();
|
||||
return rows.map((row) => row.id);
|
||||
/** Deletes the user's links `ids`; a link gone by now is skipped. */
|
||||
async deleteIds(userId: number, ids: number[]): Promise<void> {
|
||||
if (ids.length === 0) return;
|
||||
await this.db().deleteFrom('user_place_links').where('user_id', '=', userId).where('id', 'in', ids).execute();
|
||||
}
|
||||
}
|
||||
|
||||
/** A link row as `UserPlaceLinkUse`: the use it names, a trip place or a saved place. */
|
||||
function linkUseOf(row: {
|
||||
id: number;
|
||||
user_place_id: number;
|
||||
place_id: number | null;
|
||||
collection_place_id: number | null;
|
||||
}): UserPlaceLinkUse {
|
||||
return {
|
||||
link: row.id,
|
||||
user_place_id: row.user_place_id,
|
||||
kind: row.place_id === null ? 'list' : 'trip',
|
||||
id: row.place_id ?? row.collection_place_id ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { TrekRepository } from './_shared/trek-repository';
|
||||
|
||||
type DismissalsDB = Pick<DB, 'user_place_merge_dismissals'>;
|
||||
|
||||
/** A turned-down suggestion by its two entry keys, the smaller first. */
|
||||
/** A turned-down suggestion by one use of each side (`trip:<id>`, `list:<id>`), the smaller first. */
|
||||
interface DismissedPair {
|
||||
key_a: string;
|
||||
key_b: string;
|
||||
|
||||
Reference in New Issue
Block a user