commit
Match tags loosely and reload members on live updates
Proxy tags now ignore case and treat any whitespace as a space. The bot reloads members when the site sends members_update or force_refresh. Co-Authored-By: Sandrone <[email protected]>
diff --git a/src/api/live.ts b/src/api/live.ts
index c75ddf4..304c887 100644
--- a/src/api/live.ts
+++ b/src/api/live.ts
@@ -3,6 +3,7 @@
import { pingMs, reconnectMs, socketUrl } from "../config/constants.ts";
import type { Member } from "../proxy/types.ts";
import { fetchFronters, setFronters } from "./fronters.ts";
+import { refreshMembers } from "./members.ts";
interface LiveEvent {
type?: string;
@@ -23,6 +24,11 @@ function receive(raw: string): void {
if (event.type === "fronters_update" && event.data?.members) {
setFronters(event.data.members);
}
+
+ // The site sends this after any member edit, so tags and avatars apply at once
+ if (event.type === "members_update" || event.type === "force_refresh") {
+ refreshMembers();
+ }
}
// Same socket the website listens to; resync on every connect to cover missed events
diff --git a/src/proxy/tags.ts b/src/proxy/tags.ts
index e471f5b..d9e01f8 100644
--- a/src/proxy/tags.ts
+++ b/src/proxy/tags.ts
@@ -2,6 +2,13 @@
import type { Member, ProxyMatch } from "./types.ts";
+// Case is ignored, and any whitespace matches any whitespace, so "ae hi" and a non-breaking space still hit "Ae "
+function tagPattern(tag: string): string {
+ return tag
+ .replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
+ .replace(/\s+/g, "\\s+");
+}
+
// Longest tag wins, so "C1:" beats "C:"
export function matchProxyTags(
members: Member[],
@@ -18,18 +25,17 @@ export function matchProxyTags(
if (tagLength === 0) continue;
if (tagLength <= bestLength) continue;
- if (content.length < tagLength) continue;
- if (!content.startsWith(prefix)) continue;
- if (!content.endsWith(suffix)) continue;
- const inner = content.slice(
- prefix.length,
- content.length - suffix.length,
- );
+ const found = new RegExp(
+ `^${tagPattern(prefix)}([\\s\\S]*?)${tagPattern(suffix)}$`,
+ "iu",
+ ).exec(content);
+
+ if (!found) continue;
best = {
members: [member],
- content: member.keep_proxy ? content : inner.trim(),
+ content: member.keep_proxy ? content : found[1].trim(),
};
bestLength = tagLength;
}
diff --git a/tests/proxy/tags_test.ts b/tests/proxy/tags_test.ts
index 19faaf9..b289ba9 100644
--- a/tests/proxy/tags_test.ts
+++ b/tests/proxy/tags_test.ts
@@ -29,3 +29,11 @@ Deno.test("prefix and suffix match", () => {
Deno.test("no tag means no match", () => {
assertEquals(matchProxyTags(members, "plain message"), null);
});
+
+Deno.test("tags ignore case and the kind of space", () => {
+ const spaced = [member("aether", "Ae ")];
+
+ assertEquals(matchProxyTags(spaced, "ae hi")?.content, "hi");
+ assertEquals(matchProxyTags(spaced, "Ae hi")?.content, "hi");
+ assertEquals(matchProxyTags(spaced, "Aehi"), null);
+});