2
2
3
3
namespace SpamDetector ;
4
4
5
+ use DateTime ;
5
6
use Mustache ;
6
7
use MapasCulturais \i ;
7
8
use MapasCulturais \App ;
@@ -64,16 +65,27 @@ public function _init()
64
65
$ plugin = $ this ;
65
66
66
67
$ hooks = implode ('| ' , $ plugin ->config ['entities ' ]);
68
+ $ last_spam_sent = null ;
67
69
68
70
// 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 ) {
70
72
/** @var Entity $this */
71
73
$ users = $ plugin ->getAdminUsers ($ this );
72
74
$ terms = array_merge ($ plugin ->config ['termsBlock ' ], $ plugin ->config ['terms ' ]);
73
75
74
76
$ 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
+ ) {
75
88
76
- if ($ spam_terms ) {
77
89
foreach ($ users as $ user ) {
78
90
$ plugin ->createNotification ($ user ->profile , $ this , $ spam_terms );
79
91
}
@@ -97,9 +109,13 @@ public function _init()
97
109
});
98
110
99
111
// 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 ) {
101
113
/** @var Entity $this */
102
114
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 );
103
119
$ result = false ;
104
120
}
105
121
});
0 commit comments