import { Event } from 'nostr-tools' import { getTagValue } from './nostr' import { ModDetails } from '../types' /** * Extracts and normalizes mod data from an event. * * This function extracts specific tag values from an event and maps them to properties * of a `PageData` object. It handles default values and type conversions as needed. * * @param event - The event object from which to extract data. * @returns A `Partial` object containing extracted data. */ export const extractModData = (event: Event): ModDetails => { // Helper function to safely get the first value of a tag or return a default value const getFirstTagValue = (tagIdentifier: string, defaultValue = '') => { const tagValue = getTagValue(event, tagIdentifier) return tagValue ? tagValue[0] : defaultValue } // Helper function to safely parse integer values from tags const getIntTagValue = (tagIdentifier: string, defaultValue: number = -1) => { const tagValue = getTagValue(event, tagIdentifier) return tagValue ? parseInt(tagValue[0], 10) : defaultValue } return { author: event.pubkey, edited_at: event.created_at, body: event.content, site: getFirstTagValue('t'), published_at: getIntTagValue('published_at'), game: getFirstTagValue('game'), title: getFirstTagValue('title'), featuredImageUrl: getFirstTagValue('featuredImageUrl'), summary: getFirstTagValue('summary'), nsfw: Boolean(getFirstTagValue('nsfw')), screenshotsUrls: getTagValue(event, 'screenshotsUrls') || [], tags: getTagValue(event, 'tags') || [], downloadUrls: (getTagValue(event, 'downloadUrls') || []).map((item) => JSON.parse(item) ) } }