Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

feat(web): 详情页补上五星评分,写进 asset.rating

longmeidao23bcaa349f2a
diff --git a/src/peach/web_activity.py b/src/peach/web_activity.py
index 01fbae4ca..3fce89d44 100644
--- a/src/peach/web_activity.py
+++ b/src/peach/web_activity.py
@@ -81,8 +81,13 @@ def w_feedback(contract: ActivityContract, body):
"UPDATE asset SET o_count=MAX(COALESCE(o_count,0)-1,0) WHERE id=?", (aid,),
)
elif kind == "rate":
+ # 这一列的量纲是 0–100:它是 Stash 的 rating100 直接导进来的(scripts/ledger.py),
+ # taste_history 也按 rating/20 折算成 0–5 分。五颗星送出的因此是 20 的倍数。
+ # 撤销评分写回 NULL 而不是 0:排序用的是 NULLS LAST,写 0 会把「没评过」和
+ # 「评了最低分」并成一档,没评过的一整库反而排在评了一星的前面。
+ value = max(0, min(100, int(body.get("value", 0))))
connection.execute(
- "UPDATE asset SET rating=? WHERE id=?", (int(body.get("value", 0)), aid),
+ "UPDATE asset SET rating=? WHERE id=?", (value or None, aid),
)
result = dict(connection.execute(
"SELECT feedback,disposal,rating,o_count FROM asset WHERE id=?", (aid,),
diff --git a/tests/test_rm_web.py b/tests/test_rm_web.py
index 9363b15a8..27b4edebc 100644
--- a/tests/test_rm_web.py
+++ b/tests/test_rm_web.py
@@ -680,6 +680,44 @@ class WebDataTests(unittest.TestCase):
)["feedback"])
self.assertEqual(self.row()["disposal"], "trash")
+ def test_a_star_writes_twenty_points_and_taking_it_back_writes_null(self):
+ """评分这一列是 0–100,五颗星走 20 的倍数;撤销回 NULL,不是 0。
+
+ 量纲不是自选的:`scripts/ledger.py` 把 Stash 的 rating100 原样导进这一列,
+ `taste_history` 也按 rating/20 折算成 0–5 分。撤销必须写 NULL,因为排序是
+ `a.rating {d} NULLS LAST`——写 0 会把「没评过」和「评了一星」并成一档,
+ 七万多条没评过的于是排在评了一星的前面。
+ """
+ self.assertEqual(rm_web.w_feedback(
+ self.contract, {"id": 1, "kind": "rate", "value": 60},
+ )["rating"], 60)
+ self.assertEqual(self.row()["rating"], 60)
+ self.assertIsNone(rm_web.w_feedback(
+ self.contract, {"id": 1, "kind": "rate", "value": 0},
+ )["rating"])
+ self.assertIsNone(self.row()["rating"])
+
+ def test_a_rating_outside_the_scale_is_clamped_instead_of_stored(self):
+ """越界的分数夹回 0–100,不落库。前端只送 20 的倍数,接口本身仍是公开的。"""
+ self.assertEqual(rm_web.w_feedback(
+ self.contract, {"id": 1, "kind": "rate", "value": 4000},
+ )["rating"], 100)
+ self.assertIsNone(rm_web.w_feedback(
+ self.contract, {"id": 1, "kind": "rate", "value": -7},
+ )["rating"])
+
+ def test_the_rating_sort_finally_orders_by_something_other_than_o_count(self):
+ """评过分的排在前面,没评过的按 NULLS LAST 留在后面。
+
+ 补上写入路径之前,这一列全库为空,`rating` 排序静默退化成第二列
+ `a.o_count`,界面上「评分」和「高潮计数」是同一个排序。
+ """
+ rm_web.w_feedback(self.contract, {"id": 2, "kind": "rate", "value": 100})
+ rm_web.w_feedback(self.contract, {"id": 1, "kind": "rate", "value": 40})
+ ordered = [item["id"] for item in rm_web.q_items(
+ self.contract, {"sort": "rating", "limit": "10"})["items"]]
+ self.assertEqual(ordered[:2], [2, 1])
+
def test_orgasm_receipt_can_be_undone_without_crossing_zero(self):
added = rm_web.w_feedback(self.contract, {"id": 1, "kind": "o"})
self.assertEqual(added["o_count"], 1)
diff --git a/tests/test_web_ui.py b/tests/test_web_ui.py
index 13106c686..98b2984dc 100644
--- a/tests/test_web_ui.py
+++ b/tests/test_web_ui.py
@@ -915,6 +915,39 @@ class WebUiSourceTests(unittest.TestCase):
".dual-range input:focus-visible::-webkit-slider-thumb"
"{outline:2px solid var(--tungsten);outline-offset:2px}")
+ def test_the_star_rating_sits_between_the_title_and_the_spec_line(self):
+ """五颗星在标题和那行规格之间,送出的是 20 的倍数,空实两档不借色相。
+
+ 位置不是随手挑的:评分是对这一条作品的判断,和番号、标题一样属于它的身份,
+ 而反馈条那一排说的是「怎么处置它」(不合口味、看过、回收站)。两者混在一起,
+ 「我给它几分」就会读成又一个处置动作。
+
+ 再点当前那一颗是撤销,送 0;后端写回 NULL。悬停预演到指针那一颗,靠
+ `:has(~ .star:hover)` 选中它左边的几颗——把 DOM 倒过来写也能做到同一件事,
+ 代价是 Tab 与读屏顺序跟着倒过来,五颗星从「评为 5 星」开始念。
+ """
+ self.assertPageContains("const RATING_STEP=20;")
+ self.assertPageContains(
+ """ ${it.location==='online'?'':`<span class="srcstate detailtitlestate" aria-live="polite"></span>`}
+ ${ratingHtml(it.rating)}
+ <div class="smeta mono">""")
+ self.assertPageContains(
+ "const value=picked===before?0:picked;")
+ self.assertPageContains(
+ "api('/api/feedback',{method:'POST',body:JSON.stringify({id:it.id,kind:'rate',value})})")
+ self.assertPageContains("{undo:()=>postRating(before)}")
+ # 标签是语义契约:撤销那一颗要说清它现在是几星,否则读屏只听到「星」。
+ self.assertPageContains(
+ "aria-label=\"${n===on?`取消评分(当前 ${n} 星)`:`评为 ${n} 星`}\"")
+ self.assertPageContains('<symbol id="i-star" viewBox="0 0 24 24">')
+ self.assertPageContains('.ratingstars .star[data-on="true"] svg{fill:currentColor}')
+ self.assertPageContains(
+ ".ratingstars:hover .star:hover,.ratingstars:hover .star:has(~ .star:hover)"
+ "{color:var(--ink)}")
+ # 空心那一档用的是 --line,不是 --muted:它是一枚没填的形状,不是一行次要文字。
+ start = self.page.index(".ratingstars .star{")
+ self.assertIn("color:var(--line)", self.page[start:self.page.index("}", start)])
+
def test_every_button_in_the_feedback_bar_owns_a_hover_color(self):
"""详情页反馈条上每一枚都有自己的悬停配色,兜底填充用 token 不写死白。
@@ -6320,9 +6353,11 @@ class WebUiSourceTests(unittest.TestCase):
self.assertPageContains(
"[['system','跟随系统','monitor'],['light','浅色','sun'],['dark','深色','moon']]")
self.assertPageContains("${icon('ratio')}<span>${it.width||'?'}×${it.height||'?'}</span>")
- # 换下来的三枚没有别的使用者,雪碧图里也不留。
- for gone in ("i-monitor-cog", "i-star", "i-volume-2", "i-sun-moon"):
+ # 换下来的这几枚没有别的使用者,雪碧图里也不留。星是有使用者的那一枚:
+ # 详情页的五星评分,写进 `asset.rating`,不与任何别的意思共用。
+ for gone in ("i-monitor-cog", "i-volume-2", "i-sun-moon"):
self.assertPageLacks(f'<symbol id="{gone}"')
+ self.assertPageContains("${icon('star')}</button>")
self.assertPageContains('<symbol id="i-clock" viewBox="0 0 24 24">')
def test_mixed_icon_sets_land_on_one_optical_grid(self):
diff --git a/web/app.js b/web/app.js
index 18f59103e..d2ed28e73 100644
--- a/web/app.js
+++ b/web/app.js
@@ -7245,6 +7245,21 @@ function clearIdleCatalogLoading(){
grid.innerHTML='';
const count=$('#count');count.removeAttribute('aria-busy');count.removeAttribute('aria-label');
}
+/* 评分落在 `asset.rating`,量纲是 0–100:这一列是 Stash 的 rating100 直接导进来的,
+ taste_history 也按 rating/20 折算成 0–5 分。所以第 n 颗星送出的是 n*20,不是 n。
+ 再点当前那一颗表示撤销,送 0,后端写回 NULL——「没评过」和「评了一星」在排序上
+ 必须是两件事。 */
+const RATING_STEP=20;
+const ratingStarCount=value=>Math.round(Math.min(Math.max(value||0,0),100)/RATING_STEP);
+const ratingText=value=>{const n=ratingStarCount(value);return n?`${n} 星`:'未评分'};
+const ratingStarsHtml=value=>{const on=ratingStarCount(value);
+ return [1,2,3,4,5].map(n=>`<button type="button" class="star" data-rate="${n}" data-on="${n<=on}"
+ title="${n===on?'再点一次取消评分':`${n} 星`}"
+ aria-label="${n===on?`取消评分(当前 ${n} 星)`:`评为 ${n} 星`}">${icon('star')}</button>`).join('')};
+const ratingHtml=value=>`<div class="rating" id="detailRating" role="group" aria-label="评分" data-value="${Math.min(Math.max(value||0,0),100)}">
+ <div class="ratingstars">${ratingStarsHtml(value)}</div>
+ <span class="ratingvalue" aria-live="polite">${ratingText(value)}</span></div>`;
+
async function openItem(id,push=true,queueContext=null,anchor=null){
releaseHoverPreviews();
const origin=anchor?.isConnected?anchor:(detailOriginAnchor?.isConnected?detailOriginAnchor:null);
@@ -7369,6 +7384,7 @@ async function openItem(id,push=true,queueContext=null,anchor=null){
<div class="detailtitle">${srcBadge(it.location,it.cost,'srcbig')}
<div class="stitle">${javTitleHtml(it)}${partLabelBadge(it,queueContext)}${it.location==='online'?'':`<span class="srctools detailtitletools">${sourceToolButtons(it.id)}</span>`}</div></div>
${it.location==='online'?'':`<span class="srcstate detailtitlestate" aria-live="polite"></span>`}
+ ${ratingHtml(it.rating)}
<div class="smeta mono">
<span class="detailmetaitem">${icon('ratio')}<span>${it.width||'?'}×${it.height||'?'}</span></span>
<span class="detailmetaitem">${icon('hard-drive')}<span>${fmtSize(it.size||0)}</span></span>
@@ -7429,6 +7445,25 @@ async function openItem(id,push=true,queueContext=null,anchor=null){
const g=$('#gate');
const onlineGate=$('#onlineGate');
$('#addPlaylist').onclick=()=>openAddToPlaylist(it);
+ /* 一排星只挂一个监听,重绘时整块换掉 innerHTML——五个按钮各挂一份的话,
+ 每次评完都要重新接线,漏接就变成「点第一次有反应,第二次没有」。 */
+ const paintRating=value=>{it.rating=value==null?null:value;
+ const box=$('#detailRating');if(!box)return;
+ box.dataset.value=Math.min(Math.max(value||0,0),100);
+ box.querySelector('.ratingstars').innerHTML=ratingStarsHtml(value);
+ box.querySelector('.ratingvalue').textContent=ratingText(value)};
+ const postRating=async value=>{
+ const r=await api('/api/feedback',{method:'POST',body:JSON.stringify({id:it.id,kind:'rate',value})});
+ paintRating(r.rating);return r};
+ $('#detailRating')?.addEventListener('click',async event=>{
+ const star=event.target.closest('.star');if(!star)return;
+ const before=it.rating||0,picked=+star.dataset.rate*RATING_STEP;
+ const value=picked===before?0:picked;
+ setActionBusy(star);
+ try{await postRating(value);
+ actionReceipt(value?`已评 ${value/RATING_STEP} 星`:'已取消评分',
+ {undo:()=>postRating(before)});
+ }catch(error){actionFailure('评分',error)}finally{setActionBusy(star,false)}});
const paintDetailFeedback=result=>{
Object.assign(it,{feedback:result.feedback,disposal:result.disposal,o_count:result.o_count});
const stage=$('#stage');if(!stage)return;
diff --git a/web/css/15-detail.css b/web/css/15-detail.css
index bfd6ead22..7c62bccf6 100644
--- a/web/css/15-detail.css
+++ b/web/css/15-detail.css
@@ -67,6 +67,26 @@
内联 style,等于把一条配色写在 JS 里。 */
#realBar{background:color-mix(in srgb,var(--keep) 40%,transparent)}
.trace-real{margin-top:10px}
+/* 五颗星站在标题和那行规格之间:评分是对这一条作品的判断,属于身份的一部分,
+ 不是反馈条里那些「怎么处置它」的动作。空心是未评,实心是已评,只有明暗两档,
+ 不借色相——这一排本来就没有第二种含义要分。 */
+.rating{display:flex;align-items:center;gap:8px;margin:0 0 10px}
+.ratingstars{display:inline-flex;align-items:center;gap:2px}
+.ratingstars .star{width:26px;height:26px;padding:0;border:0;border-radius:var(--control-radius);
+ background:transparent;color:var(--line);display:grid;place-items:center;cursor:pointer}
+.ratingstars .star svg{width:19px;height:19px;stroke:currentColor;fill:none;stroke-width:2;
+ stroke-linecap:round;stroke-linejoin:round}
+.ratingstars .star[data-on="true"]{color:var(--ink)}
+.ratingstars .star[data-on="true"] svg{fill:currentColor}
+/* 悬停要预演的是「点下去会变成几星」,所以整排先退回未评,再把指针那一颗和它左边的点亮。
+ `:has(~ .star:hover)` 是这里唯一需要的「前一个兄弟」——把 DOM 倒过来写也能做到,
+ 代价是读屏和 Tab 顺序跟着倒过来,五颗星会从「评为 5 星」开始念。 */
+.ratingstars:hover .star{color:var(--line)}
+.ratingstars:hover .star svg{fill:none}
+.ratingstars:hover .star:hover,.ratingstars:hover .star:has(~ .star:hover){color:var(--ink)}
+.ratingstars:hover .star:hover svg,.ratingstars:hover .star:has(~ .star:hover) svg{fill:currentColor}
+.ratingstars .star:focus-visible{outline:2px solid var(--tungsten);outline-offset:2px}
+.ratingvalue{color:var(--muted);font-size:var(--fs-xs);font-variant-numeric:tabular-nums}
.fb{display:flex;align-items:center;gap:0;margin:0 0 12px;padding:0;border:1px solid var(--border-10);border-radius:var(--control-radius);background:var(--overlay-5);width:max-content;overflow:hidden;flex:none}
.fb button{width:44px;height:42px;padding:0;border:0;border-right:1px solid var(--border-10);background:transparent;border-radius:0;
cursor:pointer;display:grid;place-items:center;color:var(--muted);transition:transform .14s ease,color .14s ease,background .14s ease}
diff --git a/web/index.html b/web/index.html
index 529373be3..8cf4d38c8 100644
--- a/web/index.html
+++ b/web/index.html
@@ -75,6 +75,7 @@
<symbol id="i-player-menu-back" viewBox="0 0 32 32" fill="currentColor" stroke="none"><path d="m19.41 20.09-4.58-4.59 4.58-4.59L18 9.5l-6 6 6 6Z"/></symbol>
<symbol id="i-player-option-check" viewBox="0 0 24 24" fill="currentColor" stroke="none"><path d="M9 16.2 4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4z"/></symbol>
<symbol id="i-history" viewBox="0 0 24 24"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8" /><path d="M3 3v5h5" /><path d="M12 7v5l4 2" /></symbol>
+ <symbol id="i-star" viewBox="0 0 24 24"><path d="M11.525 2.295a.53.53 0 0 1 .95 0l2.31 4.679a2.123 2.123 0 0 0 1.595 1.16l5.166.756a.53.53 0 0 1 .294.904l-3.736 3.638a2.123 2.123 0 0 0-.611 1.878l.882 5.14a.53.53 0 0 1-.771.56l-4.618-2.428a2.122 2.122 0 0 0-1.973 0L6.396 21.01a.53.53 0 0 1-.77-.56l.881-5.139a2.122 2.122 0 0 0-.611-1.879L2.16 9.795a.53.53 0 0 1 .294-.906l5.165-.755a2.122 2.122 0 0 0 1.597-1.16z" /></symbol>
<symbol id="i-sparkles" viewBox="0 0 24 24"><path d="M11.017 2.814a1 1 0 0 1 1.966 0l1.051 5.558a2 2 0 0 0 1.594 1.594l5.558 1.051a1 1 0 0 1 0 1.966l-5.558 1.051a2 2 0 0 0-1.594 1.594l-1.051 5.558a1 1 0 0 1-1.966 0l-1.051-5.558a2 2 0 0 0-1.594-1.594l-5.558-1.051a1 1 0 0 1 0-1.966l5.558-1.051a2 2 0 0 0 1.594-1.594z" /><path d="M20 2v4" /><path d="M22 4h-4" /><circle cx="4" cy="20" r="2" /></symbol>
<symbol id="i-x" viewBox="0 0 24 24"><path d="M18 6 6 18" /><path d="m6 6 12 12" /></symbol>
<!-- JAV 入口:外框与其余 Lucide 图标同为 24×24 描边方框,字面走填色。