|
| 1 | +import { kinds, NostrEvent } from "nostr-tools"; |
| 2 | +import { isReplaceable } from "applesauce-core/helpers"; |
| 3 | +import { addEventBookmarkTag, removeEventBookmarkTag } from "applesauce-factory/operations/tag"; |
| 4 | +import { type EventStore } from "applesauce-core"; |
| 5 | + |
| 6 | +import { Action } from "../action-hub.js"; |
| 7 | +import { |
| 8 | + modifyHiddenTags, |
| 9 | + modifyPublicTags, |
| 10 | + setListDescription, |
| 11 | + setListImage, |
| 12 | + setListTitle, |
| 13 | +} from "applesauce-factory/operations/event"; |
| 14 | + |
| 15 | +function getBookmarkEvent(events: EventStore, self: string, identifier?: string) { |
| 16 | + return events.getReplaceable(identifier ? kinds.Bookmarksets : kinds.BookmarkList, self, identifier); |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * An action that adds a note or article to the bookmark list or a bookmark set |
| 21 | + * @param event the event to bookmark |
| 22 | + * @param identifier the "d" tag of the bookmark set |
| 23 | + * @param hidden set to true to add to hidden bookmarks |
| 24 | + */ |
| 25 | +export function BookmarkEvent(event: NostrEvent, identifier?: string, hidden = false): Action { |
| 26 | + return async ({ events, factory, self, publish }) => { |
| 27 | + const bookmarks = getBookmarkEvent(events, self, identifier); |
| 28 | + if (!bookmarks) throw new Error("Cant find bookmarks"); |
| 29 | + |
| 30 | + const operation = addEventBookmarkTag(event); |
| 31 | + |
| 32 | + const draft = await factory.modifyTags(bookmarks, hidden ? { hidden: operation } : operation); |
| 33 | + await publish(`Bookmark ${isReplaceable(event.kind) ? "note" : "article"}`, await factory.sign(draft)); |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * An action that removes a note or article from the bookmark list or bookmark set |
| 39 | + * @param event the event to remove from bookmarks |
| 40 | + * @param identifier the "d" tag of the bookmark set |
| 41 | + * @param hidden set to true to remove from hidden bookmarks |
| 42 | + */ |
| 43 | +export function UnbookmarkEvent(event: NostrEvent, identifier: string, hidden = false): Action { |
| 44 | + return async ({ events, factory, self, publish }) => { |
| 45 | + const bookmarks = getBookmarkEvent(events, self, identifier); |
| 46 | + if (!bookmarks) throw new Error("Cant find bookmarks"); |
| 47 | + |
| 48 | + const operation = removeEventBookmarkTag(event); |
| 49 | + |
| 50 | + const draft = await factory.modifyTags(bookmarks, hidden ? { hidden: operation } : operation); |
| 51 | + await publish("Remove bookmark", await factory.sign(draft)); |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +/** An action that creates a new bookmark list for a user */ |
| 56 | +export function CreateBookmarkList(bookmarks?: NostrEvent[]): Action { |
| 57 | + return async ({ events, factory, self, publish }) => { |
| 58 | + const existing = getBookmarkEvent(events, self); |
| 59 | + if (existing) throw new Error("Bookmark list already exists"); |
| 60 | + |
| 61 | + const draft = await factory.process( |
| 62 | + { kind: kinds.BookmarkList }, |
| 63 | + bookmarks ? modifyPublicTags(...bookmarks.map(addEventBookmarkTag)) : undefined, |
| 64 | + ); |
| 65 | + await publish("Create bookmark list", await factory.sign(draft)); |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +/** An action that creates a new bookmark set for a user */ |
| 70 | +export function CreateBookmarkSet( |
| 71 | + title: string, |
| 72 | + description: string, |
| 73 | + additional: { image?: string; hidden?: NostrEvent[]; public?: NostrEvent[] }, |
| 74 | +): Action { |
| 75 | + return async ({ events, factory, self, publish }) => { |
| 76 | + const existing = getBookmarkEvent(events, self); |
| 77 | + if (existing) throw new Error("Bookmark list already exists"); |
| 78 | + |
| 79 | + const draft = await factory.process( |
| 80 | + { kind: kinds.BookmarkList }, |
| 81 | + setListTitle(title), |
| 82 | + setListDescription(description), |
| 83 | + additional.image ? setListImage(additional.image) : undefined, |
| 84 | + additional.public ? modifyPublicTags(...additional.public.map(addEventBookmarkTag)) : undefined, |
| 85 | + additional.hidden ? modifyHiddenTags(...additional.hidden.map(addEventBookmarkTag)) : undefined, |
| 86 | + ); |
| 87 | + await publish("Create bookmark set", await factory.sign(draft)); |
| 88 | + }; |
| 89 | +} |
0 commit comments