Skip to content

Commit

Permalink
Merge pull request #1051 from sebix/hotfix-spam-discard
Browse files Browse the repository at this point in the history
Disable spam discard and automatic user blocks
  • Loading branch information
chris34 committed Apr 7, 2019
2 parents 9aa22a0 + 73c4452 commit 400ee3c
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 56 deletions.
2 changes: 0 additions & 2 deletions inyoka/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@
INYOKA_AKISMET_KEY = None
INYOKA_AKISMET_URL = None
INYOKA_AKISMET_DEFAULT_IS_SPAM = False
INYOKA_SPAM_COUNTER_TIMEOUT = 60 * 5 # seconds
INYOKA_SPAM_COUNTER_MAX = 5
INYOKA_SPAM_DETECT_LIMIT = 100

# restrictions for user avatar images
Expand Down
2 changes: 0 additions & 2 deletions inyoka/forum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,8 +1090,6 @@ def mark_ham(self):
self.hidden = False
self.save(update_fields=['hidden'])

cache.delete(u'spam/user/{}'.format(self.author.pk))

def mark_spam(self, report=True, update_akismet=True):
if update_akismet:
mark_spam(self, self.get_text(), 'forum-post')
Expand Down
10 changes: 4 additions & 6 deletions inyoka/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,6 @@ def edit(request, forum_slug=None, topic_slug=None, post_id=None,
if 'send' in request.POST and form.is_valid():
d = form.cleaned_data

is_spam_post = form._spam and not form._spam_discard

if not post: # not when editing an existing post
doublepost = Post.objects \
.filter(author=request.user, text=d['text'],
Expand Down Expand Up @@ -678,10 +676,10 @@ def edit(request, forum_slug=None, topic_slug=None, post_id=None,

post.edit(d['text'])

if is_spam_post:
if form._spam:
post.mark_spam(report=True, update_akismet=False)

if not is_spam_post:
if not form._spam:
if newtopic:
send_newtopic_notifications(request.user, post, topic, forum)
elif not post_id:
Expand All @@ -691,10 +689,10 @@ def edit(request, forum_slug=None, topic_slug=None, post_id=None,
# page and send notifications.
page.topic = topic
page.save()
if not is_spam_post:
if not form._spam:
send_discussion_notification(request.user, page)

if not is_spam_post:
if not form._spam:
subscribed = Subscription.objects.user_subscribed(request.user, topic)
if request.user.settings.get('autosubscribe', True) and not subscribed and not post_id:
subscription = Subscription(user=request.user, content_object=topic)
Expand Down
4 changes: 2 additions & 2 deletions inyoka/locale/de_CH/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ msgid "Your signature is too long, only %(length)s characters allowed"
msgstr "Dini Signatur isch z läng, nur %(length)s Zeichä sy erloubt"

#, python-format
msgid "Your text is considered spam and needs approval from one of the administrators. Please be patient, we will get to it as soon as possible. You have %(left)d attempts left before your account will be blocked."
msgstr "Di Post isch aus SPAM markiert wordä und mues zersch vomänä Administrator beguätachtet und dürägwunkä wärdä. Bitte heb Geduld, mir luägä das so baud as müglech ah. Du hesch no %(left)d Versüech bis Di Account gsperrt wird."
msgid "Your text is considered spam and needs approval from one of the administrators. Please be patient, we will get to it as soon as possible."
msgstr "Di Post isch aus SPAM markiert wordä und mues zersch vomänä Administrator beguätachtet und dürägwunkä wärdä. Bitte heb Geduld, mir luägä das so baud as müglech ah."

msgid "counting..."
msgstr "Zeuä ..."
Expand Down
4 changes: 2 additions & 2 deletions inyoka/locale/de_DE/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ msgid "Your signature is too long, only %(length)s characters allowed"
msgstr "Deine Signatur ist zu lang. Es sind maximal %(length)s Zeichen erlaubt"

#, python-format
msgid "Your text is considered spam and needs approval from one of the administrators. Please be patient, we will get to it as soon as possible. You have %(left)d attempts left before your account will be blocked."
msgstr "Dein Beitrag wurde als Spam klassifiziert und muss von einem Moderator freigeschaltet werden. Bitte habe etwas Geduld, wir werden uns sobald wie möglich darum kümmern. Du hast %(left)d verbleibende Versuche bevor dein Konto gesperrt wird."
msgid "Your text is considered spam and needs approval from one of the administrators. Please be patient, we will get to it as soon as possible."
msgstr "Dein Beitrag wurde als Spam klassifiziert und muss von einem Moderator freigeschaltet werden. Bitte habe etwas Geduld, wir werden uns sobald wie möglich darum kümmern."

msgid "counting..."
msgstr "Zähle..."
Expand Down
2 changes: 1 addition & 1 deletion inyoka/locale/django.pot
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ msgid "Your signature is too long, only %(length)s characters allowed"
msgstr ""

#, python-format
msgid "Your text is considered spam and needs approval from one of the administrators. Please be patient, we will get to it as soon as possible. You have %(left)d attempts left before your account will be blocked."
msgid "Your text is considered spam and needs approval from one of the administrators. Please be patient, we will get to it as soon as possible."
msgstr ""

msgid "counting..."
Expand Down
41 changes: 4 additions & 37 deletions inyoka/utils/spam.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import requests
from django.conf import settings
from django.contrib import messages
from django.core.cache import cache
from django.forms import ValidationError
from django.utils.translation import ugettext_lazy as _
from inyoka.utils.logger import logger

Expand Down Expand Up @@ -140,45 +138,14 @@ def mark_spam(obj, comment_content, comment_type):
def check_form_field(form, text_field, needs_check, request, content_type):
text = form.cleaned_data.get(text_field)
form._spam, form._spam_discard = False, False

if needs_check:
form._spam, form._spam_discard = is_spam(text, content_type)
if form._spam:
attempts_left = block_user_if_spammer(request.user)
msg = _(
'Your text is considered spam and needs approval from one of '
'the administrators. Please be patient, we will get to it as '
'soon as possible. You have %(left)d attempts left before your '
'account will be blocked.'
) % {
'left': attempts_left,
}
if form._spam_discard:
raise ValidationError(msg)
else:
messages.info(request, msg)
return text


def block_user_if_spammer(user):
cache_key = 'spam/user/%d' % user.pk
spam_hits = cache.get(cache_key, 0) + 1
if spam_hits >= settings.INYOKA_SPAM_COUNTER_MAX:
user.status = user.STATUS_BANNED
user.save(update_fields=['status'])
logger.info(
u'User %s (%d) hit spam counter maximum of %d. Blocked!' % (
user.username, user.pk, settings.INYOKA_SPAM_COUNTER_MAX,
)
)
else:
cache.set(
cache_key, spam_hits,
timeout=settings.INYOKA_SPAM_COUNTER_TIMEOUT
)
logger.info(
u'User %s (%d) tried to spam. Counter at %d of %d.' % (
user.username, user.pk, spam_hits,
settings.INYOKA_SPAM_COUNTER_MAX
'soon as possible.'
)
)
return settings.INYOKA_SPAM_COUNTER_MAX - spam_hits
messages.info(request, msg)
return text
6 changes: 2 additions & 4 deletions tests/apps/forum/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,7 @@ def test_newtopic_user_spam(self):
response = self.client.get('/topic/newpost-title/')
self.assertInHTML('<div class="message info">Your text is considered spam '
'and needs approval from one of the administrators. '
'Please be patient, we will get to it as soon as possible. '
'You have 4 attempts left before your account will be blocked.</div>',
'Please be patient, we will get to it as soon as possible.</div>',
response.content, count=1)
self.assertInHTML('<div class="error"><p>You do not have permissions to access this page.</p></div>',
response.content, count=1)
Expand Down Expand Up @@ -891,8 +890,7 @@ def test_new_post_user_spam(self):
response = self.client.get('/topic/%s/' % topic.slug)
self.assertInHTML('<div class="message info">Your text is considered spam '
'and needs approval from one of the administrators. '
'Please be patient, we will get to it as soon as possible. '
'You have 4 attempts left before your account will be blocked.</div>',
'Please be patient, we will get to it as soon as possible. </div>',
response.content, count=1)
self.assertInHTML('<div class="text"><p>newpost text</p></div>', response.content, count=1)

Expand Down

0 comments on commit 400ee3c

Please sign in to comment.