UNPKG

@solid-data-modules/bookmarks-rdflib

Version:

A library to manage bookmarks in Solid Pods based on rdflib.js

63 lines 2.89 kB
import { sym } from "rdflib"; import { createBookmarkWithinContainer, createBookmarkWithinDocument, deleteBookmark, updateBookmark, } from "./update-operations/index.js"; import { ContainerQuery, executeUpdate, ldp, ModuleSupport, rdf, } from "@solid-data-modules/rdflib-utils"; import { bookm } from "./namespaces.js"; import { BookmarkQuery } from "./queries/index.js"; const BOOKM_BOOKMARK = bookm("Bookmark"); export class BookmarksModuleRdfLib { constructor(config) { this.store = config.store; this.fetcher = config.fetcher; this.updater = config.updater; this.support = new ModuleSupport(config); } async listBookmarks(storageUrl) { if (await this.support.isContainer(storageUrl)) { const containerNode = sym(storageUrl); const contents = new ContainerQuery(containerNode, this.store).queryContents(); await this.support.fetchAll(contents); return contents.flatMap((document) => new BookmarkQuery(document, this.store).queryBookmarks()); } else { const bookmarkDoc = sym(storageUrl); return new BookmarkQuery(bookmarkDoc, this.store).queryBookmarks(); } } async discoverStorage(webId) { const registrations = await this.support.discoverType(sym(webId), BOOKM_BOOKMARK); return { privateUrls: urisOf([ ...registrations.private.instances, ...registrations.private.instanceContainers, ]), publicUrls: urisOf([ ...registrations.public.instances, ...registrations.public.instanceContainers, ]), }; } async createBookmark({ storageUrl, title, url, }) { const storageNode = sym(storageUrl); await this.fetcher.load(storageNode.value); const isContainer = this.store.holds(storageNode, rdf("type"), ldp("Container"), storageNode.doc()); const operation = (isContainer ? createBookmarkWithinContainer : createBookmarkWithinDocument)(storageUrl, title, url); await executeUpdate(this.fetcher, this.updater, operation); return operation.uri; } async updateBookmark({ uri, newTitle, newUrl }) { const bookmarkNode = sym(uri); await this.support.fetchNode(bookmarkNode); const operation = updateBookmark(this.store, bookmarkNode, newTitle, newUrl); await executeUpdate(this.fetcher, this.updater, operation); } async deleteBookmark(bookmarkUri) { const bookmarkNode = sym(bookmarkUri); await this.support.fetchNode(bookmarkNode); const operation = deleteBookmark(this.store, bookmarkNode); await executeUpdate(this.fetcher, this.updater, operation); } } function urisOf(nodes) { return nodes.map((it) => it.uri); } //# sourceMappingURL=BookmarksModuleRdfLib.js.map