Forum: answers to me as the only count, reply box in the branch, keyboard-safe inputs

- The "Követett" tab becomes "Válaszok": its count is the unread answers
  to the member (a reply to their post, a first-level reply in their
  thread, or a post naming them), listed on /forum/valaszok with "Mind
  olvasott"; the followed threads are its second view. The other counts
  are gone from the menu; listeners see the waiting threads on their card
  on the front page.
- Replying to a post opens the box at the bottom of its branch and scrolls
  there; another branch moves it (with the text typed so far).
- Phones: while typing, the forum's bottom menu and floating bars step
  aside and the box with its send button is scrolled above the keyboard
  (KeyboardSafe, site-wide; Android resizes the page for the keyboard).
- The "Meghallgató" badge is now "Kísérő", with a new icon.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-26 13:31:00 +00:00
co-authored by Claude Opus 5.5
parent 8bc426460d
commit 42283d740e
25 changed files with 615 additions and 90 deletions
+134
View File
@@ -0,0 +1,134 @@
//! "Válaszok": what answers the member. A reply to one of their posts (also
//! a reply to a reply of theirs, `reply_to_id`), a first-level reply in a
//! thread they opened, or a post that names them ("@Holdfény"). One stays
//! unread until the member has read that far in the thread; the forum menu
//! shows only this count.
use axum::Json;
use axum::extract::{Query, State};
use axum::http::StatusCode;
use axum_extra::extract::WithRejection;
use serde::Deserialize;
use serde_json::{Value, json};
use sqlx::PgPool;
use time::OffsetDateTime;
use uuid::Uuid;
use super::{St, rfc3339, visible_topics};
use crate::auth::Viewer;
use crate::error::{ApiError, ApiResult};
/// The posts that answer `$1`, from the threads the viewer may read.
fn answers(viewer: &Viewer) -> String {
format!(
"FROM forum.posts p \
JOIN forum.threads th ON th.id = p.thread_id \
JOIN forum.topics tp ON tp.id = th.topic_id \
LEFT JOIN forum.posts target ON target.id = COALESCE(p.reply_to_id, p.parent_id) \
LEFT JOIN forum.reads r ON r.user_id = $1 AND r.thread_id = p.thread_id \
WHERE p.deleted_at IS NULL AND th.deleted_at IS NULL AND NOT p.is_opening AND NOT p.system \
AND p.author_id IS DISTINCT FROM $1 AND ({}) \
AND (target.author_id = $1 \
OR (p.parent_id IS NULL AND th.author_id = $1) \
OR EXISTS (SELECT 1 FROM forum.mentions mn WHERE mn.post_id = p.id AND mn.user_id = $1))",
visible_topics(viewer)
)
}
const UNREAD: &str = "p.id > COALESCE(r.last_post_id, 0)";
/// How many answers wait unread (for the menu), at most 100.
pub async fn unread_count(db: &PgPool, viewer: &Viewer, me: Uuid) -> Result<i64, sqlx::Error> {
sqlx::query_scalar(&format!("SELECT count(*) FROM (SELECT 1 {} AND {UNREAD} LIMIT 100) x", answers(viewer)))
.bind(me)
.fetch_one(db)
.await
}
#[derive(Deserialize)]
pub struct ListQuery {
#[serde(default)]
page: Option<i64>,
}
#[derive(sqlx::FromRow)]
struct Row {
id: i64,
thread_id: i64,
title: String,
author_id: Option<Uuid>,
body: String,
created_at: OffsetDateTime,
kind: String,
unread: bool,
}
const PAGE: i64 = 30;
/// The member's answers, newest first.
pub async fn list(
State(state): St,
viewer: Viewer,
WithRejection(Query(q), _): WithRejection<Query<ListQuery>, ApiError>,
) -> ApiResult<Json<Value>> {
let member = viewer.require_member()?;
let page = q.page.unwrap_or(1).clamp(1, 1000);
let rows: Vec<Row> = sqlx::query_as(&format!(
"SELECT p.id, p.thread_id, th.title, p.author_id, p.body, p.created_at, \
CASE WHEN target.author_id = $1 THEN 'reply' \
WHEN p.parent_id IS NULL AND th.author_id = $1 THEN 'thread' \
ELSE 'mention' END AS kind, \
{UNREAD} AS unread \
{} ORDER BY p.id DESC LIMIT {} OFFSET $2",
answers(&viewer),
PAGE + 1
))
.bind(member.user_id)
.bind((page - 1) * PAGE)
.fetch_all(&state.db)
.await?;
let more = rows.len() as i64 > PAGE;
let rows = &rows[..rows.len().min(PAGE as usize)];
let authors = super::authors(&state.db, rows.iter().filter_map(|r| r.author_id)).await?;
let replies: Vec<Value> = rows
.iter()
.map(|r| {
json!({
"id": r.id,
"thread": { "id": r.thread_id, "title": r.title, "slug": crate::text::slug(&r.title) },
"author": r.author_id.and_then(|a| authors.get(&a).cloned()),
"excerpt": excerpt(&r.body),
"created_at": rfc3339(r.created_at),
"kind": r.kind,
"unread": r.unread,
})
})
.collect();
let unread = unread_count(&state.db, &viewer, member.user_id).await?;
Ok(Json(json!({ "replies": replies, "unread": unread, "page": page, "more": more })))
}
/// "Mind olvasott": the threads with unread answers count as read up to them.
pub async fn read_all(State(state): St, viewer: Viewer) -> ApiResult<StatusCode> {
let member = viewer.require_member()?;
sqlx::query(&format!(
"INSERT INTO forum.reads (user_id, thread_id, last_post_id) \
SELECT $1, p.thread_id, max(p.id) {} AND {UNREAD} GROUP BY p.thread_id \
ON CONFLICT (user_id, thread_id) DO UPDATE \
SET last_post_id = GREATEST(forum.reads.last_post_id, EXCLUDED.last_post_id), read_at = now()",
answers(&viewer)
))
.bind(member.user_id)
.execute(&state.db)
.await?;
Ok(StatusCode::NO_CONTENT)
}
/// The start of a post, on one line.
fn excerpt(body: &str) -> String {
let line = body.split_whitespace().collect::<Vec<_>>().join(" ");
match line.char_indices().nth(160) {
Some((at, _)) => format!("{}…", line[..at].trim_end()),
None => line,
}
}