add emojid
This commit is contained in:
parent
fc74c26cdd
commit
3fadbdecd3
7 changed files with 5082 additions and 9 deletions
|
|
@ -90,3 +90,5 @@ let admins = get_dirs "admin"
|
||||||
let categories = List.sort_uniq compare (get_dirs "category")
|
let categories = List.sort_uniq compare (get_dirs "category")
|
||||||
|
|
||||||
let random_state = Random.State.make_self_init ()
|
let random_state = Random.State.make_self_init ()
|
||||||
|
|
||||||
|
let () = Random.set_state random_state
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ type thread_data =
|
||||||
|
|
||||||
type post =
|
type post =
|
||||||
{ id : string
|
{ id : string
|
||||||
|
; emojid : string
|
||||||
; parent_id : string
|
; parent_id : string
|
||||||
; date : float
|
; date : float
|
||||||
; user_id : string
|
; user_id : string
|
||||||
|
|
@ -294,8 +295,10 @@ let build_reply ~comment ~image_info ~tag_list ?parent_id user_id =
|
||||||
let date = Unix.time () in
|
let date = Unix.time () in
|
||||||
let comment, citations = parse_comment comment in
|
let comment, citations = parse_comment comment in
|
||||||
let* nick = User.get_nick user_id in
|
let* nick = User.get_nick user_id in
|
||||||
|
let* emojid = Emojid.make_emojid id in
|
||||||
let reply =
|
let reply =
|
||||||
{ id
|
{ id
|
||||||
|
; emojid
|
||||||
; parent_id
|
; parent_id
|
||||||
; date
|
; date
|
||||||
; user_id
|
; user_id
|
||||||
|
|
@ -353,6 +356,7 @@ let make_post ~comment ?image_input ~tags ~op_or_reply_data user_id =
|
||||||
let post_exist id = Result.is_ok (Q.get_is_post id)
|
let post_exist id = Result.is_ok (Q.get_is_post id)
|
||||||
|
|
||||||
let get_post id =
|
let get_post id =
|
||||||
|
let* emojid = Emojid.Q.get_emojid id in
|
||||||
let* parent_id = Q.get_post_thread id in
|
let* parent_id = Q.get_post_thread id in
|
||||||
let* user_id = Q.get_post_user_id id in
|
let* user_id = Q.get_post_user_id id in
|
||||||
let* nick = User.get_nick user_id in
|
let* nick = User.get_nick user_id in
|
||||||
|
|
@ -365,6 +369,7 @@ let get_post id =
|
||||||
let* citations = Q.get_post_citations id in
|
let* citations = Q.get_post_citations id in
|
||||||
let reply =
|
let reply =
|
||||||
{ id
|
{ id
|
||||||
|
; emojid
|
||||||
; parent_id
|
; parent_id
|
||||||
; date
|
; date
|
||||||
; user_id
|
; user_id
|
||||||
|
|
|
||||||
4991
src/content/emoji-test.txt
Normal file
4991
src/content/emoji-test.txt
Normal file
File diff suppressed because it is too large
Load diff
2
src/dune
2
src/dune
|
|
@ -10,6 +10,7 @@
|
||||||
delete_page
|
delete_page
|
||||||
discuss
|
discuss
|
||||||
image
|
image
|
||||||
|
emojid
|
||||||
login
|
login
|
||||||
permap
|
permap
|
||||||
pp_babillard
|
pp_babillard
|
||||||
|
|
@ -33,6 +34,7 @@
|
||||||
directories
|
directories
|
||||||
dream
|
dream
|
||||||
emile
|
emile
|
||||||
|
emoji
|
||||||
fpath
|
fpath
|
||||||
lambdasoup
|
lambdasoup
|
||||||
omd
|
omd
|
||||||
|
|
|
||||||
71
src/emojid.ml
Normal file
71
src/emojid.ml
Normal file
|
|
@ -0,0 +1,71 @@
|
||||||
|
open Syntax
|
||||||
|
open Caqti_request.Infix
|
||||||
|
open Caqti_type
|
||||||
|
|
||||||
|
module Q = struct
|
||||||
|
let upload_emojid =
|
||||||
|
Db.exec
|
||||||
|
@@ (tup2 string string ->. unit) "INSERT INTO uuid_emojid VALUES (?,?)"
|
||||||
|
|
||||||
|
let get_emojid =
|
||||||
|
Db.find @@ (string ->! string) "SELECT emojid FROM uuid_emojid WHERE uuid=?"
|
||||||
|
|
||||||
|
let is_free =
|
||||||
|
Db.find
|
||||||
|
@@ (string ->! int)
|
||||||
|
"SELECT EXISTS(SELECT 1 FROM uuid_emojid WHERE emojid=?)"
|
||||||
|
end
|
||||||
|
|
||||||
|
let () =
|
||||||
|
let tables =
|
||||||
|
[| (unit ->. unit)
|
||||||
|
"CREATE TABLE IF NOT EXISTS uuid_emojid (uuid TEXT, emojid TEXT)"
|
||||||
|
|]
|
||||||
|
in
|
||||||
|
if
|
||||||
|
Array.exists Result.is_error
|
||||||
|
(Array.map (fun query -> Db.exec query ()) tables)
|
||||||
|
then Dream.error (fun log -> log "can't create emojid's tables")
|
||||||
|
|
||||||
|
let max_emojid_lenght = 16
|
||||||
|
|
||||||
|
let emojis =
|
||||||
|
Array.append Emoji.category_animals_and_nature Emoji.category_food_and_drink
|
||||||
|
|
||||||
|
let is_free emojid =
|
||||||
|
let* is_free = Q.is_free emojid in
|
||||||
|
Ok (is_free = 0)
|
||||||
|
|
||||||
|
let upload_emojid uuid emojid =
|
||||||
|
let* is_free = is_free emojid in
|
||||||
|
if is_free then
|
||||||
|
let* () = Q.upload_emojid (uuid, emojid) in
|
||||||
|
Ok ()
|
||||||
|
else Error "Invalid emojid: already taken"
|
||||||
|
|
||||||
|
let make_emojid uuid =
|
||||||
|
(* pick a list of emojis *)
|
||||||
|
let random_emojis =
|
||||||
|
Array.init max_emojid_lenght (fun _i ->
|
||||||
|
let n = Random.int (Array.length emojis) in
|
||||||
|
Array.get emojis n )
|
||||||
|
in
|
||||||
|
(* pick the smallest emojid possible *)
|
||||||
|
let* emojid, is_emojid =
|
||||||
|
Array.fold_left
|
||||||
|
(fun acc emoji ->
|
||||||
|
match acc with
|
||||||
|
| Error e -> Error e
|
||||||
|
| Ok (s, is_emojid) ->
|
||||||
|
if is_emojid then Ok (s, is_emojid)
|
||||||
|
else
|
||||||
|
let s = s ^ emoji in
|
||||||
|
let* is_emojid = is_free s in
|
||||||
|
Ok (s, is_emojid) )
|
||||||
|
(Ok ("", false))
|
||||||
|
random_emojis
|
||||||
|
in
|
||||||
|
if is_emojid then
|
||||||
|
let* () = upload_emojid uuid emojid in
|
||||||
|
Ok emojid
|
||||||
|
else Error "couldn't find a free emojid"
|
||||||
|
|
@ -1,16 +1,17 @@
|
||||||
let log = Format.printf
|
let log = Format.printf
|
||||||
|
|
||||||
(* called by clicking post_id *)
|
(* called by clicking post_id *)
|
||||||
(* insert id into reply form *)
|
(* insert emojid into reply form *)
|
||||||
let insert_quote post_id _event =
|
let insert_quote emojid _event =
|
||||||
log "quote@\n";
|
log "quote@\n";
|
||||||
Option.iter
|
Option.iter
|
||||||
(fun textarea ->
|
(fun textarea ->
|
||||||
let content = Jv.to_string @@ Jv.get textarea "value" in
|
let content = Jv.to_string @@ Jv.get textarea "value" in
|
||||||
let new_content =
|
let new_content =
|
||||||
if String.ends_with ~suffix:"\n" content || String.length content = 0
|
if String.ends_with ~suffix:"\n" content || String.length content = 0
|
||||||
then Format.sprintf "%s>>%s " content post_id
|
then (* don't skip a line *)
|
||||||
else Format.sprintf "%s@\n>>%s " content post_id
|
Format.sprintf "%s[%s] " content emojid
|
||||||
|
else Format.sprintf "%s@\n[%s] " content emojid
|
||||||
in
|
in
|
||||||
ignore @@ Jv.set textarea "value" (Jv.of_string new_content) )
|
ignore @@ Jv.set textarea "value" (Jv.of_string new_content) )
|
||||||
Jv.(find global "reply-comment")
|
Jv.(find global "reply-comment")
|
||||||
|
|
@ -24,13 +25,13 @@ let () =
|
||||||
in
|
in
|
||||||
log "quote_links leng %d@\n" (List.length quote_links);
|
log "quote_links leng %d@\n" (List.length quote_links);
|
||||||
let add_click quote_link =
|
let add_click quote_link =
|
||||||
let post_id =
|
let emojid =
|
||||||
Jv.to_string
|
Jv.to_string
|
||||||
@@ Jv.call quote_link "getAttribute" [| Jv.of_string "data-id" |]
|
@@ Jv.call quote_link "getAttribute" [| Jv.of_string "data-emojid" |]
|
||||||
in
|
in
|
||||||
ignore
|
ignore
|
||||||
@@ Jv.call quote_link "addEventListener"
|
@@ Jv.call quote_link "addEventListener"
|
||||||
[| Jv.of_string "click"; Jv.repr (insert_quote post_id) |]
|
[| Jv.of_string "click"; Jv.repr (insert_quote emojid) |]
|
||||||
in
|
in
|
||||||
List.iter add_click quote_links
|
List.iter add_click quote_links
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ let pp_post fmt t =
|
||||||
| Post post -> (None, post)
|
| Post post -> (None, post)
|
||||||
in
|
in
|
||||||
let { id
|
let { id
|
||||||
|
; emojid
|
||||||
; parent_id = _parent_id
|
; parent_id = _parent_id
|
||||||
; date
|
; date
|
||||||
; user_id
|
; user_id
|
||||||
|
|
@ -68,11 +69,11 @@ let pp_post fmt t =
|
||||||
Format.fprintf fmt
|
Format.fprintf fmt
|
||||||
{|
|
{|
|
||||||
<span class=postNo>
|
<span class=postNo>
|
||||||
<button data-id="%s" class="quote-link" title="Reply to this post">%s</button>
|
<button data-id="%s" data-emojid="%s" class="quote-link" title="Reply to this post">%s</button>
|
||||||
</span>
|
</span>
|
||||||
%a
|
%a
|
||||||
|}
|
|}
|
||||||
id id replies_view ()
|
id emojid emojid replies_view ()
|
||||||
in
|
in
|
||||||
|
|
||||||
let post_info_view fmt () =
|
let post_info_view fmt () =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue