refactor some code

This commit is contained in:
zapashcanon 2022-04-05 23:08:57 +02:00
parent 0632a713c7
commit 3796a96997
No known key found for this signature in database
GPG key ID: 8981C3C62D1D28F1
7 changed files with 330 additions and 403 deletions

View file

@ -1,4 +1,5 @@
open Db
open Syntax
(** Creating the table of all messages.
@ -57,87 +58,62 @@ let find_messages k1 k2 =
Ok comrades
(** display the list of discussions *)
let render request =
match Dream.session "user_id" request with
| None ->
let redirect_url =
Format.sprintf "/login=?redirect=%s" (Dream.to_percent_encoded "/discuss")
in
Dream.respond ~status:`See_Other ~headers:[ ("Location", redirect_url) ] ""
| Some user_id -> (
match find_comrades user_id with
| Error e -> Template_utils.render_unsafe e request
| Ok comrades -> (
let comrades =
Syntax.unwrap_list
(fun id ->
match User.get_nick id with
| Error _e as e -> e
| Ok nick -> Ok (id, nick) )
comrades
in
match comrades with
| Error e -> Template_utils.render_unsafe e request
| Ok comrades ->
let pp_one_discuss fmt (id, nick) =
Format.fprintf fmt {|<li><a href="/discuss/%s">%s</a></li>|} id nick
in
let output =
Format.asprintf "<ul>%a</ul>"
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "<br />")
pp_one_discuss )
comrades
in
Template_utils.render_unsafe output request ) )
let render =
let pp_one_discuss fmt (id, nick) =
Format.fprintf fmt {|<li><a href="/discuss/%s">%s</a></li>|} id nick
in
fun request ->
Utils.logged_in_or_redirect request (fun user_id ->
Utils.render_result request
@@ let* comrades = find_comrades user_id in
let* comrades =
Syntax.unwrap_list
(fun id ->
match User.get_nick id with
| Error _e as e -> e
| Ok nick -> Ok (id, nick) )
comrades
in
Ok
(Format.asprintf "<ul>%a</ul>"
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "<br />")
pp_one_discuss )
comrades ) )
let pp_discussion (request, user_id, comrade_id) =
let path = Format.sprintf "/discuss/%s" comrade_id in
match find_messages user_id comrade_id with
| Error e -> Template_utils.render_unsafe e request
| Ok msg -> (
match User.get_nick user_id with
| Error e -> Template_utils.render_unsafe e request
| Ok user_nick -> (
match User.get_nick comrade_id with
| Error e -> Template_utils.render_unsafe e request
| Ok comrade_nick ->
let pp_one_msg fmt (from_id, msg) =
Format.fprintf fmt "<li>%s | %s</li>"
(if from_id = user_id then user_nick else comrade_nick)
msg
in
let pp_all_msg fmt msg =
Format.fprintf fmt "<ul>%a</ul>"
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "<br />")
pp_one_msg )
msg
in
Template_utils.render_unsafe
(Format.asprintf
{|%a<br />
Utils.render_result request
@@ let* msg = find_messages user_id comrade_id in
let* user_nick = User.get_nick user_id in
let* comrade_nick = User.get_nick comrade_id in
let pp_one_msg fmt (from_id, msg) =
Format.fprintf fmt "<li>%s | %s</li>"
(if from_id = user_id then user_nick else comrade_nick)
msg
in
let pp_all_msg fmt msg =
Format.fprintf fmt "<ul>%a</ul>"
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "<br />")
pp_one_msg )
msg
in
Ok
(Format.asprintf
{|%a<br />
%s
<input value="" name="msg" type="text" />
<button type="submit" class="btn btn-primary">Send</button>
</form>|}
pp_all_msg msg
(Dream.form_tag ~action:path request) )
request ) )
pp_all_msg msg
(Dream.form_tag ~action:path request) )
(** display one discussion *)
let render_one request =
let comrade_id = Dream.param request "comrade_id" in
let path = Format.sprintf "/discuss/%s" comrade_id in
match Dream.session "user_id" request with
| None ->
let redirect_url =
Format.sprintf "/login=?redirect=%s" (Dream.to_percent_encoded path)
in
Dream.respond ~status:`See_Other ~headers:[ ("Location", redirect_url) ] ""
| Some user_id -> pp_discussion (request, user_id, comrade_id)
let renderone request =
Utils.logged_in_or_redirect request (fun user_id ->
let comrade_id = Dream.param request "comrade_id" in
pp_discussion (request, user_id, comrade_id) )
let insert_msg from_id to_id msg =
let open Syntax in
@ -146,24 +122,15 @@ let insert_msg from_id to_id msg =
(** handle posts *)
let post request =
let comrade_id = Dream.param request "comrade_id" in
let path = Format.sprintf "/discuss/%s" comrade_id in
match Dream.session "user_id" request with
| None ->
let redirect_url =
Format.sprintf "/login=?redirect=%s" (Dream.to_percent_encoded path)
in
Dream.respond ~status:`See_Other ~headers:[ ("Location", redirect_url) ] ""
| Some user_id -> (
match%lwt Dream.form request with
| `Ok [ ("msg", msg) ] -> begin
match insert_msg user_id comrade_id msg with
| Ok () -> pp_discussion (request, user_id, comrade_id)
| Error e -> Template_utils.render_unsafe e request
end
| `Ok _ -> Dream.respond ~status:`Bad_Request "invalid form"
| `Expired _ | `Many_tokens _ | `Missing_token _ | `Invalid_token _
| `Wrong_session _ | `Wrong_content_type ->
Dream.empty `Bad_Request )
Utils.logged_in_or_redirect request (fun user_id ->
match%lwt Dream.form request with
| `Ok [ ("msg", msg) ] -> begin
let comrade_id = Dream.param request "comrade_id" in
match insert_msg user_id comrade_id msg with
| Ok () -> pp_discussion (request, user_id, comrade_id)
| Error e -> Utils.render e request
end
| `Ok _ -> Dream.respond ~status:`Bad_Request "invalid form"
| `Expired _ | `Many_tokens _ | `Missing_token _ | `Invalid_token _
| `Wrong_session _ | `Wrong_content_type ->
Dream.empty `Bad_Request )