2024-10-14 13:24:43 +05:00
|
|
|
import { NDKEvent } from '@nostr-dev-kit/ndk'
|
|
|
|
import { Event } from 'nostr-tools'
|
|
|
|
import { ModDetails, ModFormState } from '../types'
|
2024-12-03 10:47:55 +01:00
|
|
|
import { getTagValue, getTagValues } from './nostr'
|
2024-07-25 20:05:28 +05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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<PageData>` object containing extracted data.
|
|
|
|
*/
|
2024-10-14 13:24:43 +05:00
|
|
|
export const extractModData = (event: Event | NDKEvent): ModDetails => {
|
2024-07-25 20:05:28 +05:00
|
|
|
// 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 {
|
2024-08-06 15:46:38 +05:00
|
|
|
id: event.id,
|
|
|
|
dTag: getFirstTagValue('d'),
|
|
|
|
aTag: getFirstTagValue('a'),
|
|
|
|
rTag: getFirstTagValue('r'),
|
2024-07-25 20:05:28 +05:00
|
|
|
author: event.pubkey,
|
2024-10-14 13:24:43 +05:00
|
|
|
edited_at: event.created_at!,
|
2024-07-25 20:05:28 +05:00
|
|
|
body: event.content,
|
|
|
|
published_at: getIntTagValue('published_at'),
|
|
|
|
game: getFirstTagValue('game'),
|
|
|
|
title: getFirstTagValue('title'),
|
|
|
|
featuredImageUrl: getFirstTagValue('featuredImageUrl'),
|
|
|
|
summary: getFirstTagValue('summary'),
|
2024-08-06 15:46:38 +05:00
|
|
|
nsfw: getFirstTagValue('nsfw') === 'true',
|
2024-11-27 12:33:09 +01:00
|
|
|
repost: getFirstTagValue('repost') === 'true',
|
2024-11-27 17:17:54 +01:00
|
|
|
originalAuthor: getFirstTagValue('originalAuthor'),
|
2024-07-25 20:05:28 +05:00
|
|
|
screenshotsUrls: getTagValue(event, 'screenshotsUrls') || [],
|
|
|
|
tags: getTagValue(event, 'tags') || [],
|
2024-12-03 10:47:55 +01:00
|
|
|
LTags: (getTagValues(event, 'L') || []).map((t) =>
|
|
|
|
t.replace('com.degmods:', '')
|
|
|
|
),
|
|
|
|
lTags: (getTagValues(event, 'l') || []).map((t) =>
|
|
|
|
t.replace('com.degmods:', '')
|
|
|
|
),
|
2024-07-25 20:05:28 +05:00
|
|
|
downloadUrls: (getTagValue(event, 'downloadUrls') || []).map((item) =>
|
|
|
|
JSON.parse(item)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2024-07-29 11:26:26 +05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a list of `ModDetails` objects from an array of events.
|
|
|
|
*
|
|
|
|
* This function filters out events that do not contain all required data,
|
|
|
|
* extracts the necessary information from the valid events, and constructs
|
|
|
|
* `ModDetails` objects.
|
|
|
|
*
|
|
|
|
* @param events - The array of event objects to be processed.
|
|
|
|
* @returns An array of `ModDetails` objects constructed from valid events.
|
|
|
|
*/
|
2024-10-14 13:24:43 +05:00
|
|
|
export const constructModListFromEvents = (
|
|
|
|
events: Event[] | NDKEvent[]
|
|
|
|
): ModDetails[] => {
|
2024-07-29 11:26:26 +05:00
|
|
|
// Filter and extract mod details from events
|
|
|
|
const modDetailsList: ModDetails[] = events
|
|
|
|
.filter(isModDataComplete) // Filter out incomplete events
|
|
|
|
.map((event) => extractModData(event)) // Extract data and construct ModDetails
|
|
|
|
|
|
|
|
return modDetailsList
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the provided event contains all the required data for constructing a `ModDetails` object.
|
|
|
|
*
|
|
|
|
* This function verifies that the event has the necessary tags and values to construct a `ModDetails` object.
|
|
|
|
*
|
|
|
|
* @param event - The event object to be checked.
|
|
|
|
* @returns `true` if the event contains all required data; `false` otherwise.
|
|
|
|
*/
|
2024-10-14 13:24:43 +05:00
|
|
|
export const isModDataComplete = (event: Event | NDKEvent): boolean => {
|
2024-07-29 11:26:26 +05:00
|
|
|
// Helper function to check if a tag value is present and not empty
|
|
|
|
const hasTagValue = (tagIdentifier: string): boolean => {
|
|
|
|
const value = getTagValue(event, tagIdentifier)
|
|
|
|
return !!value && value.length > 0 && value[0].trim() !== ''
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if all required fields are present
|
|
|
|
return (
|
2024-08-06 15:46:38 +05:00
|
|
|
hasTagValue('d') &&
|
|
|
|
hasTagValue('a') &&
|
2024-07-29 11:26:26 +05:00
|
|
|
hasTagValue('t') &&
|
|
|
|
hasTagValue('published_at') &&
|
|
|
|
hasTagValue('game') &&
|
|
|
|
hasTagValue('title') &&
|
|
|
|
hasTagValue('featuredImageUrl') &&
|
|
|
|
hasTagValue('summary') &&
|
|
|
|
hasTagValue('nsfw') &&
|
|
|
|
getTagValue(event, 'screenshotsUrls') !== null &&
|
|
|
|
getTagValue(event, 'tags') !== null &&
|
|
|
|
getTagValue(event, 'downloadUrls') !== null
|
|
|
|
)
|
|
|
|
}
|
2024-08-06 15:46:38 +05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the form state with values from existing mod data or defaults.
|
|
|
|
*
|
|
|
|
* @param existingModData - An optional object containing existing mod details. If provided, its values will be used to populate the form state.
|
|
|
|
* @returns The initial state for the form, with values from existingModData if available, otherwise default values.
|
|
|
|
*/
|
|
|
|
export const initializeFormState = (
|
|
|
|
existingModData?: ModDetails
|
|
|
|
): ModFormState => ({
|
|
|
|
dTag: existingModData?.dTag || '',
|
|
|
|
aTag: existingModData?.aTag || '',
|
|
|
|
rTag: existingModData?.rTag || window.location.host,
|
|
|
|
game: existingModData?.game || '',
|
|
|
|
title: existingModData?.title || '',
|
|
|
|
body: existingModData?.body || '',
|
|
|
|
featuredImageUrl: existingModData?.featuredImageUrl || '',
|
|
|
|
summary: existingModData?.summary || '',
|
|
|
|
nsfw: existingModData?.nsfw || false,
|
2024-11-27 12:33:09 +01:00
|
|
|
repost: existingModData?.repost || false,
|
2024-11-27 17:17:54 +01:00
|
|
|
originalAuthor: existingModData?.originalAuthor || undefined,
|
2024-08-06 15:46:38 +05:00
|
|
|
screenshotsUrls: existingModData?.screenshotsUrls || [''],
|
|
|
|
tags: existingModData?.tags.join(',') || '',
|
2024-12-03 10:47:55 +01:00
|
|
|
lTags: existingModData?.lTags || [],
|
|
|
|
LTags: existingModData?.LTags || [],
|
2024-08-06 15:46:38 +05:00
|
|
|
downloadUrls: existingModData?.downloadUrls || [
|
|
|
|
{
|
|
|
|
url: '',
|
|
|
|
hash: '',
|
|
|
|
signatureKey: '',
|
|
|
|
malwareScanLink: '',
|
|
|
|
modVersion: '',
|
|
|
|
customNote: ''
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|