]> git.mxchange.org Git - fba.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Wed, 17 Apr 2024 20:50:46 +0000 (22:50 +0200)
committerRoland Häder <roland@mxchange.org>
Wed, 17 Apr 2024 20:50:46 +0000 (22:50 +0200)
- added uncached blocks.get_reason()
- update block reason when current isn't set but updated reason is set

fba/commands.py
fba/helpers/processing.py
fba/models/blocks.py

index 6dc322f1bc4d3bec7f8cb7e4419650b5c2351b36..5627f5e8e7172c2bbf80aea7eed7b071f841e79e 100644 (file)
@@ -435,16 +435,13 @@ def fetch_blocks(args: argparse.Namespace) -> int:
             elif block["block_level"] in ["accept", "accepted"]:
                 logger.debug("block[blocked]='%s' is accepted, not wanted here - SKIPPED!", block["blocked"])
                 continue
-            elif instances.is_recent(block["blocked"]):
-                logger.debug("block[blocked]='%s' has recently been crawled - SKIPPED!", block["blocked"])
-                continue
             elif not instances.is_registered(block["blocked"]):
                 logger.debug("Hash wasn't found, adding: blocked='%s',blocker='%s'", block["blocked"], blocker)
                 federation.fetch_instances(block["blocked"], blocker, None, inspect.currentframe().f_code.co_name)
 
             block["block_level"] = blocks.alias_block_level(block["block_level"])
 
-            if processing.block(blocker, block["blocked"], block["reason"], block["block_level"]) and block["block_level"] == "reject" and config.get("bot_enabled"):
+            if processing.block(blocker, block["blocked"], block["reason"], block["block_level"]) and block["block_level"] in ["reject", "suspend"] and config.get("bot_enabled"):
                 logger.debug("Appending blocked='%s',reason='%s' for blocker='%s' ...", block["blocked"], block["block_level"], blocker)
                 blockdict.append({
                     "blocked": block["blocked"],
index 847f9fca2fed7bd9992c931b3beab0973a91434a..cd19eeb6ab66c330b98796b01ad3cf6ce6b92399 100644 (file)
@@ -108,6 +108,10 @@ def block(blocker: str, blocked: str, reason: str, block_level: str) -> bool:
         blocks.add(blocker, blocked, reason, block_level)
         added = True
     else:
+        if reason not in [None, ""] and blocks.get_reason(blocker, blocked, block_level) is None:
+            logger.debug("Updating reason='%s' for blocker='%s',blocked='%s',block_level='%s' ...", reason, blocker, blocked, block_level)
+            blocks.update_reason(reason, blocker, blocked, block_level)
+
         logger.debug("Updating last_seen for blocker='%s',blocked='%s',block_level='%s' ...", blocker, blocked, block_level)
         blocks.update_last_seen(blocker, blocked, block_level)
 
@@ -120,7 +124,7 @@ def csv_block(blocker: str, url: str, command: str):
 
     if not isinstance(url, str):
         raise ValueError(f"url[]='{url}' is not of type 'str'")
-    elif url == "":
+    elif url in [None, ""]:
         raise ValueError("Parameter 'url' is empty")
     elif not validators.url(url):
         raise ValueError(f"Parameter url='{url}' is not a valid URL")
index 3ad164e703535aa0e96f31aaabe73029d777071a..67828197b995838cc586a3653a409d53e5f7597a 100644 (file)
@@ -27,6 +27,39 @@ from fba.helpers import tidyup
 logging.basicConfig(level=logging.INFO)
 logger = logging.getLogger(__name__)
 
+def get_reason(blocker: str, blocked: str, block_level: str):
+    logger.debug("blocker='%s',blocked='%s',block_level='%s' - CALLED!", blocker, blocked, block_level)
+    domain_helper.raise_on(blocker)
+    domain_helper.raise_on(blocked)
+
+    if not isinstance(block_level, str):
+        raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not of type 'str'")
+    elif block_level == "":
+        raise ValueError("Parameter 'block_level' is empty")
+    elif block_level in ["accept", "suspend", "silence", "nsfw", "quarantined_instances"]:
+        raise ValueError(f"block_level='{block_level}' is not wanted.")
+    elif blacklist.is_blacklisted(blocker):
+        raise Exception(f"blocker='{blocker}' is blacklisted but function invoked")
+    elif blacklist.is_blacklisted(blocked):
+        raise Exception(f"blocked='{blocked}' is blacklisted but function invoked")
+    elif not is_instance_blocked(blocker, blocked, block_level):
+        raise Exception(f"blocker='{blocker}',blocked='{blocked}',block_level='{block_level}' is not registered but function is invoked")
+
+    database.cursor.execute(
+        "SELECT reason FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
+        [
+            blocker,
+            blocked,
+            block_level
+        ]
+    )
+
+    row = database.cursor.fetchone()
+    logger.debug("row[]='%s'", type(row))
+
+    logger.debug("row[reason]='%s' - EXIT!", row["reason"])
+    return row["reason"]
+
 def update_reason(reason: str, blocker: str, blocked: str, block_level: str):
     logger.debug("reason='%s',blocker='%s',blocked='%s',block_level='%s' - CALLED!", reason, blocker, blocked, block_level)
     domain_helper.raise_on(blocker)