+ end.
+
+
+% Attent un événement concernant la page admin
+% Renvoie les trolls manquants posté après Last_id ou banned_ips_refresh.
+% 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 banned_ips_refresh
+% ou timeout
+wait_event_page_admin(Last_id) ->
+ case {mnesia:subscribe({table, troll, detailed}), mnesia:subscribe({table, ip_table, detailed})} of
+ {{error, E}, _ } -> E;
+ {_, {error, E}} -> E;
+ _ ->
+ R = case euphorik_bd:trolls(Last_id) of
+ [] -> % pas de trolls
+ wait_event_page_admin();
+ Trolls ->
+ {add, Trolls}
+ end,
+ mnesia:unsubscribe({table, troll, detailed}),
+ mnesia:unsubscribe({table, ip_table, detailed}),
+ R
+ end.
+
+wait_event_page_admin() ->
+ % s'il n'y a pas de trolls que l'utilisateur n'a pas connaissance alors on attend un événement
+ receive
+ % cas où un troll est choisit comme courant
+ {mnesia_table_event, {write, troll, Troll, [Old_troll | _], _}}
+ when Old_troll#troll.date_post =:= undefined, Troll#troll.date_post =/= undefined ->
+ {del, Troll#troll.id};
+ {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};
+ {mnesia_table_event, {write, ip_table, IP, [Old_IP | _], _}}
+ when Old_IP#ip_table.ban =/= IP#ip_table.ban; Old_IP#ip_table.ban_duration =/= IP#ip_table.ban_duration ->
+ banned_ips_refresh;
+ {tcp_closed, _} ->
+ exit(normal);
+ _ ->
+ wait_event_page_admin()
+ % 60 minutes de timeout (on ne sais jamais)
+ % Après 60 minutes de connexion, le client doit donc reétablir une connexion
+ after 1000 * 60 * 60 ->
+ timeout
+ end.