diff --git a/src/main.rs b/src/main.rs index ab164f0..a6b41f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -132,16 +132,17 @@ fn login<'mw>(req: &mut Request, mut res: Response<'mw>) -> MiddlewareResult<'mw> { res.clear_jwt(); - let next = sanitize_next(req.query().get("next")); - res.ok(|o| templates::login(o, next)) + let next = sanitize_next(req.query().get("next")).map(String::from); + res.ok(|o| templates::login(o, req, next)) } fn do_login<'mw>(req: &mut Request, mut res: Response<'mw>) -> MiddlewareResult<'mw> { + let next = { let c: &PgConnection = &req.db_conn(); let form_data = try_with!(res, req.form_body()); - let next = sanitize_next(form_data.get("next")); + let next = sanitize_next(form_data.get("next")).map(String::from); if let (Some(user), Some(pw)) = (form_data.get("user"), form_data.get("password")) { use rphotos::schema::users::dsl::*; @@ -152,14 +153,16 @@ fn do_login<'mw>(req: &mut Request, if djangohashers::check_password_tolerant(pw, &hash) { info!("User {} logged in", user); res.set_jwt_user(user); - return res.redirect(next.unwrap_or("/")); + return res.redirect(next.unwrap_or("/".to_string())); } debug!("Password verification failed"); } else { debug!("No hash found for {}", user); } } - res.ok(|o| templates::login(o, next)) + next + }; + res.ok(|o| templates::login(o, req, next)) } fn sanitize_next(next: Option<&str>) -> Option<&str> { @@ -291,7 +294,7 @@ fn tag_all<'mw>(req: &mut Request, }; res.ok(|o| { templates::tags(o, - req.authorized_user(), + req, &query.order(tag_name).load(c).expect("List tags")) }) } @@ -307,7 +310,7 @@ fn tag_one<'mw>(req: &mut Request, use rphotos::schema::photo_tags::dsl::{photo_id, photo_tags, tag_id}; return res.ok(|o| { templates::tag(o, - req.authorized_user(), + req, &Photo::query(req.authorized_user().is_some()) .filter(id.eq_any(photo_tags.select(photo_id) .filter(tag_id.eq(tag.id)))) @@ -340,7 +343,7 @@ fn place_all<'mw>(req: &mut Request, let c: &PgConnection = &req.db_conn(); res.ok(|o| templates::places( o, - req.authorized_user(), + req, &query.order(place_name).load(c).expect("List places"))) } @@ -369,7 +372,7 @@ fn place_one<'mw>(req: &mut Request, place_id}; return res.ok(|o| templates::place( o, - req.authorized_user(), + req, &Photo::query(req.authorized_user().is_some()) .filter(id.eq_any(photo_places.select(photo_id) .filter(place_id.eq(place.id)))) @@ -400,7 +403,7 @@ fn person_all<'mw>(req: &mut Request, let c: &PgConnection = &req.db_conn(); res.ok(|o| templates::people( o, - req.authorized_user(), + req, &query.order(person_name).load(c).expect("list people"))) } @@ -416,7 +419,7 @@ fn person_one<'mw>(req: &mut Request, photo_people}; return res.ok(|o| templates::person( o, - req.authorized_user(), + req, &Photo::query(req.authorized_user().is_some()) .filter(id.eq_any(photo_people.select(photo_id) .filter(person_id.eq(person.id)))) @@ -437,12 +440,12 @@ fn photo_details<'mw>(req: &mut Request, if req.authorized_user().is_some() || tphoto.is_public() { return res.ok(|o| templates::details( o, + req, &tphoto.date .map(|d| vec![Link::year(d.year()), Link::month(d.year(), d.month()), Link::day(d.year(), d.month(), d.day())]) .unwrap_or_else(|| vec![]), - req.authorized_user(), &{ use rphotos::schema::people::dsl::{people, id}; use rphotos::schema::photo_people::dsl::{photo_people, photo_id, person_id}; diff --git a/src/views_by_date.rs b/src/views_by_date.rs index 07cfa97..03a6003 100644 --- a/src/views_by_date.rs +++ b/src/views_by_date.rs @@ -20,7 +20,6 @@ pub fn all_years<'mw>(req: &mut Request, use rphotos::schema::photos::dsl::{date, grade}; let c: &PgConnection = &req.db_conn(); - let user: Option = req.authorized_user(); let groups: Vec = SqlLiteral::new(format!( "select cast(extract(year from date) as int) y, count(*) c \ @@ -53,7 +52,7 @@ pub fn all_years<'mw>(req: &mut Request, } }).collect(); - res.ok(|o| templates::groups(o, "All photos", &[], user, &groups)) + res.ok(|o| templates::groups(o, req, "All photos", &[], &groups)) } pub fn months_in_year<'mw>(req: &mut Request, @@ -63,7 +62,6 @@ pub fn months_in_year<'mw>(req: &mut Request, use rphotos::schema::photos::dsl::{date, grade}; let c: &PgConnection = &req.db_conn(); - let user: Option = req.authorized_user(); let title: String = format!("Photos from {}", year); let groups: Vec = SqlLiteral::new(format!( @@ -102,7 +100,7 @@ pub fn months_in_year<'mw>(req: &mut Request, if groups.is_empty() { res.not_found("No such image") } else { - res.ok(|o| templates::groups(o, &title, &[], user, &groups)) + res.ok(|o| templates::groups(o, req, &title, &[], &groups)) } } @@ -114,7 +112,6 @@ pub fn days_in_month<'mw>(req: &mut Request, use rphotos::schema::photos::dsl::{date, grade}; let c: &PgConnection = &req.db_conn(); - let user: Option = req.authorized_user(); let lpath: Vec = vec![Link::year(year)]; let title: String = format!("Photos from {} {}", monthname(month), year); let groups: Vec = @@ -151,7 +148,7 @@ pub fn days_in_month<'mw>(req: &mut Request, if groups.is_empty() { res.not_found("No such image") } else { - res.ok(|o| templates::groups(o, &title, &lpath, user, &groups)) + res.ok(|o| templates::groups(o, req, &title, &lpath, &groups)) } } @@ -163,9 +160,9 @@ pub fn all_null_date<'mw>(req: &mut Request, let c: &PgConnection = &req.db_conn(); res.ok(|o| templates::index( o, + req, &"Photos without a date", &[], - req.authorized_user(), &Photo::query(req.authorized_user().is_some()) .filter(date.is_null()) .order(path.asc()) @@ -198,9 +195,9 @@ pub fn all_for_day<'mw>(req: &mut Request, warn!("Got {} photos, way to many", n); res.ok(|o| templates::groups( o, + req, &format!("Photos from {} {}", day, monthname(month)), &[Link::year(year), Link::month(year, month)], - req.authorized_user(), &(photos.chunks((n as f64).sqrt() as usize) .enumerate() .map(|(i, chunk)| { @@ -232,9 +229,9 @@ pub fn all_for_day<'mw>(req: &mut Request, } else { res.ok(|o| templates::index( o, + req, &format!("Photos from {} {} {}", day, monthname(month), year), &[Link::year(year), Link::month(year, month)], - req.authorized_user(), &photos, )) } @@ -267,6 +264,7 @@ pub fn part_for_day<'mw>( if let Some(photos) = chunks.nth(part) { res.ok(|o| templates::index( o, + req, &format!( "Photos from {} {} {}, {} - {}", day, monthname(month), year, @@ -284,7 +282,6 @@ pub fn part_for_day<'mw>( Link::month(year, month), Link::day(year, month, day), ], - req.authorized_user(), &photos, )) } else { @@ -305,9 +302,9 @@ pub fn on_this_day<'mw>(req: &mut Request, }; res.ok(|o| templates::groups( o, + req, &format!("Photos from {} {}", day, monthname(month)), &[], - req.authorized_user(), &SqlLiteral::new(format!( "select extract(year from date) y, count(*) c \ from photos where extract(month from date)={} \ diff --git a/templates/details.rs.html b/templates/details.rs.html index 911c4b4..0b45dc0 100644 --- a/templates/details.rs.html +++ b/templates/details.rs.html @@ -1,13 +1,15 @@ @use ::{Coord, Link}; +@use nickel::Request; +@use nickel_jwt_session::SessionRequestExtensions; @use rphotos::models::{Photo, Person, Place, Tag, Camera}; @use templates::page_base; -@(lpath: &[Link], user: Option, people: &[Person], places: &[Place], tags: &[Tag], position: Option, attribution: Option, camera: Option, photo: Photo) -@:page_base("Photo details", lpath, &user, { +@(req: &Request, lpath: &[Link], people: &[Person], places: &[Place], tags: &[Tag], position: Option, attribution: Option, camera: Option, photo: Photo) +@:page_base(req, "Photo details", lpath, {
- @if user.is_some() { + @if req.authorized_user().is_some() {

@photo.path

@if photo.is_public() {

This photo is public.

} else {

This photo is not public.

} diff --git a/templates/groups.rs.html b/templates/groups.rs.html index 548a0fa..c88bdfc 100644 --- a/templates/groups.rs.html +++ b/templates/groups.rs.html @@ -1,9 +1,10 @@ @use ::{Group, Link}; +@use nickel::Request; @use templates::page_base; -@(title: &str, lpath: &[Link], user: Option, groups: &[Group]) +@(req: &Request, title: &str, lpath: &[Link], groups: &[Group]) -@:page_base(title, lpath, &user, { +@:page_base(req, title, lpath, {
@if groups.is_empty() {

Inga bilder.

diff --git a/templates/head.rs.html b/templates/head.rs.html index 575466b..7d8fae8 100644 --- a/templates/head.rs.html +++ b/templates/head.rs.html @@ -1,6 +1,8 @@ @use ::Link; +@use nickel::Request; +@use nickel_jwt_session::SessionRequestExtensions; -@(lpath: &[Link], user: &Option) +@(req: &Request, lpath: &[Link])
Images @@ -10,6 +12,6 @@ · People · Places · On this day -@if let &Some(ref u) = user {@u (log out)} -else {(log in)} +@if let Some(ref u) = req.authorized_user() {@u (log out)} +else {(log in)}
diff --git a/templates/index.rs.html b/templates/index.rs.html index 8f79d7b..17c4ad7 100644 --- a/templates/index.rs.html +++ b/templates/index.rs.html @@ -1,10 +1,11 @@ @use ::Link; +@use nickel::Request; @use rphotos::models::Photo; @use templates::{page_base, img_link}; -@(title: &str, lpath: &[Link], user: Option, photos: &[Photo]) +@(req: &Request, title: &str, lpath: &[Link], photos: &[Photo]) -@:page_base(title, lpath, &user, { +@:page_base(req, title, lpath, {
@for p in photos {@:img_link(p)}
diff --git a/templates/login.rs.html b/templates/login.rs.html index 6844211..7401c37 100644 --- a/templates/login.rs.html +++ b/templates/login.rs.html @@ -1,14 +1,15 @@ +@use nickel::Request; @use templates::page_base; -@(next: Option<&str>) +@(req: &Request, next: Option) -@:page_base("login", &[], &None, { +@:page_base(req, "login", &[], {

-

@if let Some(next) = next { +

@if let Some(ref next) = next { } diff --git a/templates/not_found.rs.html b/templates/not_found.rs.html index db3ef21..d9ed598 100644 --- a/templates/not_found.rs.html +++ b/templates/not_found.rs.html @@ -4,7 +4,7 @@ @(req: &Request) -@:page_base("Not found", &[], &req.authorized_user(), { +@:page_base(req, "Not found", &[], {

No page or photo match that url.

@if req.authorized_user().is_none() {

At least nothing publicly visible, you might try diff --git a/templates/page_base.rs.html b/templates/page_base.rs.html index 19b6cef..f9533f7 100644 --- a/templates/page_base.rs.html +++ b/templates/page_base.rs.html @@ -1,8 +1,9 @@ @use ::Link; +@use nickel::Request; @use templates::head; @use templates::statics::photos_css; -@(title: &str, lpath: &[Link], user: &Option, content: Content) +@(req: &Request, title: &str, lpath: &[Link], content: Content) @@ -13,7 +14,7 @@ - @:head(lpath, user) + @:head(req, lpath)

@title

@:content() diff --git a/templates/people.rs.html b/templates/people.rs.html index 2020aa5..bb9d76c 100644 --- a/templates/people.rs.html +++ b/templates/people.rs.html @@ -1,8 +1,9 @@ +@use nickel::Request; @use rphotos::models::Person; @use templates::page_base; -@(user: Option, people: &[Person]) -@:page_base("Photo people", &[], &user, { +@(req: &Request, people: &[Person]) +@:page_base(req, "Photo people", &[], {
    @for p in people {
  • @p.person_name diff --git a/templates/person.rs.html b/templates/person.rs.html index 37b240f..735dd3b 100644 --- a/templates/person.rs.html +++ b/templates/person.rs.html @@ -1,8 +1,9 @@ +@use nickel::Request; @use rphotos::models::{Photo, Person}; @use templates::{page_base, img_link}; -@(user: Option, photos: &[Photo], person: Person) -@:page_base(&format!("Photos with {}", person.person_name), &[], &user, { +@(req: &Request, photos: &[Photo], person: Person) +@:page_base(req, &format!("Photos with {}", person.person_name), &[], {
    @for p in photos {@:img_link(p)}
    diff --git a/templates/place.rs.html b/templates/place.rs.html index 2012594..cc20cc6 100644 --- a/templates/place.rs.html +++ b/templates/place.rs.html @@ -1,8 +1,9 @@ +@use nickel::Request; @use rphotos::models::{Photo, Place}; @use templates::{page_base, img_link}; -@(user: Option, photos: &[Photo], place: Place) -@:page_base(&format!("Photos from {}", place.place_name), &[], &user, { +@(req: &Request, photos: &[Photo], place: Place) +@:page_base(req, &format!("Photos from {}", place.place_name), &[], {
    @for p in photos {@:img_link(p)}
    diff --git a/templates/places.rs.html b/templates/places.rs.html index 008804d..163e3f2 100644 --- a/templates/places.rs.html +++ b/templates/places.rs.html @@ -1,9 +1,10 @@ +@use nickel::Request; @use rphotos::models::Place; @use templates::page_base; -@(user: Option, places: &[Place]) +@(req: &Request, places: &[Place]) -@:page_base("Photo places", &[], &user, { +@:page_base(req, "Photo places", &[], {
      @for p in places {
    • @p.place_name diff --git a/templates/tag.rs.html b/templates/tag.rs.html index 23cffdd..7fcf26f 100644 --- a/templates/tag.rs.html +++ b/templates/tag.rs.html @@ -1,9 +1,10 @@ +@use nickel::Request; @use rphotos::models::{Photo, Tag}; @use templates::{page_base, img_link}; -@(user: Option, photos: &[Photo], tag: Tag) +@(req: &Request, photos: &[Photo], tag: Tag) -@:page_base(&format!("Photos tagged {}", tag.tag_name), &[], &user, { +@:page_base(req, &format!("Photos tagged {}", tag.tag_name), &[], {
      @for p in photos {@:img_link(p)}
      diff --git a/templates/tags.rs.html b/templates/tags.rs.html index 83cf38d..24b5149 100644 --- a/templates/tags.rs.html +++ b/templates/tags.rs.html @@ -1,8 +1,9 @@ +@use nickel::Request; @use rphotos::models::Tag; @use templates::page_base; -@(user: Option, tags: &[Tag]) -@:page_base("Photo tags", &[], &user, { +@(req: &Request, tags: &[Tag]) +@:page_base(req, "Photo tags", &[], {