From ea250f8b728a4231ce57185f377f3ccd2511349a Mon Sep 17 00:00:00 2001 From: Vishal <64505169+vishalxl@users.noreply.github.com> Date: Fri, 16 Sep 2022 22:28:42 +0530 Subject: [PATCH] fix for getParent function which should return the last e tag, which is a type 1 event for social network. wasn't doing the check for kind 1, previously which has been fixed now, precipitated by event c185bb5473912f2ad74d7d0d25464af0ddeaab0261af32d4de66709b99928ac0 which refers to a kind 42 event at end, which was being taken as parent id. --- bin/nostr_console.dart | 2 +- lib/console_ui.dart | 2 +- lib/event_ds.dart | 14 +++++++++++--- lib/settings.dart | 2 +- lib/tree_ds.dart | 26 +++++++++++++++++++++----- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/bin/nostr_console.dart b/bin/nostr_console.dart index f77d99a..4b97f7c 100644 --- a/bin/nostr_console.dart +++ b/bin/nostr_console.dart @@ -252,7 +252,7 @@ Future main(List arguments) async { // then display them all gDefaultFollows.add(userPublicKey); - getUserEvents(gListRelayUrls1, userPublicKey, gLimitPerSubscription, getSecondsDaysAgo(gDaysToGetEventsFor)); + //getUserEvents(gListRelayUrls1, userPublicKey, gLimitPerSubscription, getSecondsDaysAgo(gDaysToGetEventsFor)); getMultiUserEvents(gListRelayUrls1, gDefaultFollows, 1000, getSecondsDaysAgo(gDaysToGetEventsFor)); getMentionEvents(gListRelayUrls2, userPublicKey, gLimitPerSubscription, getSecondsDaysAgo(gDaysToGetEventsFor)); // from relay group 2 getKindEvents([0, 3, 40, 42], gListRelayUrls1, gLimitPerSubscription, getSecondsDaysAgo(gDaysToGetEventsFor* 10)); diff --git a/lib/console_ui.dart b/lib/console_ui.dart index 83133a3..474d5ea 100644 --- a/lib/console_ui.dart +++ b/lib/console_ui.dart @@ -195,7 +195,7 @@ void reAdjustAlignment() { void printProfile(Store node, String profilePubkey) { bool onlyUserPostAndLike (Tree t) => t.treeSelectorUserPostAndLike(profilePubkey); node.printTree(0, DateTime.now().subtract(Duration(days:gNumLastDays)), onlyUserPostAndLike); - + // get the latest kind 3 event for the user, which lists his 'follows' list Event? profileContactEvent = getContactEvent(profilePubkey); diff --git a/lib/event_ds.dart b/lib/event_ds.dart index e86aa12..0941f3d 100644 --- a/lib/event_ds.dart +++ b/lib/event_ds.dart @@ -65,9 +65,17 @@ class EventData { bool isHidden; // hidden by sending a reaction kind 7 event to this event, by the logged in user bool isDeleted; // deleted by kind 5 event - String getParent() { + String getParent(Map allEventsMap) { if( eTags.isNotEmpty) { - return eTags[eTags.length - 1]; + for( int i = eTags.length - 1; i >= 0; i--) { + String eventId = eTags[i]; + if( allEventsMap[eventId]?.event.eventData.kind == 1) { + String? parentId = allEventsMap[eventId]?.event.eventData.id; + if( parentId != null) { + return parentId; + } + } + } } return ""; } @@ -496,7 +504,7 @@ class EventData { return reactorNames; } - // returns the last e tag as reply to event + // returns the last e tag as reply to event for kind 42 events Event? getReplyToEvent() { for(int i = tags.length - 1; i >= 0; i--) { List tag = tags[i]; diff --git a/lib/settings.dart b/lib/settings.dart index 72eefc8..462ec3b 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -6,7 +6,7 @@ import 'package:logging/logging.dart'; final log = Logger('ExampleLogger'); // for debugging -String gCheckEventId = ""; +String gCheckEventId = "zzzz"; const int gDefaultNumWaitSeconds = 2000; // is used in main() diff --git a/lib/tree_ds.dart b/lib/tree_ds.dart index fe93ca9..78dac2e 100644 --- a/lib/tree_ds.dart +++ b/lib/tree_ds.dart @@ -668,20 +668,36 @@ class Store { return; } + if( tree.event.eventData.id == gCheckEventId) { + print("In fromEvent: got evnet id $gCheckEventId"); + } + if(tree.event.eventData.eTags.isNotEmpty ) { // is not a parent, find its parent and then add this element to that parent Tree - String parentId = tree.event.eventData.getParent(); + String parentId = tree.event.eventData.getParent(tempChildEventsMap); + if( tree.event.eventData.id == gCheckEventId) { - if(gDebug >= 0) print("In Tree FromEvents: got id: $gCheckEventId"); + if(gDebug >= 0) print("In Tree FromEvents: e tag not empty. its parent id = $parentId for id: $gCheckEventId"); } if(tempChildEventsMap.containsKey( parentId)) { - if( tempChildEventsMap[parentId]?.event.eventData.kind != 1) { // since parent can only be a kind 1 event + if( tree.event.eventData.id == gCheckEventId) { + if(gDebug >= 0) print("In Tree FromEvents: found its parent $parentId : for id: $gCheckEventId"); + } + + if( tempChildEventsMap[parentId]?.event.eventData.kind != 1) { // since parent can only be a kind 1 event if( gDebug > 1) log.info("In Tree.fromEvents: Not adding: got a kind 1 event whose parent is not a type 1 post: $newEventId . parent kind: ${tempChildEventsMap[parentId]?.event.eventData.kind}"); return; } + + tempChildEventsMap[parentId]?.children.add(tree); } else { + + if( tree.event.eventData.id == gCheckEventId) { + if(gDebug >= 0) print("In Tree FromEvents: parent not found : for id: $gCheckEventId"); + } + // in case where the parent of the new event is not in the pool of all events, // then we create a dummy event and put it at top ( or make this a top event?) TODO handle so that this can be replied to, and is fetched Event dummy = Event("","", EventData(parentId,gDummyAccountPubkey, tree.event.eventData.createdAt, 1, "Event not loaded", [], [], [], [[]], {}), [""], "[json]"); @@ -815,7 +831,7 @@ class Store { topPosts.add(newTree); } else { // if it has a parent , then add the newTree as the parent's child - String parentId = newTree.event.eventData.getParent(); + String parentId = newTree.event.eventData.getParent(allChildEventsMap); if( allChildEventsMap.containsKey(parentId)) { allChildEventsMap[parentId]?.children.add(newTree); } else { @@ -1388,7 +1404,7 @@ class Store { // for any tree node, returns its top most parent Tree getTopTree(Tree tree) { while( true) { - Tree? parent = allChildEventsMap[ tree.event.eventData.getParent()]; + Tree? parent = allChildEventsMap[ tree.event.eventData.getParent(allChildEventsMap)]; if( parent != null) { tree = parent; } else {