From d798c2cc700701d05c800f76f756c50068f0156c Mon Sep 17 00:00:00 2001 From: Bojan Mojsilovic Date: Wed, 19 Jun 2024 14:37:09 +0200 Subject: [PATCH] Fix article bookmarks --- .../BookmarkNote/BookmarkArticle.tsx | 24 ++++++++++++------- src/constants.ts | 2 ++ src/contexts/AccountContext.tsx | 4 ++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/components/BookmarkNote/BookmarkArticle.tsx b/src/components/BookmarkNote/BookmarkArticle.tsx index 3b2affc..e4e52aa 100644 --- a/src/components/BookmarkNote/BookmarkArticle.tsx +++ b/src/components/BookmarkNote/BookmarkArticle.tsx @@ -1,7 +1,7 @@ import { useIntl } from '@cookbook/solid-intl'; import { Component, createEffect, createSignal, Match, Show, Switch } from 'solid-js'; import { APP_ID } from '../../App'; -import { Kind } from '../../constants'; +import { Kind, supportedBookmarkTypes } from '../../constants'; import { useAccountContext } from '../../contexts/AccountContext'; import { useAppContext } from '../../contexts/AppContext'; import { getUserFeed } from '../../lib/feed'; @@ -24,12 +24,12 @@ const BookmarkArticle: Component<{ note: PrimalArticle | undefined, large?: bool const [isBookmarked, setIsBookmarked] = createSignal(false); const [bookmarkInProgress, setBookmarkInProgress] = createSignal(false); - createEffect(() => { const note = props.note; if (note) { - setIsBookmarked(() => account?.bookmarks.includes(note.id) || false); + const coor = `${note.msg.kind}:${note.pubkey}:${(note.msg.tags.find(t => t[0] === 'd') || [])[1]}`; + setIsBookmarked(() => account?.bookmarks.includes(coor) || false); } }) @@ -37,7 +37,7 @@ const BookmarkArticle: Component<{ note: PrimalArticle | undefined, large?: bool if (!account) return; const bookmarks = bookmarkTags.reduce((acc, t) => - t[0] === 'e' ? [...acc, t[1]] : [...acc] + supportedBookmarkTypes.includes(t[0]) ? [...acc, t[1]] : [...acc] , []); const date = Math.floor((new Date()).getTime() / 1000); @@ -52,8 +52,12 @@ const BookmarkArticle: Component<{ note: PrimalArticle | undefined, large?: bool }; const addBookmark = async (bookmarkTags: string[][]) => { - if (account && props.note && !bookmarkTags.find(b => b[0] === 'e' && b[1] === props.note?.id)) { - const bookmarksToAdd = [...bookmarkTags, ['e', props.note.id]]; + if (!account || !props.note) return; + + const aTag = ['a', `${props.note.msg.kind}:${props.note.pubkey}:${(props.note.msg.tags.find(t => t[0] === 'd') || [])[1]}`]; + + if (!bookmarkTags.find(b => b[0] === aTag[0] && b[1] === aTag[1])) { + const bookmarksToAdd = [...bookmarkTags, [ ...aTag ]]; if (bookmarksToAdd.length < 2) { logWarning('BOOKMARK ISSUE: ', `before_bookmark_${APP_ID}`); @@ -78,8 +82,12 @@ const BookmarkArticle: Component<{ note: PrimalArticle | undefined, large?: bool } const removeBookmark = async (bookmarks: string[][]) => { - if (account && bookmarks.find(b => b[0] === 'e' && b[1] === props.note?.id)) { - const bookmarksToAdd = bookmarks.filter(b => b[0] !== 'e' || b[1] !== props.note?.id); + if (!account || !props.note) return; + + const aTag = ['a', `${props.note.msg.kind}:${props.note.pubkey}:${(props.note.msg.tags.find(t => t[0] === 'd') || [])[1]}`]; + + if (bookmarks.find(b => b[0] === aTag[0] && b[1] === aTag[1])) { + const bookmarksToAdd = bookmarks.filter(b => b[0] !== aTag[0] || b[1] !== aTag[1]); if (bookmarksToAdd.length < 1) { logWarning('BOOKMARK ISSUE: ', `before_bookmark_${APP_ID}`); diff --git a/src/constants.ts b/src/constants.ts index 7b40401..68e0066 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -409,3 +409,5 @@ export const emptyInvoice: LnbcInvoice = { expiry: 0, route_hints: [], }; + +export const supportedBookmarkTypes = ['a', 'e']; diff --git a/src/contexts/AccountContext.tsx b/src/contexts/AccountContext.tsx index 3ac15c7..e59c1b5 100644 --- a/src/contexts/AccountContext.tsx +++ b/src/contexts/AccountContext.tsx @@ -23,7 +23,7 @@ import { NostrEventContent, PrimalArticle, } from '../types/primal'; -import { Kind, pinEncodePrefix, relayConnectingTimeout } from "../constants"; +import { Kind, pinEncodePrefix, relayConnectingTimeout, supportedBookmarkTypes } from "../constants"; import { isConnected, refreshSocketListeners, removeSocketListeners, socket, subscribeTo, reset, subTo } from "../sockets"; import { sendContacts, sendLike, sendMuteList, triggerImportEvents } from "../lib/notes"; // @ts-ignore Bad types in nostr-tools @@ -1531,7 +1531,7 @@ export function AccountProvider(props: { children: JSXElement }) { } const notes = content.tags.reduce((acc, t) => { - if (t[0] === 'e') { + if (supportedBookmarkTypes.includes(t[0])) { return [...acc, t[1]]; } return [...acc];