Skip to content

Commit

Permalink
First Message Highlight 1.0.2
Browse files Browse the repository at this point in the history
* Added: Option to "forget" users after a specified amount of time so they will get highlighted again (Closes #192)
  • Loading branch information
Lordmau5 committed Dec 19, 2023
1 parent 56e5d06 commit 6f049f9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
75 changes: 64 additions & 11 deletions src/first-message-highlighter/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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');
}
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/first-message-highlighter/manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"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",
"author": "deko_ration",
"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"
}

0 comments on commit 6f049f9

Please sign in to comment.