geochan/src/pp_babillard.ml

216 lines
5.6 KiB
OCaml
Raw Normal View History

2022-02-02 19:16:53 +01:00
include Bindings
2022-01-29 09:34:57 +01:00
include Babillard
open Db
2022-02-18 19:40:19 +01:00
let view_post ?is_thread_preview id =
let* { id
; parent_id = _parent_id
; date
; nick
; comment
; image_info
; tags
; replies
; citations = _citations
} =
get_post id
in
2022-01-29 09:34:57 +01:00
2022-02-18 01:37:25 +01:00
let image_view fmt () =
2022-01-29 09:34:57 +01:00
match image_info with
| Some (_image_name, image_alt) ->
(*TODO thumbnails *)
(*TODO image info like file name and size on top of image*)
2022-02-18 01:37:25 +01:00
Format.fprintf fmt
2022-01-29 09:34:57 +01:00
{|
<div class="postImageContainer">
2022-02-18 19:40:19 +01:00
<a href="/img/%s">
<img class="postImage" src="/img/%s" alt="%s" title="%s" loading="lazy">
2022-01-29 09:34:57 +01:00
</a>
</div>
|}
2022-02-18 19:40:19 +01:00
id id image_alt image_alt
2022-02-18 01:37:25 +01:00
| None -> Format.fprintf fmt ""
2022-01-29 09:34:57 +01:00
in
let pp_print_reply fmt reply =
Format.fprintf fmt {|<a class="replyLink" href="#%s">&gt;&gt;%s</a>|} reply
reply
in
2022-02-18 01:37:25 +01:00
let pp_print_replies fmt replies =
Format.fprintf fmt {|<div class="replies">%a</div>|}
2022-01-29 09:34:57 +01:00
(Format.pp_print_list ~pp_sep:Format.pp_print_space pp_print_reply)
2022-02-02 13:47:04 +01:00
replies
2022-01-29 09:34:57 +01:00
in
2022-02-18 01:37:25 +01:00
let replies_view fmt () =
2022-01-29 09:34:57 +01:00
match is_thread_preview with
2022-02-18 01:37:25 +01:00
| None -> pp_print_replies fmt replies
2022-01-29 09:34:57 +01:00
| Some () -> (
2022-02-18 19:40:19 +01:00
let res_nb = Db.find Q.count_thread_posts id in
2022-01-29 09:34:57 +01:00
match res_nb with
2022-02-18 01:37:25 +01:00
| Error _ -> Format.fprintf fmt ""
2022-01-29 09:34:57 +01:00
| Ok ((1 | 2) as nb) ->
2022-02-18 01:37:25 +01:00
Format.fprintf fmt {|<div class="replies">%d reply</div>|} (nb - 1)
2022-01-29 09:34:57 +01:00
| Ok nb ->
2022-02-18 01:37:25 +01:00
Format.fprintf fmt {|<div class="replies">%d replies</div>|} (nb - 1) )
2022-01-29 09:34:57 +01:00
in
2022-02-18 01:37:25 +01:00
let post_links_view fmt () =
2022-01-29 09:34:57 +01:00
match is_thread_preview with
| None ->
2022-02-18 01:37:25 +01:00
Format.fprintf fmt
2022-01-29 09:34:57 +01:00
{|
<span class=postNo>
2022-02-18 18:31:55 +01:00
<a href="#%s" title="Link to this post" class="quote">#</a>
<button data-id="%s" class="quoteLink" title="Reply to this post">%s</button>
2022-01-29 09:34:57 +01:00
</span>
2022-02-18 01:37:25 +01:00
%a
2022-01-29 09:34:57 +01:00
|}
2022-02-18 19:40:19 +01:00
id id id replies_view ()
2022-02-18 01:37:25 +01:00
| Some () -> Format.fprintf fmt {|
%a
|} replies_view ()
2022-01-29 09:34:57 +01:00
in
2022-02-18 01:37:25 +01:00
let post_info_view fmt () =
Format.fprintf fmt
2022-01-29 09:34:57 +01:00
{|
<div class="postInfo">
<span class="nick">%s</span>
<span class="date" data-time="%d"></span>
2022-02-18 01:37:25 +01:00
%a
2022-01-29 09:34:57 +01:00
</div>|}
2022-02-18 01:37:25 +01:00
nick date post_links_view ()
2022-01-29 09:34:57 +01:00
in
2022-02-02 13:47:04 +01:00
let pp_print_tag fmt tag =
Format.fprintf fmt {|<span class="tag">%s</span>|} tag
in
2022-02-18 01:37:25 +01:00
let pp_print_tags fmt tags =
Format.fprintf fmt {|<div class="tags">%a</div>|}
2022-02-02 13:47:04 +01:00
(Format.pp_print_list ~pp_sep:Format.pp_print_space pp_print_tag)
tags
in
let tags = List.sort String.compare tags in
2022-02-18 01:37:25 +01:00
let tags_view fmt () = pp_print_tags fmt tags in
2022-02-02 13:47:04 +01:00
2022-01-29 09:34:57 +01:00
let post_view =
2022-02-18 01:37:25 +01:00
Format.asprintf
2022-01-29 09:34:57 +01:00
{|
<div class="container">
<div class="post" id="%s">
2022-02-18 01:37:25 +01:00
%a
%a
2022-01-29 09:34:57 +01:00
<blockquote class="postComment">%s</blockquote>
2022-02-18 01:37:25 +01:00
%a
2022-01-29 09:34:57 +01:00
</div>
</div>
|}
2022-02-18 19:40:19 +01:00
id post_info_view () image_view () comment tags_view ()
2022-01-29 09:34:57 +01:00
in
Ok post_view
2022-02-02 11:58:18 +01:00
let preview_thread thread_id =
2022-02-02 19:16:53 +01:00
let* post = view_post ~is_thread_preview:() thread_id in
let^? subject = Db.find_opt Q.get_post_subject thread_id in
2022-02-02 11:58:18 +01:00
let thread_preview =
Format.sprintf
{|
<div class="threadPreview">
<div class="threadSubject">
%s
</div>
%s
</div>
|}
subject post
in
Ok thread_preview
2022-01-29 09:34:57 +01:00
let view_thread thread_id =
2022-02-18 19:40:19 +01:00
let^ _is_thread = Db.find Q.get_is_thread thread_id in
2022-02-18 01:37:25 +01:00
let^? subject = Db.find_opt Q.get_post_subject thread_id in
let^ thread_posts = Db.collect_list Q.get_thread_posts thread_id in
(*order by date *)
let dates = List.map (Db.find Q.get_post_date) thread_posts in
match List.find_opt Result.is_error dates with
| Some (Error e) -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
| Some (Ok _) -> assert false
| None -> (
let dates = List.map Result.get_ok dates in
let posts_dates = List.combine thread_posts dates in
let sorted_posts_dates =
List.sort (fun (_, a) (_, b) -> compare a b) posts_dates
2022-01-29 09:34:57 +01:00
in
2022-02-18 01:37:25 +01:00
let posts, _ = List.split sorted_posts_dates in
let view_posts = List.map view_post posts in
match List.find_opt Result.is_error view_posts with
| Some (Error e) -> Error e
2022-01-29 09:34:57 +01:00
| Some (Ok _) -> assert false
2022-02-18 01:37:25 +01:00
| None ->
let posts =
List.map Result.get_ok (List.filter Result.is_ok view_posts)
2022-01-29 09:34:57 +01:00
in
2022-02-18 01:37:25 +01:00
let posts =
Format.asprintf "%a"
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "\r\n")
Format.pp_print_string )
posts
in
let thread_view =
Format.sprintf
{|
2022-02-02 11:58:18 +01:00
<div class="thread">
<div class="threadSubject">
%s
</div>
<div class="threadPosts">
%s
</div>
</div>
|}
2022-02-18 01:37:25 +01:00
subject posts
in
Ok thread_view )
2022-01-29 09:34:57 +01:00
let get_markers () =
2022-02-18 01:37:25 +01:00
let^ thread_id_list = Db.collect_list Q.get_threads () in
2022-01-29 09:34:57 +01:00
let markers_res =
List.map
(fun thread_id ->
2022-02-02 19:16:53 +01:00
let^? lat, lng = Db.find_opt Q.get_post_gps thread_id in
2022-01-29 09:34:57 +01:00
match preview_thread thread_id with
| Ok content -> Ok (lat, lng, content, thread_id)
| Error e -> Error e )
thread_id_list
in
let markers = List.map Result.get_ok (List.filter Result.is_ok markers_res) in
let pp_marker fmt (lat, lng, content, thread_id) =
Format.fprintf fmt
{|{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [%s,%s]
},
"properties": {
"content": "%s",
"thread_id": "%s"
}}|}
(* geojson use lng lat, and not lat lng*)
(Float.to_string lng)
(Float.to_string lat) (String.escaped content) thread_id
in
let markers =
Format.asprintf "[%a]"
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt ",")
pp_marker )
markers
in
Ok markers