-include_lib("stdlib/include/qlc.hrl").
-import(lists, [reverse/1, any/2, map/2, sublist/3, filter/2]).
--import(euphorik_minichat, [resultat_transaction/1]).
+-import(euphorik_bd, [resultat_transaction/1]).
-import(qlc, [e/1, q/1, keysort/3]).
-import(mnesia, [table/1, transaction/1]).
_ ->
% demande des conversations
Conversations = conversations_detailees(Racines, N, D, P),
- %io:format("Conversations : ~p~n", [Conversations]),
% si les conversations sont vides alors on attend un nouveau message
Vide = not any(
fun(C) ->
Conversations_mises_en_forme = mise_en_forme_conversations(
if Vide ->
attend_nouveau_message(),
- %io:format("Racines = ~p, N = ~p, D = ~p, P = ~p~n", [Racines, N, D, P]),
- %io:format("Conversations = ~p~n", [conversations_detailees(Racines, N, D, P)]),
conversations_detailees(Racines, N, D, P);
true ->
Conversations
end
),
- %io:format("Conversations_mises_en_forme = ~p~n", [Conversations_mises_en_forme]),
unsubscribe(minichat),
Conversations_mises_en_forme
end.
mise_en_forme_conversation(Messages) ->
lists:foldr(
fun(Id, Acc) ->
- case euphorik_minichat:message_by_id(Id) of
+ case euphorik_bd:message_by_id(Id) of
{ok, Message} ->
[{Message, parents(Id)} | Acc];
_ ->
% Une utilisateur s'enregistre avec un tuple {Login, Password}.
register([{login, Login}, {password, Password}], IP) ->
- Can_register = euphorik_minichat:can_register(IP),
+ Can_register = euphorik_bd:can_register(IP),
if Can_register ->
- case euphorik_minichat:user_by_login(Login) of
+ case euphorik_bd:user_by_login(Login) of
{ok, _} ->
erreur("Login déjà existant");
_ ->
- User = euphorik_minichat:nouveau_user(Login, Password, generer_cookie()),
+ User = euphorik_bd:nouveau_user(Login, Password, generer_cookie()),
json_reponse_login_ok(User)
end;
true ->
end;
% Enregistrement sans {Login, Password}
register([], IP) ->
- Can_register = euphorik_minichat:can_register(IP),
+ Can_register = euphorik_bd:can_register(IP),
if Can_register ->
- User = euphorik_minichat:nouveau_user("<nick>", generer_cookie()),
+ User = euphorik_bd:nouveau_user("<nick>", generer_cookie()),
json_reponse_login_ok(User);
true ->
erreur_register_flood()
erreur_register_flood() ->
erreur("Trop de register (flood)").
-
\r
% Un utilisateur se logge (avec un couple {login, mot de passe})
login([{login, Login}, {password, Password}]) ->
- loginUser(euphorik_minichat:user_by_login_password(Login, Password));
+ loginUser(euphorik_bd:user_by_login_password(Login, Password));
% Un utilisateur se logge (avec un cookie)
login([{cookie, Cookie}]) ->
- loginUser(euphorik_minichat:user_by_cookie(Cookie)).
+ loginUser(euphorik_bd:user_by_cookie(Cookie)).
loginUser({ok, User}) ->
- euphorik_minichat:update_date_derniere_connexion(User#user.id),
+ euphorik_bd:update_date_derniere_connexion(User#user.id),
json_reponse_login_ok(User);
loginUser(_) ->
% ajoute un délais d'attente (TODO : un autre moyen plus élégant ?)
% est-ce que les messages auquel on répond existent ?
Conversations = lists:foldr(
fun({struct, [{root, Root}, {page, Page}]}, Acc) ->
- Message_existe = euphorik_minichat:message_existe(Root),
+ Message_existe = euphorik_bd:message_existe(Root),
if Message_existe ->
[{Root, Page} | Acc];
true ->
[],
Conversations_json
),
- case euphorik_minichat:set_profile(Cookie, Login, Password, Pseudo, Email, Css, list_to_atom(Nick_format_str), Main_page, Conversations) of
+ case euphorik_bd:set_profile(Cookie, Login, Password, Pseudo, Email, Css, list_to_atom(Nick_format_str), Main_page, Conversations) of
ok ->
json_reponse_ok();
login_deja_pris ->
% Renvoie les messages appropriés.
% last_message id et cookie sont facultatifs
-% TODO : erreur : {badmatch,false}
wait_event(Data) ->
Cookie = case lists:keysearch(cookie, 1, Data) of {value, {_, C}} -> C; _ -> inconnu end,
Last_message_id = case lists:keysearch(last_message_id, 1, Data) of {value, {_, Id}} -> Id; _ -> 0 end,
end,
Conversations_json
),
- User = case euphorik_minichat:user_by_cookie(Cookie) of\r
+ User = case euphorik_bd:user_by_cookie(Cookie) of\r
{ok, U} -> U;\r
_ -> inconnu\r
end,\r
lists:map(
fun({Mess, Repond_a}) ->
Est_proprietaire = User =/= inconnu andalso User#user.id =:= Mess#minichat.auteur_id,
- A_repondu_a_message = User =/= inconnu andalso euphorik_minichat:a_repondu_a_message(User#user.id, Mess#minichat.id),
- Est_une_reponse_a_user = User =/= inconnu andalso euphorik_minichat:est_une_reponse_a_user(User#user.id, Mess#minichat.id),
- % io:format("Repond_a : ~p~n", [Repond_a]),
+ A_repondu_a_message = User =/= inconnu andalso euphorik_bd:a_repondu_a_message(User#user.id, Mess#minichat.id),
+ Est_une_reponse_a_user = User =/= inconnu andalso euphorik_bd:est_une_reponse_a_user(User#user.id, Mess#minichat.id),
User_mess =
if Mess#minichat.auteur_id =:= 0 ->
inconnu;
true ->
- {ok, U2} = euphorik_minichat:user_by_id(Mess#minichat.auteur_id),
+ {ok, U2} = euphorik_bd:user_by_id(Mess#minichat.auteur_id),
U2
end,
{struct, [
{content, Mess#minichat.contenu},
{answer_to, {array, lists:map(
fun(Id_mess) ->
- {ok, M} = euphorik_minichat:message_by_id(Id_mess),
- {ok, User_reponse} = euphorik_minichat:user_by_mess(M#minichat.id),
+ {ok, M} = euphorik_bd:message_by_id(Id_mess),
+ {ok, User_reponse} = euphorik_bd:user_by_mess(M#minichat.id),
{struct, [{id, M#minichat.id}, {nick, M#minichat.pseudo}, {login, User_reponse#user.login}]}
end,
Repond_a
{answer_to, {array, Answer_to}}
]
) ->
- case euphorik_minichat:user_by_cookie(Cookie) of
+ case euphorik_bd:user_by_cookie(Cookie) of
{ok, User} ->
Strip_content = string:strip(Content),
if (Strip_content =:= []) ->
erreur("Message vide");
true ->
% TODO : non-atomique (update_pseudo+nouveau_message)
- euphorik_minichat:update_pseudo_user(User#user.id, Nick),
- case euphorik_minichat:nouveau_message(Strip_content, User#user.id, Answer_to) of
+ euphorik_bd:update_pseudo_user(User#user.id, Nick),
+ case euphorik_bd:nouveau_message(Strip_content, User#user.id, Answer_to) of
erreur -> erreur("Impossible d'ajouter un nouveau message");
_ ->
json_reponse_ok()
end ++\r
io_lib:format("~2.10.0B:~2.10.0B:~2.10.0B", [Heure, Minute, Seconde])
).
-
-
-%%%%%%%%% <Réponses JSON> %%%%%%%%%
+
json_reponse_ok() ->
{struct, [{reply, "ok"}]}.
}
]
}.
-
-
-%%%%%%%%% </réponses JSON> %%%%%%%%%