]> git.mxchange.org Git - fba.git/blob - fba/federation/misskey.py
Continued:
[fba.git] / fba / federation / misskey.py
1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published
6 # by the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17 import json
18
19 from fba import blacklist
20 from fba import config
21 from fba import fba
22 from fba import instances
23
24 def get_peers(domain: str) -> list:
25     # DEBUG: print(f"DEBUG: domain({len(domain)})={domain} - CALLED!")
26     if type(domain) != str:
27         raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
28     elif domain == "":
29         raise ValueError(f"Parameter 'domain' is empty")
30
31     # DEBUG: print(f"DEBUG: domain='{domain}' is misskey, sending API POST request ...")
32     peers = list()
33     offset = 0
34     step = config.get("misskey_limit")
35
36     # iterating through all "suspended" (follow-only in its terminology)
37     # instances page-by-page, since that troonware doesn't support
38     # sending them all at once
39     while True:
40         # DEBUG: print(f"DEBUG: Fetching offset='{offset}' from '{domain}' ...")
41         if offset == 0:
42             fetched = fba.post_json_api(domain, "/api/federation/instances", json.dumps({
43                 "sort" : "+pubAt",
44                 "host" : None,
45                 "limit": step
46             }), {
47                 "Origin": domain
48             })
49         else:
50             fetched = fba.post_json_api(domain, "/api/federation/instances", json.dumps({
51                 "sort"  : "+pubAt",
52                 "host"  : None,
53                 "limit" : step,
54                 "offset": offset - 1
55             }), {
56                 "Origin": domain
57             })
58
59         # DEBUG: print(f"DEBUG: fetched()={len(fetched)}")
60         if len(fetched) == 0:
61             # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
62             break
63         elif len(fetched) != config.get("misskey_limit"):
64             # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config.get('misskey_limit')}'")
65             offset = offset + (config.get("misskey_limit") - len(fetched))
66         else:
67             # DEBUG: print("DEBUG: Raising offset by step:", step)
68             offset = offset + step
69
70         # Check records
71         # DEBUG: print(f"DEBUG: fetched({len(fetched)})[]={type(fetched)}")
72         if isinstance(fetched, dict) and "error" in fetched and "message" in fetched["error"]:
73             print(f"WARNING: post_json_api() returned error: {fetched['error']['message']}")
74             fba.update_last_error(domain, fetched["error"]["message"])
75             break
76
77         already = 0
78         for row in fetched:
79             # DEBUG: print(f"DEBUG: row():{len(row)}")
80             if not "host" in row:
81                 print(f"WARNING: row()={len(row)} does not contain key 'host': {row},domain='{domain}'")
82                 continue
83             elif type(row["host"]) != str:
84                 print(f"WARNING: row[host][]={type(row['host'])} is not 'str'")
85                 continue
86             elif blacklist.is_blacklisted(row["host"]):
87                 # DEBUG: print(f"DEBUG: row[host]='{row['host']}' is blacklisted. domain='{domain}'")
88                 continue
89             elif row["host"] in peers:
90                 # DEBUG: print(f"DEBUG: Not adding row[host]='{row['host']}', already found.")
91                 already = already + 1
92                 continue
93
94             # DEBUG: print(f"DEBUG: Adding peer: '{row['host']}'")
95             peers.append(row["host"])
96
97         if already == len(fetched):
98             print(f"WARNING: Host returned same set of '{already}' instances, aborting loop!")
99             break
100
101     # DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
102     instances.set("total_peers", domain, len(peers))
103
104     # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
105     instances.update_last_instance_fetch(domain)
106
107     # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
108     return peers