use std; use diesel; use diesel::sqlite::SqliteConnection; use diesel::prelude::*; use futures_cpupool::{self, CpuFuture}; use r2d2::Pool; use r2d2_diesel::ConnectionManager; use models; #[derive(Clone)] pub struct State { connection_pool: Pool>, cpu_pool: futures_cpupool::CpuPool, } pub type Error = Box; pub enum SlugLookup { Miss, Hit { article_id: i32, revision: i32, }, Redirect(String), } fn decide_slug(conn: &SqliteConnection, article_id: i32, prev_title: &str, title: &str, prev_slug: &str) -> Result { if prev_slug == "" { // Never give a non-empty slug to the front page return Ok(String::new()); } if title == prev_title { return Ok(prev_slug.to_owned()); } let base_slug = ::slug::slugify(title); if base_slug == prev_slug { return Ok(base_slug); } use schema::article_revisions; let mut slug = base_slug.clone(); let mut disambiguator = 1; loop { let slug_in_use = article_revisions::table .filter(article_revisions::article_id.ne(article_id)) .filter(article_revisions::slug.eq(&slug)) .filter(article_revisions::latest.eq(true)) .count() .first::(conn)? != 0; if !slug_in_use { break Ok(slug); } disambiguator += 1; slug = format!("{}-{}", base_slug, disambiguator); } } impl State { pub fn new(connection_pool: Pool>, cpu_pool: futures_cpupool::CpuPool) -> State { State { connection_pool, cpu_pool, } } pub fn get_article_revision(&self, article_id: i32, revision: i32) -> CpuFuture, Error> { let connection_pool = self.connection_pool.clone(); self.cpu_pool.spawn_fn(move || { use schema::article_revisions; Ok(article_revisions::table .filter(article_revisions::article_id.eq(article_id)) .filter(article_revisions::revision.eq(revision)) .first::(&*connection_pool.get()?) .optional()?) }) } pub fn lookup_slug(&self, slug: String) -> CpuFuture { #[derive(Queryable)] struct ArticleRevisionStub { article_id: i32, revision: i32, latest: bool, } let connection_pool = self.connection_pool.clone(); self.cpu_pool.spawn_fn(move || { let conn = connection_pool.get()?; conn.transaction(|| { use schema::article_revisions; Ok(match article_revisions::table .filter(article_revisions::slug.eq(slug)) .order(article_revisions::sequence_number.desc()) .select(( article_revisions::article_id, article_revisions::revision, article_revisions::latest, )) .first::(&*conn) .optional()? { None => SlugLookup::Miss, Some(ref stub) if stub.latest => SlugLookup::Hit { article_id: stub.article_id, revision: stub.revision, }, Some(stub) => SlugLookup::Redirect( article_revisions::table .filter(article_revisions::latest.eq(true)) .filter(article_revisions::article_id.eq(stub.article_id)) .select(article_revisions::slug) .first::(&*conn)? ) }) }) }) } pub fn update_article(&self, article_id: i32, base_revision: i32, title: String, body: String) -> CpuFuture { let connection_pool = self.connection_pool.clone(); self.cpu_pool.spawn_fn(move || { let conn = connection_pool.get()?; conn.transaction(|| { use schema::article_revisions; let (latest_revision, prev_title, prev_slug) = article_revisions::table .filter(article_revisions::article_id.eq(article_id)) .order(article_revisions::revision.desc()) .select(( article_revisions::revision, article_revisions::title, article_revisions::slug, )) .first::<(i32, String, String)>(&*conn)?; if latest_revision != base_revision { // TODO: If it is the same edit repeated, just respond OK // TODO: If there is a conflict, transform the edit to work seamlessly unimplemented!("TODO Missing handling of revision conflicts"); } let new_revision = base_revision + 1; let slug = decide_slug(&*conn, article_id, &prev_title, &title, &prev_slug)?; #[derive(Insertable)] #[table_name="article_revisions"] struct NewRevision<'a> { article_id: i32, revision: i32, slug: &'a str, title: &'a str, body: &'a str, latest: bool, } diesel::update( article_revisions::table .filter(article_revisions::article_id.eq(article_id)) .filter(article_revisions::revision.eq(base_revision)) ) .set(article_revisions::latest.eq(false)) .execute(&*conn)?; diesel::insert(&NewRevision { article_id, revision: new_revision, slug: &slug, title: &title, body: &body, latest: true, }) .into(article_revisions::table) .execute(&*conn)?; Ok(article_revisions::table .filter(article_revisions::article_id.eq(article_id)) .filter(article_revisions::revision.eq(new_revision)) .first::(&*conn)? ) }) }) } }