2021-11-07 00:31:32 +01:00
|
|
|
type t =
|
|
|
|
|
{ nick : string
|
|
|
|
|
; password : string
|
|
|
|
|
; email : string (* TODO: make email optional ? *)
|
2021-11-07 18:52:31 +01:00
|
|
|
; bio : string
|
2021-11-08 15:24:10 +01:00
|
|
|
; avatar : string
|
2021-11-07 00:31:32 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-06 22:44:14 +01:00
|
|
|
module Q = struct
|
|
|
|
|
let create_user_table =
|
|
|
|
|
Caqti_request.exec Caqti_type.unit
|
2021-12-06 23:34:47 +01:00
|
|
|
"CREATE TABLE IF NOT EXISTS user (nick TEXT, password TEXT, email TEXT, \
|
2021-12-07 23:18:07 +01:00
|
|
|
bio TEXT, avatar BLOB, PRIMARY KEY(nick));"
|
|
|
|
|
|
|
|
|
|
let create_plant_user_table =
|
|
|
|
|
Caqti_request.exec Caqti_type.unit
|
|
|
|
|
"CREATE TABLE IF NOT EXISTS plant_user (plant_id TEXT, nick TEXT, \
|
|
|
|
|
PRIMARY KEY(plant_id), FOREIGN KEY(nick) REFERENCES user(nick));"
|
|
|
|
|
|
|
|
|
|
let create_plant_image_table =
|
|
|
|
|
Caqti_request.exec Caqti_type.unit
|
|
|
|
|
"CREATE TABLE IF NOT EXISTS plant_image (id INTEGER PRIMARY KEY, \
|
|
|
|
|
plant_id TEXT, image TEXT, FOREIGN KEY(plant_id) REFERENCES \
|
|
|
|
|
plant_user(plant_id));"
|
|
|
|
|
|
|
|
|
|
let create_plant_tag_table =
|
|
|
|
|
Caqti_request.exec Caqti_type.unit
|
|
|
|
|
"CREATE TABLE IF NOT EXISTS plant_tag (plant_id TEXT, tag TEXT, FOREIGN \
|
|
|
|
|
KEY(plant_id) REFERENCES plant_user(plant_id));"
|
|
|
|
|
|
|
|
|
|
let create_plant_gps_table =
|
|
|
|
|
Caqti_request.exec Caqti_type.unit
|
|
|
|
|
"CREATE TABLE IF NOT EXISTS plant_gps (plant_id TEXT, gps TEXT, FOREIGN \
|
|
|
|
|
KEY(plant_id) REFERENCES plant_user(plant_id));"
|
2021-12-06 22:44:14 +01:00
|
|
|
|
|
|
|
|
let get_password =
|
|
|
|
|
Caqti_request.find_opt Caqti_type.string Caqti_type.string
|
|
|
|
|
"SELECT password FROM user WHERE nick=?;"
|
|
|
|
|
|
2021-12-07 00:43:36 +01:00
|
|
|
let is_already_user =
|
2021-12-06 22:44:14 +01:00
|
|
|
Caqti_request.find_opt
|
|
|
|
|
Caqti_type.(tup2 string string)
|
2021-12-07 00:43:36 +01:00
|
|
|
Caqti_type.int
|
2021-12-06 22:44:14 +01:00
|
|
|
"SELECT EXISTS(SELECT 1 FROM user WHERE nick=? OR email=?);"
|
|
|
|
|
|
|
|
|
|
let inser_new_user =
|
|
|
|
|
Caqti_request.exec
|
|
|
|
|
Caqti_type.(tup4 string string string Caqti_type.(tup2 string string))
|
|
|
|
|
"INSERT INTO user VALUES (?, ?, ?, ?, ?);"
|
|
|
|
|
|
|
|
|
|
let list_nicks =
|
|
|
|
|
Caqti_request.collect Caqti_type.unit Caqti_type.string
|
|
|
|
|
"SELECT nick FROM user;"
|
|
|
|
|
|
|
|
|
|
let get_user =
|
|
|
|
|
Caqti_request.find_opt Caqti_type.string
|
|
|
|
|
Caqti_type.(tup4 string string string Caqti_type.(tup2 string string))
|
|
|
|
|
"SELECT * FROM user WHERE nick=?;"
|
|
|
|
|
|
|
|
|
|
let update_bio =
|
|
|
|
|
Caqti_request.exec
|
|
|
|
|
Caqti_type.(tup2 string string)
|
|
|
|
|
"UPDATE user SET bio=? WHERE nick=?;"
|
|
|
|
|
|
|
|
|
|
let get_bio =
|
|
|
|
|
Caqti_request.find_opt Caqti_type.string Caqti_type.string
|
|
|
|
|
"SELECT bio FROM user WHERE nick=?;"
|
|
|
|
|
|
|
|
|
|
let get_avatar =
|
|
|
|
|
Caqti_request.find_opt Caqti_type.string Caqti_type.string
|
|
|
|
|
"SELECT avatar FROM user WHERE nick=?;"
|
|
|
|
|
|
|
|
|
|
let upload_avatar =
|
|
|
|
|
Caqti_request.exec
|
|
|
|
|
Caqti_type.(tup2 string string)
|
|
|
|
|
"UPDATE user SET avatar=? WHERE nick=?;"
|
2021-12-07 23:18:07 +01:00
|
|
|
|
|
|
|
|
let upload_plant_id =
|
|
|
|
|
Caqti_request.exec
|
|
|
|
|
Caqti_type.(tup2 string string)
|
|
|
|
|
"INSERT INTO plant_user VALUES (?,?);"
|
|
|
|
|
|
|
|
|
|
let upload_plant_tag =
|
|
|
|
|
Caqti_request.exec
|
|
|
|
|
Caqti_type.(tup2 string string)
|
|
|
|
|
"INSERT INTO plant_tag VALUES (?,?);"
|
|
|
|
|
|
|
|
|
|
let upload_plant_gps =
|
|
|
|
|
Caqti_request.exec
|
|
|
|
|
Caqti_type.(tup2 string string)
|
|
|
|
|
"INSERT INTO plant_gps VALUES (?,?);"
|
|
|
|
|
|
|
|
|
|
let upload_plant_image =
|
|
|
|
|
Caqti_request.exec
|
|
|
|
|
Caqti_type.(tup2 string string)
|
|
|
|
|
"INSERT INTO plant_image VALUES (NULL,?,?);"
|
|
|
|
|
|
|
|
|
|
let get_user_plants =
|
|
|
|
|
Caqti_request.collect Caqti_type.string Caqti_type.string
|
|
|
|
|
"SELECT plant_id FROM plant_user WHERE nick=?;"
|
|
|
|
|
|
|
|
|
|
let count_plant_image =
|
|
|
|
|
Caqti_request.find_opt Caqti_type.string Caqti_type.int
|
|
|
|
|
"SELECT COUNT(*) FROM plant_image WHERE plant_id=?;"
|
|
|
|
|
|
|
|
|
|
let get_plant_image =
|
|
|
|
|
Caqti_request.find_opt
|
|
|
|
|
Caqti_type.(tup2 string string)
|
|
|
|
|
Caqti_type.string
|
|
|
|
|
"SELECT image FROM plant_image WHERE plant_id=? AND id=?;"
|
|
|
|
|
|
|
|
|
|
let get_tags =
|
|
|
|
|
Caqti_request.collect Caqti_type.string Caqti_type.string
|
|
|
|
|
"SELECT tag FROM plant_tag WHERE plant_id=?;"
|
2021-12-06 22:44:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
module Db =
|
|
|
|
|
( val Caqti_blocking.connect (Uri.of_string (Filename.concat "sqlite3://" Db.db))
|
|
|
|
|
|> Caqti_blocking.or_fail )
|
|
|
|
|
|
2021-12-07 23:18:07 +01:00
|
|
|
let random_state = Random.State.make_self_init ()
|
|
|
|
|
|
2021-11-07 00:31:32 +01:00
|
|
|
let () =
|
2021-12-07 23:18:07 +01:00
|
|
|
let tables =
|
|
|
|
|
[ Q.create_user_table
|
|
|
|
|
; Q.create_plant_user_table
|
|
|
|
|
; Q.create_plant_image_table
|
|
|
|
|
; Q.create_plant_tag_table
|
|
|
|
|
; Q.create_plant_gps_table
|
|
|
|
|
]
|
|
|
|
|
in
|
|
|
|
|
if
|
|
|
|
|
List.exists Result.is_error
|
|
|
|
|
(List.map (fun query -> Db.exec query ()) tables)
|
|
|
|
|
then
|
|
|
|
|
Dream.warning (fun log -> log "can't create table")
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2021-11-07 10:21:01 +01:00
|
|
|
let login ~nick ~password request =
|
2021-12-06 22:44:14 +01:00
|
|
|
let good_password = Db.find_opt Q.get_password nick in
|
2021-11-07 09:47:15 +01:00
|
|
|
match good_password with
|
2021-12-06 22:44:14 +01:00
|
|
|
| Ok foo -> (
|
|
|
|
|
match foo with
|
|
|
|
|
| Some good_password ->
|
|
|
|
|
if Bcrypt.verify password (Bcrypt.hash_of_string good_password) then
|
|
|
|
|
let _ =
|
|
|
|
|
let%lwt () = Dream.invalidate_session request in
|
|
|
|
|
Dream.put_session "nick" nick request
|
|
|
|
|
in
|
|
|
|
|
Ok ()
|
|
|
|
|
else
|
|
|
|
|
Error "wrong password"
|
|
|
|
|
| None -> Error (Format.sprintf "user `%s` doesn't exist" nick) )
|
|
|
|
|
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2021-11-07 00:31:32 +01:00
|
|
|
let register ~email ~nick ~password =
|
2021-11-05 16:55:19 +01:00
|
|
|
(* TODO: remove bad characters (e.g. delthas) *)
|
2021-11-07 00:31:32 +01:00
|
|
|
let valid_nick =
|
|
|
|
|
String.length nick < 64
|
|
|
|
|
&& String.length nick > 0
|
|
|
|
|
&& String.escaped nick = nick
|
|
|
|
|
in
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2021-11-07 00:31:32 +01:00
|
|
|
let valid_email =
|
|
|
|
|
match Emile.of_string email with
|
|
|
|
|
| Ok _ -> true
|
|
|
|
|
| Error _ -> false
|
2021-11-05 16:55:19 +01:00
|
|
|
in
|
|
|
|
|
|
2021-11-07 00:31:32 +01:00
|
|
|
let valid_password =
|
|
|
|
|
String.length password < 128 && String.length password > 0
|
|
|
|
|
in
|
2021-11-05 16:55:19 +01:00
|
|
|
|
|
|
|
|
let valid = valid_nick && valid_email && valid_password in
|
|
|
|
|
|
2021-11-07 09:47:15 +01:00
|
|
|
let password = Bcrypt.hash password in
|
|
|
|
|
let password = Bcrypt.string_of_hash password in
|
|
|
|
|
|
2021-11-07 00:31:32 +01:00
|
|
|
if not valid then
|
|
|
|
|
Error "Something is wrong"
|
2021-11-05 16:55:19 +01:00
|
|
|
else
|
2021-12-07 00:43:36 +01:00
|
|
|
let unique = Db.find_opt Q.is_already_user (nick, email) in
|
2021-11-07 00:54:35 +01:00
|
|
|
match unique with
|
2021-12-06 22:44:14 +01:00
|
|
|
| Ok unique -> (
|
|
|
|
|
match unique with
|
2021-12-07 00:43:36 +01:00
|
|
|
| Some nb -> (
|
|
|
|
|
match nb with
|
|
|
|
|
| 0 -> (
|
|
|
|
|
let res =
|
|
|
|
|
Db.exec Q.inser_new_user (nick, password, email, ("", ""))
|
|
|
|
|
in
|
|
|
|
|
match res with
|
|
|
|
|
| Ok res -> Ok res
|
|
|
|
|
| Error e ->
|
|
|
|
|
Error (Format.sprintf "db error: %s" (Caqti_error.show e)) )
|
|
|
|
|
| _ -> Error "nick or email already exists" )
|
|
|
|
|
| None -> Error "db error" )
|
2021-12-06 22:44:14 +01:00
|
|
|
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
2021-11-07 01:32:38 +01:00
|
|
|
|
2021-12-07 23:18:07 +01:00
|
|
|
let plant_view plant_id =
|
|
|
|
|
let count = Db.find_opt Q.count_plant_image plant_id in
|
|
|
|
|
match count with
|
|
|
|
|
| Ok count -> (
|
|
|
|
|
match count with
|
|
|
|
|
| Some count -> (
|
|
|
|
|
let images =
|
|
|
|
|
String.concat "\n"
|
|
|
|
|
(List.map
|
|
|
|
|
(Format.sprintf
|
|
|
|
|
{|<li><img src="/plant_pic/%s/%i" class="img-thumbnail"></li>|}
|
|
|
|
|
plant_id )
|
|
|
|
|
(List.init count succ) )
|
|
|
|
|
in
|
|
|
|
|
let tags = Db.fold Q.get_tags (fun tag acc -> tag :: acc) plant_id [] in
|
|
|
|
|
match tags with
|
|
|
|
|
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
|
|
|
|
| Ok tags ->
|
|
|
|
|
let tags = String.concat " " tags in
|
|
|
|
|
(* TODO add link to gps/map too *)
|
|
|
|
|
Ok (images ^ tags) )
|
|
|
|
|
| None -> Error "db error" )
|
|
|
|
|
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
|
|
|
|
|
2021-11-07 01:42:18 +01:00
|
|
|
let list () =
|
2021-12-06 22:44:14 +01:00
|
|
|
let users = Db.fold Q.list_nicks (fun nick acc -> nick :: acc) () [] in
|
2021-11-07 01:32:38 +01:00
|
|
|
match users with
|
|
|
|
|
| Ok users ->
|
|
|
|
|
Format.asprintf "<ul>%a</ul>"
|
|
|
|
|
(Format.pp_print_list (fun fmt -> function
|
2021-12-06 22:44:14 +01:00
|
|
|
| s -> Format.fprintf fmt {|<li><a href="/user/%s">%s</a></li>|} s s )
|
|
|
|
|
)
|
2021-11-07 01:32:38 +01:00
|
|
|
users
|
2021-12-06 22:44:14 +01:00
|
|
|
| Error e -> Format.sprintf "db error: %s" (Caqti_error.show e)
|
2021-11-07 01:42:18 +01:00
|
|
|
|
2021-11-07 10:32:17 +01:00
|
|
|
let public_profile request =
|
2021-11-07 01:42:18 +01:00
|
|
|
let nick = Dream.param "user" request in
|
2021-12-06 22:44:14 +01:00
|
|
|
let user = Db.find_opt Q.get_user nick in
|
2021-11-07 01:42:18 +01:00
|
|
|
match user with
|
2021-12-06 22:44:14 +01:00
|
|
|
| Ok user -> (
|
|
|
|
|
match user with
|
2021-12-07 23:18:07 +01:00
|
|
|
| Some (nick, password, email, (bio, _)) -> (
|
|
|
|
|
(* TODO show plants *)
|
|
|
|
|
let plant_id_list =
|
|
|
|
|
Db.fold Q.get_user_plants (fun plant_id acc -> plant_id :: acc) nick []
|
|
|
|
|
in
|
|
|
|
|
match plant_id_list with
|
|
|
|
|
| Error e -> Format.sprintf "db error: %s" (Caqti_error.show e)
|
|
|
|
|
| Ok plant_id_list ->
|
|
|
|
|
let plants_result = List.map plant_view plant_id_list in
|
|
|
|
|
if List.exists Result.is_error plants_result then
|
|
|
|
|
Format.sprintf "db error"
|
|
|
|
|
else
|
|
|
|
|
let plants =
|
|
|
|
|
String.concat "\n"
|
|
|
|
|
(List.map
|
|
|
|
|
(function
|
|
|
|
|
| Ok s -> s
|
|
|
|
|
| Error _ -> assert false )
|
|
|
|
|
plants_result )
|
|
|
|
|
in
|
|
|
|
|
Format.sprintf
|
|
|
|
|
{|nick = `%s`; password = `%s`; email = `%s`; bio = '%s';
|
2021-11-08 15:24:10 +01:00
|
|
|
<img src="/user/%s/avatar" class="img-thumbnail" alt="Your avatar picture">
|
2021-12-07 23:18:07 +01:00
|
|
|
%s
|
2021-11-08 15:24:10 +01:00
|
|
|
|}
|
2021-12-07 23:18:07 +01:00
|
|
|
nick password email (Dream.html_escape bio) nick plants )
|
2021-12-06 22:44:14 +01:00
|
|
|
| None -> "incoherent db answer" )
|
|
|
|
|
| Error e -> Format.sprintf "db error: %s" (Caqti_error.show e)
|
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
|
|
|
|
|
|
|
|
let update_bio bio nick =
|
|
|
|
|
let valid = true in
|
|
|
|
|
(* TODO check bio len and FORBIDEN WORDS *)
|
|
|
|
|
if not valid then
|
|
|
|
|
Error "Not biologic"
|
|
|
|
|
else
|
2021-12-06 22:44:14 +01:00
|
|
|
let res = Db.exec Q.update_bio (bio, nick) in
|
2021-11-07 18:52:31 +01:00
|
|
|
match res with
|
|
|
|
|
| Ok _ -> Ok ()
|
2021-12-06 22:44:14 +01:00
|
|
|
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
2021-11-07 18:52:31 +01:00
|
|
|
|
|
|
|
|
let get_bio nick =
|
2021-12-06 22:44:14 +01:00
|
|
|
let res = Db.find_opt Q.get_bio nick in
|
2021-11-07 18:52:31 +01:00
|
|
|
match res with
|
2021-12-06 22:44:14 +01:00
|
|
|
| Ok bio -> (
|
|
|
|
|
match bio with
|
|
|
|
|
| Some bio -> Ok bio
|
|
|
|
|
| None -> Error "incoherent db result" )
|
|
|
|
|
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
2021-11-08 15:24:10 +01:00
|
|
|
|
|
|
|
|
let get_avatar nick =
|
2021-12-06 22:44:14 +01:00
|
|
|
let res = Db.find_opt Q.get_avatar nick in
|
2021-11-08 15:24:10 +01:00
|
|
|
match res with
|
2021-12-06 22:44:14 +01:00
|
|
|
| Ok avatar -> (
|
|
|
|
|
match avatar with
|
|
|
|
|
| Some avatar ->
|
|
|
|
|
if String.length avatar = 0 then
|
|
|
|
|
(* TODO default avatar *)
|
|
|
|
|
Ok None
|
|
|
|
|
else
|
|
|
|
|
Ok (Some avatar)
|
|
|
|
|
| None -> Error "db error:" )
|
|
|
|
|
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
2021-11-08 15:24:10 +01:00
|
|
|
|
2021-12-07 23:18:07 +01:00
|
|
|
let get_plant_image plant_id nb =
|
|
|
|
|
let res = Db.find_opt Q.get_plant_image (plant_id, nb) in
|
|
|
|
|
match res with
|
|
|
|
|
| Ok content -> (
|
|
|
|
|
match content with
|
|
|
|
|
| Some content -> Ok (Some content)
|
|
|
|
|
| None -> Error "Image not found" )
|
|
|
|
|
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
|
|
|
|
|
2021-11-08 15:24:10 +01:00
|
|
|
let upload_avatar files nick =
|
|
|
|
|
match files with
|
|
|
|
|
| [] -> Error "No file provided"
|
|
|
|
|
| [ (_, content) ] -> (
|
|
|
|
|
(* TODO validate image data with konan etc*)
|
|
|
|
|
(* TODO file_name in db??*)
|
|
|
|
|
let valid = true in
|
|
|
|
|
if not valid then
|
|
|
|
|
Error "Invalid image"
|
|
|
|
|
else
|
2021-12-06 22:44:14 +01:00
|
|
|
let res = Db.exec Q.upload_avatar (content, nick) in
|
2021-11-08 15:24:10 +01:00
|
|
|
match res with
|
|
|
|
|
| Ok _ -> Ok ()
|
2021-12-06 22:44:14 +01:00
|
|
|
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e)) )
|
2021-11-08 15:24:10 +01:00
|
|
|
| _files -> Error "More than one file provided"
|
2021-12-07 23:18:07 +01:00
|
|
|
|
|
|
|
|
(* TODO *)
|
|
|
|
|
let is_valid_image _content = true
|
|
|
|
|
|
|
|
|
|
let add_plant tags files nick =
|
|
|
|
|
match files with
|
|
|
|
|
| files -> (
|
|
|
|
|
(* TODO parse tags*)
|
|
|
|
|
(* id for plant*)
|
|
|
|
|
let ok_list = List.map (fun (_, content) -> is_valid_image content) files in
|
|
|
|
|
let valid_files = List.for_all (fun valid -> valid) ok_list in
|
|
|
|
|
if not valid_files then
|
|
|
|
|
Error "Invalid image"
|
|
|
|
|
else
|
|
|
|
|
(* add plant to db *)
|
|
|
|
|
|
|
|
|
|
(* TODO make a plant_id *)
|
|
|
|
|
let plant_id = Uuidm.to_string (Uuidm.v4_gen random_state ()) in
|
|
|
|
|
(* add to plant_id <-> user*)
|
|
|
|
|
let res_plant = Db.exec Q.upload_plant_id (plant_id, nick) in
|
|
|
|
|
match res_plant with
|
|
|
|
|
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
|
|
|
|
| Ok _ -> (
|
|
|
|
|
(* add to plant_id <-> tag table*)
|
|
|
|
|
(* TODO iter on tags*)
|
|
|
|
|
let res_tags = Db.exec Q.upload_plant_tag (plant_id, tags) in
|
|
|
|
|
match res_tags with
|
|
|
|
|
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
|
|
|
|
| Ok _ -> (
|
|
|
|
|
(* add to plant_id <-> image*)
|
|
|
|
|
let res_images =
|
|
|
|
|
List.find_opt Result.is_error
|
|
|
|
|
(List.map
|
|
|
|
|
(fun (_, content) ->
|
|
|
|
|
Db.exec Q.upload_plant_image (plant_id, content) )
|
|
|
|
|
files )
|
|
|
|
|
in
|
|
|
|
|
match res_images with
|
|
|
|
|
| Some (Error e) ->
|
|
|
|
|
Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
|
|
|
|
| Some (Ok _) -> assert false
|
|
|
|
|
| None -> Ok () ) ) )
|