From b147e0c8c9ff5b5c7b8dca65327a93ae09cf5300 Mon Sep 17 00:00:00 2001 From: Konrad Borowski Date: Tue, 17 Dec 2019 14:28:43 +0100 Subject: [PATCH] Improve paste descriptions --- src/models/session.rs | 2 ++ src/routes/display_paste.rs | 12 +++++++++++- src/routes/mod.rs | 1 + templates/header.rs.html | 4 ++-- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/models/session.rs b/src/models/session.rs index f967083..126eb30 100644 --- a/src/models/session.rs +++ b/src/models/session.rs @@ -1,10 +1,12 @@ use crate::Connection; +use std::borrow::Cow; use warp::http::header::CONTENT_SECURITY_POLICY; use warp::http::response::{Builder, Response}; pub struct Session { pub nonce: String, pub connection: Connection, + pub description: Cow<'static, str>, } impl Session { diff --git a/src/routes/display_paste.rs b/src/routes/display_paste.rs index 9519c48..5bbd315 100644 --- a/src/routes/display_paste.rs +++ b/src/routes/display_paste.rs @@ -6,12 +6,13 @@ use crate::templates::{self, RenderRucte}; use diesel::prelude::*; use futures::future::*; use futures03::TryFutureExt; +use std::borrow::Cow; use tokio_executor::blocking; use warp::{Rejection, Reply}; pub fn display_paste( requested_identifier: String, - session: Session, + mut session: Session, ) -> impl Future { blocking::run(move || { let connection = &session.connection; @@ -34,6 +35,7 @@ pub fn display_paste( .optional() .map_err(warp::reject::custom)? .ok_or_else(warp::reject::not_found)?; + session.description = generate_description(&paste.paste); let selected_language = Some(paste.language_id); session.render().html(|o| { templates::display_paste( @@ -49,3 +51,11 @@ pub fn display_paste( }) .compat() } + +fn generate_description(paste: &str) -> Cow<'static, str> { + let mut description = paste.chars().take(239).collect(); + if description != paste { + description += "…"; + } + description +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs index fa3ff0e..2fad898 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -51,6 +51,7 @@ fn get_session(pool: PgPool) -> impl Future> Session { nonce: base64::encode(&bytes), connection, + description: "Compile and share code in multiple programming languages".into(), } }) } diff --git a/templates/header.rs.html b/templates/header.rs.html index e63ab4b..593f33e 100644 --- a/templates/header.rs.html +++ b/templates/header.rs.html @@ -1,13 +1,13 @@ @use crate::models::session::Session; -@(_session: &Session) +@(session: &Session) pastebin.run - +