ADD trolls, fin coté serveur et début coté client
[euphorik.git] / modules / erl / euphorik_bd.erl
index eb6f1d4..7a9b582 100755 (executable)
    est_banni/1,
    can_register/1,
    
+   % trolls :
+   trolls/0,
+   trolls/1,
+   put_troll/2,
+   mod_troll/2,
+   del_troll/1,
+   
    % versions :
    update_version/1,
    
@@ -119,6 +126,7 @@ reset() ->
    mnesia:clear_table(user),\r
    mnesia:clear_table(reponse_minichat),\r
    mnesia:clear_table(minichat),
+   mnesia:clear_table(troll),
    mnesia:clear_table(ip_table),\r
    % crée l'utilisateur root\r
    mnesia:transaction(fun() ->\r
@@ -333,7 +341,7 @@ nouveau_message(Mess, Auteur_id, Repond_A) ->
       % est-ce que l'auteur à trop floodé ?
       if Auteur#user.indice_flood =/= ?INDICE_SPAM_MAX, Auteur_maj#user.indice_flood =:= ?INDICE_SPAM_MAX, Delta =< ?DUREE_BLOCAGE_SPAM ->
          mnesia:write(Auteur#user{indice_flood = Auteur_maj#user.indice_flood}),
-         nouveau_message_sys(Auteur#user.pseudo ++ "(" ++ Auteur#user.login ++ ") est bloqué pour " ++ integer_to_list(trunc(?DUREE_BLOCAGE_SPAM / 1000)) ++ " secondes pour cause de flood..");
+         nouveau_message_sys(Auteur#user.pseudo ++ if Auteur#user.login =/= [] -> " (" ++ Auteur#user.login ++ ")"; true -> "" end ++ " est bloqué pour " ++ integer_to_list(trunc(?DUREE_BLOCAGE_SPAM / 1000)) ++ " secondes pour cause de flood.");
       Auteur#user.indice_flood =:= ?INDICE_SPAM_MAX, Delta =< ?DUREE_BLOCAGE_SPAM ->
          erreur;
       true ->     
@@ -574,6 +582,121 @@ can_register(IP) ->
       end
    )).
    
+
+% Renvoie tous les trolls.
+trolls() ->
+   resultat_transaction(mnesia:transaction(
+      fun() ->
+         qlc:e(qlc:q([T || T <- mnesia:table(troll)]))
+      end
+   )).
+   
+% Renvoie les trolls manquants posté après Last_id.
+% Si pas de trolls alors attend un événement tel qu'un ajout, une modification ou une suppression.
+% renvoie :
+%  {mod, Troll}
+% ou {add, [Trolls]}
+% ou {del, Troll_id}
+% ou timeout
+trolls(Last_id) ->
+   case mnesia:subscribe({table, troll, detailed}) of
+      {error, E} = E ->
+         E;
+      _ ->
+         case resultat_transaction(mnesia:transaction(
+               fun() ->
+                  qlc:e(qlc:q([T || T <- mnesia:table(troll), T#troll.id > Last_id, T#troll.date_post =:= undefined]))
+               end
+            )) of
+               [] -> % pas de trolls
+                  attend_evenement_troll();
+               Trolls ->
+                  {add, Trolls}
+         end
+   end.
+   
+attend_evenement_troll() ->
+   % s'il n'y a pas de trolls que l'utilisateur n'a pas connaissance alors on attend un événement
+   receive
+      {mnesia_table_event, {write, troll, Troll, [_Old_troll | _], _}} ->
+         {mod, Troll};
+      {mnesia_table_event, {write, troll, Troll, [], _}} ->
+         {add, [Troll]};
+      {mnesia_table_event, {delete, troll, {troll, Id}, _, _}} ->
+         {del, Id};
+      _ ->
+         attend_evenement_troll()
+   after 1000 * 60 * 60 -> 
+      timeout
+   end.
+   
+ % Renvoie l'id du nouveau troll
+ % ou max_troll_reached_per_user si le nombre de troll posté par l'utilisateur max a été atteind
+ % ou max_troll_reached si le nombre de troll posté max a été atteind
+ % ou user_unknown
+put_troll(User_id, Content) ->
+   resultat_transaction(mnesia:transaction(
+      fun() ->
+         % control le nombre de troll déjà posté
+         Nb_troll_poste_par_user = length(qlc:e(qlc:q(
+            [
+               E#troll.id || E <- mnesia:table(troll),
+               E#troll.id_user =:= User_id,
+               E#troll.date_post =:= undefined
+            ]
+         ))),
+         Nb_troll_poste_total = length(qlc:e(qlc:q(
+            [
+               E#troll.id || E <- mnesia:table(troll),
+               E#troll.date_post =:= undefined
+            ]
+         ))),
+         User = user_by_id(User_id),
+         case User of
+            {ok, _} ->
+               if Nb_troll_poste_par_user >= ?NB_MAX_TROLL_WAITING_BY_USER ->
+                     max_troll_reached_per_user;
+                  Nb_troll_poste_total >= ?NB_MAX_TROLL_WAITING ->
+                     max_troll_reached;
+                  true ->
+                     Id = nouvel_id(minichat),
+                     mnesia:write(#troll{id = Id, id_user = User_id, date_create = now(), content = Content}),
+                     Id
+               end;
+            _ ->
+               user_unknown
+         end
+      end
+   )).
+
+
+% renvoie ok | erreur
+mod_troll(Troll_id, Content) ->
+   mnesia:transaction(
+      fun() ->
+         case mnesia:wread({troll, Troll_id}) of
+            [Troll = #troll{date_post = undefined}] ->
+               mnesia:write(Troll#troll{content = Content});
+            _ ->
+               mnesia:abort("mod_troll: Troll inconnu ou déjà posté")
+          end
+      end
+   ).
+   
+   
+del_troll(Troll_id) ->
+   mnesia:transaction(
+      fun() ->
+         case mnesia:wread({troll, Troll_id}) of
+            [#troll{date_post = undefined}] ->
+               mnesia:delete({troll, Troll_id});
+            _ ->
+               mnesia:abort("mod_troll: Troll inconnu ou déjà posté")
+          end
+      end
+   ).
    
 update_version(1) ->
    mnesia:transform_table(