SeaORM: subscriptions and pins
Twelve subscription functions move to SeaORM. Lookups use the entity API; the joins, counts and upserts are SQL written to run on both databases: $n parameters, ON CONFLICT DO NOTHING in place of INSERT OR IGNORE, and CASE WHEN on the yes/no column itself rather than comparing it to 1, which Postgres would refuse for a boolean. INSERT ... SELECT ... ON CONFLICT gets a WHERE true, which SQLite needs to tell the two apart. Checked with a daemon on a copy of production: the feed list, read through the new code, comes back with every feed and its settings. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
58
src/web.rs
58
src/web.rs
@@ -676,12 +676,12 @@ async fn feeds(
|
||||
let mine: std::collections::HashMap<String, crate::db::Sub> = state
|
||||
.ctx
|
||||
.db
|
||||
.subscriptions_for(user.id)?
|
||||
.subscriptions_for(user.id).await?
|
||||
.into_iter()
|
||||
.map(|s| (s.feed_id.clone(), s))
|
||||
.collect();
|
||||
let counts = state.ctx.db.subscriber_counts()?;
|
||||
let pinned = state.ctx.db.pinned_feeds(user.id)?;
|
||||
let counts = state.ctx.db.subscriber_counts().await?;
|
||||
let pinned = state.ctx.db.pinned_feeds(user.id).await?;
|
||||
let mut out = Vec::with_capacity(mine.len());
|
||||
for sub in &subs {
|
||||
let (id, feed) = (&sub.id, &sub.cfg);
|
||||
@@ -800,12 +800,12 @@ struct PopularRow {
|
||||
/// first. Popular is the top of it, the directory is all of it, and it is all that
|
||||
/// `subscribe_popular` will subscribe you to. An OPML or a Patreon creator is listed as the
|
||||
/// feeds inside it and never itself: both lists are for finding a show.
|
||||
fn popular(state: &WebState, user_id: i64) -> Result<Vec<PopularRow>> {
|
||||
async fn popular(state: &WebState, user_id: i64) -> Result<Vec<PopularRow>> {
|
||||
let db = &state.ctx.db;
|
||||
let mine: std::collections::HashSet<String> =
|
||||
db.subscriptions_for(user_id)?.into_iter().map(|s| s.feed_id).collect();
|
||||
let counts = db.subscriber_counts()?;
|
||||
let media = db.media_feeds()?;
|
||||
db.subscriptions_for(user_id).await?.into_iter().map(|s| s.feed_id).collect();
|
||||
let counts = db.subscriber_counts().await?;
|
||||
let media = db.media_feeds().await?;
|
||||
let catalogue = crate::subscriptions(&state.ctx)?;
|
||||
let by_id: std::collections::HashMap<&str, &crate::config::Feed> =
|
||||
catalogue.iter().map(|s| (s.id.as_str(), &s.cfg)).collect();
|
||||
@@ -844,7 +844,7 @@ async fn get_popular(
|
||||
State(state): State<WebState>,
|
||||
user: crate::db::User,
|
||||
) -> Result<Json<Vec<PopularRow>>, ApiError> {
|
||||
let mut rows = popular(&state, user.id)?;
|
||||
let mut rows = popular(&state, user.id).await?;
|
||||
rows.truncate(10);
|
||||
Ok(Json(rows))
|
||||
}
|
||||
@@ -854,7 +854,7 @@ async fn get_directory(
|
||||
State(state): State<WebState>,
|
||||
user: crate::db::User,
|
||||
) -> Result<Json<Vec<PopularRow>>, ApiError> {
|
||||
let mut rows = popular(&state, user.id)?;
|
||||
let mut rows = popular(&state, user.id).await?;
|
||||
rows.sort_by_key(sort_name);
|
||||
Ok(Json(rows))
|
||||
}
|
||||
@@ -870,10 +870,10 @@ async fn subscribe_popular(
|
||||
user: crate::db::User,
|
||||
Path(id): Path<String>,
|
||||
) -> Result<Json<serde_json::Value>, ApiError> {
|
||||
if !popular(&state, user.id)?.iter().any(|p| p.id == id) {
|
||||
if !popular(&state, user.id).await?.iter().any(|p| p.id == id) {
|
||||
return Err(ApiError::bad_request(format!("{id:?} is not in the directory")));
|
||||
}
|
||||
state.ctx.db.subscribe(user.id, &id)?;
|
||||
state.ctx.db.subscribe(user.id, &id).await?;
|
||||
Ok(Json(serde_json::json!({ "id": id })))
|
||||
}
|
||||
|
||||
@@ -1202,10 +1202,10 @@ struct NewFeed {
|
||||
/// The Add feed dialog's explicit box. Like everything on a feed's own dialog it is yours, so it
|
||||
/// goes on your subscription, and before the first scan, which would otherwise skip every
|
||||
/// explicit item.
|
||||
fn explicit_on_add(state: &WebState, user_id: i64, feed_id: &str, allow: bool) -> Result<(), ApiError> {
|
||||
async fn explicit_on_add(state: &WebState, user_id: i64, feed_id: &str, allow: bool) -> Result<(), ApiError> {
|
||||
if allow {
|
||||
let sub = crate::db::Sub { feed_id: feed_id.to_owned(), allow_explicit: Some(true), ..Default::default() };
|
||||
state.ctx.db.set_subscription(user_id, &sub)?;
|
||||
state.ctx.db.set_subscription(user_id, &sub).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -1223,10 +1223,10 @@ async fn add_feed(
|
||||
.into_iter()
|
||||
.find(|s| crate::feed::same_feed(&s.cfg.url, &url))
|
||||
{
|
||||
let already = state.ctx.db.subscription(user.id, &existing.id)?.is_some();
|
||||
state.ctx.db.subscribe(user.id, &existing.id)?;
|
||||
let already = state.ctx.db.subscription(user.id, &existing.id).await?.is_some();
|
||||
state.ctx.db.subscribe(user.id, &existing.id).await?;
|
||||
if !already {
|
||||
explicit_on_add(&state, user.id, &existing.id, body.allow_explicit)?;
|
||||
explicit_on_add(&state, user.id, &existing.id, body.allow_explicit).await?;
|
||||
}
|
||||
scan_soon(&state, Some(existing.id.clone())).await;
|
||||
return Ok(Json(
|
||||
@@ -1236,8 +1236,8 @@ async fn add_feed(
|
||||
let id = crate::add_one(&state.ctx, &mut cfg, &url, body.folder, body.keywords).await?;
|
||||
cfg.save(&state.config_path)?;
|
||||
state.ctx.reload_cfg(&state.config_path)?;
|
||||
state.ctx.db.subscribe(user.id, &id)?;
|
||||
explicit_on_add(&state, user.id, &id, body.allow_explicit)?;
|
||||
state.ctx.db.subscribe(user.id, &id).await?;
|
||||
explicit_on_add(&state, user.id, &id, body.allow_explicit).await?;
|
||||
scan_soon(&state, Some(id.clone())).await;
|
||||
Ok(Json(serde_json::json!({ "id": id, "existing": false })))
|
||||
}
|
||||
@@ -1290,17 +1290,17 @@ async fn patch_feed(
|
||||
) -> Result<StatusCode, ApiError> {
|
||||
// Pinning is yours alone too, and means nothing for a feed you do not subscribe to.
|
||||
if let Some(on) = body.pinned
|
||||
&& !state.ctx.db.set_pinned(user.id, &id, on)?
|
||||
&& !state.ctx.db.set_pinned(user.id, &id, on).await?
|
||||
{
|
||||
return Err(ApiError::not_found("you do not subscribe to that feed"));
|
||||
}
|
||||
// What one person wants -- which items, whether to fetch them, how many at a time --
|
||||
// is theirs. It goes on their subscription and nobody else sees the change.
|
||||
if state.ctx.db.subscription(user.id, &id)?.is_some() {
|
||||
if state.ctx.db.subscription(user.id, &id).await?.is_some() {
|
||||
let mut mine = state
|
||||
.ctx
|
||||
.db
|
||||
.subscription(user.id, &id)?
|
||||
.subscription(user.id, &id).await?
|
||||
.unwrap_or_else(|| crate::db::Sub { feed_id: id.clone(), ..Default::default() });
|
||||
let mut touched = false;
|
||||
if let Some(v) = body.keywords.clone() {
|
||||
@@ -1320,7 +1320,7 @@ async fn patch_feed(
|
||||
touched = true;
|
||||
}
|
||||
if touched {
|
||||
state.ctx.db.set_subscription(user.id, &mine)?;
|
||||
state.ctx.db.set_subscription(user.id, &mine).await?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1400,14 +1400,14 @@ async fn remove_feed(
|
||||
) -> Result<StatusCode, ApiError> {
|
||||
// Unsubscribing is personal: it takes the feed off your list and leaves everyone
|
||||
// else's alone.
|
||||
state.ctx.db.unsubscribe(user.id, &id)?;
|
||||
state.ctx.db.unsubscribe(user.id, &id).await?;
|
||||
for child in crate::subscriptions(&state.ctx)?
|
||||
.iter()
|
||||
.filter(|s| s.cfg.group.as_deref() == Some(id.as_str()))
|
||||
{
|
||||
state.ctx.db.unsubscribe(user.id, &child.id)?;
|
||||
state.ctx.db.unsubscribe(user.id, &child.id).await?;
|
||||
}
|
||||
if state.ctx.db.subscriber_counts()?.contains_key(&id) {
|
||||
if state.ctx.db.subscriber_counts().await?.contains_key(&id) {
|
||||
return Ok(StatusCode::NO_CONTENT);
|
||||
}
|
||||
|
||||
@@ -1493,7 +1493,7 @@ async fn delete_file(
|
||||
// There is one copy of the file: deleting it deletes everyone's. Say so before doing
|
||||
// it, once, and let them decide.
|
||||
if !q.force {
|
||||
let (starred, unread) = state.ctx.db.others_wanting(id, user.id)?;
|
||||
let (starred, unread) = state.ctx.db.others_wanting(id, user.id).await?;
|
||||
let people = |n: i64| if n == 1 { "person".to_string() } else { format!("{n} people") };
|
||||
let complaint = match (starred, unread) {
|
||||
(0, 0) => None,
|
||||
@@ -1626,7 +1626,7 @@ async fn read_all_mine(
|
||||
user: crate::db::User,
|
||||
) -> Result<Json<serde_json::Value>, ApiError> {
|
||||
let ids: Vec<String> =
|
||||
state.ctx.db.subscriptions_for(user.id)?.into_iter().map(|s| s.feed_id).collect();
|
||||
state.ctx.db.subscriptions_for(user.id).await?.into_iter().map(|s| s.feed_id).collect();
|
||||
let n = state.ctx.db.mark_all_read(user.id, &ids)?;
|
||||
Ok(Json(serde_json::json!({ "marked": n })))
|
||||
}
|
||||
@@ -1668,7 +1668,7 @@ async fn export_opml(
|
||||
// Yours, not the whole catalogue: other people's feeds, and any private URLs in them, are
|
||||
// not yours to download. This used to export config.toml to whoever asked.
|
||||
let mine: std::collections::HashSet<String> =
|
||||
state.ctx.db.subscriptions_for(user.id)?.into_iter().map(|s| s.feed_id).collect();
|
||||
state.ctx.db.subscriptions_for(user.id).await?.into_iter().map(|s| s.feed_id).collect();
|
||||
let mut doc = opml::OPML {
|
||||
head: Some(opml::Head {
|
||||
title: Some("ipx subscriptions".into()),
|
||||
@@ -1718,7 +1718,7 @@ async fn import_opml(
|
||||
// file arrives as text, is read here, and is gone when the request ends.
|
||||
let doc = opml::OPML::from_str(&body.xml)
|
||||
.map_err(|e| ApiError::bad_request(format!("that is not an OPML file: {e}")))?;
|
||||
let (added, already) = crate::subscribe_opml(&state.ctx, &state.config_path, &doc, user.id)?;
|
||||
let (added, already) = crate::subscribe_opml(&state.ctx, &state.config_path, &doc, user.id).await?;
|
||||
if added > 0 {
|
||||
scan_soon(&state, None).await;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user