From 9abe554bd5b2961d3dc500cff6384e8fdff31d3d Mon Sep 17 00:00:00 2001 From: Vishal <64505169+vishalxl@users.noreply.github.com> Date: Mon, 26 Dec 2022 15:28:24 +0530 Subject: [PATCH] Improved menu options 2-5 and 2-6 and used the same functions ( which now accept array of pubkeys) --- README.md | 2 +- bin/nostr_console.dart | 2 +- lib/console_ui.dart | 18 ++++++++++-------- lib/event_ds.dart | 10 +++++----- lib/tree_ds.dart | 31 ++++++++++++++++--------------- lib/user.dart | 13 +++++++++++++ pubspec.yaml | 3 +++ 7 files changed, 49 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index baeefac..d549cb1 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ This is an experimental or pre-alpha software made to show or know what a Nostr * [ ] in mention expansion, if p tag is not found in user store, then its left as #[n], whereas it should be replaced by the pubkey * [x] notifications should show mentions too ( it does not yet) * [x] notifications , option 3, is shown only for one entry in whole thread -* [ ] hashtag regexp should have underscore +* [x] hashtag regexp should have underscore (seems to be working fine) * [x] add more default users. improve who is fetched. * [ ] after going to a dm room, screen doesn't clear * [x] when seeing a profile, if they have liked something, then likes after their name are shown white diff --git a/bin/nostr_console.dart b/bin/nostr_console.dart index b031112..f036758 100644 --- a/bin/nostr_console.dart +++ b/bin/nostr_console.dart @@ -364,7 +364,7 @@ Future main(List arguments) async { if (contactEvent != null ) { if(gDebug > 0) print("In main: found contact list: \n ${contactEvent.originalJson}"); contactEvent.eventData.contactList.forEach((contact) { - contacts.add(contact.id); + contacts.add(contact.contactPubkey); }); } else { print("Could not find your contact list."); diff --git a/lib/console_ui.dart b/lib/console_ui.dart index 277e42e..62c2e5a 100644 --- a/lib/console_ui.dart +++ b/lib/console_ui.dart @@ -196,7 +196,7 @@ Future sendEvent(Store node, Event e, [int delayAfterSend = 500]) async if( i == e.eventData.contactList.length - 1) { comma = ""; } - String strContact = '["p","${e.eventData.contactList[i].id}","$relay"]$comma'; + String strContact = '["p","${e.eventData.contactList[i].contactPubkey}","$relay"]$comma'; strTags += strContact; } @@ -293,7 +293,7 @@ void reAdjustAlignment() { } void printProfile(Store node, String profilePubkey) { - bool onlyUserPostAndLike (Tree t) => t.treeSelectorUserPostAndLike(profilePubkey); + bool onlyUserPostAndLike (Tree t) => t.treeSelectorUserPostAndLike({profilePubkey}); node.printTree(0, DateTime.now().subtract(Duration(hours:gHoursDefaultPrint)), onlyUserPostAndLike); // if contact list was found, get user's feed, and keep the contact list for later use @@ -332,7 +332,7 @@ void printProfile(Store node, String profilePubkey) { if (profileContactEvent != null ) { if( profilePubkey != userPublicKey) { - if( profileContactEvent.eventData.contactList.any((x) => (x.id == userPublicKey))) { + if( profileContactEvent.eventData.contactList.any((x) => (x.contactPubkey == userPublicKey))) { print("* They follow you"); } else { print("* They don't follow you"); @@ -346,7 +346,7 @@ void printProfile(Store node, String profilePubkey) { // print follow list stdout.write("$pronoun follow ${profileContactEvent.eventData.contactList.length} accounts: "); profileContactEvent.eventData.contactList.sort(); - profileContactEvent.eventData.contactList.forEach((x) => stdout.write("${getAuthorName(x.id, 0)}, ")); + profileContactEvent.eventData.contactList.forEach((x) => stdout.write("${getAuthorName(x.contactPubkey, 0)}, ")); print("\n"); } @@ -1283,7 +1283,7 @@ Future socialMenuUi(Store node) async { case 3: clearScreen(); - bool selectorTrees_userNotifications (Tree t) => t.treeSelectorRepliesAndLikes({userPublicKey}); + bool selectorTrees_userNotifications (Tree t) => t.treeSelectorotificationsFor({userPublicKey}); int notificationHours = gHoursDefaultPrint>24? gHoursDefaultPrint: 24; // minimum 24 Point numPrinted = node.printTree(0, DateTime.now().subtract(Duration(hours:notificationHours)), selectorTrees_userNotifications); if( numPrinted.y > 0) { @@ -1303,7 +1303,8 @@ Future socialMenuUi(Store node) async { break; case 5: clearScreen(); - Point numPrinted = node.printTree(0, DateTime.now().subtract(Duration(hours:gHoursDefaultPrint)), selectorTrees_userRepliesLikes); + bool selectorTrees_userActions (Tree t) => t.treeSelectorUserPostAndLike({userPublicKey}); + Point numPrinted = node.printTree(0, DateTime.now().subtract(Duration(hours:gHoursDefaultPrint)), selectorTrees_userActions); if( numPrinted.x > 0) { print("Showed ${numPrinted.x.toInt()} thread where you replied or liked in in last $gHoursDefaultPrint hours.\n"); } else { @@ -1312,7 +1313,8 @@ Future socialMenuUi(Store node) async { break; case 6: clearScreen(); - Point numPrinted = node.printTree(0, DateTime.now().subtract(Duration(hours:gHoursDefaultPrint)), selectorTrees_followsPosts); + bool selectorTrees_followActions (Tree t) => t.treeSelectorUserPostAndLike(getFollows( userPublicKey)); + Point numPrinted = node.printTree(0, DateTime.now().subtract(Duration(hours:gHoursDefaultPrint)), selectorTrees_followActions); if( numPrinted.x > 0) { print("Showed ${numPrinted.x.toInt()} threads where your follows participated.\n"); } else { @@ -1384,7 +1386,7 @@ Future socialMenuUi(Store node) async { bool alreadyContact = false; for(int i = 0; i < newContactEvent.eventData.contactList.length; i++) { - if( newContactEvent.eventData.contactList[i].id == pubkey.first) { + if( newContactEvent.eventData.contactList[i].contactPubkey == pubkey.first) { alreadyContact = true; break; } diff --git a/lib/event_ds.dart b/lib/event_ds.dart index 7cb6e5e..0293a33 100644 --- a/lib/event_ds.dart +++ b/lib/event_ds.dart @@ -1027,7 +1027,7 @@ String getRelayOfUser(String userPubkey, String contactPubkey) { List? contacts = gContactLists[userPubkey]; if( contacts != null) { for( int i = 0; i < contacts.length; i++) { - if( contacts[i].id == contactPubkey) { + if( contacts[i].contactPubkey == contactPubkey) { relay = contacts[i].relay; return relay; } @@ -1392,17 +1392,17 @@ List getLineWithMaxLen(String s, int startIndex, int lenPerLine, String spacesSt // The contact only stores id and relay of contact. The actual name is stored in a global variable/map class Contact implements Comparable { - String id, relay; - Contact(this.id, this.relay); + String contactPubkey, relay; + Contact(this.contactPubkey, this.relay); @override String toString() { - return 'id: $id ( ${getAuthorName(id)}) relay: $relay'; + return 'id: $contactPubkey ( ${getAuthorName(contactPubkey)}) relay: $relay'; } @override int compareTo(Contact other) { - return getAuthorName(id).compareTo(getAuthorName(other.id)); + return getAuthorName(contactPubkey).compareTo(getAuthorName(other.contactPubkey)); } } diff --git a/lib/tree_ds.dart b/lib/tree_ds.dart index 834e477..7482bb4 100644 --- a/lib/tree_ds.dart +++ b/lib/tree_ds.dart @@ -114,7 +114,7 @@ bool followsInvolved(Event e, Event? contactEvent) { } // if its an event by any of the contact - if(contactEvent.eventData.contactList.any((contact) => e.eventData.pubkey == contact.id )) { + if(contactEvent.eventData.contactList.any((contact) => e.eventData.pubkey == contact.contactPubkey )) { return true; } @@ -124,7 +124,7 @@ bool followsInvolved(Event e, Event? contactEvent) { if( reactors != null) { for( var reactor in reactors) { String reactorPubkey = reactor[0]; - if(contactEvent.eventData.contactList.any((contact) => reactorPubkey == contact.id )) { + if(contactEvent.eventData.contactList.any((contact) => reactorPubkey == contact.contactPubkey )) { return true; } } @@ -490,7 +490,7 @@ class Tree { // returns true if the tree or its children has a reply or like for the user with public key pk; and notification flags are set for such events // only new controls whether replies/likes recieved are ignored if the user has already - bool treeSelectorRepliesAndLikes(Set pubkeys, [bool onlyNew = false]) { + bool treeSelectorotificationsFor(Set pubkeys, [bool onlyNew = false]) { bool hasReaction = false; bool childMatches = false; bool isMentioned = false; @@ -529,7 +529,7 @@ class Tree { } for( int i = 0; i < children.length; i++ ) { - if( children[i].treeSelectorRepliesAndLikes(pubkeys)) { + if( children[i].treeSelectorotificationsFor(pubkeys)) { childMatches = true; } } @@ -541,17 +541,16 @@ class Tree { } // returns true if the tree or its children has a post or like by user; and notification flags are set for such events - bool treeSelectorUserPostAndLike(String pubkey) { + bool treeSelectorUserPostAndLike(Set pubkeys) { bool hasReacted = false; if( gReactions.containsKey(event.eventData.id)) { List>? reactions = gReactions[event.eventData.id]; if( reactions != null) { for( int i = 0; i < reactions.length; i++) { - if( reactions[i][0] == pubkey) { - event.eventData.newLikes.add(pubkey); + if( pubkeys.contains(reactions[i][0]) ) { + event.eventData.newLikes.add(reactions[i][0]); hasReacted = true; - break; } } } @@ -559,11 +558,13 @@ class Tree { bool childMatches = false; for( int i = 0; i < children.length; i++ ) { - if( children[i].treeSelectorUserPostAndLike(pubkey)) { + if( children[i].treeSelectorUserPostAndLike(pubkeys)) { childMatches = true; } } - if( event.eventData.pubkey == pubkey) { + + // if event is by user(s) + if( pubkeys.contains(event.eventData.pubkey)) { event.eventData.isNotification = true; return true; } @@ -2267,7 +2268,7 @@ class Store { List? contactList = userInfo.latestContactEvent?.eventData.contactList; if( contactList != null ) { for(int i = 0; i < contactList.length; i ++) { - if( contactList[i].id == pubkey) { + if( contactList[i].contactPubkey == pubkey) { followers.add(otherPubkey); return; } @@ -2298,17 +2299,17 @@ class Store { selfNumContacts = selfContacts.length; for(int i = 0; i < selfContacts.length; i ++) { // check if you follow the other account - if( selfContacts[i].id == otherPubkey) { + if( selfContacts[i].contactPubkey == otherPubkey) { isFollow = true; } // count the number of your contacts who know or follow the other account List followContactList = []; - Event? followContactEvent = getContactEvent(selfContacts[i].id); + Event? followContactEvent = getContactEvent(selfContacts[i].contactPubkey); if( followContactEvent != null) { followContactList = followContactEvent.eventData.contactList; for(int j = 0; j < followContactList.length; j++) { - if( followContactList[j].id == otherPubkey) { - mutualFollows.add(getAuthorName(selfContacts[i].id, 0)); + if( followContactList[j].contactPubkey == otherPubkey) { + mutualFollows.add(getAuthorName(selfContacts[i].contactPubkey, 0)); numSecond++; break; } diff --git a/lib/user.dart b/lib/user.dart index 76d08b7..4de9567 100644 --- a/lib/user.dart +++ b/lib/user.dart @@ -15,6 +15,19 @@ Event? getContactEvent(String pubkey) { return null; } +// returns all follows +Set getFollows(String pubkey) { + Set followPubkeys = {}; + + Event? profileContactEvent = getContactEvent(pubkey); + if( profileContactEvent != null) { + profileContactEvent.eventData.contactList.forEach((x) => followPubkeys.add(x.contactPubkey)); + //followPubkeys = profileContactEvent.eventData.contactList.toSet(); + } + + return followPubkeys; +} + Set getUserChannels(Set userEvents, String userPublicKey) { Set userChannels = {}; diff --git a/pubspec.yaml b/pubspec.yaml index e39cb64..9bfe386 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,6 +5,9 @@ homepage: https://github.com/vishalxl/nostr_console # improved user notifications , menu 2 -> 3, now likes are shown as notifications and less informative # improved 2 -> 5. better printing. and fixed white after reaction highlighed issue +# improved 2 -> 6 too, now follows posts get highlighed, and so do their reactions. +# mentions get highlighed in above menus + # 0.3.0 # added check marks; added more default users