mirror of
https://github.com/dergigi/boris.git
synced 2026-01-23 08:44:26 +01:00
- Remove dual-write logic: only write kind 39802 - Remove legacy kind 30078 read fallback - Remove migration settings flags (useReadingProgressKind, writeLegacyReadingPosition) - Simplify readingPositionService: single write/read path - Remove processReadingPositions() legacy processor - Update readsService and linksService to only query kind 39802 - Simplify NIP-39802 spec: remove migration section - Delete READING_PROGRESS_MIGRATION.md (not needed for unreleased app) - Clean up imports and comments No backward compatibility needed since app hasn't been released yet.
91 lines
3.0 KiB
TypeScript
91 lines
3.0 KiB
TypeScript
import { RelayPool } from 'applesauce-relay'
|
|
import { fetchReadArticles } from './libraryService'
|
|
import { queryEvents } from './dataFetch'
|
|
import { RELAYS } from '../config/relays'
|
|
import { KINDS } from '../config/kinds'
|
|
import { ReadItem } from './readsService'
|
|
import { processReadingProgress, processMarkedAsRead, filterValidItems, sortByReadingActivity } from './readingDataProcessor'
|
|
import { mergeReadItem } from '../utils/readItemMerge'
|
|
|
|
/**
|
|
* Fetches external URL links with reading progress from:
|
|
* - URLs with reading progress (kind:39802)
|
|
* - Manually marked as read URLs (kind:7, kind:17)
|
|
*/
|
|
export async function fetchLinks(
|
|
relayPool: RelayPool,
|
|
userPubkey: string,
|
|
onItem?: (item: ReadItem) => void
|
|
): Promise<ReadItem[]> {
|
|
console.log('🔗 [Links] Fetching external links for user:', userPubkey.slice(0, 8))
|
|
|
|
const linksMap = new Map<string, ReadItem>()
|
|
|
|
// Helper to emit items as they're added/updated
|
|
const emitItem = (item: ReadItem) => {
|
|
if (onItem && mergeReadItem(linksMap, item)) {
|
|
onItem(linksMap.get(item.id)!)
|
|
} else if (!onItem) {
|
|
linksMap.set(item.id, item)
|
|
}
|
|
}
|
|
|
|
try {
|
|
// Fetch all data sources in parallel
|
|
const [progressEvents, markedAsReadArticles] = await Promise.all([
|
|
queryEvents(relayPool, { kinds: [KINDS.ReadingProgress], authors: [userPubkey] }, { relayUrls: RELAYS }),
|
|
fetchReadArticles(relayPool, userPubkey)
|
|
])
|
|
|
|
console.log('📊 [Links] Data fetched:', {
|
|
readingProgress: progressEvents.length,
|
|
markedAsRead: markedAsReadArticles.length
|
|
})
|
|
|
|
// Process reading progress events (kind 39802)
|
|
processReadingProgress(progressEvents, linksMap)
|
|
if (onItem) {
|
|
linksMap.forEach(item => {
|
|
if (item.type === 'external') {
|
|
const hasProgress = (item.readingProgress && item.readingProgress > 0) || item.markedAsRead
|
|
if (hasProgress) emitItem(item)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Process marked-as-read and emit external items
|
|
processMarkedAsRead(markedAsReadArticles, linksMap)
|
|
if (onItem) {
|
|
linksMap.forEach(item => {
|
|
if (item.type === 'external') {
|
|
const hasProgress = (item.readingProgress && item.readingProgress > 0) || item.markedAsRead
|
|
if (hasProgress) emitItem(item)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Filter for external URLs only with reading progress
|
|
const links = Array.from(linksMap.values())
|
|
.filter(item => {
|
|
// Only external URLs
|
|
if (item.type !== 'external') return false
|
|
|
|
// Only include if there's reading progress or marked as read
|
|
const hasProgress = (item.readingProgress && item.readingProgress > 0) || item.markedAsRead
|
|
return hasProgress
|
|
})
|
|
|
|
// Apply common validation and sorting
|
|
const validLinks = filterValidItems(links)
|
|
const sortedLinks = sortByReadingActivity(validLinks)
|
|
|
|
console.log('✅ [Links] Processed', sortedLinks.length, 'total links')
|
|
return sortedLinks
|
|
|
|
} catch (error) {
|
|
console.error('Failed to fetch links:', error)
|
|
return []
|
|
}
|
|
}
|
|
|