-refreshMessage(Action) ->\r
- simple_xml_to_string(
- case {
- xmerl_xpath:string("nombreMessage", Action), % le nombre de message qu'affiche le client
- xmerl_xpath:string("page", Action) % la page désiré, la première (les derniers messages) étant la 1
- } of
- {
- [#xmlElement{content = [#xmlText{value = Nb_message_str}]}],
- [#xmlElement{content = [#xmlText{value = Page_str}]}]
- } ->
- Nb_message = list_to_integer(Nb_message_str),
- Page = list_to_integer(Page_str),
- Dernier_id = case xmerl_xpath:string("dernierMessageId", Action) of % l'id du dernier message que connait le client
- [#xmlElement{content = [#xmlText{value = D}]}] -> erlang:list_to_integer(D, 36);
- _ -> 0
- end,
- User = case xmerl_xpath:string("cookie", Action) of
- [#xmlElement{content = [#xmlText{value = Cookie}]}] ->
- case euphorik_minichat:get_user_by_cookie(Cookie) of
- {ok, U} -> U;
- _ -> inconnu
- end;
- _ -> inconnu
- end,\r
- % accrochez-vous ca va siouxer ;)\r
- Mess = lists:map(\r
- fun(Mess) ->\r
- Est_proprietaire = User =/= inconnu andalso User#user.id =:= Mess#minichat.auteur_id,\r
- A_repondu_a_message = User =/= inconnu andalso euphorik_minichat:a_repondu_a_message(User#user.id, Mess#minichat.id),\r
- Est_une_reponse_a_user = User =/= inconnu andalso euphorik_minichat: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:get_user_by_id(Mess#minichat.auteur_id),
- U2
- end, \r
- {message, [{id, erlang:integer_to_list(Mess#minichat.id, 36)}],\r
- [\r
- {date, [], [format_date(Mess#minichat.date)]},
- {systeme, [], [atom_to_list(Mess#minichat.auteur_id =:= 0)]},\r
- {proprietaire, [], [atom_to_list(Est_proprietaire)]},\r
- {repondu, [], [atom_to_list(A_repondu_a_message)]},\r
- {reponse, [], [atom_to_list(Est_une_reponse_a_user)]},\r
- {pseudo, [], [Mess#minichat.pseudo]},
- {login, [], [if User_mess =:= inconnu -> Mess#minichat.pseudo; true -> User_mess#user.login end]},\r
- {contenu, [], [Mess#minichat.contenu]},\r
- {repondA, [], xml_repond_a(Mess#minichat.id)}\r
- ]\r
- }\r
- end,\r
- get_dernieres_messages(Dernier_id, Nb_message, Page)\r
- ),
- [{reponse, [{name, "refreshMessages"}],\r
- % la fonction get_nb_page DOIT être évalué après get_dernieres_messages (merci les effets de bord)
- [{nbPage, [], [integer_to_list(euphorik_minichat:get_nb_page(Nb_message))]} | Mess]
- }];
- _ ->
- [{reponse, [{name, "refreshMessages"}], [{erreur, [], ["erreur"]}]}]
- end\r
- ).
+% 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,
+ {value, {_, Message_count}} = lists:keysearch(message_count, 1, Data),
+ Main_page = case lists:keysearch(main_page, 1, Data) of {value, {_, P}} -> P; _ -> 1 end,
+ {value, {_, {array, Conversations_json}}} = lists:keysearch(conversations, 1, Data),
+ Conversations = lists:map(fun({struct, [{racine, Racine}, {page, Page}]}) -> {Racine, Page} end, Conversations_json),
+ User = case euphorik_minichat:user_by_cookie(Cookie) of\r
+ {ok, U} -> U;\r
+ _ -> inconnu\r
+ end,\r
+ {struct, [\r
+ {reply, "new_message"},\r
+ {conversations, {array,
+ % accrochez-vous ca va siouxer ;)
+ lists:map(
+ fun({Conv, Plus}) ->
+ {struct, [
+ {last_page, not Plus},
+ {messages, {array,
+ 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]),
+ User_mess =
+ if Mess#minichat.auteur_id =:= 0 ->
+ inconnu;
+ true ->
+ {ok, U2} = euphorik_minichat:user_by_id(Mess#minichat.auteur_id),
+ U2
+ end,
+ {struct, [
+ {id, Mess#minichat.id},
+ {date, format_date(Mess#minichat.date)},
+ {system, Mess#minichat.auteur_id =:= 0},
+ {owner, Est_proprietaire},
+ {answered, A_repondu_a_message},
+ {is_a_reply, Est_une_reponse_a_user},
+ {nick, Mess#minichat.pseudo},
+ {login, if User_mess =:= inconnu -> Mess#minichat.pseudo; true -> User_mess#user.login end},
+ {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),
+ {struct, [{id, M#minichat.id}, {nick, M#minichat.pseudo}, {login, User_reponse#user.login}]}
+ end,
+ Repond_a
+ )}}
+ ]}
+ end,
+ Conv
+ )
+ }}
+ ]}
+ end,
+ euphorik_minichat_conversation:conversations(
+ Conversations,
+ Message_count,
+ Last_message_id,
+ Main_page
+ )
+ )\r
+ }}\r
+ ]}.
+