Added node list sorting

This commit is contained in:
Mark Qvist 2023-10-16 00:16:04 +02:00
parent 2322a254a8
commit 0fb9e180ba
3 changed files with 42 additions and 7 deletions

View File

@ -66,7 +66,7 @@ class Directory:
packed_list = []
for source_hash in self.directory_entries:
e = self.directory_entries[source_hash]
packed_list.append((e.source_hash, e.display_name, e.trust_level, e.hosts_node, e.preferred_delivery, e.identify))
packed_list.append((e.source_hash, e.display_name, e.trust_level, e.hosts_node, e.preferred_delivery, e.identify, e.sort_rank))
directory = {
"entry_list": packed_list,
@ -106,13 +106,17 @@ class Directory:
else:
identify = False
entries[e[0]] = DirectoryEntry(e[0], e[1], e[2], hosts_node, preferred_delivery=preferred_delivery, identify_on_connect=identify)
if len(e) > 6:
sort_rank = e[6]
else:
sort_rank = None
entries[e[0]] = DirectoryEntry(e[0], e[1], e[2], hosts_node, preferred_delivery=preferred_delivery, identify_on_connect=identify, sort_rank=sort_rank)
self.directory_entries = entries
self.announce_stream = unpacked_directory["announce_stream"]
except Exception as e:
RNS.log("Could not load directory from disk. The contained exception was: "+str(e), RNS.LOG_ERROR)
@ -220,6 +224,13 @@ class Directory:
def simplest_display_str(self, source_hash):
trust_level = self.trust_level(source_hash)
if trust_level == DirectoryEntry.WARNING or trust_level == DirectoryEntry.UNTRUSTED:
if source_hash in self.directory_entries:
dn = self.directory_entries[source_hash].display_name
if dn == None:
return RNS.prettyhexrep(source_hash)
else:
return dn+" <"+RNS.hexrep(source_hash, delimit=False)+">"
else:
return "<"+RNS.hexrep(source_hash, delimit=False)+">"
else:
if source_hash in self.directory_entries:
@ -253,6 +264,12 @@ class Directory:
else:
return DirectoryEntry.UNKNOWN
def sort_rank(self, source_hash):
if source_hash in self.directory_entries:
return self.directory_entries[source_hash].sort_rank
else:
return None
def preferred_delivery(self, source_hash):
if source_hash in self.directory_entries:
return self.directory_entries[source_hash].preferred_delivery
@ -312,6 +329,7 @@ class Directory:
if e.hosts_node:
node_list.append(e)
node_list.sort(key = lambda e: (e.sort_rank if e.sort_rank != None else 2^32, DirectoryEntry.TRUSTED-e.trust_level, e.display_name))
return node_list
def number_of_known_nodes(self):
@ -336,10 +354,11 @@ class DirectoryEntry:
DIRECT = 0x01
PROPAGATED = 0x02
def __init__(self, source_hash, display_name=None, trust_level=UNKNOWN, hosts_node=False, preferred_delivery=None, identify_on_connect=False):
def __init__(self, source_hash, display_name=None, trust_level=UNKNOWN, hosts_node=False, preferred_delivery=None, identify_on_connect=False, sort_rank=None):
if len(source_hash) == RNS.Identity.TRUNCATED_HASHLENGTH//8:
self.source_hash = source_hash
self.display_name = display_name
self.sort_rank = sort_rank
if preferred_delivery == None:
self.preferred_delivery = DirectoryEntry.DIRECT

View File

@ -984,7 +984,7 @@ class ConversationWidget(urwid.WidgetWrap):
urwid.Pile([
urwid.Text("The paper message was saved to:\n\n"+str(path)+"\n", align="center"),
urwid.Columns([("weight", 0.6, urwid.Text("")), ("weight", 0.4, urwid.Button("OK", on_press=dismiss_dialog))])
]), title=g["page"]
]), title=g["papermsg"].replace(" ", "")
)
dialog.delegate = self
bottom = self.messagelist

View File

@ -480,6 +480,12 @@ class KnownNodeInfo(urwid.WidgetWrap):
trust_level = self.app.directory.trust_level(source_hash)
trust_str = ""
node_entry = self.app.directory.find(source_hash)
sort_str = self.app.directory.sort_rank(source_hash)
if sort_str == None:
sort_str = "None"
else:
sort_str = str(sort_str)
if node_entry == None:
display_str = self.app.directory.simplest_display_str(source_hash)
else:
@ -540,6 +546,7 @@ class KnownNodeInfo(urwid.WidgetWrap):
r_trusted = urwid.RadioButton(trust_button_group, "Trusted", state=trusted_selected)
e_name = urwid.Edit(caption="Name : ",edit_text=display_str)
e_sort = urwid.Edit(caption="Sort Rank : ",edit_text=sort_str)
node_ident = RNS.Identity.recall(source_hash)
op_hash = None
@ -604,8 +611,16 @@ class KnownNodeInfo(urwid.WidgetWrap):
trust_level = DirectoryEntry.TRUSTED
display_str = e_name.get_edit_text()
sort_rank = e_sort.get_edit_text()
try:
if int(sort_rank) >= 0:
sort_rank = int(sort_rank)
else:
sort_rank = None
except:
sort_rank = None
node_entry = DirectoryEntry(source_hash, display_name=display_str, trust_level=trust_level, hosts_node=True, identify_on_connect=connect_identify_checkbox.get_state())
node_entry = DirectoryEntry(source_hash, display_name=display_str, trust_level=trust_level, hosts_node=True, identify_on_connect=connect_identify_checkbox.get_state(), sort_rank=sort_rank)
self.app.directory.remember(node_entry)
self.app.ui.main_display.sub_displays.network_display.directory_change_callback()
@ -626,6 +641,7 @@ class KnownNodeInfo(urwid.WidgetWrap):
urwid.Text("Type : "+type_string, align="left"),
e_name,
urwid.Text("Node Addr : "+addr_str, align="left"),
e_sort,
urwid.Divider(g["divider1"]),
urwid.Text(lxmf_addr_str, align="center"),
urwid.Divider(g["divider1"]),