feat: add leaderboard router, gamification backfill, and shared content creation helpers

Introduce a new `/leaderboard` endpoint ranking top 50 members by total stars, wire it into the app router and documentation. Implement `_backfill_gamification()` in `database.py` to compute XP/levels for existing accounts from prior posts, comments, votes, and follows. Extract `is_owner()`, `create_content_item()`, and `detail_context()` into `content.py` to centralize content creation, reward awarding, mention notifications, and attachment linking. Update comment creation/deletion in `comments.py` to use `award_rewards()` with `XP_COMMENT` and the new `is_owner()` helper. Add shared template partials documentation for vote bars, star buttons, post headers, and topic selectors to `AGENTS.md`.
This commit is contained in:
2026-05-30 18:16:39 +00:00
parent ab4861562b
commit a136287d36
57 changed files with 1310 additions and 674 deletions
-4
View File
@@ -265,10 +265,6 @@
transform: translateX(18px);
}
.hidden {
display: none !important;
}
.pagination {
display: flex;
align-items: center;
+30
View File
@@ -81,6 +81,9 @@
.inline-form {
display: inline;
}
.hidden {
display: none !important;
}
.text-muted {
color: var(--text-muted);
}
@@ -115,6 +118,14 @@
color: var(--text-muted);
padding: 0.5rem 0;
}
.top-authors-link {
display: block;
margin-top: 0.5rem;
font-size: 0.75rem;
font-weight: 600;
color: var(--accent);
text-align: center;
}
.no-comments-msg {
color: var(--text-muted);
font-size: 0.875rem;
@@ -986,3 +997,22 @@ img {
position: relative;
z-index: 2;
}
.vote-star {
display: inline-flex;
align-items: center;
gap: 0.25rem;
}
.vote-star::before {
content: "\2606";
}
.vote-star.voted::before {
content: "\2605";
}
.vote-star.voted {
color: var(--warning);
font-weight: 700;
}
+58
View File
@@ -0,0 +1,58 @@
.bugs-layout {
max-width: 720px;
margin: 0 auto;
}
.bugs-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 1rem;
}
.bugs-header h1 {
font-size: 1.5rem;
font-weight: 700;
}
.bug-card {
background: var(--bg-card);
border: 1px solid var(--border);
border-radius: var(--radius-lg);
padding: 1.25rem;
margin-bottom: 1rem;
}
.bug-card-header {
display: flex;
align-items: center;
gap: 0.75rem;
margin-bottom: 0.5rem;
}
.bug-title {
font-size: 1rem;
font-weight: 600;
color: var(--text-primary);
}
.bug-status {
font-size: 0.6875rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.05em;
padding: 0.125rem 0.5rem;
border-radius: 999px;
}
.bug-status.open {
background: var(--accent-light);
color: var(--accent);
}
.bug-status.closed {
background: rgba(76, 175, 80, 0.1);
color: var(--success);
}
.bug-desc {
font-size: 0.875rem;
color: var(--text-secondary);
line-height: 1.5;
margin-bottom: 0.5rem;
}
.bug-meta {
font-size: 0.75rem;
color: var(--text-muted);
}
+174 -6
View File
@@ -17,7 +17,9 @@
border: 1px solid var(--border);
}
.feed-nav-btn {
.feed-nav-btn,
.projects-tab,
.profile-tab {
padding: 0.5rem 1rem;
border-radius: var(--radius);
font-size: 0.8125rem;
@@ -178,15 +180,24 @@
color: var(--text-secondary);
}
.post-action-btn.voted,
.post-action-btn.vote-up {
.post-action-btn.vote-up:hover {
color: var(--accent);
}
.post-action-btn.vote-down {
.post-action-btn.vote-down:hover {
color: var(--danger);
}
.post-action-btn.vote-up.voted {
color: var(--accent);
font-weight: 700;
}
.post-action-btn.vote-down.voted {
color: var(--danger);
font-weight: 700;
}
.post-votes {
display: inline-flex;
align-items: center;
@@ -287,6 +298,14 @@
display: flex;
align-items: center;
gap: 0.375rem;
min-width: 0;
}
.top-author-name {
color: var(--text-secondary);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.stat-row .value {
@@ -302,8 +321,8 @@
width: 56px;
height: 56px;
border-radius: 50%;
background: #e53935;
color: #fff;
background: var(--danger);
color: var(--white);
font-size: 1.5rem;
display: flex;
align-items: center;
@@ -355,6 +374,12 @@
.feed-right {
display: none;
}
.leaderboard-page {
grid-template-columns: 1fr;
}
.leaderboard-page > aside {
display: none;
}
}
@media (max-width: 768px) {
@@ -428,3 +453,146 @@
width: 100%;
}
}
.leaderboard-page {
display: grid;
grid-template-columns: 280px 1fr 280px;
gap: 1.5rem;
align-items: start;
}
.leaderboard-page > aside {
position: sticky;
top: calc(var(--nav-height) + 1rem);
}
.leaderboard-main {
min-width: 0;
}
.featured-news {
background: var(--bg-card);
border: 1px solid var(--border);
border-radius: var(--radius-lg);
padding: 1.25rem;
}
.featured-news h3 {
font-size: 0.75rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.1em;
color: var(--text-muted);
margin-bottom: 0.75rem;
}
.featured-news-item {
display: flex;
flex-direction: column;
gap: 0.125rem;
padding: 0.5rem 0;
}
.featured-news-item:not(:last-child) {
border-bottom: 1px solid var(--border);
}
.featured-news-title {
font-size: 0.8125rem;
font-weight: 600;
color: var(--text-primary);
line-height: 1.4;
}
.featured-news-meta {
font-size: 0.6875rem;
color: var(--text-muted);
}
.leaderboard-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 0.25rem;
}
.leaderboard-header h1 {
font-size: 1.5rem;
font-weight: 700;
}
.leaderboard-you {
font-size: 0.8125rem;
font-weight: 600;
color: var(--accent);
background: var(--accent-light);
padding: 0.25rem 0.625rem;
border-radius: 999px;
}
.leaderboard-intro {
font-size: 0.8125rem;
color: var(--text-muted);
margin-bottom: 1rem;
}
.leaderboard-list {
list-style: none;
margin: 0;
padding: 0;
}
.leaderboard-row {
display: flex;
align-items: center;
gap: 0.75rem;
padding: 0.75rem 1rem;
background: var(--bg-card);
border: 1px solid var(--border);
border-radius: var(--radius);
margin-bottom: 0.5rem;
}
.leaderboard-row-self {
border-color: var(--accent);
background: var(--accent-light);
}
.leaderboard-rank {
font-weight: 700;
font-size: 0.9375rem;
color: var(--text-muted);
min-width: 2.5rem;
}
.leaderboard-name {
flex: 1;
min-width: 0;
font-weight: 600;
color: var(--text-primary);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.leaderboard-level {
font-size: 0.75rem;
color: var(--text-muted);
}
.leaderboard-stars {
font-weight: 700;
color: var(--text-primary);
}
.leaderboard-star-icon {
color: var(--accent);
}
.leaderboard-empty {
list-style: none;
padding: 2rem 1rem;
text-align: center;
color: var(--text-muted);
font-size: 0.875rem;
}
+15 -44
View File
@@ -100,10 +100,24 @@
transition: color 0.2s;
}
.comment-vote-btn:hover {
.comment-vote-btn.vote-up:hover {
color: var(--accent);
}
.comment-vote-btn.vote-down:hover {
color: var(--danger);
}
.comment-vote-btn.vote-up.voted {
color: var(--accent);
font-weight: 700;
}
.comment-vote-btn.vote-down.voted {
color: var(--danger);
font-weight: 700;
}
.comment-vote-count {
font-size: 0.75rem;
font-weight: 700;
@@ -230,49 +244,6 @@ button.comment-form-submit:hover {
background: var(--accent-hover);
}
.post-action-btn {
display: inline-flex;
align-items: center;
gap: 0.375rem;
padding: 0.375rem 0.75rem;
border-radius: var(--radius);
font-size: 0.8125rem;
font-weight: 500;
color: var(--text-muted);
transition: all 0.2s;
background: none;
border: none;
cursor: pointer;
line-height: 1;
}
.post-action-btn:hover {
background: var(--bg-card-hover);
color: var(--text-secondary);
}
.post-action-btn.vote-up {
color: var(--accent);
}
.post-action-btn.vote-down {
color: var(--danger);
}
.post-votes {
display: inline-flex;
align-items: center;
gap: 0.125rem;
}
.post-vote-count {
font-weight: 600;
font-size: 0.8125rem;
color: var(--text-secondary);
min-width: 1.25rem;
text-align: center;
}
.comment-thread-line {
position: absolute;
left: 16px;
+14 -9
View File
@@ -56,6 +56,15 @@
display: block;
}
a.profile-stat-value {
color: var(--text-primary);
text-decoration: none;
}
a.profile-stat-value:hover {
color: var(--accent);
}
.profile-stat-label {
font-size: 0.75rem;
color: var(--text-muted);
@@ -109,6 +118,11 @@
color: var(--accent);
}
.profile-badge-icon {
font-size: 0.875rem;
line-height: 1;
}
.profile-info {
background: var(--bg-card);
border: 1px solid var(--border);
@@ -161,15 +175,6 @@
border: 1px solid var(--border);
}
.profile-tab {
padding: 0.5rem 1rem;
border-radius: var(--radius);
font-size: 0.8125rem;
font-weight: 600;
color: var(--text-secondary);
transition: all 0.2s;
}
.profile-tab:hover {
color: var(--text-primary);
}
+78 -9
View File
@@ -16,15 +16,6 @@
flex-wrap: wrap;
}
.projects-tab {
padding: 0.5rem 1rem;
border-radius: var(--radius);
font-size: 0.8125rem;
font-weight: 600;
color: var(--text-secondary);
transition: all 0.2s;
}
.projects-tab:hover {
color: var(--text-primary);
}
@@ -202,3 +193,81 @@
font-size: 1rem;
}
}
.project-detail-page {
max-width: 720px;
margin: 0 auto;
}
.project-detail {
background: var(--bg-card);
border: 1px solid var(--border);
border-radius: var(--radius-lg);
padding: 1.5rem;
}
.project-detail-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
margin-bottom: 1rem;
}
.project-detail-title {
font-size: 1.5rem;
font-weight: 700;
}
.project-detail-meta {
display: flex;
flex-wrap: wrap;
gap: 0.75rem;
margin-bottom: 1rem;
font-size: 0.8125rem;
color: var(--text-muted);
}
.project-detail-meta span {
display: flex;
align-items: center;
gap: 0.25rem;
}
.project-detail-author {
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 1rem;
padding-bottom: 1rem;
border-bottom: 1px solid var(--border);
}
.project-detail-author a {
font-weight: 600;
font-size: 0.875rem;
color: var(--text-primary);
}
.project-detail-desc {
font-size: 0.9375rem;
color: var(--text-secondary);
line-height: 1.7;
margin-bottom: 1.5rem;
}
.project-detail-actions {
display: flex;
align-items: center;
gap: 0.5rem;
padding-top: 1rem;
border-top: 1px solid var(--border);
}
.project-star-btn {
display: inline-flex;
align-items: center;
gap: 0.375rem;
padding: 0.375rem 0.75rem;
border-radius: var(--radius);
font-size: 0.8125rem;
font-weight: 500;
color: var(--text-muted);
background: none;
border: none;
cursor: pointer;
transition: all 0.2s;
}
.project-star-btn:hover {
background: var(--bg-card-hover);
color: var(--warning);
}
+45 -31
View File
@@ -8,49 +8,63 @@ export class DomUtils {
this.initStopPropagation();
}
static onDataAttr(attr, event, handler) {
document.querySelectorAll(`[data-${attr}]`).forEach((el) => {
el.addEventListener(event, (e) => handler(el, e));
});
}
static show(el) {
el.style.display = "block";
}
static hide(el) {
el.style.display = "none";
}
static toggle(el) {
el.style.display = el.style.display === "none" ? "block" : "none";
}
static isShown(el) {
return el.style.display === "block";
}
initClipboardCopy() {
document.querySelectorAll("[data-copy]").forEach((btn) => {
btn.addEventListener("click", async () => {
const source = document.getElementById(btn.dataset.copy);
if (!source) return;
try {
await navigator.clipboard.writeText(source.textContent);
Toast.flash(btn, "Copied!", 2000);
} catch {
// silently fail
}
});
DomUtils.onDataAttr("copy", "click", async (btn) => {
const source = document.getElementById(btn.dataset.copy);
if (!source) return;
try {
await navigator.clipboard.writeText(source.textContent);
Toast.flash(btn, "Copied!", 2000);
} catch {
// silently fail
}
});
}
initShareButtons() {
document.querySelectorAll("[data-share]").forEach((btn) => {
btn.addEventListener("click", async (e) => {
e.preventDefault();
e.stopPropagation();
const url = new URL(btn.dataset.share || window.location.href, window.location.href).href;
try {
await navigator.clipboard.writeText(url);
Toast.flash(btn, "Copied!", 1000);
} catch {
// silently fail
}
});
DomUtils.onDataAttr("share", "click", async (btn, e) => {
e.preventDefault();
e.stopPropagation();
const url = new URL(btn.dataset.share || window.location.href, window.location.href).href;
try {
await navigator.clipboard.writeText(url);
Toast.flash(btn, "Copied!", 1000);
} catch {
// silently fail
}
});
}
initTogglers() {
document.querySelectorAll("[data-toggle]").forEach((btn) => {
btn.addEventListener("click", () => {
const target = document.getElementById(btn.dataset.toggle);
if (target) target.classList.toggle("hidden");
});
DomUtils.onDataAttr("toggle", "click", (btn) => {
const target = document.getElementById(btn.dataset.toggle);
if (target) target.classList.toggle("hidden");
});
}
initStopPropagation() {
document.querySelectorAll("[data-stop-propagation]").forEach((el) => {
el.addEventListener("click", (e) => e.stopPropagation());
});
DomUtils.onDataAttr("stop-propagation", "click", (el, e) => e.stopPropagation());
}
}
+3 -6
View File
@@ -1,4 +1,5 @@
import { TextInput } from "./TextInput.js";
import { DomUtils } from "./DomUtils.js";
export class EmojiPicker {
constructor(textarea) {
@@ -41,15 +42,11 @@ export class EmojiPicker {
}
toggle() {
if (this.wrapper.style.display === "none") {
this.wrapper.style.display = "block";
} else {
this.wrapper.style.display = "none";
}
DomUtils.toggle(this.wrapper);
}
hide() {
this.wrapper.style.display = "none";
DomUtils.hide(this.wrapper);
}
}
window.EmojiPicker = EmojiPicker;
+27
View File
@@ -4,6 +4,33 @@ export class Http {
return response.json();
}
static async sendForm(url, params = {}) {
const response = await fetch(url, {
method: "POST",
headers: {
"X-Requested-With": "fetch",
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams(params),
});
if (!response.ok) {
throw new Error(`request failed with status ${response.status}`);
}
return response.json();
}
static async postJson(url, body) {
const response = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!response.ok) {
throw new Error(`request failed with status ${response.status}`);
}
return response.json();
}
static postForm(action, data = {}) {
const form = document.createElement("form");
form.method = "POST";
+10 -9
View File
@@ -1,6 +1,7 @@
import { Http } from "./Http.js";
import { Avatar } from "./Avatar.js";
import { TextInput } from "./TextInput.js";
import { DomUtils } from "./DomUtils.js";
export class MentionInput {
constructor(element) {
@@ -25,7 +26,7 @@ export class MentionInput {
this.input.addEventListener("input", () => this.onInput());
this.input.addEventListener("keydown", (e) => this.onKeydown(e));
this.input.addEventListener("blur", () => {
setTimeout(() => { this.dropdown.style.display = "none"; }, 200);
setTimeout(() => DomUtils.hide(this.dropdown), 200);
});
}
@@ -36,7 +37,7 @@ export class MentionInput {
let match = text.match(/(?:^|\s|\x28)@([a-zA-Z0-9_-]*)$/);
if (!match) {
this.dropdown.style.display = "none";
DomUtils.hide(this.dropdown);
this.lastMatch = null;
return;
}
@@ -45,7 +46,7 @@ export class MentionInput {
this.lastMatch = { query, index: match.index + (text[match.index] === "@" ? 0 : 1) };
if (query.length < 1) {
this.dropdown.style.display = "none";
DomUtils.hide(this.dropdown);
return;
}
@@ -57,12 +58,12 @@ export class MentionInput {
const data = await Http.getJson("/profile/search?q=" + encodeURIComponent(query));
const results = data.results || [];
if (results.length === 0) {
this.dropdown.style.display = "none";
DomUtils.hide(this.dropdown);
return;
}
this.render(results);
} catch (e) {
this.dropdown.style.display = "none";
DomUtils.hide(this.dropdown);
}
}
@@ -81,12 +82,12 @@ export class MentionInput {
});
this.dropdown.appendChild(item);
}
this.dropdown.style.display = "block";
DomUtils.show(this.dropdown);
}
onKeydown(e) {
const items = this.dropdown.querySelectorAll(".mention-dropdown-item");
if (this.dropdown.style.display !== "block" || items.length === 0) {
if (!DomUtils.isShown(this.dropdown) || items.length === 0) {
return;
}
@@ -104,7 +105,7 @@ export class MentionInput {
this.insert(items[this.selectedIndex].dataset.username);
}
} else if (e.key === "Escape") {
this.dropdown.style.display = "none";
DomUtils.hide(this.dropdown);
}
}
@@ -125,7 +126,7 @@ export class MentionInput {
before = before.replace(/@+$/, "");
after = after.replace(/^@+/, "");
TextInput.applyValue(this.input, before + "@" + username + " " + after, before.length + username.length + 2);
this.dropdown.style.display = "none";
DomUtils.hide(this.dropdown);
this.lastMatch = null;
}
}
+5 -4
View File
@@ -1,5 +1,6 @@
import { Http } from "./Http.js";
import { Avatar } from "./Avatar.js";
import { DomUtils } from "./DomUtils.js";
export class MessageSearch {
constructor() {
@@ -24,7 +25,7 @@ export class MessageSearch {
const q = searchInput.value.trim();
if (q.length < 1) {
dropdown.innerHTML = "";
dropdown.style.display = "none";
DomUtils.hide(dropdown);
return;
}
debounceTimer = setTimeout(async () => {
@@ -32,7 +33,7 @@ export class MessageSearch {
const data = await Http.getJson(`/messages/search?q=${encodeURIComponent(q)}`);
const results = data.results || [];
if (results.length === 0) {
dropdown.style.display = "none";
DomUtils.hide(dropdown);
return;
}
dropdown.innerHTML = "";
@@ -43,7 +44,7 @@ export class MessageSearch {
item.innerHTML = `${Avatar.imgHtml(r.username)}<span>${r.username}</span>`;
dropdown.appendChild(item);
}
dropdown.style.display = "block";
DomUtils.show(dropdown);
} catch (e) {
// silently fail - no suggestions
}
@@ -61,7 +62,7 @@ export class MessageSearch {
document.addEventListener("click", (e) => {
if (!wrap.contains(e.target)) {
dropdown.style.display = "none";
DomUtils.hide(dropdown);
}
});
}
+4 -11
View File
@@ -1,5 +1,7 @@
// retoor <retoor@molodetz.nl>
import { Http } from "./Http.js";
export class PushManager {
constructor() {
this.supported = "serviceWorker" in navigator && "PushManager" in window && "Notification" in window;
@@ -46,8 +48,7 @@ export class PushManager {
return;
}
const keyResponse = await fetch("/push.json");
const keyData = await keyResponse.json();
const keyData = await Http.getJson("/push.json");
const applicationServerKey = Uint8Array.from(atob(keyData.publicKey), (c) => c.charCodeAt(0));
const subscription = await registration.pushManager.subscribe({
@@ -55,15 +56,7 @@ export class PushManager {
applicationServerKey: applicationServerKey,
});
const response = await fetch("/push.json", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(subscription.toJSON()),
});
if (!response.ok) {
throw new Error("Bad status code from server.");
}
await Http.postJson("/push.json", subscription.toJSON());
this.refreshTriggerVisibility();
} catch (error) {
+2 -12
View File
@@ -1,4 +1,5 @@
import { Toast } from "./Toast.js";
import { Http } from "./Http.js";
export class VoteManager {
constructor() {
@@ -20,18 +21,7 @@ export class VoteManager {
const action = form.getAttribute("action");
const value = form.querySelector('input[name="value"]').value;
try {
const response = await fetch(action, {
method: "POST",
headers: {
"X-Requested-With": "fetch",
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({ value }),
});
if (!response.ok) {
throw new Error(`vote failed with status ${response.status}`);
}
const result = await response.json();
const result = await Http.sendForm(action, { value });
this.render(action, result);
} catch (error) {
console.error("vote failed", error);