changeset 1173:18e92d7f859f

plugin history: separate seen/said timestamps Helps showing when someone has written a message if the user is idling for long on a channel.
author David Demelier <markand@malikania.fr>
date Wed, 28 Sep 2022 15:26:10 +0200
parents c958d796e771
children 41fdadaa33b3
files CHANGES.md plugins/history/history.7 plugins/history/history.js tests/test-plugin-history.c
diffstat 4 files changed, 58 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES.md	Wed Jun 29 20:52:10 2022 +0200
+++ b/CHANGES.md	Wed Sep 28 15:26:10 2022 +0200
@@ -8,6 +8,9 @@
   loop.
 - A new option `IRCCD_WITH_HTTP` has been added to accommodate the new curl
   dependency for `Irccd.Http` API.
+- The plugin `history` manages two different timestamps for message and
+  visibility. This helps showing the last message date of someone who is present
+  on a channel but idle.
 
 javascript API
 --------------
--- a/plugins/history/history.7	Wed Jun 29 20:52:10 2022 +0200
+++ b/plugins/history/history.7	Wed Sep 28 15:26:10 2022 +0200
@@ -84,8 +84,13 @@
 .Bl -tag -width unknown
 .It Va error
 Template when an internal error occured. Keywords:
+.Em target , (date) .
+.It Va silent
+Template when someone has been seen but who has never wrote any message.
+Keywords:
+.Em target , (date) .
 .It Va seen
-Template for showing last seen. Keywords:
+Template for showing last time someone was seen. Keywords:
 .Em target , (date) .
 .It Va said
 Template for showing the last message. Keywords:
--- a/plugins/history/history.js	Wed Jun 29 20:52:10 2022 +0200
+++ b/plugins/history/history.js	Wed Sep 28 15:26:10 2022 +0200
@@ -36,6 +36,7 @@
 	"error":        "#{nickname}, I'm sorry, something went wrong.",
 	"seen":         "#{nickname}, I've seen #{target} for the last time the %d-%m-%Y %H:%M",
 	"said":         "#{nickname}, #{target} said on %d-%m-%Y %H:%M: #{message}",
+	"silent":       "#{nickname}, #{target} hasn't said anything yet.",
 	"unknown":      "#{nickname}, I've never seen #{target}.",
 	"usage":        "#{nickname}, usage: #{plugin} seen | said <target>."
 };
@@ -102,7 +103,11 @@
 	var file = new File(path(server, channel), "wt");
 
 	entry.timestamp = Date.now();
-	entry.message = (message) ? message : entry.message;
+
+	if (message) {
+		entry.message = message;
+		entry.messageTimestamp = entry.timestamp;
+	}
 
 	file.write(JSON.stringify(db));
 }
@@ -151,14 +156,30 @@
 		kw.target = args[1];
 
 		if (!info) {
-			server.message(channel, Util.format(Plugin.templates.unknown, kw));
+			server.message(channel, Util.format(Plugin.templates["unknown"], kw));
 			return;
 		}
 
 		kw.date = info.timestamp;
-		kw.message = info.message ? info.message : "";
 
-		server.message(channel, Util.format(Plugin.templates[args[0] == "seen" ? "seen" : "said"], kw));
+		if (args[0] === "seen")
+			template = Plugin.templates["seen"];
+		else {
+			/*
+			 * If there is a message, we're more interested in the
+			 * message timestamp rather than when he was seen
+			 * because someone may be on a channel for a long time
+			 * (seen) but hasn't talk for long.
+			 */
+			if (info.message) {
+				kw.message = info.message;
+				kw.date = info.messageTimestamp;
+				template = Plugin.templates["said"];
+			} else
+				template = Plugin.templates["silent"];
+		}
+
+		server.message(channel, Util.format(template, kw));
 	} catch (e) {
 		server.message(channel, Util.format(Plugin.templates["error"], kw));
 	}
--- a/tests/test-plugin-history.c	Wed Jun 29 20:52:10 2022 +0200
+++ b/tests/test-plugin-history.c	Wed Sep 28 15:26:10 2022 +0200
@@ -31,7 +31,7 @@
         irc_plugin_handle(plugin, &(const struct irc_event) {           \
                 .type = t,                                              \
                 .server = server,                                       \
-                        .message = {                                    \
+                .message = {                                            \
                         .origin = "jean!jean@localhost",                \
                         .channel = "#history",                          \
                         .message = m                                    \
@@ -40,11 +40,11 @@
 } while (0)
 
 #define CALL_EX(t, o, c, m) do {                                        \
-        memset(server->conn->out, 0, sizeof (server->conn->out));         \
+        memset(server->conn->out, 0, sizeof (server->conn->out));       \
         irc_plugin_handle(plugin, &(const struct irc_event) {           \
                 .type = t,                                              \
                 .server = server,                                       \
-                        .message = {                                    \
+                .message = {                                            \
                         .origin = o,                                    \
                         .channel = c,                                   \
                         .message = m                                    \
@@ -73,6 +73,7 @@
 	irc_plugin_set_template(plugin, "error", "error=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}");
 	irc_plugin_set_template(plugin, "seen", "seen=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{target}:%H:%M");
 	irc_plugin_set_template(plugin, "said", "said=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{target}:#{message}:%H:%M");
+	irc_plugin_set_template(plugin, "silent", "silent=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{target}");
 	irc_plugin_set_template(plugin, "unknown", "unknown=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{target}");
 	irc_plugin_set_option(plugin, "file", TOP "/tests/seen.json");
 	irc_plugin_load(plugin);
@@ -128,6 +129,25 @@
 }
 
 GREATEST_TEST
+basics_silent(void)
+{
+	/* Join but without any message. */
+	irc_plugin_handle(plugin, &(const struct irc_event) {
+		.type = IRC_EVENT_JOIN,
+		.server = server,
+		.join = {
+			.origin = "jean!jean@localhost",
+			.channel = "#history"
+		}
+	});
+	CALL_EX(IRC_EVENT_COMMAND, "francis!francis@localhost", "#history", "said jean");
+
+	GREATEST_ASSERT_STR_EQ(server->conn->out, "PRIVMSG #history :silent=history:!history:test:#history:francis!francis@localhost:francis:jean\r\n");
+
+	GREATEST_PASS();
+}
+
+GREATEST_TEST
 basics_unknown(void)
 {
 	CALL_EX(IRC_EVENT_MESSAGE, "jean!jean@localhost", "#history", "hello");
@@ -160,6 +180,7 @@
 	GREATEST_RUN_TEST(basics_error);
 	GREATEST_RUN_TEST(basics_seen);
 	GREATEST_RUN_TEST(basics_said);
+	GREATEST_RUN_TEST(basics_silent);
 	GREATEST_RUN_TEST(basics_unknown);
 	GREATEST_RUN_TEST(basics_case_insensitive);
 }
@@ -175,5 +196,3 @@
 
 	return 0;
 }
-
-