Add the Read in BookOrbit button and /issues/{date}/read redirect
The signed-in issue page shows a Read in BookOrbit button (only when the
integration is active and the Standard EPUB exists) that hits
/issues/{date}/read. The route redirects to the cached BookOrbit reader
URL, or resolves the ids through OPDS on first click and caches them;
?refresh=1 re-resolves. Not indexed yet is a 503, upstream failures 502.
Includes the implementation plan.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Va5eMEmWEnjMXBsBob5FDW
This commit is contained in:
+323
-15
@@ -1,13 +1,15 @@
|
||||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
use std::io::Read;
|
||||
use std::path::{Path as FsPath, PathBuf};
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Context;
|
||||
use askama::Template;
|
||||
use axum::extract::{Extension, Path, State};
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::extract::{Extension, Path, Query, State};
|
||||
use axum::response::{IntoResponse, Redirect, Response};
|
||||
use axum_login::tower_sessions::Session;
|
||||
use jiff::civil::Date;
|
||||
use serde::Deserialize;
|
||||
use sqlx::Row;
|
||||
|
||||
use crate::db::Db;
|
||||
@@ -32,6 +34,8 @@ pub struct Download {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IssueView {
|
||||
pub issue: Issue,
|
||||
/// Signed-in BookOrbit redirect route when the Standard EPUB can be downloaded.
|
||||
pub read_href: Option<String>,
|
||||
pub downloads: Vec<Download>,
|
||||
pub from_json: bool,
|
||||
pub world_html: Option<String>,
|
||||
@@ -255,18 +259,21 @@ pub async fn load(
|
||||
} else {
|
||||
recover_world_html(&row, config)
|
||||
};
|
||||
let downloads = [
|
||||
(
|
||||
"EPUB",
|
||||
row.epub_path.as_deref(),
|
||||
Some(config.publish.epub_dir.join(crate::publish::issue_filename(
|
||||
date,
|
||||
Edition::Standard,
|
||||
"epub",
|
||||
))),
|
||||
let standard_download = download(
|
||||
"EPUB",
|
||||
row.epub_path.as_deref(),
|
||||
Some(config.publish.epub_dir.join(crate::publish::issue_filename(
|
||||
date,
|
||||
Edition::Standard,
|
||||
"epub",
|
||||
),
|
||||
(
|
||||
))),
|
||||
"epub",
|
||||
);
|
||||
let read_href = (config.bookorbit.is_active() && standard_download.is_some())
|
||||
.then(|| format!("/issues/{date}/read"));
|
||||
let downloads = [
|
||||
standard_download,
|
||||
download(
|
||||
"X4 EPUB",
|
||||
row.x4_path.as_deref(),
|
||||
Some(config.publish.epub_dir.join(crate::publish::issue_filename(
|
||||
@@ -276,13 +283,14 @@ pub async fn load(
|
||||
))),
|
||||
"epub",
|
||||
),
|
||||
("XTC", row.xtc_path.as_deref(), None, "xtc"),
|
||||
download("XTC", row.xtc_path.as_deref(), None, "xtc"),
|
||||
]
|
||||
.into_iter()
|
||||
.filter_map(|(label, raw, fallback, kind)| download(label, raw, fallback, kind))
|
||||
.flatten()
|
||||
.collect();
|
||||
Ok(Some(IssueView {
|
||||
issue,
|
||||
read_href,
|
||||
downloads,
|
||||
from_json,
|
||||
world_html,
|
||||
@@ -881,6 +889,7 @@ struct IssueFullTemplate {
|
||||
issue_number: i64,
|
||||
stats_line: String,
|
||||
front_page_html: String,
|
||||
read_href: Option<String>,
|
||||
downloads: Vec<Download>,
|
||||
sections: Vec<FullSection>,
|
||||
has_world: bool,
|
||||
@@ -1003,6 +1012,7 @@ pub async fn render_full(
|
||||
issue_number: view.issue.meta.issue_number,
|
||||
stats_line: view.issue.meta.stats_line(),
|
||||
front_page_html: view.issue.editorial.front_page_html.clone(),
|
||||
read_href: view.read_href,
|
||||
downloads: view.downloads,
|
||||
sections,
|
||||
has_world: view.issue.world_briefing.is_some() || view.world_html.is_some(),
|
||||
@@ -1186,6 +1196,96 @@ pub async fn behind(
|
||||
.into_response())
|
||||
}
|
||||
|
||||
/// Query parameters accepted by the BookOrbit reader redirect.
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
pub struct ReadQuery {
|
||||
#[serde(default)]
|
||||
refresh: Option<String>,
|
||||
}
|
||||
|
||||
/// Open the Standard edition of an issue in BookOrbit's browser reader.
|
||||
pub async fn read(
|
||||
State(state): State<AppState>,
|
||||
auth: AuthSession,
|
||||
Path(date): Path<Date>,
|
||||
Query(query): Query<ReadQuery>,
|
||||
) -> Result<Response, WebError> {
|
||||
let _viewer = auth
|
||||
.user()
|
||||
.await
|
||||
.map(Viewer::from)
|
||||
.ok_or_else(|| WebError::Unauthenticated {
|
||||
next: format!("/issues/{date}/read"),
|
||||
})?;
|
||||
let config = state.config();
|
||||
if !config.bookorbit.is_active() {
|
||||
return Err(WebError::NotFound);
|
||||
}
|
||||
let Some(row) = state.db.issue_by_date(date).await? else {
|
||||
return Err(WebError::NotFound);
|
||||
};
|
||||
let refresh = query.refresh.as_deref() == Some("1");
|
||||
if refresh {
|
||||
state.db.set_bookorbit_ids(date, None).await?;
|
||||
} else if let (Some(book_id), Some(file_id)) = (row.bookorbit_book_id, row.bookorbit_file_id) {
|
||||
let ids = crate::bookorbit::BookorbitIds { book_id, file_id };
|
||||
return Ok(Redirect::to(&crate::bookorbit::reader_url(
|
||||
config.bookorbit.public_url(),
|
||||
ids,
|
||||
))
|
||||
.into_response());
|
||||
}
|
||||
|
||||
let client = crate::http::build_client(Duration::from_secs(5))
|
||||
.map_err(|error| WebError::BadGateway(format!("BookOrbit error: {error}")))?;
|
||||
let opds_user = config
|
||||
.bookorbit
|
||||
.opds_user
|
||||
.as_deref()
|
||||
.ok_or(WebError::NotFound)?;
|
||||
let opds_pass = config
|
||||
.bookorbit
|
||||
.opds_pass
|
||||
.as_deref()
|
||||
.ok_or(WebError::NotFound)?;
|
||||
let issue_title = crate::types::issue_title(date);
|
||||
match crate::bookorbit::find_issue(
|
||||
&client,
|
||||
config.bookorbit.api_url(),
|
||||
opds_user,
|
||||
opds_pass,
|
||||
&issue_title,
|
||||
date,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(Some(ids)) => {
|
||||
state
|
||||
.db
|
||||
.set_bookorbit_ids(date, Some((ids.book_id, ids.file_id)))
|
||||
.await?;
|
||||
tracing::info!(
|
||||
%date,
|
||||
book_id = ids.book_id,
|
||||
file_id = ids.file_id,
|
||||
"cached BookOrbit issue ids"
|
||||
);
|
||||
Ok(Redirect::to(&crate::bookorbit::reader_url(
|
||||
config.bookorbit.public_url(),
|
||||
ids,
|
||||
))
|
||||
.into_response())
|
||||
}
|
||||
Ok(None) => Err(WebError::ServiceUnavailable(
|
||||
"BookOrbit has not indexed this issue yet. Try again in a minute.".to_string(),
|
||||
)),
|
||||
Err(error) => {
|
||||
tracing::warn!(%date, %error, "BookOrbit issue lookup failed");
|
||||
Err(WebError::BadGateway(error.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn article_href(date: Date, article_id: ArticleId) -> String {
|
||||
format!("/issues/{date}/articles/{article_id}")
|
||||
}
|
||||
@@ -2091,6 +2191,214 @@ mod tests {
|
||||
assert!(!issue.contains("Download XTC"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn bookorbit_read_is_hidden_and_not_found_when_inactive() {
|
||||
let (_dir, db, source) = seeded_issue(false).await;
|
||||
crate::web::users::add(&db, "reader", "correct horse battery", false)
|
||||
.await
|
||||
.unwrap();
|
||||
let app = crate::server::router(crate::server::AppState::new(
|
||||
db,
|
||||
crate::config::Config::default(),
|
||||
None,
|
||||
));
|
||||
let cookie = login_cookie(&app, "reader", "correct horse battery").await;
|
||||
|
||||
let issue = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri(format!("/issues/{}", source.meta.date))
|
||||
.header(header::COOKIE, &cookie)
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let issue = response_text(issue).await;
|
||||
assert!(issue.contains("Download EPUB"));
|
||||
assert!(!issue.contains("Read in BookOrbit"));
|
||||
|
||||
let read = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri(format!("/issues/{}/read", source.meta.date))
|
||||
.header(header::COOKIE, cookie)
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(read.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn bookorbit_read_button_and_cached_redirect_use_the_public_url() {
|
||||
let (dir, db, source) = seeded_issue(true).await;
|
||||
crate::web::users::add(&db, "reader", "correct horse battery", false)
|
||||
.await
|
||||
.unwrap();
|
||||
let epub_dir = dir.path().join("epubs");
|
||||
std::fs::create_dir(&epub_dir).unwrap();
|
||||
std::fs::write(
|
||||
epub_dir.join(crate::publish::issue_filename(
|
||||
source.meta.date,
|
||||
Edition::Standard,
|
||||
"epub",
|
||||
)),
|
||||
b"standard epub",
|
||||
)
|
||||
.unwrap();
|
||||
db.set_bookorbit_ids(source.meta.date, Some((12, 34)))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut config = crate::config::Config::default();
|
||||
config.publish.epub_dir = epub_dir.clone();
|
||||
config.bookorbit.enabled = true;
|
||||
config.bookorbit.opds_user = Some("reader".into());
|
||||
config.bookorbit.opds_pass = Some("secret".into());
|
||||
config.bookorbit.api_url = "http://127.0.0.1:9".into();
|
||||
let app = crate::server::router(crate::server::AppState::new(
|
||||
db.clone(),
|
||||
config.clone(),
|
||||
None,
|
||||
));
|
||||
let cookie = login_cookie(&app, "reader", "correct horse battery").await;
|
||||
|
||||
let issue = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri(format!("/issues/{}", source.meta.date))
|
||||
.header(header::COOKIE, &cookie)
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let issue = response_text(issue).await;
|
||||
assert!(issue.contains("Read in BookOrbit"));
|
||||
assert!(issue.contains(&format!("href=\"/issues/{}/read\"", source.meta.date)));
|
||||
|
||||
let read = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri(format!("/issues/{}/read", source.meta.date))
|
||||
.header(header::COOKIE, cookie)
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(read.status(), StatusCode::SEE_OTHER);
|
||||
assert_eq!(
|
||||
read.headers().get(header::LOCATION).unwrap(),
|
||||
"https://bookorbit.hallada.net/read/12/34"
|
||||
);
|
||||
|
||||
config.bookorbit.public_url = "https://example.test/".into();
|
||||
let app = crate::server::router(crate::server::AppState::new(db, config, None));
|
||||
let cookie = login_cookie(&app, "reader", "correct horse battery").await;
|
||||
let read = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri(format!("/issues/{}/read", source.meta.date))
|
||||
.header(header::COOKIE, cookie)
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(read.status(), StatusCode::SEE_OTHER);
|
||||
assert_eq!(
|
||||
read.headers().get(header::LOCATION).unwrap(),
|
||||
"https://example.test/read/12/34"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn bookorbit_read_button_is_hidden_without_the_standard_epub() {
|
||||
let (dir, db, source) = seeded_issue(true).await;
|
||||
crate::web::users::add(&db, "reader", "correct horse battery", false)
|
||||
.await
|
||||
.unwrap();
|
||||
let mut config = crate::config::Config::default();
|
||||
config.publish.epub_dir = dir.path().join("missing-epubs");
|
||||
config.bookorbit.enabled = true;
|
||||
config.bookorbit.opds_user = Some("reader".into());
|
||||
config.bookorbit.opds_pass = Some("secret".into());
|
||||
let app = crate::server::router(crate::server::AppState::new(db, config, None));
|
||||
let cookie = login_cookie(&app, "reader", "correct horse battery").await;
|
||||
|
||||
let issue = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri(format!("/issues/{}", source.meta.date))
|
||||
.header(header::COOKIE, cookie)
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let issue = response_text(issue).await;
|
||||
assert!(!issue.contains("Read in BookOrbit"));
|
||||
assert!(!issue.contains(&format!("href=\"/issues/{}/read\"", source.meta.date)));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn bookorbit_read_reports_upstream_connection_errors() {
|
||||
let (_dir, db, source) = seeded_issue(true).await;
|
||||
crate::web::users::add(&db, "reader", "correct horse battery", false)
|
||||
.await
|
||||
.unwrap();
|
||||
let mut config = crate::config::Config::default();
|
||||
config.bookorbit.enabled = true;
|
||||
config.bookorbit.opds_user = Some("reader".into());
|
||||
config.bookorbit.opds_pass = Some("secret".into());
|
||||
config.bookorbit.api_url = "http://127.0.0.1:9".into();
|
||||
let app = crate::server::router(crate::server::AppState::new(db, config, None));
|
||||
let cookie = login_cookie(&app, "reader", "correct horse battery").await;
|
||||
|
||||
let read = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri(format!("/issues/{}/read", source.meta.date))
|
||||
.header(header::COOKIE, cookie)
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(read.status(), StatusCode::BAD_GATEWAY);
|
||||
assert!(response_text(read).await.contains("BookOrbit"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn anonymous_bookorbit_read_redirects_to_login() {
|
||||
let (_dir, db, source) = seeded_issue(true).await;
|
||||
let mut config = crate::config::Config::default();
|
||||
config.bookorbit.enabled = true;
|
||||
config.bookorbit.opds_user = Some("reader".into());
|
||||
config.bookorbit.opds_pass = Some("secret".into());
|
||||
let app = crate::server::router(crate::server::AppState::new(db, config, None));
|
||||
|
||||
let read = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri(format!("/issues/{}/read", source.meta.date))
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(read.status(), StatusCode::FOUND);
|
||||
assert_eq!(
|
||||
read.headers().get(header::LOCATION).unwrap(),
|
||||
format!("/login?next=%2Fissues%2F{}%2Fread", source.meta.date).as_str()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_sizes_are_human_readable() {
|
||||
assert_eq!(format_file_size(42), "42 B");
|
||||
|
||||
@@ -456,6 +456,12 @@ pub enum WebError {
|
||||
Unauthenticated { next: String },
|
||||
#[error("bad request: {0}")]
|
||||
BadRequest(String),
|
||||
/// BookOrbit has not indexed the requested issue yet.
|
||||
#[error("service unavailable: {0}")]
|
||||
ServiceUnavailable(String),
|
||||
/// A BookOrbit OPDS request failed.
|
||||
#[error("bad gateway: {0}")]
|
||||
BadGateway(String),
|
||||
#[error("request origin did not match this site")]
|
||||
Csrf,
|
||||
#[error(transparent)]
|
||||
@@ -511,6 +517,14 @@ impl IntoResponse for WebError {
|
||||
Self::BadRequest(ref message) => {
|
||||
(StatusCode::BAD_REQUEST, "Bad request", message.as_str())
|
||||
}
|
||||
Self::ServiceUnavailable(ref message) => (
|
||||
StatusCode::SERVICE_UNAVAILABLE,
|
||||
"Not indexed yet",
|
||||
message.as_str(),
|
||||
),
|
||||
Self::BadGateway(ref message) => {
|
||||
(StatusCode::BAD_GATEWAY, "BookOrbit error", message.as_str())
|
||||
}
|
||||
Self::Db(ref error) => {
|
||||
tracing::error!(%error, "web database request failed");
|
||||
(
|
||||
@@ -660,6 +674,7 @@ pub fn router(config: &crate::config::Config) -> axum::Router<crate::server::App
|
||||
.route("/issues/{date}/articles/{article_id}", get(issue::article))
|
||||
.route("/issues/{date}/world", get(issue::world))
|
||||
.route("/issues/{date}/behind", get(issue::behind))
|
||||
.route("/issues/{date}/read", get(issue::read))
|
||||
.route_layer(login_required!(
|
||||
session::Backend,
|
||||
login_url = "/login",
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<article class="reader-page mx-auto mt-10 w-full max-w-[68ch] px-4 sm:px-6 lg:mt-12 lg:px-0">
|
||||
<header class="mb-8 text-center"><p class="font-sans text-[0.72rem] uppercase tracking-[0.12em] text-muted">{{ display_date }} · No. {{ issue_number }}</p><p class="mt-2 font-sans text-sm text-muted">{{ stats_line }}</p></header>
|
||||
<section aria-labelledby="brief-heading" data-toc-entry="/issues/{{ date }}"><h1 id="brief-heading" class="reader-section-heading">The Brief</h1><div class="editorial prose-body mt-5 [&>p:first-child]:first-letter:float-left [&>p:first-child]:first-letter:mr-2 [&>p:first-child]:first-letter:mt-1 [&>p:first-child]:first-letter:font-serif [&>p:first-child]:first-letter:text-[4.6rem] [&>p:first-child]:first-letter:font-semibold [&>p:first-child]:first-letter:leading-[0.72]">{{ front_page_html|safe }}</div></section>
|
||||
{% if !downloads.is_empty() %}<div class="my-8 flex flex-wrap gap-2 border-y border-rule py-4">{% for download in downloads %}<a class="btn" href="{{ download.href }}">Download {{ download.label }} <span class="ml-1 text-muted">{{ download.size }}</span></a>{% endfor %}</div>{% endif %}
|
||||
{% if read_href.is_some() || !downloads.is_empty() %}<div class="my-8 flex flex-wrap gap-2 border-y border-rule py-4">{% match read_href %}{% when Some with (href) %}<a class="btn" href="{{ href }}" target="_blank" rel="noopener">Read in BookOrbit</a>{% when None %}{% endmatch %}{% for download in downloads %}<a class="btn" href="{{ download.href }}">Download {{ download.label }} <span class="ml-1 text-muted">{{ download.size }}</span></a>{% endfor %}</div>{% endif %}
|
||||
<section class="mt-12" aria-labelledby="index-heading"><h2 id="index-heading" class="text-center text-3xl font-semibold leading-[1.1] tracking-[-0.01em]">In This Issue</h2>
|
||||
{% for section in sections %}<section class="mt-14"><h3 class="reader-section-heading">{{ section.name }}</h3><ul class="m-0 list-none p-0">{% for entry in section.entries %}<li class="border-b border-rule py-6" data-toc-entry="{{ entry.href }}"><h4 class="font-serif font-semibold leading-[1.1] tracking-[-0.01em] {% if entry.is_lead %}text-3xl{% else %}text-2xl{% endif %}"><a class="text-ink no-underline hover:text-accent" href="{{ entry.href }}">{{ entry.title }}</a></h4><p class="mt-2 font-sans text-sm text-muted">{{ entry.source }} · {{ entry.reading_minutes }} min read</p>{% if !entry.summary.is_empty() %}<p class="index-summary mt-4">{{ entry.summary }}</p>{% endif %}{% match entry.why %}{% when Some with (why) %}<p class="mt-4 border-l-2 border-accent pl-3 italic text-ink-2">Why it's here: {{ why }}</p>{% when None %}{% endmatch %}{% match entry.rating %}{% when Some with (widget) %}{% include "_rating_widget.html" %}{% when None %}{% endmatch %}</li>{% endfor %}</ul></section>{% endfor %}
|
||||
</section>
|
||||
|
||||
Reference in New Issue
Block a user