view_post -> pp_post

This commit is contained in:
Swrup 2022-02-21 00:51:44 +01:00
parent 258175565e
commit fe94594592

View file

@ -2,8 +2,8 @@ include Bindings
include Babillard include Babillard
open Db open Db
let view_post ?is_thread_preview id = let pp_post fmt ~hide_replies post =
let* { id let { id
; parent_id = _parent_id ; parent_id = _parent_id
; date ; date
; nick ; nick
@ -13,14 +13,12 @@ let view_post ?is_thread_preview id =
; replies ; replies
; citations = _citations ; citations = _citations
} = } =
get_post id post
in in
let image_view fmt () = let image_view fmt () =
match image_info with match image_info with
| Some (_image_name, image_alt) -> | Some (_image_name, image_alt) ->
(*TODO thumbnails *)
(*TODO image info like file name and size on top of image*)
Format.fprintf fmt Format.fprintf fmt
{| {|
<div class="post-image-container"> <div class="post-image-container">
@ -44,21 +42,24 @@ let view_post ?is_thread_preview id =
in in
let replies_view fmt () = let replies_view fmt () =
match is_thread_preview with if hide_replies then
| None -> pp_print_replies fmt replies (* TODO put thread_posts count in thread_info ? *)
| Some () -> (
let res_nb = Db.find Q.count_thread_posts id in let res_nb = Db.find Q.count_thread_posts id in
match res_nb with match res_nb with
| Error _ -> Format.fprintf fmt "" | Error _ -> Format.fprintf fmt ""
| Ok ((1 | 2) as nb) -> | Ok ((1 | 2) as nb) ->
Format.fprintf fmt {|<div class="replies">%d reply</div>|} (nb - 1) Format.fprintf fmt {|<div class="replies">%d reply</div>|} (nb - 1)
| Ok nb -> | Ok nb ->
Format.fprintf fmt {|<div class="replies">%d replies</div>|} (nb - 1) ) Format.fprintf fmt {|<div class="replies">%d replies</div>|} (nb - 1)
else pp_print_replies fmt replies
in in
let post_links_view fmt () = let post_links_view fmt () =
match is_thread_preview with if hide_replies then
| None -> Format.fprintf fmt {|
%a
|} replies_view ()
else
Format.fprintf fmt Format.fprintf fmt
{| {|
<span class=postNo> <span class=postNo>
@ -68,9 +69,6 @@ let view_post ?is_thread_preview id =
%a %a
|} |}
id id id replies_view () id id id replies_view ()
| Some () -> Format.fprintf fmt {|
%a
|} replies_view ()
in in
let post_info_view fmt () = let post_info_view fmt () =
@ -95,8 +93,8 @@ let view_post ?is_thread_preview id =
let tags = List.sort String.compare tags in let tags = List.sort String.compare tags in
let tags_view fmt () = pp_print_tags fmt tags in let tags_view fmt () = pp_print_tags fmt tags in
let post_view = let pp =
Format.asprintf Format.fprintf fmt
{| {|
<div class="container"> <div class="container">
<div class="post" id="%s"> <div class="post" id="%s">
@ -109,11 +107,15 @@ let view_post ?is_thread_preview id =
|} |}
id post_info_view () image_view () comment tags_view () id post_info_view () image_view () comment tags_view ()
in in
Ok post_view pp
let preview_thread thread_id = let preview_thread thread_id =
let* post = view_post ~is_thread_preview:() thread_id in let* post_data = get_post thread_id in
let^? subject, _lat, _lng = Db.find_opt Q.get_thread_info thread_id in let^? subject, _lat, _lng = Db.find_opt Q.get_thread_info thread_id in
let post =
(Format.asprintf "%a" (fun fmt data -> pp_post fmt ~hide_replies:true data))
post_data
in
let thread_preview = let thread_preview =
Format.sprintf Format.sprintf
{| {|
@ -151,7 +153,7 @@ let view_thread thread_id =
match List.find_opt Result.is_error dates with match List.find_opt Result.is_error dates with
| Some (Error e) -> Error (Format.sprintf "db error: %s" (Caqti_error.show e)) | Some (Error e) -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
| Some (Ok _) -> assert false | Some (Ok _) -> assert false
| None -> ( | None ->
let dates = List.map Result.get_ok dates in let dates = List.map Result.get_ok dates in
let posts_dates = List.combine thread_posts dates in let posts_dates = List.combine thread_posts dates in
let sorted_posts_dates = let sorted_posts_dates =
@ -159,20 +161,19 @@ let view_thread thread_id =
in in
let posts, _ = List.split sorted_posts_dates in let posts, _ = List.split sorted_posts_dates in
let view_posts = List.map view_post posts in let posts_data = List.map get_post posts in
match List.find_opt Result.is_error view_posts with let res_opt = List.find_opt Result.is_error posts_data in
| Some (Error e) -> Error e if Option.is_some res_opt then
| Some (Ok _) -> assert false let res = Result.get_error (Option.get res_opt) in
| None -> Error res
let posts = else
List.map Result.get_ok (List.filter Result.is_ok view_posts) let posts_data = List.map Result.get_ok posts_data in
in
let posts = let posts =
Format.asprintf "%a" Format.asprintf "%a"
(Format.pp_print_list (Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "\r\n") ~pp_sep:(fun fmt () -> Format.fprintf fmt "\r\n")
Format.pp_print_string ) (fun fmt data -> pp_post fmt ~hide_replies:false data) )
posts posts_data
in in
let thread_view = let thread_view =
Format.sprintf Format.sprintf
@ -188,7 +189,7 @@ let view_thread thread_id =
|} |}
subject posts subject posts
in in
Ok thread_view ) Ok thread_view
let get_markers () = let get_markers () =
let^ threads = Db.collect_list Q.get_threads () in let^ threads = Db.collect_list Q.get_threads () in