From 83465f65f47d8e7d53b4e553a12da95d7905f3a8 Mon Sep 17 00:00:00 2001
From: Rob Swindell <rob@synchro.net>
Date: Sun, 8 Nov 2020 22:39:31 -0800
Subject: [PATCH] Optimize the newsgroup list part of previous commit.

Don't regenerate the newsgroup_list for *every* authenticated NNTP command.
---
 exec/nntpservice.js | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)

diff --git a/exec/nntpservice.js b/exec/nntpservice.js
index 74d39bacbd..e682d2f4f0 100644
--- a/exec/nntpservice.js
+++ b/exec/nntpservice.js
@@ -148,6 +148,21 @@ function count_msgs(msgbase)
 	return { total: count, first: first, last: last };
 }
 
+function get_newsgroup_list()
+{
+	// list of newsgroup names the logged-in user has access to
+	var newsgroup_list = [];
+	if(include_mail) {
+		newsgroup_list.push("mail");
+	}
+	for(var g in msg_area.grp_list) {
+		for(var s in msg_area.grp_list[g].sub_list) {
+			newsgroup_list.push(msg_area.grp_list[g].sub_list[s].newsgroup);
+		}
+	}
+	return newsgroup_list;
+}
+
 function bogus_cmd(cmdline)
 {
 	log(LOG_DEBUG, "Received bogus command: '" + cmdline + "'");
@@ -288,17 +303,6 @@ while(client.socket.is_connected && !quit) {
 		}
 	}
 
-	// list of newsgroup names the logged-in user has access to
-	var newsgroup_list = [];
-	if(include_mail) {
-		newsgroup_list.push("mail");
-	}
-	for(g in msg_area.grp_list) {
-		for(s in msg_area.grp_list[g].sub_list) {
-			newsgroup_list.push(msg_area.grp_list[g].sub_list[s].newsgroup);
-		}
-	}
-
 	/* These commands require login/authentication */
 	switch(cmd[0].toUpperCase()) {
 
@@ -778,17 +782,17 @@ while(client.socket.is_connected && !quit) {
 				if(hdr.newsgroups==undefined || force_newsgroups)
 					hdr.newsgroups = selected.newsgroup;
 				else {
-					var ng_list = hdr.newsgroups.split(',');
+					var nghdr_list = hdr.newsgroups.split(',');
+					var newsgroup_list = get_newsgroup_list();
 					var filtered_list = [];
-					for(n in ng_list) {
-						if(filter_newsgroups && newsgroup_list.indexOf(ng_list[n]) < 0)
+					for(var n in nghdr_list) {
+						if(filter_newsgroups && newsgroup_list.indexOf(nghdr_list[n]) < 0)
 							continue;
-						filtered_list.push(ng_list[n]);
+						filtered_list.push(nghdr_list[n]);
 					}
-					hdr.newsgroups = filtered_list.join(',');
-					/* Tracker1's mod for adding the correct newsgroup name		*/
 					if(filtered_list.indexOf(selected.newsgroup) < 0)
-						hdr.newsgroups = selected.newsgroup + ',' + hdr.newsgroups;
+						filtered_list.push(selected.newsgroup);
+					hdr.newsgroups = filtered_list.join(',');
 				}
 
 				if(hdr.from_org==undefined && !hdr.from_net_type)
-- 
GitLab