2024-03-16 16:37:47 -05:00
|
|
|
import { nip19 } from "nostr-tools";
|
|
|
|
|
2024-02-11 16:26:33 -06:00
|
|
|
export const findKind0Fields = async (kind0) => {
|
|
|
|
let fields = {}
|
|
|
|
|
2024-02-11 00:00:27 -06:00
|
|
|
const usernameProperties = ['name', 'displayName', 'display_name', 'username', 'handle', 'alias'];
|
|
|
|
|
|
|
|
const findTruthyPropertyValue = (object, properties) => {
|
|
|
|
for (const property of properties) {
|
|
|
|
if (object[property]) {
|
|
|
|
return object[property];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const username = findTruthyPropertyValue(kind0, usernameProperties);
|
|
|
|
|
2024-02-11 16:26:33 -06:00
|
|
|
if (username) {
|
|
|
|
fields.username = username;
|
|
|
|
}
|
|
|
|
|
|
|
|
const avatar = findTruthyPropertyValue(kind0, ['picture', 'avatar', 'profilePicture', 'profile_picture']);
|
|
|
|
|
|
|
|
if (avatar) {
|
|
|
|
fields.avatar = avatar;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:29:57 -06:00
|
|
|
export const parseEvent = (event) => {
|
2024-02-11 16:26:33 -06:00
|
|
|
// Initialize an object to store the extracted data
|
|
|
|
const eventData = {
|
2024-02-27 18:29:57 -06:00
|
|
|
id: event.id,
|
2024-03-16 16:37:47 -05:00
|
|
|
pubkey: event.pubkey || '',
|
2024-02-11 16:26:33 -06:00
|
|
|
content: event.content || '',
|
2024-04-22 19:09:46 -05:00
|
|
|
kind: event.kind || '',
|
2024-02-11 16:26:33 -06:00
|
|
|
title: '',
|
|
|
|
summary: '',
|
|
|
|
image: '',
|
|
|
|
published_at: '',
|
2024-04-24 11:19:31 -05:00
|
|
|
topics: [], // Added to hold all topics
|
2024-02-11 16:26:33 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Iterate over the tags array to extract data
|
|
|
|
event.tags.forEach(tag => {
|
|
|
|
switch (tag[0]) { // Check the key in each key-value pair
|
|
|
|
case 'title':
|
|
|
|
eventData.title = tag[1];
|
|
|
|
break;
|
|
|
|
case 'summary':
|
|
|
|
eventData.summary = tag[1];
|
|
|
|
break;
|
|
|
|
case 'image':
|
|
|
|
eventData.image = tag[1];
|
|
|
|
break;
|
|
|
|
case 'published_at':
|
|
|
|
eventData.published_at = tag[1];
|
|
|
|
break;
|
2024-03-16 16:37:47 -05:00
|
|
|
case 'author':
|
|
|
|
eventData.author = tag[1];
|
|
|
|
break;
|
2024-04-22 19:09:46 -05:00
|
|
|
case 'd':
|
|
|
|
eventData.d = tag[1];
|
|
|
|
break;
|
2024-04-24 11:19:31 -05:00
|
|
|
case 't':
|
|
|
|
tag[1] !== "plebdevs" && eventData.topics.push(tag[1]);
|
|
|
|
break;
|
2024-03-16 16:37:47 -05:00
|
|
|
default:
|
|
|
|
break;
|
2024-02-11 16:26:33 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-07-22 16:50:09 -05:00
|
|
|
// if published_at is an empty string, then set it to event.created_at
|
|
|
|
if (!eventData.published_at) {
|
|
|
|
eventData.published_at = event.created_at;
|
|
|
|
}
|
|
|
|
|
2024-02-11 16:26:33 -06:00
|
|
|
return eventData;
|
|
|
|
};
|
2024-03-16 16:37:47 -05:00
|
|
|
|
2024-07-22 16:11:46 -05:00
|
|
|
export const parseCourseEvent = (event) => {
|
|
|
|
// Initialize an object to store the extracted data
|
|
|
|
const eventData = {
|
|
|
|
id: event.id,
|
|
|
|
pubkey: event.pubkey || '',
|
|
|
|
content: event.content || '',
|
|
|
|
kind: event.kind || '',
|
|
|
|
name: '',
|
|
|
|
description: '',
|
|
|
|
image: '',
|
|
|
|
published_at: '',
|
2024-07-22 17:16:36 -05:00
|
|
|
created_at: event.created_at,
|
2024-07-22 16:11:46 -05:00
|
|
|
topics: [],
|
|
|
|
d: '',
|
2024-07-22 17:16:36 -05:00
|
|
|
tags: event.tags
|
2024-07-22 16:11:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Iterate over the tags array to extract data
|
|
|
|
event.tags.forEach(tag => {
|
|
|
|
switch (tag[0]) { // Check the key in each key-value pair
|
|
|
|
case 'name':
|
|
|
|
eventData.name = tag[1];
|
|
|
|
break;
|
|
|
|
case 'description':
|
|
|
|
eventData.description = tag[1];
|
|
|
|
break;
|
|
|
|
case 'image':
|
|
|
|
eventData.image = tag[1];
|
|
|
|
break;
|
|
|
|
case 'picture':
|
|
|
|
eventData.image = tag[1];
|
|
|
|
break;
|
|
|
|
case 'published_at':
|
|
|
|
eventData.published_at = tag[1];
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
eventData.d = tag[1];
|
|
|
|
break;
|
|
|
|
// How do we get topics / tags?
|
|
|
|
case 'l':
|
|
|
|
// Grab index 1 and any subsequent elements in the array
|
|
|
|
tag.slice(1).forEach(topic => {
|
|
|
|
eventData.topics.push(topic);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return eventData;
|
|
|
|
}
|
|
|
|
|
2024-03-16 16:37:47 -05:00
|
|
|
export const hexToNpub = (hex) => {
|
|
|
|
return nip19.npubEncode(hex);
|
|
|
|
}
|