remove useless /thread_view ; fix some 404 errors

This commit is contained in:
Swrup 2022-02-19 18:17:03 +01:00
parent 3d6cc4ad4a
commit ff688a0759
3 changed files with 49 additions and 35 deletions

View file

@ -182,6 +182,10 @@ module Q = struct
Caqti_request.find Caqti_type.string Caqti_type.string
"SELECT thread_id FROM threads WHERE thread_id=? LIMIT 1;"
let get_is_post =
Caqti_request.find Caqti_type.string Caqti_type.string
"SELECT post_id FROM post_user WHERE post_id=? LIMIT 1;"
let get_post_thread =
Caqti_request.find Caqti_type.string Caqti_type.string
"SELECT thread_id FROM threads WHERE post_id=? LIMIT 1;"
@ -444,3 +448,10 @@ let get_post id =
{ id; parent_id; date; nick; comment; image_info; tags; replies; citations }
in
Ok reply
let thread_exists id =
match Db.find Q.get_is_thread id with Error _ -> false | Ok _ -> true
(* true if post is an op too *)
let post_exists id =
match Db.find Q.get_is_post id with Error _ -> false | Ok _ -> true

View file

@ -52,13 +52,16 @@ let login_post request =
render_unsafe (Login.f ~nick ~password request) request
| _ -> assert false
let user request =
let users request =
render_unsafe (Result.fold ~ok:Fun.id ~error:Fun.id (User.list ())) request
let user_profile request =
render_unsafe
(Result.fold ~ok:Fun.id ~error:Fun.id (User.public_profile request))
request
let nick = Dream.param request "user" in
if User.exists nick then
render_unsafe
(Result.fold ~ok:Fun.id ~error:Fun.id (User.public_profile nick))
request
else Dream.respond ~status:`Not_Found "404: User does not exists"
let logout request =
let _ = Dream.invalidate_session request in
@ -101,22 +104,26 @@ let profile_post request =
let avatar_image request =
let nick = Dream.param request "user" in
let avatar = User.get_avatar nick in
match avatar with
| Ok (Some avatar) ->
Dream.respond ~headers:[ ("Content-Type", "image") ] avatar
| Ok None | Error _ -> (
match Content.read "/assets/img/default_avatar.png" with
| None -> Dream.empty `Not_Found
| Some avatar -> Dream.respond ~headers:[ ("Content-Type", "image") ] avatar
)
if User.exists nick then
let avatar = User.get_avatar nick in
match avatar with
| Ok (Some avatar) ->
Dream.respond ~headers:[ ("Content-Type", "image") ] avatar
| Ok None | Error _ -> (
match Content.read "/assets/img/default_avatar.png" with
| None -> Dream.empty `Not_Found
| Some avatar ->
Dream.respond ~headers:[ ("Content-Type", "image") ] avatar )
else Dream.respond ~status:`Not_Found "404: User does not exists"
let post_image request =
let post_id = Dream.param request "post_id" in
let image = Babillard.get_post_image_content post_id in
match image with
| Ok image -> Dream.respond ~headers:[ ("Content-Type", "image") ] image
| Error _ -> Dream.empty `Not_Found
if Babillard.post_exists post_id then
let image = Babillard.get_post_image_content post_id in
match image with
| Ok image -> Dream.respond ~headers:[ ("Content-Type", "image") ] image
| Error _ -> Dream.empty `Not_Found
else Dream.respond ~status:`Not_Found "404: Image does not exists"
let markers request =
let markers = Pp_babillard.get_markers () in
@ -169,19 +176,13 @@ let newthread_post request =
let thread_get request =
let thread_id = Dream.param request "thread_id" in
let thread_view = Pp_babillard.view_thread thread_id in
match thread_view with
| Error e -> render_unsafe e request
| Ok thread_view ->
render_unsafe (Thread_page.f thread_view thread_id request) request
(* get thread view but not wrapped in template, so we can display it on /babillard*)
let thread_view request =
let thread_id = Dream.param request "thread_id" in
let thread_view = Pp_babillard.view_thread thread_id in
match thread_view with
| Error e -> render_unsafe e request
| Ok thread_view -> Dream.html (Thread_page.f thread_view thread_id request)
if Babillard.thread_exists thread_id then
let thread_view = Pp_babillard.view_thread thread_id in
match thread_view with
| Error e -> render_unsafe e request
| Ok thread_view ->
render_unsafe (Thread_page.f thread_view thread_id request) request
else Dream.respond ~status:`Not_Found "404: Thread not found"
(*form to reply to a thread *)
let reply_post request =
@ -234,8 +235,7 @@ let routes =
; get_ "/post_pic/:post_id" post_image
; get_ "/profile" profile_get
; post "/profile" profile_post
; get_ "/thread_view/:thread_id" thread_view
; get_ "/user" user
; get_ "/users" users
; get_ "/user/:user" user_profile
; get_ "/user/:user/avatar" avatar_image
; get_ "/thread/:thread_id" thread_get
@ -246,7 +246,8 @@ let routes =
[ get_ "/register" register_get; post "/register" register_post ]
else []
let not_found _ =
Dream.respond ~status:`Not_Found "404: This page does not exists!"
let () =
let logger = if App.log then Dream.logger else Fun.id in

View file

@ -118,8 +118,7 @@ let list () =
)
users )
let public_profile request =
let nick = Dream.param request "user" in
let public_profile nick =
let^? nick, _password, _email, (bio, _) = Db.find_opt Q.get_user nick in
let user_info =
Format.sprintf
@ -169,3 +168,6 @@ let upload_avatar files nick =
let^ () = Db.exec Q.upload_avatar (content, nick) in
Ok ()
| _files -> Error "More than one file provided"
let exists nick =
match Db.find_opt Q.get_user nick with Error _ -> false | Ok _ -> true