diff --git a/src/common/playlist.js b/src/common/playlist.js index 43128e31..4c716108 100644 --- a/src/common/playlist.js +++ b/src/common/playlist.js @@ -2,6 +2,65 @@ * @import {PlaylistItem, Track} from "@definitions/types.d.ts" */ +import { Temporal } from "@js-temporal/polyfill"; + +/** + * Filter tracks by playlist membership using an indexed lookup. + * + * @param {Track[]} tracks + * @param {PlaylistItem[]} playlistItems + */ +export function filterByPlaylist(tracks, playlistItems) { + // Group playlist items by criteria shape, building a Set index per shape. + const shapes = playlistItems + .reduce( + (acc, playlistItem) => { + const shapeKey = playlistItem.criteria + .map((c) => `${c.field}\0${(c.transformations ?? []).join(",")}`) + .join("\0\0"); + + const group = acc.get(shapeKey) ?? acc + .set(shapeKey, { criteria: playlistItem.criteria, keys: new Set() }) + .get(shapeKey); + + group?.keys.add( + playlistItem.criteria.map((c) => + transform(c.value, c.transformations) + ).join( + "\0", + ), + ); + + return acc; + }, + /** @type {Map }>} */ (new Map()), + ) + .values() + .map((group) => ({ + fields: group.criteria.map((c) => ({ + parts: c.field.split("."), + transformations: c.transformations, + })), + keys: group.keys, + })) + .toArray(); + + return tracks.filter((track) => + shapes.some((shape) => + shape.keys.has( + shape.fields + .map(({ parts, transformations }) => + transform( + parts.reduce((v, f) => v?.[f], /** @type {any} */ (track)), + transformations, + ) + ) + .join("\0"), + ) + ) + ); +} + /** * Bundle playlist items into their respective playlists. * @@ -20,32 +79,19 @@ export function gather(items) { playlistMap.set(item.playlist, { items: [item], name: item.playlist, - unordered: item.position == null, + unordered: item.positionedAfter == null, }); - } else if (item.position == null) { + } else { existing.items.push(item); - existing.unordered = true; + existing.unordered = existing.unordered === false + ? false + : item.positionedAfter == null; } } return playlistMap; } -/** - * @param {any} val - * @param {string[] | undefined} transformations - */ -function transform(val, transformations) { - if (!val || !transformations) return val; - return transformations.reduce((v, t) => { - try { - return v[t](); - } catch (_) { - return v; - } - }, val); -} - /** * Check if a track matches the criteria of a playlist item. * @@ -78,58 +124,79 @@ export function match(track, item) { } /** - * Filter tracks by playlist membership using an indexed lookup. + * Sort playlist items by their `positionedAfter` linked-list order. + * Items with no `positionedAfter` are placed first. * - * @param {Track[]} tracks - * @param {PlaylistItem[]} playlistItems + * @param {PlaylistItem[]} items + * @returns {PlaylistItem[]} */ -export function filterByPlaylist(tracks, playlistItems) { - // Group playlist items by criteria shape, building a Set index per shape. - const shapes = playlistItems - .reduce( - (acc, playlistItem) => { - const shapeKey = playlistItem.criteria - .map((c) => `${c.field}\0${(c.transformations ?? []).join(",")}`) - .join("\0\0"); +export function sort(items) { + if (items.length <= 1) return items; - const group = acc.get(shapeKey) ?? acc - .set(shapeKey, { criteria: playlistItem.criteria, keys: new Set() }) - .get(shapeKey); + /** @type {Map} */ + const afterMap = new Map(); - group?.keys.add( - playlistItem.criteria.map((c) => - transform(c.value, c.transformations) - ).join( - "\0", - ), + for (const item of items) { + const key = item.positionedAfter ?? null; + const group = afterMap.get(key); + if (group) { + group.push(item); + } else { + afterMap.set(key, [item]); + } + } + + // Sort each group by updatedAt so collisions have a deterministic order. + for (const group of afterMap.values()) { + if (group.length > 1) { + group.sort((a, b) => { + if (!a.updatedAt || !b.updatedAt) return a.updatedAt ? 1 : -1; + return Temporal.ZonedDateTime.compare( + Temporal.ZonedDateTime.from(a.updatedAt), + Temporal.ZonedDateTime.from(b.updatedAt), ); + }); + } + } - return acc; - }, - /** @type {Map }>} */ (new Map()), - ) - .values() - .map((group) => ({ - fields: group.criteria.map((c) => ({ - parts: c.field.split("."), - transformations: c.transformations, - })), - keys: group.keys, - })) - .toArray(); + /** @type {PlaylistItem[]} */ + const sorted = []; + const visited = new Set(); - return tracks.filter((track) => - shapes.some((shape) => - shape.keys.has( - shape.fields - .map(({ parts, transformations }) => - transform( - parts.reduce((v, f) => v?.[f], /** @type {any} */ (track)), - transformations, - ) - ) - .join("\0"), - ) - ) - ); + /** @type {PlaylistItem[]} */ + const queue = [...(afterMap.get(null) ?? [])]; + + while (queue.length > 0) { + const current = /** @type {PlaylistItem} */ (queue.shift()); + if (visited.has(current.id)) continue; + visited.add(current.id); + sorted.push(current); + + const next = afterMap.get(current.id); + if (next) queue.unshift(...next); + } + + // Append any items not reachable from a head (e.g. broken chains). + for (const item of items) { + if (!visited.has(item.id)) { + sorted.push(item); + } + } + + return sorted; +} + +/** + * @param {any} val + * @param {string[] | undefined} transformations + */ +function transform(val, transformations) { + if (!val || !transformations) return val; + return transformations.reduce((v, t) => { + try { + return v[t](); + } catch (_) { + return v; + } + }, val); } diff --git a/src/components/orchestrator/favourites/worker.js b/src/components/orchestrator/favourites/worker.js index 762ca48a..a1e0fefb 100644 --- a/src/components/orchestrator/favourites/worker.js +++ b/src/components/orchestrator/favourites/worker.js @@ -1,3 +1,4 @@ +import { Temporal } from "@js-temporal/polyfill"; import { ostiary, rpc } from "@common/worker.js"; import { filterFavourites } from "./common.js"; @@ -39,7 +40,7 @@ function itemMatchKey(item) { */ function createFavouriteItem(track) { const transformations = ["toLowerCase"]; - const now = new Date().toISOString(); + const now = Temporal.Now.zonedDateTimeISO().toString(); return /** @type {PlaylistItem} */ ({ $type: "sh.diffuse.output.playlistItem", diff --git a/src/definitions/index.ts b/src/definitions/index.ts index 197d820c..96e9ae46 100644 --- a/src/definitions/index.ts +++ b/src/definitions/index.ts @@ -1,3 +1,4 @@ +export * as ShDiffuseOutputCollaboration from "./types/sh/diffuse/output/collaboration.ts"; export * as ShDiffuseOutputFacet from "./types/sh/diffuse/output/facet.ts"; export * as ShDiffuseOutputPlaylistItem from "./types/sh/diffuse/output/playlistItem.ts"; export * as ShDiffuseOutputTheme from "./types/sh/diffuse/output/theme.ts"; diff --git a/src/definitions/output/collaboration.json b/src/definitions/output/collaboration.json new file mode 100644 index 00000000..51b4a644 --- /dev/null +++ b/src/definitions/output/collaboration.json @@ -0,0 +1,49 @@ +{ + "lexicon": 1, + "id": "sh.diffuse.output.collaboration", + "defs": { + "main": { + "type": "record", + "record": { + "type": "object", + "required": ["id", "collaborators", "subject"], + "properties": { + "id": { "type": "string" }, + "collaborators": { + "type": "array", + "items": { "type": "ref", "ref": "#collaborator" } + }, + "createdAt": { "type": "string", "format": "datetime" }, + "subject": { + "type": "union", + "refs": ["#playlist"] + }, + "updatedAt": { "type": "string", "format": "datetime" } + } + } + }, + "collaborator": { + "type": "object", + "required": ["id", "hint"], + "properties": { + "id": { "type": "string" }, + "hint": { + "type": "string", + "description": "Hint at where this collaborator resides and/or what the id represents." + } + } + }, + "playlist": { + "type": "object", + "required": ["id", "hint", "kind"], + "properties": { + "id": { "type": "string" }, + "hint": { + "type": "string", + "description": "Hint at where this collaborator resides and/or what the id represents." + }, + "kind": { "type": "string", "const": "playlist" } + } + } + } +} diff --git a/src/definitions/output/playlistItem.json b/src/definitions/output/playlistItem.json index 0a6af7a1..269c45c7 100644 --- a/src/definitions/output/playlistItem.json +++ b/src/definitions/output/playlistItem.json @@ -15,7 +15,10 @@ "items": { "type": "ref", "ref": "#criterion" } }, "playlist": { "type": "string" }, - "position": { "type": "integer" }, + "positionedAfter": { + "type": "string", + "description": "Id of the item that this item should be positioned after" + }, "updatedAt": { "type": "string", "format": "datetime" } } } diff --git a/src/facets/tools/v3-import.html.txt b/src/facets/tools/v3-import.html.txt index 721ad5e8..36bdf5cc 100644 --- a/src/facets/tools/v3-import.html.txt +++ b/src/facets/tools/v3-import.html.txt @@ -79,6 +79,7 @@