Implement user profile and pastes retrieval features: add user.html and user.js for user display, create API endpoints for user data, and enhance UI with new styles and elements.

### Copilot Message
This commit is contained in:
whoswhip
2025-05-19 15:12:51 -04:00
parent 1382fb9f07
commit 2126c3a078
9 changed files with 634 additions and 10 deletions
+21 -2
View File
@@ -33,8 +33,20 @@ document.addEventListener("DOMContentLoaded", async function () {
function addInfo(paste) {
document.getElementById("paste-title").innerText = paste.title;
document.getElementById("paste-date").innerText = convertUnixToLocal(paste.created);
document.getElementById("paste-syntax").innerText = paste.syntax === "none" ? "" : paste.syntax || "";
document.getElementById("paste-date").innerText = `${convertUnixToLocal(paste.created)}`;
document.getElementById("paste-syntax").innerText = paste.syntax;
document.getElementById("paste-size").innerText = paste.size === 0 ? "" : formatSize(paste.size);
document.getElementById("paste-author").innerText = paste.username;
document.getElementById("author-link").href = paste.username === "Anonymous" ? "" : `/u/${paste.username}`;
if (paste.username === "Anonymous") {
document.getElementById("author-link").innerText = "Anonymous";
document.getElementById("author-link").href = "";
document.getElementById("author-link").style.pointerEvents = "none";
document.getElementById("author-link").style.cursor = "not-allowed";
document.getElementById("author-link").style.color = "var(--text-color)";
document.getElementById("author-link").style.textDecoration = "none";
}
document.getElementById("paste-views").innerText = paste.views;
document.getElementById("info-throbber").style.display = "none";
document.getElementById("copy-button").addEventListener("click", function () {
navigator.clipboard.writeText(window.location.href);
@@ -51,6 +63,7 @@ function addInfo(paste) {
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
showNotification("Paste downloaded!", "info", 2000);
});
document.getElementById("raw-button").addEventListener("click", function () {
window.location.href = `/raw/${paste.id}`;
@@ -111,4 +124,10 @@ async function decryptAES(content, password) {
function convertUnixToLocal(unix) {
return new Date(unix * 1000).toLocaleString();
}
function formatSize(bytes) {
if (bytes < 1024) return bytes + ' B';
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(2) + ' KB';
return (bytes / (1024 * 1024)).toFixed(2) + ' MB';
}
+76
View File
@@ -0,0 +1,76 @@
let currentPage = 0;
const PAGE_SIZE = 25;
let totalPastes = 0;
let lastPage = false;
async function loadUserPastes(userData, page = 0) {
const pastesRes = await fetch(`/api/users/${userData.uuid}/pastes?limit=${PAGE_SIZE}&page=${page}`);
const data = await pastesRes.json();
let pastes = [];
if (pastesRes.ok) {
pastes = data.pastes || [];
totalPastes = data.total || 0;
lastPage = pastes.length < PAGE_SIZE;
}
const list = document.getElementById("user-pastes-list");
list.innerHTML = "";
if (!pastes.length) {
list.innerHTML = "<p style='color:var(--neutral-400)'>No pastes found.</p>";
return;
}
for (const paste of pastes) {
const li = document.createElement("li");
const link = document.createElement("a");
link.href = `/${paste.id}`;
link.innerHTML = `
<p>${paste.title}</p>
<p>${new Date(paste.created * 1000).toLocaleDateString()}</p>
<p>${paste.syntax || "Plaintext"}</p>
`;
li.appendChild(link);
list.appendChild(li);
}
document.getElementById("paste-pages").innerText = `${page + 1}/${data.pages + 1}`;
document.getElementById("next-button").disabled = page === 0;
document.getElementById("previous-button").disabled = lastPage;
}
document.addEventListener("DOMContentLoaded", async function () {
const username = window.location.pathname.split("/")[2]
if (!username) {
window.location.href = "/error?code=404&message=User not found";
return;
}
const userInfoRes = await fetch(`/api/users/${username}`);
if (!userInfoRes.ok) {
document.getElementById("user-name").innerText = "User not found";
document.getElementById("user-pastes-list").innerHTML = "<p style='color:var(--neutral-400)'>No user data.</p>";
return;
}
const user = await userInfoRes.json();
const userData = user.user ? user.user : user;
document.title = `${userData.username} - Sharpbin`;
document.getElementById("user-name").innerText = userData.username;
document.getElementById("user-uuid").innerText = userData.uuid;
await loadUserPastes(userData, 0);
currentPage = 0;
document.getElementById("user-prev-page").onclick = async function () {
if (currentPage > 0) {
currentPage--;
await loadUserPastes(userData, currentPage);
}
};
document.getElementById("user-next-page").onclick = async function () {
if (!lastPage) {
currentPage++;
await loadUserPastes(userData, currentPage);
}
};
});
function formatSize(bytes) {
if (bytes < 1024) return bytes + ' B';
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(2) + ' KB';
return (bytes / (1024 * 1024)).toFixed(2) + ' MB';
}