From b99a972dd537702488d93a2f1fcf6e9dbdea92ac Mon Sep 17 00:00:00 2001 From: Joseph Thomas Date: Mon, 19 Jul 2021 09:32:52 -0600 Subject: [PATCH 1/3] Tidy up flash message cookies. --- src/middleware/flash.ml | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/middleware/flash.ml b/src/middleware/flash.ml index d3ab6b1b..4a7a8d1e 100644 --- a/src/middleware/flash.ml +++ b/src/middleware/flash.ml @@ -19,24 +19,27 @@ let storage = let flash_cookie = "dream.flash" - - let flash_messages inner_handler request = let outbox = ref [] in let request = Dream.with_local storage outbox request in let%lwt response = inner_handler request in let entries = List.rev !outbox in - let content = - List.fold_right (fun (x,y) a -> `String x :: `String y :: a) entries [] in - let value = `List content |> Yojson.Basic.to_string in - Dream.set_cookie flash_cookie value request response ~max_age:five_minutes - |> Lwt.return - - + let existing = Dream.cookie flash_cookie request in + let resp = match existing, entries with + | None, [] -> response + | Some _, [] -> Dream.set_cookie flash_cookie "" request response ~expires:0. + | _, _ -> + let content = + List.fold_right (fun (x,y) a -> `String x :: `String y :: a) entries [] in + let value = `List content |> Yojson.Basic.to_string in + Dream.set_cookie flash_cookie value request response ~max_age:five_minutes + in + Lwt.return resp let (|>?) = Option.bind + let flash request = let rec group x = match x with From c4ccc29ff7278d02e6f93f877189aee136ca581d Mon Sep 17 00:00:00 2001 From: Joseph Thomas Date: Mon, 19 Jul 2021 09:38:32 -0600 Subject: [PATCH 2/3] Avoid unnecessary whitespace changes. --- src/middleware/flash.ml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/middleware/flash.ml b/src/middleware/flash.ml index 4a7a8d1e..684d4383 100644 --- a/src/middleware/flash.ml +++ b/src/middleware/flash.ml @@ -19,6 +19,8 @@ let storage = let flash_cookie = "dream.flash" + + let flash_messages inner_handler request = let outbox = ref [] in let request = Dream.with_local storage outbox request in @@ -36,10 +38,10 @@ let flash_messages inner_handler request = in Lwt.return resp + let (|>?) = Option.bind - let flash request = let rec group x = match x with From de51d1804284b3dc795e836762ea1d11263e26e8 Mon Sep 17 00:00:00 2001 From: Anton Bachin Date: Tue, 20 Jul 2021 18:24:33 +0300 Subject: [PATCH 3/3] Minimize meaningful diff --- src/middleware/flash.ml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/middleware/flash.ml b/src/middleware/flash.ml index 684d4383..018471c7 100644 --- a/src/middleware/flash.ml +++ b/src/middleware/flash.ml @@ -31,10 +31,10 @@ let flash_messages inner_handler request = | None, [] -> response | Some _, [] -> Dream.set_cookie flash_cookie "" request response ~expires:0. | _, _ -> - let content = - List.fold_right (fun (x,y) a -> `String x :: `String y :: a) entries [] in - let value = `List content |> Yojson.Basic.to_string in - Dream.set_cookie flash_cookie value request response ~max_age:five_minutes + let content = + List.fold_right (fun (x,y) a -> `String x :: `String y :: a) entries [] in + let value = `List content |> Yojson.Basic.to_string in + Dream.set_cookie flash_cookie value request response ~max_age:five_minutes in Lwt.return resp