gallery now supports multiple users

This commit is contained in:
2025-12-06 02:07:32 +01:00
parent 2df7625657
commit e7388bbb25
6 changed files with 97 additions and 51 deletions

View File

@@ -19,6 +19,13 @@ sentinel.style.marginBottom = '300px';
sentinel.style.display = 'none';
resultsContainer.parentNode.appendChild(sentinel);
function getAuthHeaders(extra = {}) {
const token = localStorage.getItem("token");
return token
? { ...extra, Authorization: `Bearer ${token}` }
: extra;
}
function initializeMasonry() {
if (typeof Masonry === 'undefined') {
setTimeout(initializeMasonry, 100);
@@ -84,7 +91,9 @@ function getProxiedImageUrl(item) {
async function loadFavorites() {
try {
const res = await fetch('/api/gallery/favorites');
const res = await fetch('/api/gallery/favorites', {
headers: getAuthHeaders()
});
if (!res.ok) return;
const data = await res.json();
favorites.clear();
@@ -100,7 +109,10 @@ async function toggleFavorite(item) {
try {
if (isFav) {
await fetch(`/api/gallery/favorites/${encodeURIComponent(id)}`, { method: 'DELETE' });
await fetch(`/api/gallery/favorites/${encodeURIComponent(id)}`, {
method: 'DELETE',
headers: getAuthHeaders()
});
favorites.delete(id);
} else {
const tagsArray = getTagsArray(item);
@@ -109,7 +121,9 @@ async function toggleFavorite(item) {
await fetch('/api/gallery/favorites', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
headers: getAuthHeaders({
'Content-Type': 'application/json'
}),
body: JSON.stringify({
id: item.id,
title: item.title || tagsArray.join(', ') || 'Untitled',
@@ -121,6 +135,7 @@ async function toggleFavorite(item) {
})
});
favorites.add(id);
}
@@ -238,7 +253,9 @@ async function searchGallery(isLoadMore = false) {
let data;
if (favoritesMode) {
const res = await fetch('/api/gallery/favorites');
const res = await fetch('/api/gallery/favorites', {
headers: getAuthHeaders()
});
data = await res.json();
let favoritesResults = data.favorites || [];

View File

@@ -1,6 +1,13 @@
const itemMainContentContainer = document.getElementById('item-main-content');
let currentItem = null;
function getAuthHeaders(extra = {}) {
const token = localStorage.getItem("token");
return token
? { ...extra, Authorization: `Bearer ${token}` }
: extra;
}
function getProxiedItemUrl(url, headers = null) {
if (!url || !headers) {
return url;
@@ -52,14 +59,19 @@ async function toggleFavorite() {
try {
if (wasFavorited) {
await fetch(`/api/gallery/favorites/${encodeURIComponent(currentItem.id)}`, { method: 'DELETE' });
await fetch(`/api/gallery/favorites/${encodeURIComponent(currentItem.id)}`, {
method: 'DELETE',
headers: getAuthHeaders()
});
} else {
const serializedHeaders = currentItem.headers ? JSON.stringify(currentItem.headers) : "";
const tagsString = Array.isArray(currentItem.tags) ? currentItem.tags.join(',') : (currentItem.tags || '');
await fetch('/api/gallery/favorites', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
headers: getAuthHeaders({
'Content-Type': 'application/json'
}),
body: JSON.stringify({
id: currentItem.id,
title: currentItem.title || 'Waifu',
@@ -70,6 +82,7 @@ async function toggleFavorite() {
headers: serializedHeaders
})
});
}
btn.classList.toggle('favorited', !wasFavorited);
@@ -216,7 +229,9 @@ function renderError(msg) {
async function loadFromFavorites(id) {
try {
const res = await fetch(`/api/gallery/favorites/${encodeURIComponent(id)}`);
const res = await fetch(`/api/gallery/favorites/${encodeURIComponent(id)}`, {
headers: getAuthHeaders()
});
if (!res.ok) throw new Error('Not found');
const { favorite: fav } = await res.json();
@@ -269,7 +284,9 @@ async function loadFromProvider(provider, id) {
renderItem(item);
document.getElementById('page-title').textContent = `WaifuBoard - ${item.title}`;
const favRes = await fetch(`/api/gallery/favorites/${encodeURIComponent(id)}`);
const favRes = await fetch(`/api/gallery/favorites/${encodeURIComponent(id)}`, {
headers: getAuthHeaders()
});
if (favRes.ok) {
document.getElementById('fav-btn')?.classList.add('favorited');
const btn = document.getElementById('fav-btn');