Merge branch 'improve-descriptions' into 'master'

Improve paste descriptions

See merge request pastebin.run/server!90
This commit is contained in:
Konrad Borowski 2019-12-17 13:45:13 +00:00
commit 87bad84cb3
4 changed files with 16 additions and 3 deletions

View File

@ -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 {

View File

@ -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<Item = impl Reply, Error = Rejection> {
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
}

View File

@ -51,6 +51,7 @@ fn get_session(pool: PgPool) -> impl Future<Output = Result<Session, Rejection>>
Session {
nonce: base64::encode(&bytes),
connection,
description: "Compile and share code in multiple programming languages".into(),
}
})
}

View File

@ -1,13 +1,13 @@
@use crate::models::session::Session;
@(_session: &Session)
@(session: &Session)
<!DOCTYPE html>
<meta charset=utf-8>
<meta name=viewport content="width=device-width, initial-scale=1">
<title>pastebin.run</title>
<link rel=stylesheet href="/static/style.css">
<meta name=description content="Pastebin.run is a website for privately storing and sharing text online.">
<meta name=description content="@session.description">
<header id=header>
<div>
<a href="/">