2022-04-04 21:12:31 +02:00
|
|
|
open Syntax
|
2022-04-08 00:43:00 +02:00
|
|
|
open Caqti_request.Infix
|
|
|
|
|
open Caqti_type
|
2021-12-22 11:27:53 +01:00
|
|
|
|
2021-11-07 00:31:32 +01:00
|
|
|
type t =
|
2022-03-08 21:20:01 +01:00
|
|
|
{ user_id : string
|
|
|
|
|
; nick : string
|
2021-11-07 00:31:32 +01:00
|
|
|
; password : string
|
2021-12-09 02:17:48 +01:00
|
|
|
; email : string
|
2021-11-07 18:52:31 +01:00
|
|
|
; bio : string
|
2022-03-09 19:59:46 +01:00
|
|
|
; metadata : (string * string) list
|
2021-11-07 00:31:32 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-08 00:43:00 +02:00
|
|
|
let () =
|
|
|
|
|
let tables =
|
|
|
|
|
[| (unit ->. unit)
|
|
|
|
|
"CREATE TABLE IF NOT EXISTS user (user_id TEXT, nick TEXT, password \
|
|
|
|
|
TEXT, email TEXT, bio TEXT, PRIMARY KEY(user_id))"
|
|
|
|
|
; (unit ->. unit)
|
|
|
|
|
"CREATE TABLE IF NOT EXISTS banished (nick TEXT, email TEXT)"
|
|
|
|
|
; (unit ->. unit)
|
|
|
|
|
"CREATE TABLE IF NOT EXISTS user_metadata (user_id TEXT, metadata \
|
|
|
|
|
TEXT, 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 user tables")
|
2022-03-08 01:19:35 +01:00
|
|
|
|
2022-04-08 00:43:00 +02:00
|
|
|
module Q = struct
|
2022-03-08 01:19:35 +01:00
|
|
|
let upload_metadata =
|
2022-04-08 00:43:00 +02:00
|
|
|
Db.exec
|
|
|
|
|
@@ (tup2 string string ->. unit) "INSERT INTO user_metadata VALUES (?, ?)"
|
2022-03-08 01:19:35 +01:00
|
|
|
|
|
|
|
|
let delete_metadata =
|
2022-04-08 00:43:00 +02:00
|
|
|
Db.exec @@ (string ->. unit) "DELETE FROM user_metadata WHERE user_id=?"
|
2022-03-08 21:20:01 +01:00
|
|
|
|
2022-03-14 17:20:02 +01:00
|
|
|
let get_user_id_from_email =
|
2022-04-08 00:43:00 +02:00
|
|
|
Db.find @@ (string ->! string) "SELECT user_id FROM user WHERE email=?"
|
2022-03-08 01:19:35 +01:00
|
|
|
|
2021-12-06 22:44:14 +01:00
|
|
|
let get_password =
|
2022-04-08 00:43:00 +02:00
|
|
|
Db.find @@ (string ->! string) "SELECT password FROM user WHERE user_id=?"
|
2021-12-06 22:44:14 +01:00
|
|
|
|
2021-12-07 00:43:36 +01:00
|
|
|
let is_already_user =
|
2022-04-08 00:43:00 +02:00
|
|
|
Db.find
|
|
|
|
|
@@ (tup2 string string ->! int)
|
|
|
|
|
"SELECT EXISTS(SELECT 1 FROM user WHERE nick=? OR email=?)"
|
2021-12-06 22:44:14 +01:00
|
|
|
|
2022-03-06 19:23:52 +01:00
|
|
|
let upload_user =
|
2022-04-08 00:43:00 +02:00
|
|
|
Db.exec
|
|
|
|
|
@@ (tup4 string string string (tup2 string string) ->. unit)
|
|
|
|
|
"INSERT INTO user VALUES (?, ?, ?, ?, ?)"
|
2021-12-06 22:44:14 +01:00
|
|
|
|
2022-04-08 00:43:00 +02:00
|
|
|
let list_nicks = Db.collect_list @@ (unit ->* string) "SELECT nick FROM user"
|
2021-12-06 22:44:14 +01:00
|
|
|
|
|
|
|
|
let get_user =
|
2022-04-08 00:43:00 +02:00
|
|
|
Db.find
|
|
|
|
|
@@ (* there is no "tup6" *)
|
2022-04-04 21:38:09 +02:00
|
|
|
(string ->! tup4 string string string (tup2 string string))
|
2022-04-04 21:15:04 +02:00
|
|
|
"SELECT * FROM user WHERE user_id=?"
|
2021-12-06 22:44:14 +01:00
|
|
|
|
|
|
|
|
let update_bio =
|
2022-04-08 00:43:00 +02:00
|
|
|
Db.exec
|
|
|
|
|
@@ (tup2 string string ->. unit) "UPDATE user SET bio=? WHERE user_id=?"
|
2021-12-06 22:44:14 +01:00
|
|
|
|
2022-03-08 21:20:01 +01:00
|
|
|
let update_nick =
|
2022-04-08 00:43:00 +02:00
|
|
|
Db.exec
|
|
|
|
|
@@ (tup2 string string ->. unit) "UPDATE user SET nick=? WHERE user_id=?"
|
2022-03-06 19:23:52 +01:00
|
|
|
|
|
|
|
|
let update_email =
|
2022-04-08 00:43:00 +02:00
|
|
|
Db.exec
|
|
|
|
|
@@ (tup2 string string ->. unit) "UPDATE user SET email=? WHERE user_id=?"
|
2022-03-06 19:23:52 +01:00
|
|
|
|
|
|
|
|
let update_password =
|
2022-04-08 00:43:00 +02:00
|
|
|
Db.exec
|
|
|
|
|
@@ (tup2 string string ->. unit)
|
|
|
|
|
"UPDATE user SET password=? WHERE user_id=?"
|
2022-03-06 19:23:52 +01:00
|
|
|
|
2022-04-08 00:43:00 +02:00
|
|
|
let get_bio =
|
|
|
|
|
Db.find @@ (string ->! string) "SELECT bio FROM user WHERE user_id=?"
|
2021-12-06 22:44:14 +01:00
|
|
|
|
2022-04-08 00:43:00 +02:00
|
|
|
let get_email =
|
|
|
|
|
Db.find @@ (string ->! string) "SELECT email FROM user WHERE user_id=?"
|
2022-02-23 22:39:48 +01:00
|
|
|
|
2022-04-08 00:43:00 +02:00
|
|
|
let delete_user =
|
|
|
|
|
Db.exec @@ (string ->. unit) "DELETE FROM user WHERE user_id=?"
|
2022-02-23 22:39:48 +01:00
|
|
|
|
|
|
|
|
let upload_banished =
|
2022-04-08 00:43:00 +02:00
|
|
|
Db.exec @@ (tup2 string string ->. unit) "INSERT INTO banished VALUES (?,?)"
|
2022-02-23 22:39:48 +01:00
|
|
|
|
|
|
|
|
let get_banished =
|
2022-04-08 00:43:00 +02:00
|
|
|
Db.find
|
|
|
|
|
@@ (tup2 string string ->! tup2 string string)
|
|
|
|
|
"SELECT * FROM banished WHERE nick=? OR email=?"
|
2021-12-06 22:44:14 +01:00
|
|
|
end
|
|
|
|
|
|
2022-04-08 00:43:00 +02:00
|
|
|
let get_nick =
|
|
|
|
|
Db.find @@ (string ->! string) "SELECT nick FROM user WHERE user_id=?"
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2022-04-08 00:43:00 +02:00
|
|
|
let get_id_from_nick =
|
|
|
|
|
Db.find @@ (string ->! string) "SELECT user_id FROM user WHERE nick=?"
|
2022-04-04 14:39:25 +02:00
|
|
|
|
2022-04-08 00:43:00 +02:00
|
|
|
let exist id = Result.is_ok (Q.get_user id)
|
2022-04-01 05:31:52 +02:00
|
|
|
|
2022-04-08 00:43:00 +02:00
|
|
|
let exist_nick nick = Result.is_ok (get_id_from_nick nick)
|
2022-02-22 00:15:30 +01:00
|
|
|
|
2022-04-08 00:43:00 +02:00
|
|
|
let exist_email email = Result.is_ok (Q.get_user_id_from_email email)
|
2022-03-08 01:19:35 +01:00
|
|
|
|
2022-04-08 00:43:00 +02:00
|
|
|
let get_metadata =
|
|
|
|
|
let query =
|
|
|
|
|
Db.find
|
|
|
|
|
@@ (string ->! string) "SELECT metadata FROM user_metadata WHERE user_id=?"
|
|
|
|
|
in
|
|
|
|
|
fun nick ->
|
|
|
|
|
let* metadata = query nick in
|
|
|
|
|
let metadata : (string * string) list = Marshal.from_string metadata 0 in
|
|
|
|
|
Ok metadata
|
2022-03-08 21:20:01 +01:00
|
|
|
|
|
|
|
|
let get_user user_id =
|
2022-04-08 00:43:00 +02:00
|
|
|
let* user_id, nick, password, (email, bio) = Q.get_user user_id in
|
2022-03-08 21:20:01 +01:00
|
|
|
let* metadata = get_metadata user_id in
|
2022-04-04 14:39:25 +02:00
|
|
|
Ok { user_id; nick; password; email; bio; metadata }
|
2022-03-06 19:23:52 +01:00
|
|
|
|
2022-04-08 00:43:00 +02:00
|
|
|
let is_banished login = Result.is_ok (Q.get_banished (login, login))
|
2022-03-08 21:20:01 +01:00
|
|
|
|
|
|
|
|
let login ~login ~password request =
|
2022-04-01 05:24:49 +02:00
|
|
|
let try_password user_id =
|
2022-04-08 00:43:00 +02:00
|
|
|
let* good_password = Q.get_password user_id in
|
2022-03-14 17:20:02 +01:00
|
|
|
if Bcrypt.verify password (Bcrypt.hash_of_string good_password) then
|
|
|
|
|
let _unit_lwt = Dream.invalidate_session request in
|
|
|
|
|
let _unit_lwt = Dream.put_session "user_id" user_id request in
|
|
|
|
|
let* nick = get_nick user_id in
|
|
|
|
|
let _unit_lwt = Dream.put_session "nick" nick request in
|
|
|
|
|
Ok ()
|
2022-04-01 05:24:49 +02:00
|
|
|
else if is_banished login then Error "YOU ARE BANISHED"
|
2022-03-14 17:20:02 +01:00
|
|
|
else Error "wrong password"
|
2022-04-01 05:24:49 +02:00
|
|
|
in
|
|
|
|
|
|
2022-04-08 00:43:00 +02:00
|
|
|
let id_from_nick = get_id_from_nick login in
|
|
|
|
|
let id_from_email = Q.get_user_id_from_email login in
|
|
|
|
|
let user_id_list =
|
|
|
|
|
List.filter_map Result.to_option [ id_from_nick; id_from_email ]
|
|
|
|
|
in
|
|
|
|
|
try
|
|
|
|
|
List.iter
|
|
|
|
|
(fun id -> if Result.is_ok @@ try_password id then raise Exit)
|
|
|
|
|
user_id_list;
|
|
|
|
|
Error "invalid login"
|
|
|
|
|
with Exit -> Ok ()
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2022-03-06 19:23:52 +01:00
|
|
|
let valid_nick nick =
|
|
|
|
|
String.length nick < 64
|
|
|
|
|
&& String.length nick > 0
|
|
|
|
|
&& Dream.html_escape nick = nick
|
|
|
|
|
|
2022-03-06 20:55:12 +01:00
|
|
|
let valid_password password =
|
|
|
|
|
String.length password < 128 && String.length password > 0
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2022-03-14 17:20:02 +01:00
|
|
|
let valid_email email = Result.is_ok @@ Emile.of_string email
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2022-03-06 20:55:12 +01:00
|
|
|
let register ~email ~nick ~password =
|
|
|
|
|
let valid = valid_nick nick && valid_email email && valid_password password in
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2021-11-07 09:47:15 +01:00
|
|
|
let password = Bcrypt.hash password in
|
|
|
|
|
let password = Bcrypt.string_of_hash password in
|
|
|
|
|
|
2022-02-18 02:40:04 +01:00
|
|
|
if not valid then Error "Something is wrong"
|
2021-11-05 16:55:19 +01:00
|
|
|
else
|
2022-04-08 00:43:00 +02:00
|
|
|
let* nb = Q.is_already_user (nick, email) in
|
2022-02-18 01:37:25 +01:00
|
|
|
if nb = 0 then
|
2022-04-04 14:39:25 +02:00
|
|
|
let user_id = Uuidm.to_string (Uuidm.v4_gen App.random_state ()) in
|
2022-04-08 00:43:00 +02:00
|
|
|
let* () = Q.upload_user (user_id, nick, password, (email, "")) in
|
|
|
|
|
Q.upload_metadata (user_id, Marshal.to_string [] [])
|
2022-02-18 02:40:04 +01:00
|
|
|
else Error "nick or email already exists"
|
2021-11-07 01:32:38 +01:00
|
|
|
|
2021-11-07 01:42:18 +01:00
|
|
|
let list () =
|
2022-04-08 00:43:00 +02:00
|
|
|
let* users = Q.list_nicks () in
|
2022-01-14 21:36:25 +01:00
|
|
|
Ok
|
|
|
|
|
(Format.asprintf "<ul>%a</ul>"
|
|
|
|
|
(Format.pp_print_list (fun fmt -> function
|
|
|
|
|
| s -> Format.fprintf fmt {|<li><a href="/user/%s">%s</a></li>|} s s )
|
|
|
|
|
)
|
|
|
|
|
users )
|
2021-11-07 01:42:18 +01:00
|
|
|
|
2021-11-07 10:32:17 +01:00
|
|
|
let profile request =
|
|
|
|
|
match Dream.session "nick" request with
|
|
|
|
|
| None -> "not logged in"
|
|
|
|
|
| Some nick -> Format.sprintf "Hello %s !" nick
|
2021-11-07 18:52:31 +01:00
|
|
|
|
2022-03-08 21:20:01 +01:00
|
|
|
let update_bio bio user_id =
|
2022-01-14 21:04:52 +01:00
|
|
|
let bio = Dream.html_escape bio in
|
|
|
|
|
let valid = String.length bio < 10000 in
|
2022-04-08 00:43:00 +02:00
|
|
|
if not valid then Error "Not biologic" else Q.update_bio (bio, user_id)
|
2022-02-23 22:39:48 +01:00
|
|
|
|
2022-03-08 21:20:01 +01:00
|
|
|
let upload_avatar files user_id =
|
2021-11-08 15:24:10 +01:00
|
|
|
match files with
|
|
|
|
|
| [] -> Error "No file provided"
|
2022-04-04 10:31:08 +02:00
|
|
|
| [ (name_opt, content) ] ->
|
2022-04-04 14:39:25 +02:00
|
|
|
let* image = Image.make_image (name_opt, "avatar", content) in
|
|
|
|
|
let* () = Image.upload_avatar image user_id in
|
2022-04-04 10:31:08 +02:00
|
|
|
Ok ()
|
2021-11-08 15:24:10 +01:00
|
|
|
| _files -> Error "More than one file provided"
|
2022-02-23 22:39:48 +01:00
|
|
|
|
2022-03-08 21:20:01 +01:00
|
|
|
let is_admin user_id =
|
|
|
|
|
match get_nick user_id with
|
|
|
|
|
| Error _e -> false
|
|
|
|
|
| Ok nick -> List.mem nick App.admins
|
2022-02-23 22:39:48 +01:00
|
|
|
|
2022-03-08 21:20:01 +01:00
|
|
|
let banish user_id =
|
|
|
|
|
let* nick = get_nick user_id in
|
2022-04-08 00:43:00 +02:00
|
|
|
let* email = Q.get_email user_id in
|
|
|
|
|
let* () = Q.delete_user user_id in
|
|
|
|
|
Q.upload_banished (nick, email)
|
2022-03-06 19:23:52 +01:00
|
|
|
|
2022-04-08 00:43:00 +02:00
|
|
|
let delete_user user_id = Q.delete_user user_id
|
2022-03-06 20:55:12 +01:00
|
|
|
|
2022-03-08 21:20:01 +01:00
|
|
|
let update_nick nick user_id =
|
|
|
|
|
if valid_nick nick then
|
2022-04-08 00:43:00 +02:00
|
|
|
if not (exist_nick nick) then Q.update_nick (nick, user_id)
|
2022-03-08 23:18:56 +01:00
|
|
|
else Error "nick already taken"
|
|
|
|
|
else Error "invalid nick"
|
2022-03-06 20:55:12 +01:00
|
|
|
|
2022-03-08 21:20:01 +01:00
|
|
|
let update_email email user_id =
|
2022-03-06 20:55:12 +01:00
|
|
|
if valid_email email then
|
2022-04-08 00:43:00 +02:00
|
|
|
if not (exist_email email) then Q.update_email (email, user_id)
|
2022-04-01 05:31:52 +02:00
|
|
|
else Error "email already taken"
|
2022-03-06 20:55:12 +01:00
|
|
|
else Error "invalid email"
|
|
|
|
|
|
2022-03-08 21:20:01 +01:00
|
|
|
let update_password password user_id =
|
2022-03-06 20:55:12 +01:00
|
|
|
if valid_password password then
|
2022-04-08 00:43:00 +02:00
|
|
|
let password = Bcrypt.hash password |> Bcrypt.string_of_hash in
|
|
|
|
|
Q.update_password (password, user_id)
|
2022-03-06 20:55:12 +01:00
|
|
|
else Error "invalid password"
|
2022-03-08 01:19:35 +01:00
|
|
|
|
2022-03-08 21:20:01 +01:00
|
|
|
let update_metadata count label content user_id =
|
2022-03-08 01:19:35 +01:00
|
|
|
let label = Dream.html_escape label in
|
|
|
|
|
let content = Dream.html_escape content in
|
|
|
|
|
if String.length label > 200 || String.length content > 400 then
|
|
|
|
|
Error "label or content is too long"
|
|
|
|
|
else
|
2022-03-08 21:20:01 +01:00
|
|
|
let* metadata = get_metadata user_id in
|
2022-03-14 17:20:02 +01:00
|
|
|
let length = List.length metadata in
|
|
|
|
|
if count < 0 || count > length then Error "invalid count"
|
|
|
|
|
else
|
|
|
|
|
let n = max (count + 1) @@ length in
|
|
|
|
|
let metadata = Array.of_list metadata in
|
|
|
|
|
let metadata =
|
|
|
|
|
List.init n (fun i ->
|
|
|
|
|
if i = count then (label, content) else metadata.(i) )
|
|
|
|
|
in
|
|
|
|
|
let metadata =
|
|
|
|
|
List.filter (fun (l, c) -> not (l = "" && c = "")) metadata
|
|
|
|
|
in
|
|
|
|
|
if List.length metadata >= 42 then Error "to many metadata"
|
|
|
|
|
else
|
|
|
|
|
let s = Marshal.to_string metadata [] in
|
2022-04-08 00:43:00 +02:00
|
|
|
let* () = Q.delete_metadata user_id in
|
|
|
|
|
Q.upload_metadata (user_id, s)
|
2022-03-08 01:19:35 +01:00
|
|
|
|
2022-03-17 01:05:25 +01:00
|
|
|
let pp_metadata fmt (label, content) =
|
2022-03-08 01:19:35 +01:00
|
|
|
Format.fprintf fmt
|
|
|
|
|
{|
|
|
|
|
|
<div class="row">
|
|
|
|
|
<div class="col metadata-label">%s</div>
|
|
|
|
|
<div class="col metadata-content">%s</div>
|
|
|
|
|
</div>
|
|
|
|
|
|}
|
|
|
|
|
label content
|
|
|
|
|
|
2022-03-17 01:05:25 +01:00
|
|
|
let pp_metadata_form fmt is_last count (label, content) request =
|
2022-03-08 01:19:35 +01:00
|
|
|
let form_tag = Dream.form_tag ~action:"/profile" request in
|
2022-03-09 19:59:46 +01:00
|
|
|
let button_text = if is_last then "Add" else "Save" in
|
2022-03-08 01:19:35 +01:00
|
|
|
Format.fprintf fmt
|
|
|
|
|
{|
|
|
|
|
|
<div class="row">
|
|
|
|
|
%s
|
|
|
|
|
<input name="label" class="metadata-label" value="%s"/>
|
|
|
|
|
<input name="content" class="metadata-content" value="%s"/>
|
|
|
|
|
<button name="count" value="%d" type="submit" class="btn btn-primary">%s</button>
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
|}
|
|
|
|
|
form_tag label content count button_text
|
|
|
|
|
|
|
|
|
|
let pp_metadata_table fmt metadata =
|
|
|
|
|
Format.fprintf fmt
|
|
|
|
|
{|
|
|
|
|
|
<div class="metadata-table">
|
|
|
|
|
%a
|
|
|
|
|
</div>
|
|
|
|
|
|}
|
|
|
|
|
(Format.pp_print_list ~pp_sep:Format.pp_print_space pp_metadata)
|
|
|
|
|
metadata
|
|
|
|
|
|
|
|
|
|
let pp_metadata_table_form fmt metadata request =
|
2022-03-09 19:59:46 +01:00
|
|
|
let l = List.mapi (fun i e -> (i, e)) metadata in
|
2022-03-08 01:19:35 +01:00
|
|
|
Format.fprintf fmt
|
|
|
|
|
{|
|
|
|
|
|
<div class="metadata-form-table">
|
|
|
|
|
%a
|
|
|
|
|
%a
|
|
|
|
|
</div>
|
|
|
|
|
|}
|
2022-03-09 19:59:46 +01:00
|
|
|
(Format.pp_print_list ~pp_sep:Format.pp_print_space
|
|
|
|
|
(fun fmt (count, metadata) ->
|
|
|
|
|
pp_metadata_form fmt false count metadata request ) )
|
|
|
|
|
l
|
|
|
|
|
(fun fmt (count, metadata) ->
|
|
|
|
|
pp_metadata_form fmt true count metadata request )
|
|
|
|
|
(List.length l, ("", ""))
|
2022-03-08 01:19:35 +01:00
|
|
|
|
2022-03-08 21:20:01 +01:00
|
|
|
let public_profile user_id =
|
|
|
|
|
let* user = get_user user_id in
|
2022-03-08 01:19:35 +01:00
|
|
|
let user_info =
|
|
|
|
|
Format.asprintf
|
|
|
|
|
{|
|
|
|
|
|
<h1>%s</h1>
|
|
|
|
|
<br />
|
|
|
|
|
<div class="row">
|
|
|
|
|
<div class="col-md-6">
|
|
|
|
|
<blockquote>%s</blockquote>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="col-md-6">
|
|
|
|
|
<img src="/user/%s/avatar" class="img-thumbnail" alt="Your avatar picture">
|
|
|
|
|
</div>
|
2022-03-31 01:43:59 +02:00
|
|
|
<a href="/discuss/%s">Speak to me !</a>
|
2022-03-08 01:19:35 +01:00
|
|
|
<div class="col-md-6">
|
|
|
|
|
%a
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|}
|
2022-03-31 01:43:59 +02:00
|
|
|
user.nick user.bio user.nick user_id pp_metadata_table user.metadata
|
2022-03-08 01:19:35 +01:00
|
|
|
in
|
|
|
|
|
Ok user_info
|