Skip to content

Commit 9f48d66

Browse files
committed
Ajuste disparo de e-mail para recebimento a cada 24h para cada entidade Ref.: #84
1 parent 582cb07 commit 9f48d66

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

Plugin.php

+19-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SpamDetector;
44

5+
use DateTime;
56
use Mustache;
67
use MapasCulturais\i;
78
use MapasCulturais\App;
@@ -64,16 +65,27 @@ public function _init()
6465
$plugin = $this;
6566

6667
$hooks = implode('|', $plugin->config['entities']);
68+
$last_spam_sent = null;
6769

6870
// Verifica se existem termos maliciosos e dispara o e-mail e a notificação
69-
$app->hook("entity(<<{$hooks}>>).save:after", function () use ($plugin, $app) {
71+
$app->hook("entity(<<{$hooks}>>).save:after", function () use ($plugin, $last_spam_sent) {
7072
/** @var Entity $this */
7173
$users = $plugin->getAdminUsers($this);
7274
$terms = array_merge($plugin->config['termsBlock'], $plugin->config['terms']);
7375

7476
$spam_terms = $plugin->getSpamTerms($this, $terms);
77+
$current_date_time = new DateTime();
78+
$current_timestamp = $current_date_time->getTimestamp();
79+
$eligible_spam = $last_spam_sent ?? $this->spam_sent_email;
80+
81+
if (
82+
$spam_terms &&
83+
(
84+
!$eligible_spam ||
85+
($current_timestamp - $eligible_spam->getTimestamp()) >= 86400
86+
)
87+
) {
7588

76-
if ($spam_terms) {
7789
foreach ($users as $user) {
7890
$plugin->createNotification($user->profile, $this, $spam_terms);
7991
}
@@ -97,9 +109,13 @@ public function _init()
97109
});
98110

99111
// remove a permissão de publicar caso encontre termos que estão na lista de termos elegível a bloqueio
100-
$app->hook("entity(<<{$hooks}>>).canUser(publish)", function ($user, &$result) use($plugin, $app) {
112+
$app->hook("entity(<<{$hooks}>>).canUser(publish)", function ($user, &$result) use($plugin, &$last_spam_sent) {
101113
/** @var Entity $this */
102114
if($plugin->getSpamTerms($this, $plugin->config['termsBlock']) && !$user->is('admin')) {
115+
$last_spam_sent = $this->spam_sent_email ?? null;
116+
$this->spam_sent_email = new DateTime();
117+
$this->spam_sent_email->add(new \DateInterval('PT10S'));
118+
$this->save(true);
103119
$result = false;
104120
}
105121
});

0 commit comments

Comments
 (0)