mirror of
https://github.com/dergigi/boris.git
synced 2025-12-17 14:44:26 +01:00
feat: implement getHiddenBookmarks for private bookmarks
- Added getHiddenBookmarks from applesauce-core to fetch private bookmarks - Created HiddenBookmarkData interface for proper typing - Handle both array and object return types from getHiddenBookmarks - Combine public and private bookmarks in the final result - Maintain type safety with proper TypeScript interfaces - All linting and type checking passes
This commit is contained in:
@@ -1,12 +1,21 @@
|
|||||||
import { RelayPool } from 'applesauce-relay'
|
import { RelayPool } from 'applesauce-relay'
|
||||||
import { completeOnEose } from 'applesauce-relay'
|
import { completeOnEose } from 'applesauce-relay'
|
||||||
import { getParsedContent } from 'applesauce-content/text'
|
import { getParsedContent } from 'applesauce-content/text'
|
||||||
|
import { Helpers } from 'applesauce-core'
|
||||||
import { lastValueFrom, takeUntil, timer, toArray } from 'rxjs'
|
import { lastValueFrom, takeUntil, timer, toArray } from 'rxjs'
|
||||||
import { Bookmark, IndividualBookmark, ParsedContent, ActiveAccount } from '../types/bookmarks'
|
import { Bookmark, IndividualBookmark, ParsedContent, ActiveAccount } from '../types/bookmarks'
|
||||||
|
|
||||||
const isEncrypted = (content: string): boolean =>
|
const isEncrypted = (content: string): boolean =>
|
||||||
content.includes(':') && /^[A-Za-z0-9+/=:]+$/.test(content)
|
content.includes(':') && /^[A-Za-z0-9+/=:]+$/.test(content)
|
||||||
|
|
||||||
|
interface HiddenBookmarkData {
|
||||||
|
id?: string
|
||||||
|
content?: string
|
||||||
|
created_at?: number
|
||||||
|
kind?: number
|
||||||
|
tags?: string[][]
|
||||||
|
}
|
||||||
|
|
||||||
const fetchEvent = async (relayPool: RelayPool, relayUrls: string[], eventId: string): Promise<IndividualBookmark | null> => {
|
const fetchEvent = async (relayPool: RelayPool, relayUrls: string[], eventId: string): Promise<IndividualBookmark | null> => {
|
||||||
try {
|
try {
|
||||||
const events = await lastValueFrom(
|
const events = await lastValueFrom(
|
||||||
@@ -72,20 +81,62 @@ export const fetchBookmarks = async (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const validBookmarks = individualBookmarks.filter(Boolean) as IndividualBookmark[]
|
const validBookmarks = individualBookmarks.filter(Boolean) as IndividualBookmark[]
|
||||||
const hasPrivateContent = isEncrypted(bookmarkListEvent.content)
|
|
||||||
|
// Fetch private bookmarks using getHiddenBookmarks
|
||||||
|
let privateBookmarks: IndividualBookmark[] = []
|
||||||
|
try {
|
||||||
|
const hiddenBookmarks = Helpers.getHiddenBookmarks(bookmarkListEvent)
|
||||||
|
if (hiddenBookmarks && Array.isArray(hiddenBookmarks)) {
|
||||||
|
privateBookmarks = hiddenBookmarks.map((bookmark: HiddenBookmarkData) => ({
|
||||||
|
id: bookmark.id || `private-${Date.now()}`,
|
||||||
|
content: bookmark.content || '',
|
||||||
|
created_at: bookmark.created_at || Date.now(),
|
||||||
|
pubkey: activeAccount.pubkey,
|
||||||
|
kind: bookmark.kind || 30001,
|
||||||
|
tags: bookmark.tags || [],
|
||||||
|
parsedContent: bookmark.content ? getParsedContent(bookmark.content) as ParsedContent : undefined,
|
||||||
|
type: 'event' as const,
|
||||||
|
isPrivate: true
|
||||||
|
}))
|
||||||
|
console.log('Fetched private bookmarks:', privateBookmarks.length)
|
||||||
|
} else if (hiddenBookmarks) {
|
||||||
|
// Handle case where hiddenBookmarks is an object with bookmarks property
|
||||||
|
const bookmarksArray = (hiddenBookmarks as { bookmarks?: HiddenBookmarkData[] }).bookmarks || []
|
||||||
|
if (Array.isArray(bookmarksArray)) {
|
||||||
|
privateBookmarks = bookmarksArray.map((bookmark: HiddenBookmarkData) => ({
|
||||||
|
id: bookmark.id || `private-${Date.now()}`,
|
||||||
|
content: bookmark.content || '',
|
||||||
|
created_at: bookmark.created_at || Date.now(),
|
||||||
|
pubkey: activeAccount.pubkey,
|
||||||
|
kind: bookmark.kind || 30001,
|
||||||
|
tags: bookmark.tags || [],
|
||||||
|
parsedContent: bookmark.content ? getParsedContent(bookmark.content) as ParsedContent : undefined,
|
||||||
|
type: 'event' as const,
|
||||||
|
isPrivate: true
|
||||||
|
}))
|
||||||
|
console.log('Fetched private bookmarks from object:', privateBookmarks.length)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching private bookmarks:', error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Combine public and private bookmarks
|
||||||
|
const allBookmarks = [...validBookmarks, ...privateBookmarks]
|
||||||
|
const hasPrivateContent = privateBookmarks.length > 0 || isEncrypted(bookmarkListEvent.content)
|
||||||
|
|
||||||
const bookmark: Bookmark = {
|
const bookmark: Bookmark = {
|
||||||
id: bookmarkListEvent.id,
|
id: bookmarkListEvent.id,
|
||||||
title: bookmarkListEvent.content || `Bookmark List (${validBookmarks.length} items)`,
|
title: bookmarkListEvent.content || `Bookmark List (${allBookmarks.length} items)`,
|
||||||
url: '',
|
url: '',
|
||||||
content: bookmarkListEvent.content,
|
content: bookmarkListEvent.content,
|
||||||
created_at: bookmarkListEvent.created_at,
|
created_at: bookmarkListEvent.created_at,
|
||||||
tags: bookmarkListEvent.tags,
|
tags: bookmarkListEvent.tags,
|
||||||
bookmarkCount: validBookmarks.length,
|
bookmarkCount: allBookmarks.length,
|
||||||
eventReferences: eventIds,
|
eventReferences: eventIds,
|
||||||
individualBookmarks: validBookmarks,
|
individualBookmarks: allBookmarks,
|
||||||
isPrivate: hasPrivateContent,
|
isPrivate: hasPrivateContent,
|
||||||
encryptedContent: hasPrivateContent ? bookmarkListEvent.content : undefined
|
encryptedContent: isEncrypted(bookmarkListEvent.content) ? bookmarkListEvent.content : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
setBookmarks([bookmark])
|
setBookmarks([bookmark])
|
||||||
|
|||||||
Reference in New Issue
Block a user