have aaaaall the images

This commit is contained in:
sakrecoer 2026-08-03 16:03:58 +02:00
parent 29f6ca9691
commit d0aeff8488

View file

@ -265,23 +265,51 @@ function renderRandomPictures(picturesData, containerId, randomize = false) {
const container = document.getElementById(containerId); const container = document.getElementById(containerId);
if (!container) return; if (!container) return;
let selected; // First, flatten all pictures from all events
if (randomize) { let allPictures = [];
const shuffled = shuffleArray(picturesData);
selected = shuffled.slice(0, 6);
} else {
const sorted = [...picturesData].sort((a, b) => b.created_at - a.created_at);
selected = sorted.slice(0, 6);
}
container.innerHTML = ''; picturesData.forEach(event => {
// Check if this event has multiple imeta tags
const imetaTags = [];
event.tags.forEach(tag => {
if (tag[0] === 'imeta') {
imetaTags.push(tag);
}
});
selected.forEach(picture => { if (imetaTags.length > 0) {
let imageUrl = ''; // Parse each imeta tag
let alt = picture.content || ''; imetaTags.forEach(imetaTag => {
let imageUrl = '';
let alt = event.content || '';
if (picture.tags) { // The imeta tag is an array of strings
picture.tags.forEach(tag => { imetaTag.forEach(item => {
if (typeof item === 'string') {
if (item.startsWith('url ')) {
imageUrl = item.replace('url ', '').trim();
}
if (item.startsWith('alt ')) {
alt = item.replace('alt ', '').trim();
}
}
});
if (imageUrl) {
allPictures.push({
url: imageUrl,
alt: alt,
created_at: event.created_at,
pubkey: event.pubkey
});
}
});
} else {
// Fallback: single image event (kind 20 with one imeta)
let imageUrl = '';
let alt = event.content || '';
event.tags.forEach(tag => {
if (tag[0] === 'imeta') { if (tag[0] === 'imeta') {
const imetaStr = tag[1]; const imetaStr = tag[1];
const urlMatch = imetaStr.match(/url ([^\s]+)/); const urlMatch = imetaStr.match(/url ([^\s]+)/);
@ -290,12 +318,35 @@ function renderRandomPictures(picturesData, containerId, randomize = false) {
if (altMatch) alt = altMatch[1]; if (altMatch) alt = altMatch[1];
} }
}); });
}
if (imageUrl) {
allPictures.push({
url: imageUrl,
alt: alt,
created_at: event.created_at,
pubkey: event.pubkey
});
}
}
});
// Now select from all pictures
let selected;
if (randomize) {
const shuffled = shuffleArray(allPictures);
selected = shuffled.slice(0, 6);
} else {
const sorted = [...allPictures].sort((a, b) => b.created_at - a.created_at);
selected = sorted.slice(0, 6);
}
container.innerHTML = '';
selected.forEach(picture => {
const item = document.createElement('div'); const item = document.createElement('div');
item.className = 'photo-item'; item.className = 'photo-item';
item.dataset.image = imageUrl; item.dataset.image = picture.url;
item.dataset.alt = alt; item.dataset.alt = picture.alt;
const date = new Date(picture.created_at * 1000); const date = new Date(picture.created_at * 1000);
const dateStr = date.toLocaleDateString('en-US', { const dateStr = date.toLocaleDateString('en-US', {
@ -305,7 +356,7 @@ function renderRandomPictures(picturesData, containerId, randomize = false) {
item.dataset.pubkey = picture.pubkey || ''; item.dataset.pubkey = picture.pubkey || '';
item.innerHTML = ` item.innerHTML = `
${imageUrl ? `<img src="${imageUrl}" alt="${alt}" loading="lazy">` : ''} ${picture.url ? `<img src="${picture.url}" alt="${picture.alt}" loading="lazy">` : ''}
<div class="photo-date"><span>📅 ${dateStr}</span></div> <div class="photo-date"><span>📅 ${dateStr}</span></div>
`; `;