feat: add tag detector for TOWER message flagging

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-27 15:00:37 +05:30
parent 891986d3ba
commit 4c721f0b19
2 changed files with 78 additions and 0 deletions
@@ -0,0 +1,55 @@
import { detectTags, isFlagged } from './tag-detector';
const ADMINS = ['1234567890@s.whatsapp.net', '0987654321@s.whatsapp.net'];
describe('detectTags', () => {
it('detects #important hashtag (case-insensitive)', () => {
expect(detectTags('Check this #IMPORTANT update', 'user@s.whatsapp.net', ADMINS))
.toContain('#important');
});
it('detects #upparivar hashtag (case-insensitive)', () => {
expect(detectTags('Welcome to #UPParivar community', 'user@s.whatsapp.net', ADMINS))
.toContain('#upparivar');
});
it('detects #event hashtag', () => {
expect(detectTags('Join our #event on Saturday', 'user@s.whatsapp.net', ADMINS))
.toContain('#event');
});
it('detects /tower command prefix', () => {
expect(detectTags('/tower save this message', 'user@s.whatsapp.net', ADMINS))
.toContain('#tower-command');
});
it('detects multiple tags in one message', () => {
const tags = detectTags('#important #event happening', 'user@s.whatsapp.net', ADMINS);
expect(tags).toContain('#important');
expect(tags).toContain('#event');
});
it('detects admin sender', () => {
expect(detectTags('Regular message', '1234567890@s.whatsapp.net', ADMINS))
.toContain('#admin');
});
it('returns empty array for untagged message from non-admin', () => {
expect(detectTags('Just a regular chat message', 'nobody@s.whatsapp.net', ADMINS))
.toEqual([]);
});
it('returns empty array for empty text', () => {
expect(detectTags('', 'nobody@s.whatsapp.net', ADMINS)).toEqual([]);
});
});
describe('isFlagged', () => {
it('returns true when tags array is non-empty', () => {
expect(isFlagged(['#important'])).toBe(true);
});
it('returns false when tags array is empty', () => {
expect(isFlagged([])).toBe(false);
});
});
+23
View File
@@ -0,0 +1,23 @@
const HASHTAGS: Array<{ pattern: RegExp; tag: string }> = [
{ pattern: /#important/i, tag: '#important' },
{ pattern: /#upparivar/i, tag: '#upparivar' },
{ pattern: /#event/i, tag: '#event' },
];
export function detectTags(text: string, senderJid: string, adminJids: string[]): string[] {
const tags: string[] = [];
for (const { pattern, tag } of HASHTAGS) {
if (pattern.test(text)) tags.push(tag);
}
if (text.trimStart().startsWith('/tower')) tags.push('#tower-command');
if (adminJids.includes(senderJid)) tags.push('#admin');
return tags;
}
export function isFlagged(tags: string[]): boolean {
return tags.length > 0;
}