mirror of
https://github.com/vishalxl/nostr_console.git
synced 2025-06-30 02:21:08 +02:00
fetched reacted-to events not in store
also added location flag but did not use it. improved function structure in tree.dart
This commit is contained in:
@ -26,6 +26,7 @@ const String difficultyArg = "difficulty";
|
||||
const String translateArg = "translate";
|
||||
const String colorArg = "color";
|
||||
const String overWriteFlag = "overwrite";
|
||||
const String locationArg = "location";
|
||||
|
||||
Future<void> main(List<String> arguments) async {
|
||||
|
||||
@ -40,6 +41,7 @@ Future<void> main(List<String> arguments) async {
|
||||
..addOption(colorArg, abbr:"c")
|
||||
..addOption(difficultyArg, abbr:"y")
|
||||
..addFlag(overWriteFlag, abbr:"e", defaultsTo: false)
|
||||
..addOption(locationArg, abbr:"l")
|
||||
..addFlag("debug");
|
||||
try {
|
||||
ArgResults argResults = parser.parse(arguments);
|
||||
@ -80,6 +82,16 @@ Future<void> main(List<String> arguments) async {
|
||||
translator = GoogleTranslator();
|
||||
}
|
||||
|
||||
// get location of user if given
|
||||
if( argResults[locationArg] != null) {
|
||||
gUserLocation = argResults[locationArg];
|
||||
userPrivateKey = "";
|
||||
}
|
||||
|
||||
if( gUserLocation.length > 0){
|
||||
print("Going to add $gUserLocation as the location tag with each post.");
|
||||
}
|
||||
|
||||
if( argResults[pubkeyArg] != null) {
|
||||
userPublicKey = argResults[pubkeyArg];
|
||||
if( userPublicKey.length != 64){
|
||||
|
@ -96,6 +96,8 @@ List<String> gDefaultFollows = [
|
||||
// dummy account pubkey
|
||||
const String gDummyAccountPubkey = "Non";
|
||||
|
||||
String gUserLocation = "";
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// UI and Color
|
||||
const int gMinValidTextWidth = 60; // minimum text width acceptable
|
||||
const int gDefaultTextWidth = 96; // default text width
|
||||
|
192
lib/tree_ds.dart
192
lib/tree_ds.dart
@ -987,93 +987,8 @@ class Store {
|
||||
return numMessagesDecrypted;
|
||||
}
|
||||
|
||||
/***********************************************************************************************************************************/
|
||||
// @method create top level Tree from events.
|
||||
// first create a map. then process each element in the map by adding it to its parent ( if its a child tree)
|
||||
factory Store.fromEvents(Set<Event> events) {
|
||||
if( events.isEmpty) {
|
||||
List<DirectMessageRoom> temp = [];
|
||||
|
||||
return Store( [], {}, [], [], [], temp, {});
|
||||
}
|
||||
|
||||
// create a map tempChildEventsMap from list of events, key is eventId and value is event itself
|
||||
Map<String, Tree> tempChildEventsMap = {};
|
||||
events.forEach((event) {
|
||||
// only add in map those kinds that are supported or supposed to be added ( 0 1 3 7 40)
|
||||
if( typesInEventMap.contains(event.eventData.kind)) {
|
||||
tempChildEventsMap[event.eventData.id] = Tree.withoutStore( event, []);
|
||||
}
|
||||
});
|
||||
|
||||
processDeleteEvents(tempChildEventsMap); // handle returned values perhaps later
|
||||
processReactions(events, tempChildEventsMap);
|
||||
|
||||
// once tempChildEventsMap has been created, create connections between them so we get a tree structure from all these events.
|
||||
List<Tree> topLevelTrees = [];// this will become the children of the main top node. These are events without parents, which are printed at top.
|
||||
List<String> tempWithoutParent = [];
|
||||
List<Channel> channels = [];
|
||||
List<Channel> encryptedChannels = [];
|
||||
List<DirectMessageRoom> tempDirectRooms = [];
|
||||
Set<String> dummyEventIds = {};
|
||||
Set<String> allEncryptedGroupInviteIds = {};
|
||||
|
||||
tempChildEventsMap.forEach((newEventId, tree) {
|
||||
int eKind = tree.event.eventData.kind;
|
||||
|
||||
// these are handled in another iteration ( cause first private messages need to be populated)
|
||||
if( eKind >= 140 && eKind <= 142 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if( eKind == 42 || eKind == 40) {
|
||||
handleChannelEvents(channels, tempChildEventsMap, tree.event);
|
||||
return;
|
||||
}
|
||||
|
||||
if( eKind == 4) {
|
||||
handleDirectMessage(tempDirectRooms, tempChildEventsMap, tree.event);
|
||||
return;
|
||||
}
|
||||
|
||||
if( eKind == gSecretMessageKind) {
|
||||
// add the event id to given structure if its a valid message
|
||||
if( isValidDirectMessage(tree.event.eventData, acceptableKind: gSecretMessageKind)) {
|
||||
//print("adding to enc list");
|
||||
allEncryptedGroupInviteIds.add(tree.event.eventData.id);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if( eKind == 7) {
|
||||
processReaction(tree.event, tempChildEventsMap);
|
||||
return;
|
||||
}
|
||||
|
||||
// if reacted to event is not in store, then add it to dummy list so it can be fetched
|
||||
try {
|
||||
if( tree.event.eventData.eTags.length > 0) {
|
||||
if (tree.event.eventData.eTags.last.length > 0) {
|
||||
String reactedToId = tree.event.eventData.eTags.last[0];
|
||||
// only if the reaction was made recently in last 3 days
|
||||
// TODO while storing need to store these newly fetched events too; otherwise these are fetched everytime
|
||||
if( false && !tempChildEventsMap.containsKey(reactedToId) && tree.event.eventData.createdAt > getSecondsDaysAgo(3)) {
|
||||
print("liked event not found in store.");
|
||||
dummyEventIds.add(reactedToId);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(e) {
|
||||
print(e);
|
||||
}
|
||||
|
||||
if( tree.event.eventData.id == gCheckEventId) {
|
||||
print("In fromEvent: got evnet id $gCheckEventId");
|
||||
}
|
||||
|
||||
if( tree.event.eventData.kind != 1) {
|
||||
return;
|
||||
}
|
||||
static void handleInitialKind1(Tree tree, Map<String, Tree> tempChildEventsMap,
|
||||
List<Tree> topLevelTrees, List<String> tempWithoutParent, Set<String> eventIdsToFetch) {
|
||||
|
||||
// find its parent and then add this element to that parent Tree
|
||||
String parentId = tree.event.eventData.getParent(tempChildEventsMap);
|
||||
@ -1138,7 +1053,7 @@ class Store {
|
||||
Tree dummyTopNode = Tree.withoutStore(dummy, []);
|
||||
dummyTopNode.children.add(tree);
|
||||
tempWithoutParent.add(tree.event.eventData.id);
|
||||
dummyEventIds.add(parentId);
|
||||
eventIdsToFetch.add(parentId);
|
||||
topLevelTrees.add(dummyTopNode);
|
||||
}
|
||||
//printWarning("Added unknown event as top : ${parentId}");
|
||||
@ -1154,6 +1069,91 @@ class Store {
|
||||
} else {
|
||||
// is not a parent, has no parent tag. then make it its own top tree, which will be done later in this function
|
||||
}
|
||||
}
|
||||
|
||||
/***********************************************************************************************************************************/
|
||||
// @method create top level Tree from events.
|
||||
// first create a map. then process each element in the map by adding it to its parent ( if its a child tree)
|
||||
factory Store.fromEvents(Set<Event> events) {
|
||||
if( events.isEmpty) {
|
||||
List<DirectMessageRoom> temp = [];
|
||||
|
||||
return Store( [], {}, [], [], [], temp, {});
|
||||
}
|
||||
|
||||
// create a map tempChildEventsMap from list of events, key is eventId and value is event itself
|
||||
Map<String, Tree> tempChildEventsMap = {};
|
||||
events.forEach((event) {
|
||||
// only add in map those kinds that are supported or supposed to be added ( 0 1 3 7 40)
|
||||
if( typesInEventMap.contains(event.eventData.kind)) {
|
||||
tempChildEventsMap[event.eventData.id] = Tree.withoutStore( event, []);
|
||||
}
|
||||
});
|
||||
|
||||
processDeleteEvents(tempChildEventsMap); // handle returned values perhaps later
|
||||
processReactions(events, tempChildEventsMap);
|
||||
|
||||
// once tempChildEventsMap has been created, create connections between them so we get a tree structure from all these events.
|
||||
List<Tree> topLevelTrees = [];// this will become the children of the main top node. These are events without parents, which are printed at top.
|
||||
List<String> tempWithoutParent = [];
|
||||
List<Channel> channels = [];
|
||||
List<Channel> encryptedChannels = [];
|
||||
List<DirectMessageRoom> tempDirectRooms = [];
|
||||
Set<String> eventIdsToFetch = {};
|
||||
Set<String> allEncryptedGroupInviteIds = {};
|
||||
|
||||
tempChildEventsMap.forEach((newEventId, tree) {
|
||||
int eKind = tree.event.eventData.kind;
|
||||
|
||||
// these are handled in another iteration ( cause first private messages need to be populated)
|
||||
if( eKind >= 140 && eKind <= 142 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if( eKind == 42 || eKind == 40) {
|
||||
handleChannelEvents(channels, tempChildEventsMap, tree.event);
|
||||
return;
|
||||
}
|
||||
|
||||
if( eKind == 4) {
|
||||
handleDirectMessage(tempDirectRooms, tempChildEventsMap, tree.event);
|
||||
return;
|
||||
}
|
||||
|
||||
if( eKind == gSecretMessageKind) {
|
||||
// add the event id to given structure if its a valid message
|
||||
if( isValidDirectMessage(tree.event.eventData, acceptableKind: gSecretMessageKind)) {
|
||||
//print("adding to enc list");
|
||||
allEncryptedGroupInviteIds.add(tree.event.eventData.id);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if( eKind == 7) {
|
||||
processReaction(tree.event, tempChildEventsMap);
|
||||
return;
|
||||
}
|
||||
|
||||
// if reacted to event is not in store, then add it to dummy list so it can be fetched
|
||||
if( tree.event.eventData.eTags.length > 0 && tree.event.eventData.eTags.last.length > 0) {
|
||||
String reactedToId = tree.event.eventData.eTags.last[0];
|
||||
if( !tempChildEventsMap.containsKey(reactedToId) && tree.event.eventData.createdAt > getSecondsDaysAgo(3)) {
|
||||
//print("liked event not found in store.");
|
||||
eventIdsToFetch.add(reactedToId);
|
||||
}
|
||||
}
|
||||
|
||||
if( tree.event.eventData.id == gCheckEventId) {
|
||||
print("In fromEvent: got evnet id $gCheckEventId");
|
||||
}
|
||||
|
||||
if( tree.event.eventData.kind != 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// will handle kind 1
|
||||
handleInitialKind1(tree, tempChildEventsMap, topLevelTrees, tempWithoutParent, eventIdsToFetch);
|
||||
|
||||
}); // going over tempChildEventsMap and adding children to their parent's .children list
|
||||
|
||||
// for pubkeys that don't have any kind 0 events ( but have other evnets), add then to global kind0 store so they can still be accessed
|
||||
@ -1185,7 +1185,6 @@ class Store {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
//print("num channels created by 104: ${encryptedChannels.length}");
|
||||
// add parent trees as top level child trees of this tree
|
||||
for( var tree in tempChildEventsMap.values) {
|
||||
@ -1197,24 +1196,11 @@ class Store {
|
||||
if(gDebug != 0) print("In Tree FromEvents: number of events without parent in fromEvents = ${tempWithoutParent.length}");
|
||||
|
||||
// get dummy events and encryped channel create events
|
||||
sendEventsRequest(gListRelayUrls1, dummyEventIds.union(usersEncryptedChannelIds));
|
||||
sendEventsRequest(gListRelayUrls1, eventIdsToFetch.union(usersEncryptedChannelIds));
|
||||
|
||||
// get encrypted channel events, get 141/142 by their mention of channels to which user has been invited through kind 104. get 140 by its event id.
|
||||
getMentionEvents(gListRelayUrls1, usersEncryptedChannelIds, gLimitFollowPosts, getSecondsDaysAgo(gDefaultNumLastDays), "#e"); // from relay group 2
|
||||
|
||||
//sendEventsRequest(gListRelayUrls1, usersEncryptedChannelIds);
|
||||
|
||||
/*
|
||||
print("allEncryptedGroupInviteIds = ${allEncryptedGroupInviteIds.length} $allEncryptedGroupInviteIds");
|
||||
print("usersEncryptedGroupIds = ${usersEncryptedGroupIds.length} $usersEncryptedGroupIds");
|
||||
|
||||
Set<String> d1 = usersEncryptedGroupIds.difference(allEncryptedGroupInviteIds);
|
||||
print("d1 = ${d1.length} $d1");
|
||||
|
||||
Set<String> d2 = allEncryptedGroupInviteIds.difference(usersEncryptedGroupIds);
|
||||
print("d2 = ${d2.length} $d2");
|
||||
*/
|
||||
|
||||
// create a dummy top level tree and then create the main Tree object
|
||||
return Store( topLevelTrees, tempChildEventsMap, tempWithoutParent, channels, encryptedChannels, tempDirectRooms, allEncryptedGroupInviteIds);
|
||||
} // end fromEvents()
|
||||
|
Reference in New Issue
Block a user