2022-04-05 23:08:57 +02:00
|
|
|
open Utils
|
|
|
|
|
open Syntax
|
2022-03-06 20:55:12 +01:00
|
|
|
|
2021-11-05 16:55:19 +01:00
|
|
|
let asset_loader _root path _request =
|
|
|
|
|
match Content.read ("assets/" ^ path) with
|
|
|
|
|
| None -> Dream.empty `Not_Found
|
2022-04-04 16:57:08 +02:00
|
|
|
| Some asset ->
|
|
|
|
|
Dream.respond ~headers:[ ("Cache-Control", "max-age=151200") ] asset
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2021-11-07 10:38:19 +01:00
|
|
|
let page name request =
|
|
|
|
|
match Content.read (name ^ ".md") with
|
2021-11-05 16:55:19 +01:00
|
|
|
| None -> Dream.empty `Not_Found
|
2021-11-07 10:38:19 +01:00
|
|
|
| Some page ->
|
|
|
|
|
let content = Omd.of_string page |> Omd.to_html in
|
2022-04-05 23:08:57 +02:00
|
|
|
render content request
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2022-02-16 17:00:29 +01:00
|
|
|
let about request = page "about" request
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2022-04-05 23:08:57 +02:00
|
|
|
let register_get request = render (Register.f request) request
|
2021-11-07 01:32:38 +01:00
|
|
|
|
|
|
|
|
let register_post request =
|
|
|
|
|
match%lwt Dream.form request with
|
2022-03-08 23:05:38 +01:00
|
|
|
| `Ok [ ("email", email); ("nick", nick); ("password", password) ] -> (
|
|
|
|
|
match User.register ~email ~nick ~password with
|
2022-04-05 23:08:57 +02:00
|
|
|
| Error e -> render e request
|
2022-03-08 23:05:38 +01:00
|
|
|
| Ok () ->
|
|
|
|
|
let res =
|
|
|
|
|
Result.fold ~error:Fun.id
|
|
|
|
|
~ok:(fun _ -> "User created ! Welcome !")
|
|
|
|
|
(User.login ~login:nick ~password request)
|
|
|
|
|
in
|
2022-04-05 23:08:57 +02:00
|
|
|
render res request )
|
2022-04-05 23:22:55 +02:00
|
|
|
| form -> Utils.handle_invalid_form form
|
2021-11-07 01:32:38 +01:00
|
|
|
|
2022-04-05 23:08:57 +02:00
|
|
|
let login_get request = render (Login.f request) request
|
2021-11-07 01:32:38 +01:00
|
|
|
|
|
|
|
|
let login_post request =
|
|
|
|
|
match%lwt Dream.form request with
|
2022-03-08 21:20:01 +01:00
|
|
|
| `Ok [ ("login", login); ("password", password) ] -> (
|
|
|
|
|
match User.login ~login ~password request with
|
2022-04-05 23:08:57 +02:00
|
|
|
| Error e -> render e request
|
2022-02-20 18:02:25 +01:00
|
|
|
| Ok () ->
|
|
|
|
|
let url =
|
|
|
|
|
match Dream.query request "redirect" with
|
2022-02-20 18:37:24 +01:00
|
|
|
| None -> "/"
|
2022-02-20 18:02:25 +01:00
|
|
|
| Some redirect -> Dream.from_percent_encoded redirect
|
|
|
|
|
in
|
|
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", url) ]
|
|
|
|
|
"Logged in: Happy geo-posting!" )
|
2022-04-05 23:22:55 +02:00
|
|
|
| form -> Utils.handle_invalid_form form
|
2021-11-07 01:32:38 +01:00
|
|
|
|
2022-02-23 22:39:48 +01:00
|
|
|
let admin_get request =
|
2022-03-08 21:20:01 +01:00
|
|
|
match Dream.session "user_id" request with
|
2022-02-23 22:39:48 +01:00
|
|
|
| None ->
|
|
|
|
|
let redirect_url =
|
|
|
|
|
Format.sprintf "/login?redirect=%s" (Dream.to_percent_encoded "/admin")
|
|
|
|
|
in
|
|
|
|
|
Dream.respond ~status:`See_Other ~headers:[ ("Location", redirect_url) ] ""
|
2022-03-08 21:20:01 +01:00
|
|
|
| Some user_id ->
|
|
|
|
|
if not (User.is_admin user_id) then Dream.respond ~status:`Forbidden ""
|
2022-02-23 22:39:48 +01:00
|
|
|
else
|
|
|
|
|
let res =
|
|
|
|
|
match Babillard.get_reports () with
|
|
|
|
|
| Error e -> e
|
|
|
|
|
| Ok (posts, reports) ->
|
|
|
|
|
Pp_babillard.admin_page_content posts reports request
|
|
|
|
|
in
|
2022-04-05 23:08:57 +02:00
|
|
|
render res request
|
2022-02-23 22:39:48 +01:00
|
|
|
|
|
|
|
|
let admin_post request =
|
2022-04-05 23:08:57 +02:00
|
|
|
Utils.logged_in_or_redirect request (fun user_id ->
|
|
|
|
|
if not (User.is_admin user_id) then Dream.respond ~status:`Forbidden ""
|
|
|
|
|
else
|
|
|
|
|
match%lwt Dream.form request with
|
|
|
|
|
| `Ok [ ("action", action); ("post_id", id) ] -> (
|
|
|
|
|
(* TODO: use let* and Utils.render_result ? *)
|
|
|
|
|
let res =
|
|
|
|
|
match Babillard.get_post id with
|
|
|
|
|
| Error _e as e -> e
|
|
|
|
|
| Ok post -> (
|
|
|
|
|
let evil_user_id = post.user_id in
|
|
|
|
|
match Babillard.moderation_action_from_string action with
|
|
|
|
|
| None -> Error "Invalid action"
|
|
|
|
|
| Some action -> (
|
|
|
|
|
match action with
|
|
|
|
|
| Delete -> Babillard.try_delete_post ~user_id:evil_user_id id
|
|
|
|
|
| Banish -> User.banish evil_user_id
|
|
|
|
|
| Ignore -> Babillard.ignore_report id ) )
|
|
|
|
|
in
|
|
|
|
|
match res with
|
|
|
|
|
| Error e -> render e request
|
|
|
|
|
| Ok () ->
|
|
|
|
|
(* TODO: ??? *)
|
|
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", "/admin") ]
|
|
|
|
|
"" )
|
2022-04-05 23:22:55 +02:00
|
|
|
| form -> Utils.handle_invalid_form form )
|
2022-02-23 22:39:48 +01:00
|
|
|
|
2022-02-21 00:19:35 +01:00
|
|
|
let catalog request =
|
|
|
|
|
let catalog_content =
|
|
|
|
|
Result.fold ~ok:Fun.id ~error:Fun.id (Pp_babillard.catalog_content ())
|
|
|
|
|
in
|
2022-04-05 23:08:57 +02:00
|
|
|
render (Catalog_page.f catalog_content) request
|
2022-02-21 00:19:35 +01:00
|
|
|
|
2022-02-21 09:46:27 +01:00
|
|
|
let delete_get request =
|
|
|
|
|
let post_id = Dream.param request "post_id" in
|
|
|
|
|
let post_preview =
|
|
|
|
|
Result.fold ~ok:Fun.id ~error:Fun.id (Pp_babillard.view_post post_id)
|
|
|
|
|
in
|
2022-04-05 23:08:57 +02:00
|
|
|
render (Delete_page.f post_preview post_id request) request
|
2022-02-21 09:46:27 +01:00
|
|
|
|
|
|
|
|
let delete_post request =
|
2022-04-05 23:08:57 +02:00
|
|
|
Utils.logged_in_or_redirect request (fun user_id ->
|
|
|
|
|
(* match on Dream.form needed for hidden csrf field *)
|
|
|
|
|
match%lwt Dream.form request with
|
|
|
|
|
| `Ok [] -> (
|
|
|
|
|
(* TODO: use let* and Utils.render_result ? *)
|
|
|
|
|
let post_id = Dream.param request "post_id" in
|
|
|
|
|
match Babillard.try_delete_post ~user_id post_id with
|
|
|
|
|
| Error e -> render e request
|
|
|
|
|
| Ok () ->
|
|
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", "/") ]
|
|
|
|
|
"Your post was deleted!" )
|
2022-04-05 23:22:55 +02:00
|
|
|
| form -> Utils.handle_invalid_form form )
|
2022-02-22 07:10:52 +01:00
|
|
|
|
|
|
|
|
let report_get request =
|
|
|
|
|
let post_id = Dream.param request "post_id" in
|
|
|
|
|
let post_preview =
|
|
|
|
|
Result.fold ~ok:Fun.id ~error:Fun.id (Pp_babillard.view_post post_id)
|
|
|
|
|
in
|
2022-04-05 23:08:57 +02:00
|
|
|
render (Report_page.f post_preview post_id request) request
|
2022-02-22 07:10:52 +01:00
|
|
|
|
|
|
|
|
let report_post request =
|
2022-04-05 23:08:57 +02:00
|
|
|
Utils.logged_in_or_redirect request (fun user_id ->
|
|
|
|
|
match%lwt Dream.form request with
|
|
|
|
|
| `Ok [ ("reason", reason) ] ->
|
|
|
|
|
Utils.render_result request
|
|
|
|
|
@@
|
|
|
|
|
let post_id = Dream.param request "post_id" in
|
|
|
|
|
let* () = Babillard.report ~user_id ~reason post_id in
|
|
|
|
|
Ok "The post was reported!"
|
2022-04-05 23:22:55 +02:00
|
|
|
| form -> Utils.handle_invalid_form form )
|
2022-02-21 09:46:27 +01:00
|
|
|
|
2022-02-20 02:50:49 +01:00
|
|
|
let user request =
|
2022-04-05 23:08:57 +02:00
|
|
|
render (Result.fold ~ok:Fun.id ~error:Fun.id (User.list ())) request
|
2021-11-07 01:42:18 +01:00
|
|
|
|
2022-01-14 21:36:25 +01:00
|
|
|
let user_profile request =
|
2022-02-19 18:17:03 +01:00
|
|
|
let nick = Dream.param request "user" in
|
2022-04-08 00:43:00 +02:00
|
|
|
match User.get_id_from_nick nick with
|
2022-03-08 21:20:01 +01:00
|
|
|
| Error _e -> Dream.respond ~status:`Not_Found "User does not exists"
|
2022-04-08 00:43:00 +02:00
|
|
|
| Ok user_id -> render_result request @@ User.public_profile user_id
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2021-11-07 10:26:50 +01:00
|
|
|
let logout request =
|
|
|
|
|
let _ = Dream.invalidate_session request in
|
|
|
|
|
let content = "Logged out !" in
|
2022-04-05 23:08:57 +02:00
|
|
|
render content request
|
2021-11-07 10:26:50 +01:00
|
|
|
|
2022-03-06 20:55:12 +01:00
|
|
|
let account_get request =
|
2022-04-05 23:08:57 +02:00
|
|
|
Utils.logged_in_or_redirect request (fun user_id ->
|
|
|
|
|
Utils.render_result request
|
|
|
|
|
@@ let* user = User.get_user user_id in
|
|
|
|
|
Ok (User_account.f user request) )
|
2022-03-06 20:55:12 +01:00
|
|
|
|
|
|
|
|
(*TODO ask for password *)
|
|
|
|
|
let account_post request =
|
2022-04-05 23:08:57 +02:00
|
|
|
Utils.logged_in_or_redirect request (fun user_id ->
|
|
|
|
|
match%lwt Dream.form request with
|
|
|
|
|
| `Ok [ ("delete", _) ] ->
|
|
|
|
|
Utils.render_result request
|
|
|
|
|
@@ (*TODO ask for confirmation *)
|
|
|
|
|
let* () = User.delete_user user_id in
|
|
|
|
|
let _unit_lwt = Dream.invalidate_session request in
|
|
|
|
|
Ok "Your account was deleted"
|
|
|
|
|
| `Ok [ ("email", email) ] ->
|
|
|
|
|
Utils.render_result request
|
|
|
|
|
@@ let* () = User.update_email email user_id in
|
|
|
|
|
Ok "Your email was updated!"
|
|
|
|
|
| `Ok
|
|
|
|
|
[ ("confirm-new-password", confirm_password)
|
|
|
|
|
; ("new-password", password)
|
|
|
|
|
] ->
|
|
|
|
|
Utils.render_result request
|
|
|
|
|
@@
|
2022-03-06 20:55:12 +01:00
|
|
|
if password = confirm_password then
|
2022-04-05 23:08:57 +02:00
|
|
|
let* () = User.update_password password user_id in
|
|
|
|
|
Ok "Your password was updated!"
|
|
|
|
|
else Error "Password confirmation does not match"
|
2022-04-05 23:22:55 +02:00
|
|
|
| form -> Utils.handle_invalid_form form )
|
2022-03-06 20:55:12 +01:00
|
|
|
|
2021-11-08 15:24:10 +01:00
|
|
|
let profile_get request =
|
2022-04-05 23:08:57 +02:00
|
|
|
Utils.logged_in_or_redirect request (fun user_id ->
|
|
|
|
|
Utils.render_result request
|
|
|
|
|
@@ let* user = User.get_user user_id in
|
|
|
|
|
Ok (User_profile.f user request) )
|
2021-11-07 18:52:31 +01:00
|
|
|
|
|
|
|
|
let profile_post request =
|
2022-04-05 23:08:57 +02:00
|
|
|
Utils.logged_in_or_redirect request (fun user_id ->
|
|
|
|
|
match%lwt Dream.form request with
|
|
|
|
|
| `Ok [ ("bio", bio) ] -> (
|
|
|
|
|
match User.update_bio bio user_id with
|
2022-03-08 18:48:12 +01:00
|
|
|
| Ok () ->
|
|
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", "/profile") ]
|
2022-04-05 23:08:57 +02:00
|
|
|
"Your bio was updated!"
|
|
|
|
|
| Error e -> render e request )
|
|
|
|
|
| `Ok [ ("nick", nick) ] -> (
|
|
|
|
|
match User.update_nick nick user_id with
|
2022-01-14 21:36:25 +01:00
|
|
|
| Ok () ->
|
|
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", "/profile") ]
|
2022-04-05 23:08:57 +02:00
|
|
|
"Your display nick was updated!"
|
|
|
|
|
| Error e -> render e request )
|
|
|
|
|
| `Ok [ ("content", content); ("count", count); ("label", label) ] -> (
|
|
|
|
|
match int_of_string_opt count with
|
|
|
|
|
| None -> render "Error: invalid count" request
|
|
|
|
|
| Some count -> (
|
|
|
|
|
match User.update_metadata count label content user_id with
|
|
|
|
|
| Ok () ->
|
|
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", "/profile") ]
|
|
|
|
|
"Your display nick was updated!"
|
|
|
|
|
| Error e -> render e request ) )
|
2022-03-14 17:20:02 +01:00
|
|
|
| `Ok _ -> Dream.respond ~status:`Bad_Request "invalid form"
|
2022-04-05 23:08:57 +02:00
|
|
|
| `Many_tokens _ | `Missing_token _ | `Invalid_token _ | `Wrong_session _
|
|
|
|
|
| `Expired _ | `Wrong_content_type -> (
|
2022-04-05 23:22:55 +02:00
|
|
|
(* TODO: why is this here ?! *)
|
2022-04-05 23:08:57 +02:00
|
|
|
match%lwt Dream.multipart request with
|
|
|
|
|
| `Ok [ ("file", file) ] -> (
|
|
|
|
|
match User.upload_avatar file user_id with
|
|
|
|
|
| Ok () ->
|
|
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", "/profile") ]
|
|
|
|
|
"Your avatar was updated!"
|
|
|
|
|
| Error e -> render e request )
|
2022-04-05 23:22:55 +02:00
|
|
|
| form -> Utils.handle_invalid_form form ) )
|
2021-11-08 15:24:10 +01:00
|
|
|
|
2022-04-04 14:39:25 +02:00
|
|
|
let get_post_image ~thumbnail request =
|
|
|
|
|
let id = Dream.param request "id" in
|
|
|
|
|
let image =
|
2022-04-05 23:08:57 +02:00
|
|
|
(if thumbnail then Image.get_thumbnail else Image.get_content) id
|
2022-04-04 14:39:25 +02:00
|
|
|
in
|
|
|
|
|
match image with
|
2022-04-05 23:08:57 +02:00
|
|
|
| Error e -> render e request
|
2022-04-04 14:39:25 +02:00
|
|
|
| Ok image_opt -> (
|
|
|
|
|
match image_opt with
|
|
|
|
|
| None -> Dream.respond ~status:`Not_Found "Image does not exists"
|
2022-04-04 17:33:49 +02:00
|
|
|
| Some image ->
|
|
|
|
|
(* posts images do not change so we cache them *)
|
|
|
|
|
Dream.respond
|
|
|
|
|
~headers:
|
|
|
|
|
[ ("Cache-Control", "max-age=3628800, immutable")
|
|
|
|
|
; ("Content-Type", "image")
|
|
|
|
|
]
|
|
|
|
|
image )
|
2022-04-04 14:39:25 +02:00
|
|
|
|
|
|
|
|
let get_avatar_image request =
|
2022-02-19 21:21:35 +01:00
|
|
|
let nick = Dream.param request "user" in
|
2022-04-08 00:43:00 +02:00
|
|
|
match User.get_id_from_nick nick with
|
2022-03-08 21:20:01 +01:00
|
|
|
| Error _e -> Dream.respond ~status:`Not_Found "User does not exists"
|
|
|
|
|
| Ok user_id -> (
|
2022-04-04 14:39:25 +02:00
|
|
|
let avatar = Image.get_user_content user_id in
|
2022-02-19 18:17:03 +01:00
|
|
|
match avatar with
|
|
|
|
|
| Ok (Some avatar) ->
|
|
|
|
|
Dream.respond ~headers:[ ("Content-Type", "image") ] avatar
|
2022-04-04 14:39:25 +02:00
|
|
|
| Ok None -> (
|
2022-02-19 18:17:03 +01:00
|
|
|
match Content.read "/assets/img/default_avatar.png" with
|
2022-02-20 01:58:33 +01:00
|
|
|
| None -> failwith "can't find default avatar"
|
2022-02-19 18:17:03 +01:00
|
|
|
| Some avatar ->
|
2022-04-04 14:39:25 +02:00
|
|
|
Dream.respond ~headers:[ ("Content-Type", "image") ] avatar )
|
2022-04-05 23:08:57 +02:00
|
|
|
| Error e -> render e request )
|
2021-12-29 21:07:17 +01:00
|
|
|
|
2022-02-17 03:59:23 +01:00
|
|
|
let markers request =
|
|
|
|
|
let markers = Pp_babillard.get_markers () in
|
2022-01-14 13:23:45 +01:00
|
|
|
match markers with
|
|
|
|
|
| Ok markers ->
|
|
|
|
|
Dream.respond ~headers:[ ("Content-Type", "application/json") ] markers
|
2022-04-05 23:08:57 +02:00
|
|
|
| Error e -> render e request
|
2021-12-29 21:07:17 +01:00
|
|
|
|
2022-04-05 23:08:57 +02:00
|
|
|
let babillard_get request = render (Babillard_page.f request) request
|
2021-12-29 21:07:17 +01:00
|
|
|
|
2022-02-22 06:07:29 +01:00
|
|
|
let babillard_post request =
|
2022-04-05 23:08:57 +02:00
|
|
|
Utils.logged_in_or_redirect request (fun user_id ->
|
|
|
|
|
match%lwt Dream.multipart request with
|
|
|
|
|
| `Ok
|
|
|
|
|
[ ("alt", [ (_, alt) ])
|
|
|
|
|
; ("category", categories)
|
2022-04-19 00:06:23 +02:00
|
|
|
; ("comment", [ (_, comment) ])
|
2022-04-05 23:08:57 +02:00
|
|
|
; ("file", file)
|
|
|
|
|
; ("lat-input", [ (_, lat) ])
|
|
|
|
|
; ("lng-input", [ (_, lng) ])
|
|
|
|
|
; ("subject", [ (_, subject) ])
|
|
|
|
|
; ("tags", [ (_, tags) ])
|
|
|
|
|
]
|
|
|
|
|
| `Ok
|
|
|
|
|
( ("alt", [ (_, alt) ])
|
2022-04-19 00:06:23 +02:00
|
|
|
:: ("comment", [ (_, comment) ])
|
2022-04-05 23:08:57 +02:00
|
|
|
:: ("file", file)
|
|
|
|
|
:: ("lat-input", [ (_, lat) ])
|
|
|
|
|
:: ("lng-input", [ (_, lng) ])
|
|
|
|
|
:: ("subject", [ (_, subject) ])
|
|
|
|
|
:: ("tags", [ (_, tags) ])
|
|
|
|
|
:: ([] as categories) ) -> (
|
|
|
|
|
let categories = List.map snd categories in
|
|
|
|
|
match (Float.of_string_opt lat, Float.of_string_opt lng) with
|
|
|
|
|
| None, _ -> render "Invalide coordinate" request
|
|
|
|
|
| _, None -> render "Invalide coordinate" request
|
|
|
|
|
| Some lat, Some lng -> (
|
|
|
|
|
let op_or_reply_data = `Op_data (categories, subject, lat, lng) in
|
|
|
|
|
let res =
|
|
|
|
|
match file with
|
|
|
|
|
| [] -> Babillard.make_post ~comment ~tags ~op_or_reply_data user_id
|
|
|
|
|
| _ :: _ :: _ -> Error "More than one image"
|
|
|
|
|
| [ (image_name, image_content) ] ->
|
|
|
|
|
let image_input = (image_name, alt, image_content) in
|
|
|
|
|
Babillard.make_post ~comment ~image_input ~tags ~op_or_reply_data
|
|
|
|
|
user_id
|
|
|
|
|
in
|
|
|
|
|
match res with
|
|
|
|
|
| Ok thread_id ->
|
|
|
|
|
let adress = Format.asprintf "/thread/%s" thread_id in
|
|
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", adress) ]
|
|
|
|
|
"Your thread was posted!"
|
|
|
|
|
| Error e -> render e request ) )
|
2022-04-05 23:22:55 +02:00
|
|
|
| form -> Utils.handle_invalid_form form )
|
2021-12-29 21:07:17 +01:00
|
|
|
|
2022-02-27 13:45:43 +01:00
|
|
|
let thread_feed_get request =
|
|
|
|
|
let thread_id = Dream.param request "thread_id" in
|
2022-04-08 00:43:00 +02:00
|
|
|
match Pp_babillard.feed thread_id with
|
|
|
|
|
| Error e -> render e request
|
|
|
|
|
| Ok feed ->
|
|
|
|
|
Dream.respond ~headers:[ ("Content-Type", "application/atom+xml") ] feed
|
2022-02-27 13:45:43 +01:00
|
|
|
|
2021-12-29 21:07:17 +01:00
|
|
|
let thread_get request =
|
2022-02-19 21:21:35 +01:00
|
|
|
let thread_id = Dream.param request "thread_id" in
|
2022-04-08 00:43:00 +02:00
|
|
|
let thread_view = Pp_babillard.view_thread thread_id in
|
|
|
|
|
render
|
|
|
|
|
( match thread_view with
|
|
|
|
|
| Error e -> e
|
|
|
|
|
| Ok thread_view -> Thread_page.f thread_view thread_id request )
|
|
|
|
|
request
|
2021-12-29 21:07:17 +01:00
|
|
|
|
|
|
|
|
(*form to reply to a thread *)
|
2022-01-12 19:34:55 +01:00
|
|
|
let reply_post request =
|
2022-04-05 23:08:57 +02:00
|
|
|
Utils.logged_in_or_redirect request (fun user_id ->
|
|
|
|
|
match%lwt Dream.multipart request with
|
|
|
|
|
| `Ok
|
|
|
|
|
[ ("alt", [ (_, alt) ])
|
2022-04-19 00:06:23 +02:00
|
|
|
; ("comment", [ (_, comment) ])
|
2022-04-05 23:08:57 +02:00
|
|
|
; ("file", file)
|
|
|
|
|
; ("tags", [ (_, tags) ])
|
|
|
|
|
] -> (
|
|
|
|
|
let parent_id = Dream.param request "thread_id" in
|
|
|
|
|
let op_or_reply_data = `Reply_data parent_id in
|
|
|
|
|
let res =
|
|
|
|
|
match file with
|
|
|
|
|
| [] -> Babillard.make_post ~comment ~tags ~op_or_reply_data user_id
|
|
|
|
|
| [ (image_name, image_content) ] ->
|
|
|
|
|
let image_input = (image_name, alt, image_content) in
|
|
|
|
|
Babillard.make_post ~comment ~image_input ~tags ~op_or_reply_data
|
|
|
|
|
user_id
|
|
|
|
|
| _ :: _ :: _ -> Error "More than one image"
|
|
|
|
|
in
|
|
|
|
|
match res with
|
|
|
|
|
| Ok post_id ->
|
|
|
|
|
let adress = Format.sprintf "/thread/%s#%s" parent_id post_id in
|
|
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", adress) ]
|
|
|
|
|
"Your reply was posted!"
|
|
|
|
|
| Error e -> render e request )
|
2022-04-05 23:22:55 +02:00
|
|
|
| form -> Utils.handle_invalid_form form )
|
2021-12-29 21:07:17 +01:00
|
|
|
|
2022-02-20 21:36:56 +01:00
|
|
|
let error_template _error _debug_info response =
|
|
|
|
|
let status = Dream.status response in
|
|
|
|
|
let code = Dream.status_to_int status in
|
|
|
|
|
|
|
|
|
|
(*TODO improve: can't use template.elm.html because it needs "request" *)
|
|
|
|
|
let%lwt body = Dream.body response in
|
|
|
|
|
let reason =
|
|
|
|
|
if String.equal "" body then Dream.status_to_string status else body
|
|
|
|
|
in
|
|
|
|
|
Dream.set_body response (Format.sprintf "%d: %s" code reason);
|
|
|
|
|
Lwt.return response
|
|
|
|
|
|
2022-02-19 22:13:45 +01:00
|
|
|
let routes =
|
|
|
|
|
(* this is just so that they're visually aligned *)
|
|
|
|
|
let get_ = Dream.get in
|
|
|
|
|
let post = Dream.post in
|
|
|
|
|
|
|
|
|
|
[ get_ "/" babillard_get
|
2022-02-22 06:07:29 +01:00
|
|
|
; post "/" babillard_post
|
2022-02-19 22:13:45 +01:00
|
|
|
; get_ "/about" about
|
2022-03-06 20:55:12 +01:00
|
|
|
; get_ "/account" account_get
|
|
|
|
|
; post "/account" account_post
|
2022-02-23 22:39:48 +01:00
|
|
|
; get_ "/admin" admin_get
|
|
|
|
|
; post "/admin" admin_post
|
2022-02-19 22:13:45 +01:00
|
|
|
; get_ "/assets/**" (Dream.static ~loader:asset_loader "")
|
2022-02-22 00:01:35 +01:00
|
|
|
; get_ "/catalog" catalog
|
|
|
|
|
; get_ "/delete/:post_id" delete_get
|
|
|
|
|
; post "/delete/:post_id" delete_post
|
2022-03-31 01:43:59 +02:00
|
|
|
; get_ "/discuss" Discuss.render
|
2022-04-05 23:08:57 +02:00
|
|
|
; get_ "/discuss/:comrade_id" Discuss.renderone
|
2022-03-31 01:43:59 +02:00
|
|
|
; post "/discuss/:comrade_id" Discuss.post
|
2022-04-04 14:39:25 +02:00
|
|
|
; get_ "/img/:id" (get_post_image ~thumbnail:false)
|
|
|
|
|
; get_ "/img/s/:id" (get_post_image ~thumbnail:true)
|
2022-02-19 22:13:45 +01:00
|
|
|
; get_ "/login" login_get
|
|
|
|
|
; post "/login" login_post
|
|
|
|
|
; get_ "/logout" logout
|
|
|
|
|
; get_ "/markers" markers
|
|
|
|
|
; get_ "/profile" profile_get
|
|
|
|
|
; post "/profile" profile_post
|
2022-02-22 07:10:52 +01:00
|
|
|
; get_ "/report/:post_id" report_get
|
|
|
|
|
; post "/report/:post_id" report_post
|
2022-02-22 00:01:35 +01:00
|
|
|
; get_ "/thread/:thread_id" thread_get
|
|
|
|
|
; post "/thread/:thread_id" reply_post
|
2022-02-27 13:45:43 +01:00
|
|
|
; get_ "/thread/:thread_id/feed" thread_feed_get
|
2022-02-20 02:50:49 +01:00
|
|
|
; get_ "/user" user
|
2022-02-19 22:13:45 +01:00
|
|
|
; get_ "/user/:user" user_profile
|
2022-04-04 14:39:25 +02:00
|
|
|
; get_ "/user/:user/avatar" get_avatar_image
|
2022-02-19 22:13:45 +01:00
|
|
|
]
|
2022-02-19 17:46:01 +01:00
|
|
|
@
|
|
|
|
|
if App.open_registration then
|
|
|
|
|
[ get_ "/register" register_get; post "/register" register_post ]
|
|
|
|
|
else []
|
|
|
|
|
|
2021-11-05 16:55:19 +01:00
|
|
|
let () =
|
2022-02-19 22:29:17 +01:00
|
|
|
let logger = if App.log then Dream.logger else Fun.id in
|
2022-02-20 21:36:56 +01:00
|
|
|
Dream.run ~port:App.port ~error_handler:(Dream.error_template error_template)
|
|
|
|
|
@@ logger @@ Dream.cookie_sessions
|
2022-02-18 03:24:23 +01:00
|
|
|
(* this should replace memory/cookie sessions but it doesn't work :-(
|
|
|
|
|
@@ Dream.sql_pool Db.db_uri
|
|
|
|
|
@@ Dream.sql_sessions
|
|
|
|
|
*)
|
2022-02-19 22:13:45 +01:00
|
|
|
@@ Dream.router routes
|