From 6f049f9dd561190fbc15672199b55b4d7bb7f2a7 Mon Sep 17 00:00:00 2001 From: Lordmau5 Date: Tue, 19 Dec 2023 20:07:08 +0100 Subject: [PATCH] First Message Highlight 1.0.2 * Added: Option to "forget" users after a specified amount of time so they will get highlighted again (Closes #192) --- src/first-message-highlighter/index.js | 75 ++++++++++++++++++--- src/first-message-highlighter/manifest.json | 4 +- 2 files changed, 66 insertions(+), 13 deletions(-) diff --git a/src/first-message-highlighter/index.js b/src/first-message-highlighter/index.js index 0fae9872..2ae5848f 100644 --- a/src/first-message-highlighter/index.js +++ b/src/first-message-highlighter/index.js @@ -1,11 +1,12 @@ class FirstMessageHighlight extends Addon { - known_users = new Set(); + known_users = new Map(); constructor(...args) { super(...args); this.inject('chat'); + this.inject('site'); this.settings.add('first_message_highlight.priority', { default: 0, @@ -59,10 +60,28 @@ class FirstMessageHighlight extends Addon { } }); + this.settings.add('first_message_highlight.forget_user_after', { + default: 0, + ui: { + path: 'Add-Ons > First Message Highlight >> Settings', + title: 'Forget User After', + description: 'Forget a user after a specified amount of time so they can be highlighted again.', + component: 'setting-select-box', + data: [ + { value: 0, title: 'Disabled' }, + { value: 1000 * 60 * 5, title: '5 Minutes' }, + { value: 1000 * 60 * 15, title: '15 Minutes' }, + { value: 1000 * 60 * 30, title: '30 Minutes' }, + { value: 1000 * 60 * 60, title: '60 Minutes' }, + { value: 1000 * 60 * 60 * 2, title: '2 Hours' }, + { value: 1000 * 60 * 60 * 3, title: '3 Hours' }, + ], + } + }); + this.chat.addHighlightReason('first-message', "User's first message during this session"); const outerThis = this; - this.messageHighlighter = { type: 'message_highlighter', priority: 0, @@ -73,22 +92,22 @@ class FirstMessageHighlight extends Addon { if (msg.isHistorical) { if (this.settings.get('first_message_highlight.remember_historical') - && outerThis.remembersUser(msg)) return; + && !outerThis.shouldHighlight(msg)) return; if (this.settings.get('first_message_highlight.highlight_historical')) outerThis.highlightMessage(this, msg); return; } - if(!outerThis.remembersUser(msg)) + if(outerThis.shouldHighlight(msg)) outerThis.highlightMessage(this, msg); } } } onEnable() { - if (ffz.site.getUser() !== null) - this.known_users.add(ffz.site.getUser().id); + this.on('settings:changed:first_message_highlight.forget_user_after', this.clearChatLines, this); + this.chat.addTokenizer(this.messageHighlighter); this.emit('chat:update-lines'); } @@ -99,13 +118,47 @@ class FirstMessageHighlight extends Addon { this.emit('chat:update-lines'); } - remembersUser(msg) { - if (msg.fh_known_user == null) - msg.fh_known_user = this.known_users.has(msg.user.userID); + clearChatLines() { + for(const { message } of this.chat.iterateMessages()) { + message.first_highlight = null; + } + + this.known_users.clear(); - if (msg.fh_known_user) return true; + this.emit('chat:update-lines'); + } + + shouldHighlight(msg) { + // If we have a cached value, return it. + if (msg.first_highlight != null) + return msg.first_highlight; + + // Don't highlight messages without a user, or with the local user + if (!msg.user?.userID || msg.user.userID == this.site.getUser()?.id) + return false; + + // Get the last timestamp for this user + const last_ts = this.known_users.get(msg.user.userID) ?? 0; + + // If the last timestamp is in the past... + if (last_ts < msg.timestamp) { + // Save this timestamp as the new last timestamp + this.known_users.set(msg.user.userID, msg.timestamp); + + // How long should we remember a user? + const forget_user_after = this.settings.get('first_message_highlight.forget_user_after'); + + // We highlight the message if one of the following is true: + // 1. last_ts is unset (so zero) + // 2. forgot_user_after is NOT zero and last_ts is more than that much time in the past + if (last_ts === 0 || (forget_user_after !== 0 && msg.timestamp - last_ts >= forget_user_after)) { + msg.first_highlight = true; + return true; + } + } - this.known_users.add(msg.user.userID); + // If we got here, this is not a new message to highlight. + msg.first_highlight = false; return false; } diff --git a/src/first-message-highlighter/manifest.json b/src/first-message-highlighter/manifest.json index 3a6870c3..7e8c8382 100644 --- a/src/first-message-highlighter/manifest.json +++ b/src/first-message-highlighter/manifest.json @@ -1,7 +1,7 @@ { "enabled": true, "requires": [], - "version": "1.0.2", + "version": "1.0.3", "icon": "https://i.imgur.com/Db2zP1l.png", "short_name": "first_msg_highlight", "name": "First Message Highlight", @@ -9,5 +9,5 @@ "description": "Addon to highlight the first message of a user during the current session. Useful for community-driven chats and welcoming.", "settings": "add_ons.first_message_highlight", "created": "2021-12-01T14:32:46.254Z", - "updated": "2023-03-08T17:54:06.268Z" + "updated": "2023-12-19T19:07:09.545Z" } \ No newline at end of file