- Search (/forum/kereses, the magnifier in the forum bar): thread titles
and posts the viewer may read, Hungarian stems and word beginnings
without accents ("alvas" finds "alvásban"), one result per thread with
the matching words marked.
- Muting a member (post menu, profile; the list in the settings): their
threads leave the lists, their posts fold up, their replies and mentions
no longer count or send e-mails. They are not told.
- The admin archives a topic (Moderálás → Témák): it stays readable but
takes no new threads or replies, and folds away on the front page.
- The public topics and threads are in the sitemap; the forum's own pages
are kept from robots.
- A reply e-mail goes to the one answered, also on the second level.
- Migration 0043 also prepares charts attached to a forum question.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
136 lines
4.9 KiB
Rust
136 lines
4.9 KiB
Rust
//! "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 NOT EXISTS (SELECT 1 FROM forum.mutes mu WHERE mu.user_id = $1 AND mu.muted_id = p.author_id) \
|
|
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,
|
|
}
|
|
}
|