From 202f1d14e2ef07f6b6a039d1a34e5246ef7a7997 Mon Sep 17 00:00:00 2001 From: Vishal <64505169+vishalxl@users.noreply.github.com> Date: Wed, 2 Nov 2022 20:29:34 +0530 Subject: [PATCH] added colored warnings in console_ui * some extra spam prevention ( avoided bots from 4, 42, 142) and added a spam address * reduced initial days fetched if events are read from file --- bin/nostr_console.dart | 21 ++++++++++++------ lib/console_ui.dart | 50 +++++++++++++++++++++++------------------- lib/event_ds.dart | 1 + lib/settings.dart | 8 ++++--- lib/tree_ds.dart | 12 ++++++++++ pubspec.yaml | 2 +- 6 files changed, 60 insertions(+), 34 deletions(-) diff --git a/bin/nostr_console.dart b/bin/nostr_console.dart index a8935e6..f332aa7 100644 --- a/bin/nostr_console.dart +++ b/bin/nostr_console.dart @@ -247,15 +247,22 @@ Future main(List arguments) async { // the default in case no arguments are given is: // get a user's events with all default users events // get mentions for user - // get all kind 0, 3, 40, 42 events + // get all kind 0, 3, 4x, 14x events // then get the events of user-id's mentioned in p-tags of received events and the contact list // then display them all - 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, 140, 141, 142], gListRelayUrls1, gLimitPerSubscription, getSecondsDaysAgo(gDaysToGetEventsFor* 10)); + int daysToGetEventsFor = gDaysToGetEventsFor; + + // if more than 1000 posts have already been read from the file, then don't get too many day's events. Only for last 3 days. + if(numFilePosts > 1000) { + daysToGetEventsFor = 3; + } + + getUserEvents(gListRelayUrls1, userPublicKey, gLimitPerSubscription, getSecondsDaysAgo(daysToGetEventsFor)); + getMultiUserEvents(gListRelayUrls1, gDefaultFollows, 1000, getSecondsDaysAgo(daysToGetEventsFor)); + getMentionEvents(gListRelayUrls2, userPublicKey, gLimitPerSubscription, getSecondsDaysAgo(daysToGetEventsFor)); // from relay group 2 + getKindEvents([0, 3, 40, 42, 140, 141, 142], gListRelayUrls1, gLimitPerSubscription, getSecondsDaysAgo(daysToGetEventsFor* 100)); // get all type 3 etc // TODO get all 40 events, and then get all #e for them ( responses to them) @@ -283,11 +290,11 @@ Future main(List arguments) async { contacts.add(contact.id); }); } - getContactFeed(gListRelayUrls1, contacts, gLimitPerSubscription, getSecondsDaysAgo(2 * gDaysToGetEventsFor)); + getContactFeed(gListRelayUrls1, contacts, gLimitPerSubscription, getSecondsDaysAgo(2 * daysToGetEventsFor)); // calculate top mentioned ptags, and then get the events for those users List pTags = getpTags(initialEvents, gMaxPtagsToGet); - getMultiUserEvents(gListRelayUrls1, pTags, gLimitPerSubscription, getSecondsDaysAgo(gDaysToGetEventsFor)); + getMultiUserEvents(gListRelayUrls1, pTags, gLimitPerSubscription, getSecondsDaysAgo(daysToGetEventsFor)); stdout.write('Waiting for feed to come in..............'); Future.delayed(const Duration(milliseconds: gDefaultNumWaitSeconds * 1), () { diff --git a/lib/console_ui.dart b/lib/console_ui.dart index b21e33e..ed9144d 100644 --- a/lib/console_ui.dart +++ b/lib/console_ui.dart @@ -281,9 +281,9 @@ void printProfile(Store node, String profilePubkey) { } int showMenu(List menuOptions, String menuName) { - print("\n$menuName\n${getNumDashes(menuName.length)}"); - print('Pick an option:'); while(true) { + print("\n$menuName\n${getNumDashes(menuName.length)}"); + print('Pick an option:'); for(int i = 0; i < menuOptions.length;i++) { print(" ${i+1}. ${menuOptions[i]}"); } @@ -306,7 +306,7 @@ int showMenu(List menuOptions, String menuName) { print(e); } } - print("\nInvalid option. Kindly try again. The valid options are from 1 to ${menuOptions.length}\n"); + printWarning("\nInvalid option. Kindly try again. The valid options are from 1 to ${menuOptions.length}"); } } @@ -341,11 +341,11 @@ Future otherMenuUi(Store node) async { pubkey.forEach( (x) => print(" $x ( ${gKindONames[x]?.name} )")); if( pubkey.length > 1) { if( pubkey.length > 1) { - print("Got multiple users with the same name. Try again, and try to type a more unique name or id-prefix"); + printWarning("Got multiple users with the same name. Try again, and/or type a more unique name or their full public keys."); } } else { if (pubkey.isEmpty ) { - print("Could not find the user with that id or username."); + printWarning("Could not find the user with that id or username."); } else { printProfile(node, pubkey.first); @@ -371,7 +371,7 @@ Future otherMenuUi(Store node) async { if( words != "") { bool onlyWords (Tree t) => t.treeSelectorHasWords(words.toLowerCase()); node.printTree(0, DateTime.now().subtract(Duration(days:gNumLastDays)), onlyWords); // search for last gNumLastDays only - } else print("Blank word entered. Try again."); + } else printWarning("Blank word entered. Try again."); break; @@ -388,7 +388,7 @@ Future otherMenuUi(Store node) async { case 5: // follow new contact // in case the program was invoked with --pubkey, then user can't send messages if( userPrivateKey == "") { - print("Since no user private key has been supplied, posts/messages can't be sent. Invoke with --prikey \n"); + printWarning("Since no user private key has been supplied, posts/messages can't be sent. Invoke with --prikey"); break; } @@ -401,15 +401,15 @@ Future otherMenuUi(Store node) async { print(pubkey); if( pubkey.length > 1) { if( pubkey.length > 1) { - print("Got multiple users with the same name. Try again, and type a more unique name or id-prefix"); + printWarning("Got multiple users with the same name. Try again, and type a more unique name or id-prefix"); } } else { if (pubkey.isEmpty && userName.length != 64) { - print("Could not find the user with that id or username. You can try again by providing the full 64 byte long hex public key."); + printWarning("Could not find the user with that id or username. You can try again by providing the full 64 byte long hex public key."); } else { if( pubkey.isEmpty) { - print("Could not find the user with that id or username in internal store/list. However, since the given id is 64 bytes long, taking that as hex public key and adding them as contact."); + printWarning("Could not find the user with that id or username in internal store/list. However, since the given id is 64 bytes long, taking that as hex public key and adding them as contact."); pubkey.add(userName); } @@ -469,11 +469,11 @@ Future otherMenuUi(Store node) async { gNumLastDays = int.parse(newNumDays); print("Changed number of days printed to $gNumLastDays"); } on FormatException catch (e) { - print("Invalid input. Kindly try again."); + printWarning("Invalid input. Kindly try again."); if( gDebug > 0) print(" ${e.message}"); continue; } on Exception catch (e) { - print("Invalid input. Kindly try again."); + printWarning("Invalid input. Kindly try again."); if( gDebug > 0) print(" ${e}"); continue; } @@ -490,7 +490,11 @@ Future otherMenuUi(Store node) async { sendDeleteEvent(node, eventIdToDelete.first); await processAnyIncomingEvents(node, false); // get latest event, this takes 300 ms } else { - print("Invalid Event Id(s) entered = {$eventIdToDelete}"); + if( eventIdToDelete.length == 0) { + printWarning("Could not find the given event id. Kindly try again, by entering a 64 byte long hex event id, or by entering a unique prefix for the given event id."); + } else { + printWarning("Invalid Event Id(s). Kindly enter a more unique id."); + } } break; @@ -609,7 +613,7 @@ Future channelMenuUI(Store node) async { // in case the program was invoked with --pubkey, then user can't send messages if( userPrivateKey == "") { - print("Since no user private key has been supplied, posts/messages can't be sent. Invoke with --prikey \n"); + printWarning("Since no user private key has been supplied, posts/messages can't be sent. Invoke with --prikey \n"); } else { // send message to the given room @@ -708,7 +712,7 @@ Future updateEncryptedChannel(Store node, String channelId, await processAnyIncomingEvents(node, false); // get latest event, this takes 300 ms } else { - print("warning: could not find keys for the channel. Could not update."); + printWarning("Could not find shared-secret keys for the channel. Could not update."); } } @@ -748,7 +752,7 @@ Future addUsersToEncryptedChannel(Store node, String fullChannelId, String for(int i = 0; i < newPubKeys.length; i++) { if( newPubKeys[i].length != 64) { - print("Invalid pubkey. The given pubkey should be 64 byte long."); + printWarning("Invalid pubkey. The given pubkey should be 64 byte long."); continue; } toAdd.add(newPubKeys[i]); @@ -772,7 +776,7 @@ Future addUsersToEncryptedChannel(Store node, String fullChannelId, String print("sending kind 141 invites to: $participants"); await updateEncryptedChannel(node, fullChannelId, channelName, channelAbout, channelPic, content, tags, participants, newParticipants); } else { - print("no new users added. "); + printWarning("Note: No new users added. "); } } } @@ -836,7 +840,7 @@ Future encryptedChannelMenuUI(Store node) async { // in case the program was invoked with --pubkey, then user can't send messages if( userPrivateKey == "") { - print("Since no user private key has been supplied, posts/messages can't be sent. Invoke with --prikey \n"); + printWarning("Since no user private key has been supplied, posts/messages can't be sent. Invoke with --prikey"); } else { if( messageToSend.startsWith('/add ')) { await addUsersToEncryptedChannel(node, fullChannelId, messageToSend); @@ -853,7 +857,7 @@ Future encryptedChannelMenuUI(Store node) async { await sendChatMessage(node, fullChannelId, encryptedMessageToSend, "142"); pageNum = 1; // reset it } else { - printInColor("\nCould not encrypt and send message. Do confirm that you have access to this encrypted channel\n", redColor); + printWarning("\nCould not encrypt and send message. Do confirm that you have access to this encrypted channel"); } } } @@ -922,7 +926,7 @@ Future PrivateMenuUI(Store node) async { while(showChannelOption) { String fullChannelId = node.showDirectRoom(directRoomId, pageNum); if( fullChannelId == "") { - print("Could not find the given direct room."); + printWarning("Could not find the given direct room."); showChannelOption = false; break; } @@ -940,12 +944,12 @@ Future PrivateMenuUI(Store node) async { } else { // in case the program was invoked with --pubkey, then user can't send messages if( userPrivateKey == "") { - print("Since no user private key has been supplied, posts/messages can't be sent. Invoke with --prikey \n"); + printWarning("Since no user private key has been supplied, posts/messages can't be sent. Invoke with --prikey \n"); } // send message to the given room await sendDirectMessage(node, fullChannelId, messageToSend); await processAnyIncomingEvents(node, false); // get latest message - print("in privateMenuUI: sent message"); + //print("in privateMenuUI: sent message"); pageNum = 1; // reset it } } @@ -1011,7 +1015,7 @@ Future mainMenuUi(Store node) async { case 2: // in case the program was invoked with --pubkey, then user can't send messages if( userPrivateKey == "") { - print("Since no user private key has been supplied, posts/messages can't be sent. Invoke with --prikey \n"); + printWarning("Since no user private key has been supplied, posts/messages can't be sent. Invoke with --prikey \n"); break; } stdout.write("Type comment to post/reply (type '+' to send a like): "); diff --git a/lib/event_ds.dart b/lib/event_ds.dart index b4036f5..f105102 100644 --- a/lib/event_ds.dart +++ b/lib/event_ds.dart @@ -16,6 +16,7 @@ int gDebug = 0; String getStrInColor(String s, String commentColor) => stdout.supportsAnsiEscapes ?"$commentColor$s$gColorEndMarker":s; void printInColor(String s, String commentColor) => stdout.supportsAnsiEscapes ?stdout.write("$commentColor$s$gColorEndMarker"):stdout.write(s); +void printWarning(String s) => stdout.supportsAnsiEscapes ?stdout.write("$gWarningColor$s$gColorEndMarker\n"):stdout.write("$s\n"); // translate diff --git a/lib/settings.dart b/lib/settings.dart index cd1dd0a..c95375e 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -38,8 +38,8 @@ const String relayNostrInfo = 'wss://relay.nostr.info'; String defaultServerUrl = "wss://relay.damus.io"; List gListRelayUrls1 = [ defaultServerUrl, - relayNostrInfo, - "wss://nostr-verified.wellorder.net" + relayNostrInfo + // "wss://nostr-verified.wellorder.net" ]; List gListRelayUrls2 = [ @@ -196,7 +196,9 @@ List gBots = [ "3b57518d02e6acfd5eb7198530b2e351e5a52278fb2499d14b66db2 "f4161c88558700d23af18d8a6386eb7d7fed769048e1297811dcc34e86858fb2", // bitcoin_bot "105dfb7467b6286f573cae17146c55133d0dcc8d65e5239844214412218a6c36", // zerohedge "e89538241bf737327f80a9e31bb5771ccbe8a4508c04f1d1c0ce7336706f1bee", // Bitcoin news - "6a9eb714c2889aa32e449cfbb7854bc9780feed4ff3d887e03910dcb22aa560a" // "bible bot" + "6a9eb714c2889aa32e449cfbb7854bc9780feed4ff3d887e03910dcb22aa560a", // "bible bot" + + "3104f98515b3aa147d55d9c2951e0f953b829d8724381d8f0d824125d7727634" // 42 spammer ]; //////////////////////////////////////////////////////////////////////////////////////////////////////////////// difficulty related settings diff --git a/lib/tree_ds.dart b/lib/tree_ds.dart index 09a2708..f2f47d3 100644 --- a/lib/tree_ds.dart +++ b/lib/tree_ds.dart @@ -637,6 +637,10 @@ class Store { } if( channel.lastUpdated < ce.eventData.createdAt) { + if( participants.contains(userPublicKey) && !channel.participants.contains(userPublicKey) ) { + //printInColor("\nReceived new invite to a new group with id: $chatRoomId\n", greenColor); + } + channel.participants = participants; channel.lastUpdated = ce.eventData.createdAt; for(int i = 0; i < channel.messageIds.length; i++) { @@ -928,6 +932,11 @@ class Store { return; } + //ignore bots + if( [4, 42, 142].contains( newEvent.eventData.kind ) && gBots.contains(newEvent.eventData.pubkey)) { + return; + } + // handle reaction events and return if we could not find the reacted to. Continue otherwise to add this to notification set newEventIdsSet if( newEvent.eventData.kind == 7) { if( processReaction(newEvent, allChildEventsMap) == "") { @@ -1960,6 +1969,9 @@ Store getTree(Set events) { return Store([], {}, [], [], [], temp); } + // remove bots from 42/142/4 messages + events.removeWhere((event) => [42, 142, 4].contains(event.eventData.kind) && gBots.contains( event.eventData.pubkey) ); + // remove all events other than kind 0 (meta data), 1(posts replies likes), 3 (contact list), 7(reactions), 40 and 42 (chat rooms) events.removeWhere( (event) => !Store.typesInEventMap.contains(event.eventData.kind)); diff --git a/pubspec.yaml b/pubspec.yaml index 591a9b1..275e921 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ version: 0.0.9-beta homepage: https://github.com/vishalxl/nostr_console # Release 0.0.9 - encrypted channels; fixes - +# environment: sdk: '>=2.17.3 <3.0.0'