improved logic that new event are not printed twice

This commit is contained in:
vishalxl 2022-08-17 23:13:48 +05:30
parent ee35160fec
commit 42111a7569
4 changed files with 37 additions and 39 deletions

View File

@ -54,6 +54,8 @@ int showMenu(List<String> menuOptions) {
print(" ${i+1}. ${menuOptions[i]}");
}
stdout.write("Type menu option/number: ");
//print(">");
String? userOptionInput = stdin.readLineSync();
String userOption = userOptionInput??"";
@ -95,8 +97,8 @@ Future<void> terminalMenuUi(Tree node, var contactList) async {
const int waitMilliSeconds = 400;
Future.delayed(const Duration(milliseconds: waitMilliSeconds), () {
node.insertEvents(getRecievedEvents());
node.printNotifications(getRecievedEvents());
List<String> newEventsId = node.insertEvents(getRecievedEvents());
node.printNotifications(newEventsId);
clearEvents();
});
@ -119,16 +121,20 @@ Future<void> terminalMenuUi(Tree node, var contactList) async {
break;
case 2:
print("Type comment to post/reply: ");
stdout.write("Type comment to post/reply: ");
String? $contentVar = stdin.readLineSync();
String content = $contentVar??"";
if( content == "") {
break;
}
print("Type id of event to reply to ( leave blank if you want to make a post): ");
stdout.write("Type initial few letters of the id of event to reply to ( leave blank if you want to make a post; type x if you want to cancel): ");
String? $replyToVar = stdin.readLineSync();
String replyToId = $replyToVar??"";
if( replyToId == "x") {
print("Cancelling post/reply.");
break;
}
String strTags = node.getTagStr(replyToId, exename);
int createdAt = DateTime.now().millisecondsSinceEpoch ~/1000;

View File

@ -3,12 +3,10 @@ import 'dart:convert';
import 'package:intl/intl.dart';
const int screenWidth = 120;
const bool enableVerticalLines = false;
const int spacesPerDepth = 8;
const int keyLenPrinted = 6;
const int max_depth_allowed = 7;
const int leftShiftDeepThreadsBy = 3;
const int maxDepthAllowed = 7;
const int leftShiftThreadsBy = 3;
const String commentColor = "\x1B[32m"; // green
const String notificationColor = "\x1b[36m"; // cyan
@ -26,12 +24,7 @@ List<String> gBots = [ "3b57518d02e6acfd5eb7198530b2e351e5a52278fb2499d14b66db2
int gDebug = 0;
void printDepth(int d) {
if( d == 0) {
return;
}
for( int i = 0; i < spacesPerDepth * d ; i++) {
stdout.write(" ");
}

View File

@ -40,8 +40,10 @@ class Tree {
return Tree( events[0], topLevelTrees, mAllEvents); // TODO remove events[0]
} // end fromEvents()
bool insertEvents(List<Event> newEvents) {
//print("In insertEvents num events: ${newEvents.length}");
/*
* @insertEvents inserts the given new events into the tree, and returns the id the ones actually inserted
*/
List<String> insertEvents(List<Event> newEvents) {
List<String> newEventsId = [];
newEvents.forEach((element) {
@ -72,7 +74,7 @@ class Tree {
}
});
return true;
return newEventsId;
}
void addChild(Event child) {
@ -114,8 +116,8 @@ class Tree {
// if the thread becomes too 'deep' then reset its depth, so that its
// children will not be displayed too much the right, but are shifted
// left by about <leftShiftDeepThreadsBy> places
if( depth > max_depth_allowed) {
depth = max_depth_allowed - leftShiftDeepThreadsBy;
if( depth > maxDepthAllowed) {
depth = maxDepthAllowed - leftShiftThreadsBy;
printDepth(depth+1);
stdout.write("+-------------------------------+\n");
@ -137,41 +139,40 @@ class Tree {
return t;
}
void printNotifications(List<Event> events) {
/*
* @printNotifications Add the given events to the Tree, and print the events as notifications
* It should be ensured that these are only kind 1 events
*/
void printNotifications(List<String> newEventsId) {
// remove duplicate
Set temp = {};
events.retainWhere((x) => temp.add(x.eventData.id));
stdout.write("\n\n\n\n\n\n---------------------------------------\nNotifications:");
newEventsId.retainWhere((event) => temp.add(newEventsId));
if( events.isEmpty) {
stdout.write("\n\n\n\n\n\n---------------------------------------\nNotifications:");
if( newEventsId.isEmpty) {
stdout.write("No new replies/posts.");
return;
}
stdout.write("Number of new replies/posts = ${events.length}\n");
stdout.write("Number of new replies/posts = ${newEventsId.length}\n");
stdout.write("\nHere are the threads with new replies: \n\n");
//events.forEach((element) {element.eventData.printEventData(0);});
events.forEach((element) {
newEventsId.forEach((eventID) {
// ignore if not in Tree
if( allEvents[element.eventData.id] == null) {
if( allEvents[eventID] == null) {
return;
}
if( element.eventData.kind != 1) {
return; // only type 1 are printed
}
Tree ?t = allEvents[element.eventData.id];
Tree ?t = allEvents[eventID];
if( t != null) {
t.e.eventData.isNotification = true;
Tree topTree = getTopTree(t);
topTree.printTree(0, false, 0);
print("\n");
}
});
}
/*
* @getTagsFromEvent Searches for all events, and creates a json of e-tag type which can be sent with event
* Also adds 'client' tag with application name.
@ -225,9 +226,11 @@ int ascendingTimeTree(Tree a, Tree b) {
return 1;
}
Tree getTree(events) {
if( events.length == 0) {
/*
* @function getTree Creates a Tree out of these received List of events.
*/
Tree getTree(List<Event> events) {
if( events.isEmpty) {
print("Warning: In printEventsAsTree: events length = 0");
return Tree(Event("","",EventData("non","", 0, 0, "", [], [], [], [[]]), [""], "[json]"), [], {});
}

View File

@ -12,8 +12,6 @@ Event exampleEventChild = Event('event', 'id4', exampleEdataChild, ['relay name'
Tree exampleNode = Tree(exampleEvent, [], {});
Tree exampleNodeChild = Tree(exampleEventChild, [], {});
void main() {
test('PrintEmptyEvent', () {
expect(EventData("non","",1,1,"", [], [], [], [[]]).toString(), "");
@ -33,7 +31,6 @@ void main() {
test('createNodeTree_ordered', () {
Event exampleEvent1 = Event.fromJson('["EVENT","latest",{"id":"167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de","pubkey":"e37d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722388,"kind":1,"tags":[],"content":"nostr is not federated is it? this is like a global feed of all nostr freaks?","sig":"6db0b287015d9529dfbacef91561cb4e32afd6968edd8454867b8482bde01452e17b6f3de69bffcb2d9deba2a52d3c9ff82e04f7b18eb32428daf7eab5fd27c5"}]', "");
Event exampleEvent2 = Event.fromJson('["EVENT","latest",{"id":"f3a267ecbb631012da618de620bc1fe265f6429f412359bf02330b437cf88e67","pubkey":"e37d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722463,"kind":1,"tags":[["e","167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de"]],"content":"I dont get the technical stuff about relays and things","sig":"9f68031687214a24862226f291e3baadd956dc14ba9c5c552f8c881a40aacd34feda667ef4e4b09711cd43950eec2d272d5b11bd7636de5f457f38f31eaff398"}]', "");
Event exampleEvent3 = Event.fromJson('["EVENT","latest",{"id":"dfc5765da281c0ad99cb8693fc98c87f0f86ad56042a414f06f19d41c1315fc3","pubkey":"e37d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722537,"kind":1,"tags":[["e","167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de"],["e","f3a267ecbb631012da618de620bc1fe265f6429f412359bf02330b437cf88e67"]],"content":"different clients make sense to me. I can use different clients to access nostr but is just one giant soup like twitter","sig":"d4fdc288e3cb95fc5ab46177fc0982d2aaa3b028eef6649f8200500da9c2e9a16c7a0462638afef7635bfea3094ec10901de759a48e362b60cb08f7e6585e02f"}]', "");
@ -47,7 +44,6 @@ void main() {
test('createNodeTree_unordered1', () {
Event exampleEvent1 = Event.fromJson('["EVENT","latest",{"id":"167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de","pubkey":"e37d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722388,"kind":1,"tags":[],"content":"nostr is not federated is it? this is like a global feed of all nostr freaks?","sig":"6db0b287015d9529dfbacef91561cb4e32afd6968edd8454867b8482bde01452e17b6f3de69bffcb2d9deba2a52d3c9ff82e04f7b18eb32428daf7eab5fd27c5"}]', "");
Event exampleEvent2 = Event.fromJson('["EVENT","latest",{"id":"f3a267ecbb631012da618de620bc1fe265f6429f412359bf02330b437cf88e67","pubkey":"e37d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722463,"kind":1,"tags":[["e","167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de"]],"content":"I dont get the technical stuff about relays and things","sig":"9f68031687214a24862226f291e3baadd956dc14ba9c5c552f8c881a40aacd34feda667ef4e4b09711cd43950eec2d272d5b11bd7636de5f457f38f31eaff398"}]', "");
Event exampleEvent3 = Event.fromJson('["EVENT","latest",{"id":"dfc5765da281c0ad99cb8693fc98c87f0f86ad56042a414f06f19d41c1315fc3","pubkey":"e37d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722537,"kind":1,"tags":[["e","167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de"],["e","f3a267ecbb631012da618de620bc1fe265f6429f412359bf02330b437cf88e67"]],"content":"different clients make sense to me. I can use different clients to access nostr but is just one giant soup like twitter","sig":"d4fdc288e3cb95fc5ab46177fc0982d2aaa3b028eef6649f8200500da9c2e9a16c7a0462638afef7635bfea3094ec10901de759a48e362b60cb08f7e6585e02f"}]', "");