X-Git-Url: http://git.euphorik.ch/?p=euphorik.git;a=blobdiff_plain;f=modules%2Ferl%2Feuphorik_minichat_conversation.erl;h=2db3586bcd58ccbee86216349592fb7278edfb4c;hp=1e76e409abfa8e20c3f5a8c1b60a08f0702e7b59;hb=08193845f5b45402b85977911dbd0693a4d183de;hpb=221a44d7aeaf467ef9854fe21af93bc964e92ef1 diff --git a/modules/erl/euphorik_minichat_conversation.erl b/modules/erl/euphorik_minichat_conversation.erl index 1e76e40..2db3586 100755 --- a/modules/erl/euphorik_minichat_conversation.erl +++ b/modules/erl/euphorik_minichat_conversation.erl @@ -1,4 +1,21 @@ -% coding: utf-8 +% coding: utf-8 +% Copyright 2008 Grégory Burri +% +% This file is part of Euphorik. +% +% Euphorik is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% Euphorik is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with Euphorik. If not, see . +% % Ce module permet la gestion des conversations du minichat d'euphorik. % Un message (enfant) peut répondre à des messages (ses parents). % Un message (parent) peut avoir plusieurs réponses (enfants) @@ -7,6 +24,7 @@ % @type Message() = {integer(), [integer()]} % @type Conversation_detailee() = {[integer()], [integer()], [integer()], bool()} % @type Conversation = {[Message()] , bool()} bool() : si true alors il y a encore des messages dans les pages suivantes. + -module(euphorik_minichat_conversation). -export([ @@ -57,19 +75,23 @@ mise_en_forme_conversations([{Principale, Plus_principale} | Conversations]) -> % Ajoute les parents de chaque message. % @spec mise_en_forme_conversation([integer()]) -> [{integer(), [integer()]}] mise_en_forme_conversation(Messages) -> - lists:foldr( - fun(Id, Acc) -> - case euphorik_bd:message_by_id(Id) of - {ok, Message} -> - [{Message, parents(Id)} | Acc]; - _ -> - Acc - end - end, - [], - Messages - ). - + resultat_transaction(mnesia:transaction( + fun() -> + lists:foldr( + fun(Id, Acc) -> + case euphorik_bd:message_by_id_sans_transaction(Id) of + {ok, Message} -> + [{Message, parents(Id)} | Acc]; + _ -> + Acc + end + end, + [], + Messages + ) + end + )). + % Renvoie une liste de conversations, le première élément correspond à la conversation principale. % Les autres éléments sont des tuples {C, Cn, X}, voir conversation/4 pour plus d'infos. @@ -159,16 +181,16 @@ conversation(R, N, D, P) -> % Renvoie un tuple {C, X} où C est la conversation complète et X les messages répondant à des mess qui ne font pas partie de la conversation % Attention : les messages de C et de X sont ordrés du plus grand Id au plus petit. % @spec conversation([integer()], [integer()], [integer()]) -> {} -conversation(Messages, [M | Reste], X) -> - Est_deja_traite = any(fun(E) -> E =:= M end, Messages), +conversation(Conv, [M | Reste], X) -> + Est_deja_traite = any(fun(E) -> E =:= M end, Conv), if Est_deja_traite -> - conversation(Messages, Reste, X); + conversation(Conv, Reste, X); true -> Enfants = enfants(M), Parents = parents(M), % un message est dit externe si un de ses parent ne fait pas partie de la conversation ou si un de ses parents fait partie de X - Est_message_externe = Parents -- Messages =/= [] orelse intersection(Parents, X) =/= [], - conversation([M | Messages], lists:merge(Reste, Enfants), if Est_message_externe -> [M | X]; true -> X end) + Est_message_externe = Parents -- Conv =/= [] orelse intersection(Parents, X) =/= [], + conversation([M | Conv], lists:merge(Reste, Enfants), if Est_message_externe -> [M | X]; true -> X end) end; conversation(Messages, [], X) -> {Messages, X}. @@ -179,7 +201,12 @@ conversation(Messages, [], X) -> % @spec enfants(integer()) -> [integer()] enfants(M) -> resultat_transaction(transaction(fun() -> - e(q([E#reponse_minichat.repondant || E <- qlc:sort(table(reponse_minichat), [{order, ascending}]), E#reponse_minichat.cible =:= M])) + e( + qlc:sort( + q([E#reponse_minichat.repondant || E <- table(reponse_minichat), E#reponse_minichat.cible =:= M]), + [{order, ascending}] + ) + ) end)). @@ -188,7 +215,12 @@ enfants(M) -> % @spec parents(integer()) -> [integer()] parents(M) -> resultat_transaction(transaction(fun() -> - e(q([E#reponse_minichat.cible || E <- keysort(1, table(reponse_minichat), [{order, ascending}]), E#reponse_minichat.repondant =:= M])) + e( + qlc:sort( + q([E#reponse_minichat.cible || E <- table(reponse_minichat), E#reponse_minichat.repondant =:= M]), + [{order, ascending}] + ) + ) end)).