Commit d40a4ec40a

d40a4ec40a5decbb2b9456862ac62c4c442ed956

parent: bb0f42fcde

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-22 19:11 UTC

Add Qlty config; deduplicate app.js media and like handling

mediaOf, showLiked, and postsUrl replace logic that was repeated in
buildCard, renderLightbox, and the like handler. Behaviour matches the
previous script in a jsdom run.

Layout: unified · split

.qlty/.gitignore added +7
@@ -0,0 +1,7 @@
1*
2!configs
3!configs/**
4!hooks
5!hooks/**
6!qlty.toml
7!.gitignore
.qlty/qlty.toml added +44
@@ -0,0 +1,44 @@
1config_version = "0"
2
3exclude_patterns = [
4 "**/vendor/**",
5 "**/node_modules/**",
6 "**/testdata/**",
7 "**/dist/**",
8 "**/.build/**",
9 "**/build/**",
10 "**/*.min.*",
11 "**/generated/**",
12 ".claude/**",
13 "**/Pods/**",
14]
15
16test_patterns = [
17 "**/test/**",
18 "**/tests/**",
19 "**/Tests/**",
20 "**/*_test.*",
21 "**/test_*.*",
22 "**/*.test.*",
23 "**/*Tests.swift",
24 "e2e/**",
25]
26
27[[source]]
28name = "default"
29default = true
30
31[[plugin]]
32name = "osv-scanner"
33
34[[plugin]]
35name = "trufflehog"
36
37# Early returns are guard clauses, which keep the happy path unindented.
38[smells.return_statements]
39enabled = false
40
41# Total file complexity is the sum of its functions, which function_complexity
42# already checks.
43[smells.file_complexity]
44enabled = false
static/app.js +52 −52
@@ -43,15 +43,8 @@
4343 loading = true;
4444 loader.classList.remove('hidden');
4545
46 let url;
47 if (view === 'dashboard') {
48 url = `/api/dashboard?offset=${offset}&type=${postType}`;
49 } else if (view === 'blog') {
50 url = `/api/blog/${encodeURIComponent(blog)}?offset=${offset}&type=${postType}`;
51 } else if (view === 'search') {
52 url = `/api/search?q=${encodeURIComponent(query)}`;
53 exhausted = true; // tagged endpoint returns one batch
54 }
46 const url = postsUrl();
47 if (view === 'search') exhausted = true; // tagged endpoint returns one batch
5548
5649 try {
5750 const resp = await fetch(url);
@@ -59,16 +52,11 @@
5952 const data = await resp.json();
6053 const newPosts = Array.isArray(data) ? data : (data || []);
6154
62 if (!newPosts.length) {
55 renderPosts(newPosts);
56 offset += newPosts.length;
57 if (newPosts.length < 20) {
6358 exhausted = true;
6459 endMsg.classList.remove('hidden');
65 } else {
66 renderPosts(newPosts);
67 offset += newPosts.length;
68 if (newPosts.length < 20) {
69 exhausted = true;
70 endMsg.classList.remove('hidden');
71 }
7260 }
7361 } catch (err) {
7462 console.error('Fetch error:', err);
@@ -78,6 +66,13 @@
7866 }
7967 }
8068
69 function postsUrl() {
70 if (view === 'dashboard') return `/api/dashboard?offset=${offset}&type=${postType}`;
71 if (view === 'blog') return `/api/blog/${encodeURIComponent(blog)}?offset=${offset}&type=${postType}`;
72 if (view === 'search') return `/api/search?q=${encodeURIComponent(query)}`;
73 return undefined;
74 }
75
8176 // ── Render ─────────────────────────────────────────────────
8277 function renderPosts(newPosts) {
8378 const startIndex = posts.length;
@@ -90,29 +85,29 @@
9085 });
9186 }
9287
93 function buildCard(post, idx) {
94 let mediaSrc = null;
95 let isVideo = false;
96 let thumb = null;
97
88 // mediaOf returns the post's displayable media, or null for other post types.
89 function mediaOf(post) {
9890 if (post.type === 'photo' && post.photos && post.photos.length) {
99 mediaSrc = post.photos[0].original_size.url;
100 } else if (post.type === 'video' && post.video_url) {
101 mediaSrc = post.video_url;
102 thumb = post.thumbnail_url;
103 isVideo = true;
91 return { src: post.photos[0].original_size.url, isVideo: false };
10492 }
93 if (post.type === 'video' && post.video_url) {
94 return { src: post.video_url, poster: post.thumbnail_url, isVideo: true };
95 }
96 return null;
97 }
10598
106 if (!mediaSrc) return null;
99 function buildCard(post, idx) {
100 const media = mediaOf(post);
101 if (!media) return null;
107102
108103 const card = document.createElement('div');
109104 card.className = 'post-card';
110105 card.dataset.index = idx;
111106
112 if (isVideo) {
107 if (media.isVideo) {
113108 const vid = document.createElement('video');
114 vid.src = mediaSrc;
115 if (thumb) vid.poster = thumb;
109 vid.src = media.src;
110 if (media.poster) vid.poster = media.poster;
116111 vid.muted = true;
117112 vid.loop = true;
118113 vid.playsInline = true;
@@ -121,7 +116,7 @@
121116 card.appendChild(vid);
122117 } else {
123118 const img = document.createElement('img');
124 img.src = mediaSrc;
119 img.src = media.src;
125120 img.loading = 'lazy';
126121 img.alt = '';
127122 card.appendChild(img);
@@ -162,26 +157,25 @@
162157 lbPrev.style.visibility = lightboxIndex > 0 ? 'visible' : 'hidden';
163158 lbNext.style.visibility = lightboxIndex < posts.length - 1 ? 'visible' : 'hidden';
164159
165 if (post.type === 'photo' && post.photos && post.photos.length) {
166 const img = document.createElement('img');
167 img.src = post.photos[0].original_size.url;
168 img.alt = '';
169 lbMedia.appendChild(img);
170 } else if (post.type === 'video' && post.video_url) {
160 const media = mediaOf(post);
161 if (media && media.isVideo) {
171162 const vid = document.createElement('video');
172 vid.src = post.video_url;
173 if (post.thumbnail_url) vid.poster = post.thumbnail_url;
163 vid.src = media.src;
164 if (media.poster) vid.poster = media.poster;
174165 vid.controls = true;
175166 vid.autoplay = true;
176167 lbMedia.appendChild(vid);
168 } else if (media) {
169 const img = document.createElement('img');
170 img.src = media.src;
171 img.alt = '';
172 lbMedia.appendChild(img);
177173 }
178174
179175 // Actions
180176 lbLike.dataset.id = post.id_string || post.id;
181177 lbLike.dataset.key = post.reblog_key;
182 lbLike.dataset.liked = post.liked ? 'true' : 'false';
183 lbLike.classList.toggle('liked', !!post.liked);
184 lbLike.textContent = post.liked ? '♥ Liked' : '♡ Like';
178 showLiked(!!post.liked);
185179
186180 lbReblog.dataset.id = post.id_string || post.id;
187181 lbReblog.dataset.key = post.reblog_key;
@@ -205,14 +199,23 @@
205199 if (lightboxIndex < posts.length - 1) { lightboxIndex++; renderLightbox(); }
206200 });
207201
202 const lightboxKeys = {
203 Escape: closeLightbox,
204 ArrowLeft: () => lbPrev.click(),
205 ArrowRight: () => lbNext.click(),
206 };
208207 document.addEventListener('keydown', e => {
209 if (lightbox.classList.contains('hidden')) return;
210 if (e.key === 'Escape') closeLightbox();
211 if (e.key === 'ArrowLeft') lbPrev.click();
212 if (e.key === 'ArrowRight') lbNext.click();
208 const action = lightboxKeys[e.key];
209 if (action && !lightbox.classList.contains('hidden')) action();
213210 });
214211
215212 // ── Like ───────────────────────────────────────────────────
213 function showLiked(liked) {
214 lbLike.dataset.liked = liked ? 'true' : 'false';
215 lbLike.classList.toggle('liked', liked);
216 lbLike.textContent = liked ? '♥ Liked' : '♡ Like';
217 }
218
216219 lbLike.addEventListener('click', async () => {
217220 const id = lbLike.dataset.id;
218221 const key = lbLike.dataset.key;
@@ -221,11 +224,8 @@
221224 try {
222225 const resp = await fetch(url, { method: 'GET' });
223226 if (!resp.ok) throw new Error(await resp.text());
224 const post = posts[lightboxIndex];
225 post.liked = !liked;
226 lbLike.dataset.liked = post.liked ? 'true' : 'false';
227 lbLike.classList.toggle('liked', post.liked);
228 lbLike.textContent = post.liked ? '♥ Liked' : '♡ Like';
227 posts[lightboxIndex].liked = !liked;
228 showLiked(!liked);
229229 } catch (err) {
230230 console.error('Like error:', err);
231231 }