Browse Source

various fixes; mostly related to channel history

Andrew Dolgov 5 months ago
parent
commit
5199479151
3 changed files with 55 additions and 38 deletions
  1. 1 1
      include/functions.php
  2. 5 2
      index.php
  3. 49 35
      js/tt-irc.js

+ 1 - 1
include/functions.php

@@ -28,7 +28,7 @@
 	define('SINGLE_USER_MODE', false);
 	define('EMOTICONS_MAP', 'emoticons/emoticons.json');
 	define('DEFAULT_BUFFER_SIZE', 128);
-	define('KEEP_MESSAGES_MAX', 4096);
+	define('KEEP_MESSAGES_MAX', 1024);
 
 	if (DB_TYPE == 'mysql') {
 		define('DB_KEY_FIELD', 'param');

+ 5 - 2
index.php

@@ -171,12 +171,15 @@
 					</div>
 				</div>
 				<div class="modal-footer">
-					<button class="btn btn-info pull-left" type="submit" onclick="model.showHistory()">
-						<span class='glyphicon glyphicon-time'></span> <?php echo __('History') ?></button>
 					<button class="btn btn-primary" type="submit">
 						<span class='glyphicon glyphicon-search'></span> <?php echo __('Search') ?></button>
 					<button class="btn btn-default" data-toggle='modal' data-target='#searchBox'>
 						<span class='glyphicon glyphicon-remove'></span> <?php echo __('Close') ?></button>
+
+					<!-- goes after so it doesn't get clicked instead of search on submit -->
+					<button class="btn btn-info pull-left" onclick="model.showHistory()">
+						<span class='glyphicon glyphicon-time'></span> <?php echo __('History') ?></button>
+
 				</div>
 			</div>
 		</div>

+ 49 - 35
js/tt-irc.js

@@ -104,8 +104,8 @@ const Connection = function(data) {
 
 	self.displayLines = ko.computed(function() {
 		return self.lines()
-			.sort(function (a,b) { return a.ts() - b.ts() })
-			.slice(-BUFFER_MAX_LINES);
+			.slice(-BUFFER_MAX_LINES)
+			.sort((a,b) => { return a.ts() - b.ts() });
 
 	});
 
@@ -114,7 +114,8 @@ const Connection = function(data) {
 			return self.lines()
 				.filter((msg) =>
 					msg.message().match(self.searchQuery()) || msg.message_formatted().match(self.searchQuery()))
-				.sort(function (a,b) { return a.ts() - b.ts() });
+				.sort((a,b) => { return a.ts() - b.ts() });
+
 	});
 
 	self.update = async function(data) {
@@ -226,7 +227,7 @@ const Message = function(data, chan) {
 			opts = {timeStyle: 'short', hour12: false}
 		}
 
-		return self.ts().toLocaleTimeString("en-US", opts);
+		return self.ts().toLocaleTimeString("en-GB", opts);
 	};
 
 	/*if (emoticons_map && self.message()) {
@@ -234,11 +235,12 @@ const Message = function(data, chan) {
 	}*/
 
 	self.rewrite_url_titles = function(msg, allow_extended) {
-		var msg = $("<span>" + msg + "</span>");
+		msg = $("<span>" + msg + "</span>");
+
 		const links = msg.find("a").filter(':not([data-processed])');
 
 		$.each(links, function(i, link) {
-			var link = $(link);
+			link = $(link);
 
 			//console.log('checking', link[0]);
 
@@ -451,6 +453,8 @@ const Channel = function(connection_id, title, tab_type) {
 	self.nicklist = ko.observableArray([]).extend({deferred:true});
 	self.connection_id = ko.observable(connection_id);
 	self.lines = ko.observableArray([]);
+	self._sorted_lines_last_id = ko.observable(-1);
+	self._sorted_lines = ko.observableArray([]).extend({deferred:true});
 	self._topic = ko.observableArray([]);
 	self.topicEventSynthesized = ko.observable(false);
 	self.highlight = ko.observable(false);
@@ -486,27 +490,36 @@ const Channel = function(connection_id, title, tab_type) {
 		owner: self
 	});
 
+	self.displayLines = ko.computed(function() {
+
+		const last_line = self.lines().last();
+		const last_id = last_line ? last_line.id() : -1;
+
+		if (self._sorted_lines_last_id() != last_id) {
+
+			self._sorted_lines(self.lines()
+				.sort((a,b) => { return a.ts() - b.ts() }))
+
+			self._sorted_lines_last_id(last_id);
+		}
+
+		return self._sorted_lines().slice(-BUFFER_MAX_LINES);
+
+	}).extend({deferred:true});
+
 	self.displaySearchResults = ko.computed(function() {
 		if (self.searchQuery()) {
 			const query = self.searchQuery().toLowerCase();
 
-			return self.lines()
+			return self._sorted_lines()
 				.filter((msg) => (msg.sender() && msg.sender().toLowerCase().match(query)) ||
 					(msg.userhost() && msg.userhost().toLowerCase().match(query)) ||
 					msg.message().toLowerCase().match(query) ||
-					msg.message_formatted().toLowerCase().match(query))
-				.sort(function (a,b) { return a.ts() - b.ts() });
+					msg.message_formatted().toLowerCase().match(query));
 
 		}
 	});
 
-	self.displayLines = ko.computed(function() {
-		return self.lines()
-			.sort(function (a,b) { return a.ts() - b.ts() })
-			.slice(-BUFFER_MAX_LINES);
-
-	});
-
 	self.saveLastRead = function() {
 		if (!model.window_active())
 			return;
@@ -525,8 +538,11 @@ const Channel = function(connection_id, title, tab_type) {
 
 	};
 
-	self.getHistory = function(offset) {
-		if (self.history_requested() || self.history_complete() || self.lines().length >= BUFFER_MAX_LINES)
+	self.getHistory = function(offset, get_all) {
+		if (self.history_requested() || self.history_complete())
+			return;
+
+		if (!get_all && self.lines().length >= BUFFER_MAX_LINES)
 			return;
 
 		self.history_requested(true);
@@ -548,21 +564,15 @@ const Channel = function(connection_id, title, tab_type) {
 
 			const restore_height = $("#log").prop('scrollHeight') - $("#log").scrollTop();
 
-			handle_update(resp).then((added) => {
+			handle_update(resp).then(() => {
 				$("#log").scrollTop($("#log").prop('scrollHeight') - restore_height);
 
-				console.log('hist lines added', added);
-
-				if (added > 0 && resp[1].length == default_buffer_size) {
-					//update_buffer();
+				console.log('hist lines added');
 
-					window.setTimeout(function() {
-						if ($("#log").prop('scrollHeight') == $('#log').height()) {
-							self.getHistory(0);
-						}
-					}, 100);
-				} else {
+				if (resp[1].length != default_buffer_size) {
 					self.history_complete(true);
+				} else if (get_all) {
+					self.getHistory(0, get_all);
 				}
 			});
 		});
@@ -728,6 +738,8 @@ function Model() {
 	self.search = function() {
 		const query = $("#searchBox .search_query").val();
 
+		console.log('searching for', query);
+
 		self.activeChannel().searchQuery(query);
 
 		if (!query)
@@ -749,6 +761,7 @@ function Model() {
 	self.showHistory = function() {
 		$("#searchBox .search_query").val(".");
 
+		self.activeChannel().getHistory(0, true);
 		self.search();
 
 		return false;
@@ -905,6 +918,7 @@ function Model() {
 				chan.highlight(false);
 				chan.unread(0);
 				chan.saveLastRead();
+				chan.searchQuery("");
 
 				if ($("#log").prop('scrollHeight') == $('#log').height()) {
 					chan.getHistory(0);
@@ -1242,13 +1256,15 @@ function init_second_stage(params) {
 	$("#overlay").hide();
 
 	$('#searchBox').on('shown.bs.modal', function() {
-		model.activeChannel().getHistory();
-
 		$("#searchBox .search_query")
 			.val(model.activeChannel().searchQuery())
 			.focus();
 	});
 
+	$('#searchBox').on('hidden.bs.modal', function() {
+		model.activeChannel().searchQuery("");
+	});
+
 	$("#input-prompt")
 		.val("")
 		.on('blur', function() {
@@ -2611,12 +2627,10 @@ function push_message(connection_id, channel, message, message_type, no_tab_hl)
 					connection_id: connection_id,
 				};
 
-				dchmsg.ts.setHours(0);
-				dchmsg.ts.setMinutes(0);
-				dchmsg.ts.setSeconds(0);
+				dchmsg.ts.setHours(0, 0, 0);
 
 				dchmsg.message = __("Date changed to %d").replace("%d",
-						dchmsg.ts.toLocaleString('en-US',
+						dchmsg.ts.toLocaleString('en-GB',
 							{weekday: "long", month: "long", day: "numeric"}));
 
 				chan.lines.push(new Message(dchmsg, chan));