Skip to content

Commit

Permalink
fix mutelist
Browse files Browse the repository at this point in the history
  • Loading branch information
vivganes committed Aug 25, 2024
1 parent 85e37ac commit 4729cf2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/app/buffer/EventBuffer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventBuffer } from "./EventBuffer"

fdescribe('Event Buffer',() => {
describe('Event Buffer',() => {

it('should return items for 2 and 4',()=>{
const eventBuffer = new EventBuffer<number>()
Expand Down
3 changes: 2 additions & 1 deletion src/app/component/event-feed/event-feed.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ export class EventFeedComponent implements OnInit,OnDestroy{
if (this.ndkProvider.appData.mutedTopics && this.ndkProvider.appData.mutedTopics !== '') {
mutedTopicsArr = this.ndkProvider.appData.mutedTopics.split(',');
}
this.events = new Set([...fetchedEvents].filter(HashTagFilter.notHashTags(mutedTopicsArr)));
const filteredEvents = [...fetchedEvents].filter(HashTagFilter.notHashTags(mutedTopicsArr))
this.events = new Set(filteredEvents);
}

addEventToFeed(postedEvent: NDKEvent){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export class UserPicAndNameComponent {
if(this.hexKey){
this.populateUser();
}else if(this.npub){
console.log("pic and name "+this.npub)
this.populateUserUsingNpub();
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/app/filter/HashTagFilter.spec.ts
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);
});
});
});
23 changes: 9 additions & 14 deletions src/app/filter/HashTagFilter.ts
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;
}
}

0 comments on commit 4729cf2

Please sign in to comment.