70 lines
2.5 KiB
TypeScript
70 lines
2.5 KiB
TypeScript
import type { ObjectType } from '../objectCache'
|
|
|
|
const OBJECT_TYPES: ObjectType[] = ['author', 'series', 'publication', 'review', 'purchase', 'sponsoring', 'review_tip', 'payment_note']
|
|
|
|
export async function updatePublishedStatus(params: { eventId: string; published: false | string[] }): Promise<void> {
|
|
const { objectCache } = await import('../objectCache')
|
|
const { writeService } = await import('../writeService')
|
|
|
|
const updatedFromUnpublished = await tryUpdateFromUnpublished({ objectCache, writeService, eventId: params.eventId, published: params.published })
|
|
if (updatedFromUnpublished) {
|
|
return
|
|
}
|
|
|
|
await tryUpdateFromAllObjects({ objectCache, writeService, eventId: params.eventId, published: params.published })
|
|
}
|
|
|
|
async function tryUpdateFromUnpublished(params: {
|
|
objectCache: typeof import('../objectCache').objectCache
|
|
writeService: typeof import('../writeService').writeService
|
|
eventId: string
|
|
published: false | string[]
|
|
}): Promise<boolean> {
|
|
for (const objectType of OBJECT_TYPES) {
|
|
try {
|
|
const unpublished = await params.objectCache.getUnpublished(objectType)
|
|
const matching = unpublished.find((obj) => isCacheObjectWithEventId(obj, params.eventId))
|
|
if (matching) {
|
|
await params.writeService.updatePublished(objectType, matching.id, params.published)
|
|
return true
|
|
}
|
|
} catch (error) {
|
|
console.warn(`[NostrService] Error checking unpublished in ${objectType}:`, error)
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
async function tryUpdateFromAllObjects(params: {
|
|
objectCache: typeof import('../objectCache').objectCache
|
|
writeService: typeof import('../writeService').writeService
|
|
eventId: string
|
|
published: false | string[]
|
|
}): Promise<void> {
|
|
for (const objectType of OBJECT_TYPES) {
|
|
try {
|
|
const allObjects = await params.objectCache.getAll(objectType)
|
|
const match = allObjects.find((obj) => isCacheObjectWithEventId(obj, params.eventId))
|
|
if (match) {
|
|
await params.writeService.updatePublished(objectType, match.id, params.published)
|
|
return
|
|
}
|
|
} catch (error) {
|
|
console.warn(`[NostrService] Error searching for event in ${objectType}:`, error)
|
|
}
|
|
}
|
|
}
|
|
|
|
type CacheObjectWithEventId = {
|
|
id: string
|
|
event: { id: string }
|
|
}
|
|
|
|
function isCacheObjectWithEventId(value: unknown, eventId: string): value is CacheObjectWithEventId {
|
|
if (typeof value !== 'object' || value === null) {
|
|
return false
|
|
}
|
|
const obj = value as Partial<CacheObjectWithEventId>
|
|
return typeof obj.id === 'string' && typeof obj.event?.id === 'string' && obj.event.id === eventId
|
|
}
|