geochan/src/babillard.ml

426 lines
13 KiB
OCaml
Raw Normal View History

2022-03-31 00:01:52 +02:00
open Syntax
2022-04-08 00:43:00 +02:00
open Caqti_request.Infix
open Caqti_type
2021-12-29 21:07:17 +01:00
2022-03-17 01:05:25 +01:00
type moderation_action =
| Ignore
| Delete
| Banish
let moderation_action_to_string = function
| Ignore -> "ignore"
| Delete -> "delete"
| Banish -> "banish"
let moderation_action_from_string = function
| "ignore" -> Some Ignore
| "delete" -> Some Delete
| "banish" -> Some Banish
| _ -> None
2022-01-29 09:23:57 +01:00
type thread_data =
{ subject : string
2022-01-29 09:23:57 +01:00
; lng : float
; lat : float
}
type post =
{ id : string
2022-03-23 04:34:54 +01:00
; emojid : string
; parent_id : string
2022-02-23 14:30:06 +01:00
; date : float
2022-03-08 21:20:01 +01:00
; user_id : string
; nick : string
; comment : string
2022-02-18 19:40:19 +01:00
; image_info : (string * string) option
; tags : string list
; replies : string list
; citations : string list
}
type t =
| Op of thread_data * post
| Post of post
2022-04-08 00:43:00 +02:00
let () =
let tables =
[| (unit ->. unit)
"CREATE TABLE IF NOT EXISTS post_user (post_id TEXT, user_id TEXT, \
PRIMARY KEY(post_id), FOREIGN KEY(user_id) REFERENCES user(user_id) \
ON DELETE CASCADE)"
; (* one row for each thread, with thread's data *)
(unit ->. unit)
"CREATE TABLE IF NOT EXISTS thread_info (thread_id TEXT, subject \
TEXT, lat FLOAT, lng FLOAT, FOREIGN KEY(thread_id) REFERENCES \
post_user(post_id) ON DELETE CASCADE)"
; (* map thread and reply to the thread *)
(unit ->. unit)
"CREATE TABLE IF NOT EXISTS thread_post (thread_id TEXT, post_id \
TEXT, FOREIGN KEY(thread_id) REFERENCES post_user(post_id) ON DELETE \
CASCADE, FOREIGN KEY(post_id) REFERENCES post_user(post_id) ON \
DELETE CASCADE)"
; (unit ->. unit)
"CREATE TABLE IF NOT EXISTS post_replies (post_id TEXT, reply_id \
TEXT, FOREIGN KEY(post_id) REFERENCES post_user(post_id) ON DELETE \
CASCADE, FOREIGN KEY(reply_id) REFERENCES post_user(post_id) ON \
DELETE CASCADE)"
; (unit ->. unit)
"CREATE TABLE IF NOT EXISTS post_citations (post_id TEXT, cited_id \
TEXT, FOREIGN KEY(post_id) REFERENCES post_user(post_id) ON DELETE \
CASCADE, FOREIGN KEY(cited_id) REFERENCES post_user(post_id) ON \
DELETE CASCADE)"
; (unit ->. unit)
"CREATE TABLE IF NOT EXISTS post_date (post_id TEXT, date FLOAT, \
FOREIGN KEY(post_id) REFERENCES post_user(post_id) ON DELETE \
CASCADE)"
; (unit ->. unit)
"CREATE TABLE IF NOT EXISTS post_comment (post_id TEXT, comment TEXT, \
FOREIGN KEY(post_id) REFERENCES post_user(post_id) ON DELETE \
CASCADE)"
; (unit ->. unit)
"CREATE TABLE IF NOT EXISTS post_tags (post_id TEXT, tag TEXT, \
FOREIGN KEY(post_id) REFERENCES post_user(post_id) ON DELETE \
CASCADE)"
; (unit ->. unit)
"CREATE TABLE IF NOT EXISTS report (user_id TEXT, reason TEXT, date \
FLOAT,post_id TEXT, FOREIGN KEY(post_id) REFERENCES \
post_user(post_id) ON DELETE CASCADE, FOREIGN KEY(user_id) \
REFERENCES user(user_id) ON DELETE CASCADE)"
|]
in
if
Array.exists Result.is_error
(Array.map (fun query -> Db.exec query ()) tables)
then Dream.error (fun log -> log "can't create babillard's tables")
2022-02-22 07:10:52 +01:00
2022-04-08 00:43:00 +02:00
module Q = struct
2022-02-23 22:39:48 +01:00
let upload_report =
2022-04-08 00:43:00 +02:00
Db.exec
@@ (tup4 string string float string ->. unit)
"INSERT INTO report VALUES (?,?,?,?)"
2022-02-23 22:39:48 +01:00
let get_reports =
2022-04-08 00:43:00 +02:00
Db.collect_list
@@ (unit ->* tup4 string string float string) "SELECT * FROM report"
2022-02-23 22:39:48 +01:00
2021-12-29 21:07:17 +01:00
let upload_post_id =
2022-04-08 00:43:00 +02:00
Db.exec
@@ (tup2 string string ->. unit) "INSERT INTO post_user VALUES (?,?)"
2021-12-29 21:07:17 +01:00
let upload_thread_info =
2022-04-08 00:43:00 +02:00
Db.exec
@@ (tup4 string string float float ->. unit)
"INSERT INTO thread_info VALUES (?,?,?,?)"
let upload_thread_post =
2022-04-08 00:43:00 +02:00
Db.exec
@@ (tup2 string string ->. unit) "INSERT INTO thread_post VALUES (?,?)"
2021-12-29 21:07:17 +01:00
let upload_post_reply =
2022-04-08 00:43:00 +02:00
Db.exec
@@ (tup2 string string ->. unit) "INSERT INTO post_replies VALUES (?,?)"
2021-12-29 21:07:17 +01:00
let upload_post_comment =
2022-04-08 00:43:00 +02:00
Db.exec
@@ (tup2 string string ->. unit) "INSERT INTO post_comment VALUES (?,?)"
2021-12-29 21:07:17 +01:00
let upload_post_tag =
2022-04-08 00:43:00 +02:00
Db.exec
@@ (tup2 string string ->. unit) "INSERT INTO post_tags VALUES (?,?)"
2021-12-29 21:07:17 +01:00
let upload_post_date =
2022-04-08 00:43:00 +02:00
Db.exec @@ (tup2 string float ->. unit) "INSERT INTO post_date VALUES (?,?)"
2021-12-29 21:07:17 +01:00
2022-03-08 21:20:01 +01:00
let get_post_user_id =
2022-04-08 00:43:00 +02:00
Db.find
@@ (string ->! string) "SELECT user_id FROM post_user WHERE post_id=?"
2021-12-29 21:07:17 +01:00
let get_post_comment =
2022-04-08 00:43:00 +02:00
Db.find
@@ (string ->! string) "SELECT comment FROM post_comment WHERE post_id=?"
2021-12-29 21:07:17 +01:00
let get_post_tags =
2022-04-08 00:43:00 +02:00
Db.collect_list
@@ (string ->* string) "SELECT tag FROM post_tags WHERE post_id=?"
2021-12-29 21:07:17 +01:00
let get_post_date =
2022-04-08 00:43:00 +02:00
Db.find @@ (string ->! float) "SELECT date FROM post_date WHERE post_id=?"
2021-12-29 21:07:17 +01:00
let get_post_citations =
2022-04-08 00:43:00 +02:00
Db.collect_list
@@ (string ->* string) "SELECT post_id FROM post_citations WHERE post_id=?"
2021-12-29 21:07:17 +01:00
let get_post_replies =
2022-04-08 00:43:00 +02:00
Db.collect_list
@@ (string ->* string) "SELECT reply_id FROM post_replies WHERE post_id=?"
2021-12-29 21:07:17 +01:00
let get_thread_posts =
2022-04-08 00:43:00 +02:00
Db.collect_list
@@ (string ->* string) "SELECT post_id FROM thread_post WHERE thread_id=?"
2021-12-29 21:07:17 +01:00
2022-01-14 14:05:09 +01:00
let count_thread_posts =
2022-04-08 00:43:00 +02:00
Db.find
@@ (string ->! int)
"SELECT COUNT(post_id) FROM thread_post WHERE thread_id=?"
let get_is_post =
2022-04-08 00:43:00 +02:00
Db.find
@@ (string ->! string)
"SELECT post_id FROM post_user WHERE post_id=? LIMIT 1"
let get_post_thread =
2022-04-08 00:43:00 +02:00
Db.find
@@ (string ->! string)
"SELECT thread_id FROM thread_post WHERE post_id=? LIMIT 1"
2021-12-29 21:07:17 +01:00
let get_thread_info =
2022-04-08 00:43:00 +02:00
Db.find
@@ (string ->! tup3 string float float)
"SELECT subject,lat,lng FROM thread_info WHERE thread_id=?"
2021-12-29 21:07:17 +01:00
2022-04-08 00:43:00 +02:00
let get_threads =
Db.collect_list @@ (unit ->* string) "SELECT thread_id FROM thread_info"
2022-02-21 09:46:27 +01:00
2022-04-08 00:43:00 +02:00
let delete_post =
Db.exec @@ (string ->. unit) "DELETE FROM post_user WHERE post_id=?"
2021-12-29 21:07:17 +01:00
end
2022-04-08 00:43:00 +02:00
let ignore_report =
Db.exec @@ (string ->. unit) "DELETE FROM report WHERE post_id=?"
2021-12-29 21:07:17 +01:00
(*TODO switch to markdown !*)
(* insert html into the comment, and keep tracks of citations :
-wraps lines starting with ">" with a <span class="quote">
-make raw posts uuid into links
(*TODO fix bad link if post is in other thread*)
-keeps tracks of every post cited in this comment
- add <br> at each line *)
2021-12-29 21:07:17 +01:00
let parse_comment comment =
2022-02-26 15:28:23 +01:00
let citations = ref [] in
let pp_word fmt w =
let trim_w = String.trim w in
(* '>' is '&gt;' after html_escape *)
2022-02-27 19:58:32 +01:00
if String.length trim_w >= 8 then
let sub_w = String.sub trim_w 8 (String.length trim_w - 8) in
2022-02-27 19:58:32 +01:00
if
String.starts_with ~prefix:{|&gt;&gt;|} trim_w
&& Option.is_some (Uuidm.of_string sub_w)
then begin
2022-02-26 15:28:23 +01:00
citations := sub_w :: !citations;
2022-02-27 19:58:32 +01:00
Format.fprintf fmt {|<a href="#%s">%s</a>|} sub_w w
end
2022-02-26 15:28:23 +01:00
else Format.pp_print_string fmt w
else Format.pp_print_string fmt w
in
2022-02-26 15:28:23 +01:00
let pp_line fmt l =
let trim_w = String.trim l in
(*insert quote*)
2022-02-26 15:28:23 +01:00
let words = String.split_on_char ' ' l in
if
String.starts_with ~prefix:{|&gt;|} trim_w
&& not (String.starts_with ~prefix:{|&gt;&gt;|} trim_w)
then
Format.fprintf fmt {|<span class="quote">%a</span>|}
(Format.pp_print_list ~pp_sep:Format.pp_print_space pp_word)
words
2022-02-27 19:58:32 +01:00
else Format.pp_print_list ~pp_sep:Format.pp_print_space pp_word fmt words
in
2022-01-14 21:04:52 +01:00
let comment = String.trim comment in
let lines = String.split_on_char '\n' comment in
(*insert <br>*)
let comment =
Format.asprintf "%a"
(Format.pp_print_list
2022-02-26 15:28:23 +01:00
~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n<br>")
pp_line )
lines
in
(* remove duplicate cited_id *)
2022-02-26 15:28:23 +01:00
let citations = List.sort_uniq String.compare !citations in
(comment, citations)
2021-12-29 21:07:17 +01:00
2022-03-16 03:06:59 +01:00
let upload_post ~image post =
2022-01-29 09:23:57 +01:00
let thread_data, reply =
match post with
2022-01-29 09:23:57 +01:00
| Op (thread_data, reply) -> (Some thread_data, reply)
| Post reply -> (None, reply)
2021-12-29 21:07:17 +01:00
in
let { id; parent_id; date; user_id; comment; tags; citations; _ } = reply in
2022-01-29 09:23:57 +01:00
2022-04-08 00:43:00 +02:00
let* () = Q.upload_post_id (id, user_id) in
let* () = Q.upload_post_comment (id, comment) in
let* () = Q.upload_post_date (id, date) in
let* () = Q.upload_thread_post (parent_id, id) in
2022-03-16 03:06:59 +01:00
let* () =
match image with None -> Ok () | Some image -> Image.upload image id
2021-12-29 21:07:17 +01:00
in
2022-04-08 00:43:00 +02:00
match unwrap_list (fun tag -> Q.upload_post_tag (id, tag)) tags with
| Error _e as e -> e
| Ok _ -> (
match
unwrap_list (fun cited_id -> Q.upload_post_reply (cited_id, id)) citations
with
| Error _e as e -> e
| Ok _ ->
let* () =
match thread_data with
| None -> Ok ()
| Some { subject; lng; lat } ->
Q.upload_thread_info (id, subject, lat, lng)
in
Ok id )
2021-12-29 21:07:17 +01:00
2022-03-16 03:06:59 +01:00
let build_reply ~comment ~image_info ~tag_list ?parent_id user_id =
2022-01-14 21:04:52 +01:00
let comment = Dream.html_escape comment in
let id = Uuidm.to_string (Uuidm.v4_gen App.random_state ()) in
2022-01-29 09:23:57 +01:00
(* parent_id is None if this reply is supposed to be a new thread *)
let parent_id = Option.value parent_id ~default:id in
2022-02-18 02:40:04 +01:00
if Option.is_none (Uuidm.of_string parent_id) then Error "invalid thread id"
else if String.length comment > 10000 then Error "invalid comment"
2022-03-16 03:06:59 +01:00
else if List.length tag_list > 30 then Error "too much tags"
else if List.exists (fun tag -> String.length tag > 100) tag_list then
Error "tag too long"
2022-04-04 12:45:20 +02:00
else if Option.is_none image_info && String.length (String.trim comment) = 0
then Error "Your post must contain an image or a comment"
else
2022-03-16 03:06:59 +01:00
let tag_list =
List.map String.lowercase_ascii
@@ List.sort_uniq String.compare
@@ List.filter (( <> ) "")
@@ List.map String.trim
@@ List.map Dream.html_escape tag_list
in
let date = Unix.time () in
let comment, citations = parse_comment comment in
let* nick = User.get_nick user_id in
2022-03-23 04:34:54 +01:00
let* emojid = Emojid.make_emojid id in
2022-03-16 03:06:59 +01:00
let reply =
{ id
2022-03-23 04:34:54 +01:00
; emojid
2022-03-16 03:06:59 +01:00
; parent_id
; date
; user_id
; nick
; comment
; image_info
; tags = tag_list
; replies = []
; citations
}
in
Ok reply
let build_op ~comment ~image_info ~tag_list ~categories ~subject ~lat ~lng
user_id =
2022-01-25 14:07:28 +01:00
let subject = Dream.html_escape subject in
2022-03-09 14:36:19 +01:00
if List.exists (fun s -> not (List.mem s App.categories)) categories then
Error "Invalid category"
2022-01-25 14:07:28 +01:00
else
2022-03-09 14:36:19 +01:00
let tag_list = categories @ tag_list in
(* TODO latlng validation? *)
let is_valid_latlng = true in
if not is_valid_latlng then Error "Invalid coordinate"
else if String.length subject > 600 then Error "Invalid subject"
else
2022-03-16 03:06:59 +01:00
let* reply = build_reply ~comment ~image_info ~tag_list user_id in
2022-04-05 23:08:57 +02:00
Ok ({ subject; lng; lat }, reply)
2022-03-16 03:06:59 +01:00
let make_post ~comment ?image_input ~tags ~op_or_reply_data user_id =
let tag_list = String.split_on_char ',' tags in
let* image, image_info =
match image_input with
| None -> Ok (None, None)
| Some image_input ->
2022-04-05 23:08:57 +02:00
let* image = Image.make_image image_input in
2022-03-16 03:06:59 +01:00
Ok (Some image, Some (image.name, image.alt))
in
let* post =
match op_or_reply_data with
| `Reply_data parent_id ->
let* reply =
build_reply ~comment ~image_info ~tag_list ~parent_id user_id
in
Ok (Post reply)
| `Op_data (categories, subject, lat, lng) ->
let* thread_data, reply =
build_op ~comment ~image_info ~tag_list ~categories ~subject ~lat ~lng
user_id
in
Ok (Op (thread_data, reply))
2022-03-09 14:36:19 +01:00
in
2022-03-16 03:06:59 +01:00
upload_post ~image post
(* true if post is an op too *)
2022-04-08 00:43:00 +02:00
let post_exist id = Result.is_ok (Q.get_is_post id)
let get_post id =
2022-03-23 04:34:54 +01:00
let* emojid = Emojid.Q.get_emojid id in
2022-04-08 00:43:00 +02:00
let* parent_id = Q.get_post_thread id in
let* user_id = Q.get_post_user_id id in
2022-03-08 21:20:01 +01:00
let* nick = User.get_nick user_id in
2022-04-08 00:43:00 +02:00
let* comment = Q.get_post_comment id in
let* date = Q.get_post_date id in
let* image_info = Image.get_info id in
2022-04-08 00:43:00 +02:00
let* tags = Q.get_post_tags id in
let* replies = Q.get_post_replies id in
let* citations = Q.get_post_citations id in
let reply =
2022-03-06 19:23:52 +01:00
{ id
2022-03-23 04:34:54 +01:00
; emojid
2022-03-06 19:23:52 +01:00
; parent_id
; date
2022-03-08 21:20:01 +01:00
; user_id
2022-03-06 19:23:52 +01:00
; nick
; comment
; image_info
; tags
; replies
; citations
}
in
Ok reply
let get_thread_data id =
2022-04-08 00:43:00 +02:00
let* subject, lat, lng = Q.get_thread_info id in
Ok { subject; lat; lng }
let get_op id =
let* thread_data = get_thread_data id in
2022-02-21 02:24:17 +01:00
let* post = get_post id in
Ok (thread_data, post)
let get_posts ids = unwrap_list get_post ids
let get_ops ids = unwrap_list get_op ids
2022-02-21 09:46:27 +01:00
2022-03-08 21:20:01 +01:00
let try_delete_post ~user_id id =
2022-02-21 09:46:27 +01:00
let* post = get_post id in
2022-04-08 00:43:00 +02:00
if post.user_id = user_id || User.is_admin user_id then Q.delete_post id
2022-02-21 09:46:27 +01:00
else Error "You can only delete your posts"
2022-02-22 07:10:52 +01:00
2022-03-08 21:20:01 +01:00
let report ~user_id ~reason id =
2022-02-27 19:58:32 +01:00
if not (post_exist id) then Error "This post exists not"
2022-02-23 14:30:06 +01:00
else if String.length reason > 2000 then Error "Your reason is too long.."
2022-02-22 07:10:52 +01:00
else
2022-02-23 14:30:06 +01:00
let reason = Dream.html_escape reason in
let date = Unix.time () in
2022-04-08 00:43:00 +02:00
Q.upload_report (user_id, reason, date, id)
2022-02-23 22:39:48 +01:00
let get_reports () =
2022-04-08 00:43:00 +02:00
let* reports = Q.get_reports () in
2022-02-23 22:39:48 +01:00
let* posts =
2022-03-08 21:20:01 +01:00
unwrap_list (fun (_reporter_id, _reason, _date, id) -> get_post id) reports
in
(* add reporter_nick to reports so we can display it *)
let* reports =
unwrap_list
(fun (reporter_id, reason, date, id) ->
let* reporter_nick = User.get_nick reporter_id in
Ok (reporter_id, reporter_nick, reason, date, id) )
reports
2022-02-23 22:39:48 +01:00
in
Ok (posts, reports)