Skip to content

Commit

Permalink
moderation: add rule results to dict and eval
Browse files Browse the repository at this point in the history
  • Loading branch information
yashlamba committed Nov 19, 2024
1 parent 8ef8e40 commit 2d457ac
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
6 changes: 3 additions & 3 deletions invenio.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ from zenodo_rdm.github.schemas import CitationMetadataSchema
from zenodo_rdm.legacy.resources import record_serializers
from zenodo_rdm.metrics.config import METRICS_CACHE_UPDATE_INTERVAL
from zenodo_rdm.moderation.errors import UserBlockedException
from zenodo_rdm.moderation.handlers import CommunityScoreHandler, RecordScoreHandler
from zenodo_rdm.moderation.handlers import CommunityModerationHandler, RecordModerationHandler
from zenodo_rdm.openaire.records.components import OpenAIREComponent
from zenodo_rdm.permissions import (
ZenodoCommunityPermissionPolicy,
Expand Down Expand Up @@ -817,11 +817,11 @@ RDM_RECORDS_SERVICE_COMPONENTS = DefaultRecordsComponents + [
"""Addd OpenAIRE component to records service."""

RDM_CONTENT_MODERATION_HANDLERS = [
RecordScoreHandler(),
RecordModerationHandler(),
]
"""Records content moderation handlers."""
RDM_COMMUNITY_CONTENT_MODERATION_HANDLERS = [
CommunityScoreHandler(),
CommunityModerationHandler(),
]
"""Community content moderation handlers."""

Expand Down
26 changes: 13 additions & 13 deletions site/zenodo_rdm/moderation/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@
MODERATION_EXEMPT_USERS = []
"""List of users exempt from moderation."""

MODERATION_RECORD_SCORE_RULES = [
verified_user_rule,
links_rule,
files_rule,
text_sanitization_rule,
match_query_rule,
]
MODERATION_RECORD_SCORE_RULES = {
"verified_user_rule": verified_user_rule,
"links_rule": links_rule,
"files_rule": files_rule,
"text_sanitization_rule": text_sanitization_rule,
"match_query_rule": match_query_rule,
}
"""Scoring rules for record moderation."""

MODERATION_COMMUNITY_SCORE_RULES = [
links_rule,
text_sanitization_rule,
verified_user_rule,
match_query_rule,
]
MODERATION_COMMUNITY_SCORE_RULES = {
"links_rule": links_rule,
"text_sanitization_rule": text_sanitization_rule,
"verified_user_rule": verified_user_rule,
"match_query_rule": match_query_rule,
}
"""Scoring rules for communtiy moderation."""

MODERATION_PERCOLATOR_INDEX_PREFIX = "moderation-queries"
Expand Down
30 changes: 20 additions & 10 deletions site/zenodo_rdm/moderation/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from .uow import ExceptionOp


class BaseScoreHandler:
class BaseModerationHandler:
"""Base handler to calculate moderation scores based on rules."""

def __init__(self, rules=None):
Expand All @@ -56,7 +56,11 @@ def rules(self):
"""Get scoring rules."""
if isinstance(self._rules, str):
return current_app.config[self._rules]
return self._rules or []
return self._rules or {}

def evaluate_result(self, params):
"""Evaluate aggregate result based on params."""
return sum(params.values())

@property
def should_apply_actions(self):
Expand All @@ -77,17 +81,19 @@ def run(self, identity, draft=None, record=None, user=None, uow=None):
)
return

score = 0
for rule in self.rules:
score += rule(identity, draft=draft, record=record)
results = {}
for name, rule in self.rules.items():
results[name] = rule(identity, draft=draft, record=record)

action_ctx = {
"user_id": user.id,
"record_pid": record.pid.pid_value,
"score": score,
"results": results,
}
current_app.logger.debug("Moderation score calculated", extra=action_ctx)
if score > current_scores.spam_threshold:

evaluation = self.evaluate_result(results)
if evaluation > current_scores.spam_threshold:
action_ctx["action"] = "block"
if self.should_apply_actions:
# If user is verified, we need to (re)open the moderation
Expand All @@ -102,9 +108,11 @@ def run(self, identity, draft=None, record=None, user=None, uow=None):
"Block moderation action triggered",
extra=action_ctx,
)
elif score < current_scores.ham_threshold:

elif evaluation < current_scores.ham_threshold:
action_ctx["action"] = "approve"

# If the user is already verified, we don't need to verify again
if user.verified:
current_app.logger.debug(
"User is verified, skipping moderation actions",
Expand Down Expand Up @@ -187,7 +195,7 @@ def _block(self, user, uow, action_ctx):
raise UserBlockedException()


class RecordScoreHandler(BaseHandler, BaseScoreHandler):
class RecordModerationHandler(BaseHandler, BaseModerationHandler):
"""Handler for calculating scores for records."""

def __init__(self):
Expand Down Expand Up @@ -222,7 +230,9 @@ def publish(self, identity, draft=None, record=None, uow=None, **kwargs):
self.run(identity, record=record, user=user, uow=uow)


class CommunityScoreHandler(community_moderation.BaseHandler, BaseScoreHandler):
class CommunityModerationHandler(
community_moderation.BaseHandler, BaseModerationHandler
):
"""Handler for calculating scores for communities."""

def __init__(self):
Expand Down

0 comments on commit 2d457ac

Please sign in to comment.