from fba import config
from fba import fba
from fba import instances
+from fba import network
-def get_peers(domain: str) -> list:
+def fetch_peers(domain: str) -> list:
# DEBUG: print(f"DEBUG: domain({len(domain)})={domain} - CALLED!")
- if type(domain) != str:
+ if not isinstance(domain, str):
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' is empty")
+ raise ValueError("Parameter 'domain' is empty")
# DEBUG: print(f"DEBUG: domain='{domain}' is misskey, sending API POST request ...")
peers = list()
while True:
# DEBUG: print(f"DEBUG: Fetching offset='{offset}' from '{domain}' ...")
if offset == 0:
- fetched = fba.post_json_api(domain, "/api/federation/instances", json.dumps({
+ fetched = network.post_json_api(domain, "/api/federation/instances", json.dumps({
"sort" : "+pubAt",
"host" : None,
"limit": step
"Origin": domain
})
else:
- fetched = fba.post_json_api(domain, "/api/federation/instances", json.dumps({
+ fetched = network.post_json_api(domain, "/api/federation/instances", json.dumps({
"sort" : "+pubAt",
"host" : None,
"limit" : step,
# DEBUG: print(f"DEBUG: fetched()={len(fetched)}")
if len(fetched) == 0:
- # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
+ # DEBUG: print(f"DEBUG: Returned zero bytes, exiting loop, domain='{domain}'")
break
elif len(fetched) != config.get("misskey_limit"):
# DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config.get('misskey_limit')}'")
offset = offset + (config.get("misskey_limit") - len(fetched))
else:
- # DEBUG: print("DEBUG: Raising offset by step:", step)
+ # DEBUG: print(f"DEBUG: Raising offset by step={step}")
offset = offset + step
# Check records
already = 0
for row in fetched:
- # DEBUG: print(f"DEBUG: row():{len(row)}")
+ # DEBUG: print(f"DEBUG: row()={len(row)}")
if not "host" in row:
print(f"WARNING: row()={len(row)} does not contain key 'host': {row},domain='{domain}'")
continue
- elif type(row["host"]) != str:
+ elif not isinstance(row["host"], str):
print(f"WARNING: row[host][]={type(row['host'])} is not 'str'")
continue
elif blacklist.is_blacklisted(row["host"]):
break
# DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
- instances.set("total_peers", domain, len(peers))
+ instances.set_data("total_peers", domain, len(peers))
# DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
instances.update_last_instance_fetch(domain)
- # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
+ # DEBUG: print(f"DEBUG: Returning peers[]='{type(peers)}'")
return peers
+
+def fetch_blocks(domain: str) -> dict:
+ # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
+ if not isinstance(domain, str):
+ raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
+ elif domain == "":
+ raise ValueError("Parameter 'domain' is empty")
+
+ # DEBUG: print("DEBUG: Fetching misskey blocks from domain:", domain)
+ blocklist = {
+ "suspended": [],
+ "blocked" : []
+ }
+
+ offset = 0
+ step = config.get("misskey_limit")
+ while True:
+ # iterating through all "suspended" (follow-only in its terminology)
+ # instances page-by-page, since that troonware doesn't support
+ # sending them all at once
+ try:
+ # DEBUG: print(f"DEBUG: Fetching offset='{offset}' from '{domain}' ...")
+ if offset == 0:
+ # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
+ fetched = network.post_json_api(domain, "/api/federation/instances", json.dumps({
+ "sort" : "+pubAt",
+ "host" : None,
+ "suspended": True,
+ "limit" : step
+ }), {
+ "Origin": domain
+ })
+ else:
+ # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
+ fetched = network.post_json_api(domain, "/api/federation/instances", json.dumps({
+ "sort" : "+pubAt",
+ "host" : None,
+ "suspended": True,
+ "limit" : step,
+ "offset" : offset - 1
+ }), {
+ "Origin": domain
+ })
+
+ # DEBUG: print("DEBUG: fetched():", len(fetched))
+ if len(fetched) == 0:
+ # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
+ break
+ elif len(fetched) != config.get("misskey_limit"):
+ # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config.get('misskey_limit')}'")
+ offset = offset + (config.get("misskey_limit") - len(fetched))
+ else:
+ # DEBUG: print("DEBUG: Raising offset by step:", step)
+ offset = offset + step
+
+ count = 0
+ for instance in fetched:
+ # Is it there?
+ if instance["isSuspended"] and not fba.has_key(blocklist["suspended"], "domain", instance):
+ count = count + 1
+ blocklist["suspended"].append(
+ {
+ "domain": fba.tidyup_domain(instance["host"]),
+ # no reason field, nothing
+ "reason": None
+ }
+ )
+
+ # DEBUG: print(f"DEBUG: count={count}")
+ if count == 0:
+ # DEBUG: print("DEBUG: API is no more returning new instances, aborting loop!")
+ break
+
+ except BaseException as exc:
+ print("WARNING: Caught error, exiting loop:", domain, exc)
+ instances.update_last_error(domain, exc)
+ offset = 0
+ break
+
+ while True:
+ # same shit, different asshole ("blocked" aka full suspend)
+ try:
+ if offset == 0:
+ # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
+ fetched = network.post_json_api(domain, "/api/federation/instances", json.dumps({
+ "sort" : "+pubAt",
+ "host" : None,
+ "blocked": True,
+ "limit" : step
+ }), {
+ "Origin": domain
+ })
+ else:
+ # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
+ fetched = network.post_json_api(domain, "/api/federation/instances", json.dumps({
+ "sort" : "+pubAt",
+ "host" : None,
+ "blocked": True,
+ "limit" : step,
+ "offset" : offset - 1
+ }), {
+ "Origin": domain
+ })
+
+ # DEBUG: print("DEBUG: fetched():", len(fetched))
+ if len(fetched) == 0:
+ # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
+ break
+ elif len(fetched) != config.get("misskey_limit"):
+ # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config.get('misskey_limit')}'")
+ offset = offset + (config.get("misskey_limit") - len(fetched))
+ else:
+ # DEBUG: print("DEBUG: Raising offset by step:", step)
+ offset = offset + step
+
+ count = 0
+ for instance in fetched:
+ # Is it there?
+ if instance["isBlocked"] and not fba.has_key(blocklist["blocked"], "domain", instance):
+ count = count + 1
+ blocklist["blocked"].append({
+ "domain": fba.tidyup_domain(instance["host"]),
+ "reason": None
+ })
+
+ # DEBUG: print(f"DEBUG: count={count}")
+ if count == 0:
+ # DEBUG: print("DEBUG: API is no more returning new instances, aborting loop!")
+ break
+
+ except BaseException as exc:
+ print("ERROR: Exception during POST:", domain, exc)
+ instances.update_last_error(domain, exc)
+ offset = 0
+ break
+
+ # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
+ instances.update_last_instance_fetch(domain)
+
+ # DEBUG: print(f"DEBUG: Returning for domain='{domain}',blocked()={len(blocklist['blocked'])},suspended()={len(blocklist['suspended'])}")
+ return {
+ "reject" : blocklist["blocked"],
+ "followers_only": blocklist["suspended"]
+ }