-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
45 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { NDKEvent } from "@nostr-dev-kit/ndk"; | ||
import { HashTagFilter } from "./HashTagFilter"; | ||
|
||
describe('HashTagFilter', () => { | ||
describe('notHashTags', () => { | ||
const filter = HashTagFilter.notHashTags(['test', 'mute']); | ||
|
||
it('should return true for events without hashtags', () => { | ||
const event = { content: 'This is a normal post' } as NDKEvent; | ||
expect(filter(event, 0, [])).toBe(true); | ||
}); | ||
|
||
it('should return false for events with muted hashtags', () => { | ||
const event = { content: 'This post contains a #test hashtag' } as NDKEvent; | ||
expect(filter(event, 0, [])).toBe(false); | ||
}); | ||
|
||
it('should return true for events with non-muted hashtags', () => { | ||
const event = { content: 'This post contains a #allowed hashtag' } as NDKEvent; | ||
expect(filter(event, 0, [])).toBe(true); | ||
}); | ||
|
||
it('should be case-insensitive', () => { | ||
const event = { content: 'This post contains a #TEST hashtag' } as NDKEvent; | ||
expect(filter(event, 0, [])).toBe(false); | ||
}); | ||
|
||
it('should return true for events with undefined content', () => { | ||
const event = { } as NDKEvent; | ||
expect(filter(event, 0, [])).toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,19 @@ | ||
import { NDKEvent, NDKTag } from "@nostr-dev-kit/ndk"; | ||
|
||
export class HashTagFilter{ | ||
|
||
|
||
|
||
static notHashTags(mutedTags:string[]):(value: NDKEvent, index: number, array: NDKEvent[]) => boolean{ | ||
console.log("notHashTags: mutedTags "+ mutedTags); | ||
let filter:(value: NDKEvent) => boolean | ||
filter = (event:NDKEvent):boolean=>{ | ||
const tagsInEvent:NDKTag[] = event.getMatchingTags('t'); | ||
const tagsList = HashTagFilter.getHashTagsListFromMatchingTags(tagsInEvent); | ||
return tagsList.every((tagInEvent) => !mutedTags.includes(tagInEvent)) | ||
const eventTextLowerCase = event.content?.toLowerCase(); | ||
if(!eventTextLowerCase) return true; | ||
for (let tag of mutedTags) { | ||
if (eventTextLowerCase.includes('#' + tag.toLowerCase())) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return filter; | ||
} | ||
|
||
static getHashTagsListFromMatchingTags(matchingTags: NDKTag[]){ | ||
const tags = []; | ||
for (let tag of matchingTags){ | ||
tags.push(tag[1]); | ||
} | ||
return tags; | ||
} | ||
} |