fixed code so that wrong notifications don't get printed

it was printing every thread where any follow was involved; and was highlighting them too.

now only follows posts/likes are highlighted and shown as notifications. reduces the flood of notifications.
This commit is contained in:
Vishal 2022-12-31 03:00:39 +05:30
parent 2d603c2aec
commit 9a5b0b48ba
2 changed files with 60 additions and 43 deletions

View File

@ -1,6 +1,5 @@
import 'dart:io'; import 'dart:io';
import 'dart:convert'; import 'dart:convert';
import 'dart:math';
import 'package:nostr_console/event_ds.dart'; import 'package:nostr_console/event_ds.dart';
import 'package:nostr_console/tree_ds.dart'; import 'package:nostr_console/tree_ds.dart';
import 'package:nostr_console/relays.dart'; import 'package:nostr_console/relays.dart';
@ -1623,7 +1622,6 @@ Future<void> mainMenuUi(Store node) async {
switch(option) { switch(option) {
case 1: case 1:
node.printStoreTrees(0, DateTime.now().subtract(Duration(hours:gHoursDefaultPrint)), selectorTrees_all); node.printStoreTrees(0, DateTime.now().subtract(Duration(hours:gHoursDefaultPrint)), selectorTrees_all);
break; break;

View File

@ -595,7 +595,7 @@ class Tree {
} // end treeSelectorUserPostAndLike() } // end treeSelectorUserPostAndLike()
// returns true if the tree (or its children, depending on flag) has a post or like by user; and notification flags are set for such events // returns true if the tree (or its children, depending on flag) has a post or like by user; and notification flags are set for such events
bool treeSelectorDMtoFromUser(Set<String> pubkeys, { bool enableNotifications = true, bool checkChildrenToo = true}) { bool treeSelectorDMtoFromUser(Set<String> pubkeys, { bool enableNotifications = true}) {
if( event.eventData.kind != 4) { if( event.eventData.kind != 4) {
return false; return false;
@ -1576,39 +1576,39 @@ class Store {
if( gDebug > 0) print("In printNotifications: Could not find event $eventID in tree"); if( gDebug > 0) print("In printNotifications: Could not find event $eventID in tree");
return; return;
} else { } else {
switch(t.event.eventData.kind) { if( isRelevantForNotification(t)) {
case 1: switch(t.event.eventData.kind) {
t.event.eventData.isNotification = true; case 1:
Tree topTree = getTopTree(t); t.event.eventData.isNotification = true;
topNotificationTree.add(topTree); Tree topTree = getTopTree(t);
break; topNotificationTree.add(topTree);
case 7: break;
Event event = t.event; case 7:
if(gDebug > 0) ("Got notification of type 7"); Event event = t.event;
String reactorId = event.eventData.pubkey; if(gDebug > 0) ("Got notification of type 7");
int lastEIndex = event.eventData.eTags.length - 1; String reactorId = event.eventData.pubkey;
String reactedTo = event.eventData.eTags[lastEIndex][0]; int lastEIndex = event.eventData.eTags.length - 1;
Event? reactedToEvent = allChildEventsMap[reactedTo]?.event; String reactedTo = event.eventData.eTags[lastEIndex][0];
if( reactedToEvent != null) { Event? reactedToEvent = allChildEventsMap[reactedTo]?.event;
Tree? reactedToTree = allChildEventsMap[reactedTo]; if( reactedToEvent != null) {
if( reactedToTree != null) { Tree? reactedToTree = allChildEventsMap[reactedTo];
if(event.eventData.content == "+" ) { if( reactedToTree != null) {
reactedToTree.event.eventData.newLikes.add( reactorId); if(event.eventData.content == "+" ) {
Tree topTree = getTopTree(reactedToTree); reactedToTree.event.eventData.newLikes.add( reactorId);
topNotificationTree.add(topTree); Tree topTree = getTopTree(reactedToTree);
} else if(event.eventData.content == "!" ) { topNotificationTree.add(topTree);
reactedToTree.event.eventData.isHidden = true; }
} else {
if(gDebug > 0) print("Could not find reactedTo tree");
} }
} else { } else {
if(gDebug > 0) print("Could not find reactedTo tree"); if(gDebug > 0) print("Could not find reactedTo event");
} }
} else { break;
if(gDebug > 0) print("Could not find reactedTo event"); default:
} if(gDebug > 0) print("got an event thats not 1 or 7(reaction). its kind = ${t.event.eventData.kind} count17 = $countNotificationEvents");
break; break;
default: }
if(gDebug > 0) print("got an event thats not 1 or 7(reaction). its kind = ${t.event.eventData.kind} count17 = $countNotificationEvents");
break;
} }
} }
}); });
@ -2070,11 +2070,23 @@ class Store {
} }
// threads where the user and follows have involved themselves are returnes as true ( relevant) // threads where the user and follows have involved themselves are returnes as true ( relevant)
bool isRelevant(Tree tree) { bool isRelevantForNotification(Tree tree) {
if( tree.treeSelectorUserPostAndLike(gFollowList.union(gDefaultFollows).union({userPublicKey}),
enableNotifications: false,
checkChildrenToo: false)
|| tree.treeSelectorDMtoFromUser({userPublicKey},
enableNotifications: false)) {
return true;
}
if( tree.treeSelectorUserPostAndLike(gFollowList) return false;
|| tree.treeSelectorUserPostAndLike({userPublicKey}) }
|| tree.treeSelectorDMtoFromUser({userPublicKey})
// threads where the user and follows have involved themselves are returnes as true ( relevant)
bool isRelevantForFileSave(Tree tree) {
if( tree.treeSelectorUserPostAndLike(gFollowList.union(gDefaultFollows).union({userPublicKey}), enableNotifications: false)
|| tree.treeSelectorDMtoFromUser({userPublicKey}, enableNotifications: false)
|| tree.treeSelectorUserReplies(gFollowList)) { || tree.treeSelectorUserReplies(gFollowList)) {
return true; return true;
} }
@ -2119,7 +2131,7 @@ class Store {
} }
} }
if( !isRelevant(tree)) { if( !isRelevantForFileSave(tree)) {
continue; continue;
} }
@ -2539,6 +2551,18 @@ class Store {
return ""; return "";
} }
// set isHidden for reactedTo if it exists in map
if( comment == "!" ) {
if( event.eventData.pubkey == userPublicKey) {
// is a hide reaction by the user; set reactedToid as hidden
tempChildEventsMap[reactedToId]?.event.eventData.isHidden = true;
return reactedToId;
} else {
// is hidden reaction by someone else; do nothing then
return "";
}
}
// check if the reaction already exists by this user // check if the reaction already exists by this user
if( gReactions.containsKey(reactedToId)) { if( gReactions.containsKey(reactedToId)) {
for( int i = 0; i < ((gReactions[reactedToId]?.length)??0); i++) { for( int i = 0; i < ((gReactions[reactedToId]?.length)??0); i++) {
@ -2575,12 +2599,7 @@ class Store {
newReactorList.add(temp); newReactorList.add(temp);
gReactions[reactedToId] = newReactorList; gReactions[reactedToId] = newReactorList;
} }
// set isHidden for reactedTo if it exists in map
if( comment == "!" && event.eventData.pubkey == userPublicKey) {
tempChildEventsMap[reactedToId]?.event.eventData.isHidden = true;
}
return reactedToId; return reactedToId;
} else { } else {
// case where its not a kind 7 event, or we can't find the reactedTo event due to absense of e tag. // case where its not a kind 7 event, or we can't find the reactedTo event due to absense of e tag.