This commit is contained in:
zapashcanon 2022-04-08 00:43:00 +02:00
parent 190d086206
commit ce7bb9d386
No known key found for this signature in database
GPG key ID: 8981C3C62D1D28F1
9 changed files with 442 additions and 500 deletions

View file

@ -1,4 +1,3 @@
open Db
open Syntax
(** Creating the table of all messages.
@ -17,43 +16,45 @@ module Q = struct
open Caqti_type
let create_msg_table =
(unit ->. unit)
"CREATE TABLE IF NOT EXISTS msg ( msg_id TEXT, from_id TEXT, to_id TEXT, \
msg TEXT, PRIMARY KEY(msg_id), FOREIGN KEY(from_id) REFERENCES \
user(user_id) ON DELETE CASCADE, FOREIGN KEY(to_id) REFERENCES \
user(user_id) ON DELETE CASCADE)"
Db.exec
@@ (unit ->. unit)
"CREATE TABLE IF NOT EXISTS msg ( msg_id TEXT, from_id TEXT, to_id \
TEXT, msg TEXT, PRIMARY KEY(msg_id), FOREIGN KEY(from_id) REFERENCES \
user(user_id) ON DELETE CASCADE, FOREIGN KEY(to_id) REFERENCES \
user(user_id) ON DELETE CASCADE)"
let find_comrades =
(tup2 string string ->* tup2 string string)
"SELECT from_id, to_id FROM msg WHERE from_id=? OR to_id=?"
Db.collect_list
@@ (tup2 string string ->* tup2 string string)
"SELECT from_id, to_id FROM msg WHERE from_id=? OR to_id=?"
let find_messages =
(tup2 (tup2 string string) (tup2 string string) ->* tup2 string string)
"SELECT from_id, msg FROM msg WHERE (from_id=? AND to_id=?) OR \
(from_id=? AND to_id=?)"
Db.collect_list
@@ (tup2 (tup2 string string) (tup2 string string) ->* tup2 string string)
"SELECT from_id, msg FROM msg WHERE (from_id=? AND to_id=?) OR \
(from_id=? AND to_id=?)"
let insert_msg =
(tup3 string string string ->. unit)
"INSERT INTO msg VALUES (NULL, ?, ?, ?)"
Db.exec
@@ (tup3 string string string ->. unit)
"INSERT INTO msg VALUES (NULL, ?, ?, ?)"
end
let () =
match Db.exec Q.create_msg_table () with
| Ok () -> ()
| Error _e -> Dream.error (fun log -> log "can't create msg table")
Result.iter_error
(fun _e -> Dream.error (fun log -> log "can't create table"))
(Q.create_msg_table ())
(** let's find who the user is talking to so we can know if they're dangerous *)
let find_comrades user_id =
let^ comrades = Db.collect_list Q.find_comrades (user_id, user_id) in
let* comrades = Q.find_comrades (user_id, user_id) in
let comrades =
List.map (fun (l, r) -> if l = user_id then r else l) comrades
in
Ok (List.sort_uniq String.compare comrades)
(** find all messages between two товарищи *)
let find_messages k1 k2 =
let^ comrades = Db.collect_list Q.find_messages ((k1, k2), (k2, k1)) in
Ok comrades
let find_messages k1 k2 = Q.find_messages ((k1, k2), (k2, k1))
(** display the list of discussions *)
let render =
@ -113,9 +114,7 @@ let renderone request =
let comrade_id = Dream.param request "comrade_id" in
pp_discussion (request, user_id, comrade_id) )
let insert_msg from_id to_id msg =
let^ () = Db.exec Q.insert_msg (from_id, to_id, msg) in
Ok ()
let insert_msg from_id to_id msg = Q.insert_msg (from_id, to_id, msg)
(** handle posts *)
let post request =