remove useless /thread_view ; fix some 404 errors
This commit is contained in:
parent
3d6cc4ad4a
commit
ff688a0759
3 changed files with 49 additions and 35 deletions
|
|
@ -182,6 +182,10 @@ module Q = struct
|
||||||
Caqti_request.find Caqti_type.string Caqti_type.string
|
Caqti_request.find Caqti_type.string Caqti_type.string
|
||||||
"SELECT thread_id FROM threads WHERE thread_id=? LIMIT 1;"
|
"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 =
|
let get_post_thread =
|
||||||
Caqti_request.find Caqti_type.string Caqti_type.string
|
Caqti_request.find Caqti_type.string Caqti_type.string
|
||||||
"SELECT thread_id FROM threads WHERE post_id=? LIMIT 1;"
|
"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 }
|
{ id; parent_id; date; nick; comment; image_info; tags; replies; citations }
|
||||||
in
|
in
|
||||||
Ok reply
|
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
|
||||||
|
|
|
||||||
|
|
@ -52,13 +52,16 @@ let login_post request =
|
||||||
render_unsafe (Login.f ~nick ~password request) request
|
render_unsafe (Login.f ~nick ~password request) request
|
||||||
| _ -> assert false
|
| _ -> assert false
|
||||||
|
|
||||||
let user request =
|
let users request =
|
||||||
render_unsafe (Result.fold ~ok:Fun.id ~error:Fun.id (User.list ())) request
|
render_unsafe (Result.fold ~ok:Fun.id ~error:Fun.id (User.list ())) request
|
||||||
|
|
||||||
let user_profile request =
|
let user_profile request =
|
||||||
|
let nick = Dream.param request "user" in
|
||||||
|
if User.exists nick then
|
||||||
render_unsafe
|
render_unsafe
|
||||||
(Result.fold ~ok:Fun.id ~error:Fun.id (User.public_profile request))
|
(Result.fold ~ok:Fun.id ~error:Fun.id (User.public_profile nick))
|
||||||
request
|
request
|
||||||
|
else Dream.respond ~status:`Not_Found "404: User does not exists"
|
||||||
|
|
||||||
let logout request =
|
let logout request =
|
||||||
let _ = Dream.invalidate_session request in
|
let _ = Dream.invalidate_session request in
|
||||||
|
|
@ -101,6 +104,7 @@ let profile_post request =
|
||||||
|
|
||||||
let avatar_image request =
|
let avatar_image request =
|
||||||
let nick = Dream.param request "user" in
|
let nick = Dream.param request "user" in
|
||||||
|
if User.exists nick then
|
||||||
let avatar = User.get_avatar nick in
|
let avatar = User.get_avatar nick in
|
||||||
match avatar with
|
match avatar with
|
||||||
| Ok (Some avatar) ->
|
| Ok (Some avatar) ->
|
||||||
|
|
@ -108,15 +112,18 @@ let avatar_image request =
|
||||||
| Ok None | Error _ -> (
|
| Ok None | Error _ -> (
|
||||||
match Content.read "/assets/img/default_avatar.png" with
|
match Content.read "/assets/img/default_avatar.png" with
|
||||||
| None -> Dream.empty `Not_Found
|
| None -> Dream.empty `Not_Found
|
||||||
| Some avatar -> Dream.respond ~headers:[ ("Content-Type", "image") ] avatar
|
| 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_image request =
|
||||||
let post_id = Dream.param request "post_id" in
|
let post_id = Dream.param request "post_id" in
|
||||||
|
if Babillard.post_exists post_id then
|
||||||
let image = Babillard.get_post_image_content post_id in
|
let image = Babillard.get_post_image_content post_id in
|
||||||
match image with
|
match image with
|
||||||
| Ok image -> Dream.respond ~headers:[ ("Content-Type", "image") ] image
|
| Ok image -> Dream.respond ~headers:[ ("Content-Type", "image") ] image
|
||||||
| Error _ -> Dream.empty `Not_Found
|
| Error _ -> Dream.empty `Not_Found
|
||||||
|
else Dream.respond ~status:`Not_Found "404: Image does not exists"
|
||||||
|
|
||||||
let markers request =
|
let markers request =
|
||||||
let markers = Pp_babillard.get_markers () in
|
let markers = Pp_babillard.get_markers () in
|
||||||
|
|
@ -169,19 +176,13 @@ let newthread_post request =
|
||||||
|
|
||||||
let thread_get request =
|
let thread_get request =
|
||||||
let thread_id = Dream.param request "thread_id" in
|
let thread_id = Dream.param request "thread_id" in
|
||||||
|
if Babillard.thread_exists thread_id then
|
||||||
let thread_view = Pp_babillard.view_thread thread_id in
|
let thread_view = Pp_babillard.view_thread thread_id in
|
||||||
match thread_view with
|
match thread_view with
|
||||||
| Error e -> render_unsafe e request
|
| Error e -> render_unsafe e request
|
||||||
| Ok thread_view ->
|
| Ok thread_view ->
|
||||||
render_unsafe (Thread_page.f thread_view thread_id request) request
|
render_unsafe (Thread_page.f thread_view thread_id request) request
|
||||||
|
else Dream.respond ~status:`Not_Found "404: Thread not found"
|
||||||
(* 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)
|
|
||||||
|
|
||||||
(*form to reply to a thread *)
|
(*form to reply to a thread *)
|
||||||
let reply_post request =
|
let reply_post request =
|
||||||
|
|
@ -234,8 +235,7 @@ let routes =
|
||||||
; get_ "/post_pic/:post_id" post_image
|
; get_ "/post_pic/:post_id" post_image
|
||||||
; get_ "/profile" profile_get
|
; get_ "/profile" profile_get
|
||||||
; post "/profile" profile_post
|
; post "/profile" profile_post
|
||||||
; get_ "/thread_view/:thread_id" thread_view
|
; get_ "/users" users
|
||||||
; get_ "/user" user
|
|
||||||
; get_ "/user/:user" user_profile
|
; get_ "/user/:user" user_profile
|
||||||
; get_ "/user/:user/avatar" avatar_image
|
; get_ "/user/:user/avatar" avatar_image
|
||||||
; get_ "/thread/:thread_id" thread_get
|
; get_ "/thread/:thread_id" thread_get
|
||||||
|
|
@ -246,7 +246,8 @@ let routes =
|
||||||
[ get_ "/register" register_get; post "/register" register_post ]
|
[ get_ "/register" register_get; post "/register" register_post ]
|
||||||
else []
|
else []
|
||||||
|
|
||||||
|
let not_found _ =
|
||||||
|
Dream.respond ~status:`Not_Found "404: This page does not exists!"
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
let logger = if App.log then Dream.logger else Fun.id in
|
let logger = if App.log then Dream.logger else Fun.id in
|
||||||
|
|
|
||||||
|
|
@ -118,8 +118,7 @@ let list () =
|
||||||
)
|
)
|
||||||
users )
|
users )
|
||||||
|
|
||||||
let public_profile request =
|
let public_profile nick =
|
||||||
let nick = Dream.param request "user" in
|
|
||||||
let^? nick, _password, _email, (bio, _) = Db.find_opt Q.get_user nick in
|
let^? nick, _password, _email, (bio, _) = Db.find_opt Q.get_user nick in
|
||||||
let user_info =
|
let user_info =
|
||||||
Format.sprintf
|
Format.sprintf
|
||||||
|
|
@ -169,3 +168,6 @@ let upload_avatar files nick =
|
||||||
let^ () = Db.exec Q.upload_avatar (content, nick) in
|
let^ () = Db.exec Q.upload_avatar (content, nick) in
|
||||||
Ok ()
|
Ok ()
|
||||||
| _files -> Error "More than one file provided"
|
| _files -> Error "More than one file provided"
|
||||||
|
|
||||||
|
let exists nick =
|
||||||
|
match Db.find_opt Q.get_user nick with Error _ -> false | Ok _ -> true
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue