From c37e2b038f570767eaf55a64cb39e7278f82b998 Mon Sep 17 00:00:00 2001 From: Vishal <64505169+vishalxl@users.noreply.github.com> Date: Thu, 3 Nov 2022 23:25:28 +0530 Subject: [PATCH] improved ordering of channel display .. by correctly placing empty channels depending on their creation date. improved kind 0,3 fetching. improved display around channel menu and printNotifications. --- bin/nostr_console.dart | 3 ++- lib/console_ui.dart | 14 +++++++--- lib/event_ds.dart | 1 - lib/settings.dart | 7 ++--- lib/tree_ds.dart | 61 +++++++++++++++++++++++++++++------------- pubspec.yaml | 2 +- 6 files changed, 60 insertions(+), 28 deletions(-) diff --git a/bin/nostr_console.dart b/bin/nostr_console.dart index f332aa7..8fdca7a 100644 --- a/bin/nostr_console.dart +++ b/bin/nostr_console.dart @@ -262,7 +262,8 @@ Future main(List arguments) async { 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 + getKindEvents([0, 3], gListRelayUrls1, gLimitPerSubscription, getSecondsDaysAgo(daysToGetEventsFor* 100)); // get all type 3 etc + getKindEvents([40, 42, 140, 141, 142], gListRelayUrls1, gLimitPerSubscription * 3, getSecondsDaysAgo(daysToGetEventsFor)); // get all type 3 etc // TODO get all 40 events, and then get all #e for them ( responses to them) diff --git a/lib/console_ui.dart b/lib/console_ui.dart index ed9144d..9a9571a 100644 --- a/lib/console_ui.dart +++ b/lib/console_ui.dart @@ -637,6 +637,9 @@ Future channelMenuUI(Store node) async { case 3: await createPublicChannel(node); + justShowedChannels = false; + + // TODO put user in the newly created channel break; case 4: @@ -785,7 +788,7 @@ Future addUsersToEncryptedChannel(Store node, String fullChannelId, String Future encryptedChannelMenuUI(Store node) async { - + gSpecificDebug = 1; bool continueChatMenu = true; bool justShowedChannels = false; @@ -877,6 +880,7 @@ Future encryptedChannelMenuUI(Store node) async { case 3: await createEncryptedChannel(node); + justShowedChannels = false; break; case 4: @@ -895,7 +899,7 @@ Future PrivateMenuUI(Store node) async { bool continueChatMenu = true; while(continueChatMenu) { - await processAnyIncomingEvents(node); // this takes 300 ms + await processAnyIncomingEvents(node, true); // this takes 300 ms node.printDirectRoomInfo(showAllRooms); @@ -924,6 +928,8 @@ Future PrivateMenuUI(Store node) async { } int pageNum = 1; while(showChannelOption) { + + String fullChannelId = node.showDirectRoom(directRoomId, pageNum); if( fullChannelId == "") { printWarning("Could not find the given direct room."); @@ -956,7 +962,9 @@ Future PrivateMenuUI(Store node) async { } else { print("Refreshing..."); } - await processAnyIncomingEvents(node); + + await processAnyIncomingEvents(node, false); + } break; diff --git a/lib/event_ds.dart b/lib/event_ds.dart index f105102..66ceda3 100644 --- a/lib/event_ds.dart +++ b/lib/event_ds.dart @@ -12,7 +12,6 @@ import 'dart:convert' as convert; import "package:pointycastle/export.dart"; import 'package:kepler/kepler.dart'; -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); diff --git a/lib/settings.dart b/lib/settings.dart index ae3de61..2be660d 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -1,7 +1,8 @@ import 'dart:io'; import 'package:logging/logging.dart'; - +int gDebug = 0; +int gSpecificDebug = 0; final log = Logger('ExampleLogger'); @@ -38,8 +39,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 = [ diff --git a/lib/tree_ds.dart b/lib/tree_ds.dart index 5d7703d..e688981 100644 --- a/lib/tree_ds.dart +++ b/lib/tree_ds.dart @@ -21,11 +21,18 @@ bool selectorShowAllRooms(ScrollableMessages room) { bool showAllRooms (ScrollableMessages room) => selectorShowAllRooms(room); -int getLatestMessageTime(List _messageIds) { - if( _messageIds.length <= 0 || gStore == null) { +int getLatestMessageTime(ScrollableMessages channel) { + + List _messageIds = channel.messageIds; + if(gStore == null) { return 0; } + if(_messageIds.length == 0) { + int createdAt = channel.createdAt; + return createdAt; + } + int latest = 0; for(int i = 0; i < _messageIds.length; i++) { if( gStore != null) { @@ -65,8 +72,8 @@ int scrollableCompareTo(ScrollableMessages a, ScrollableMessages b) { if( gStore == null) return 0; - int otherLatest = getLatestMessageTime(b.messageIds); - int thisLatest = getLatestMessageTime(a.messageIds); + int otherLatest = getLatestMessageTime(b); + int thisLatest = getLatestMessageTime(a); if( thisLatest < otherLatest) { return 1; @@ -82,8 +89,9 @@ int scrollableCompareTo(ScrollableMessages a, ScrollableMessages b) { class ScrollableMessages { String topHeader; List messageIds; + int createdAt; - ScrollableMessages(this.topHeader, this.messageIds); + ScrollableMessages(this.topHeader, this.messageIds, this.createdAt); void addMessageToRoom(String messageId, Map tempChildEventsMap) { int newEventTime = (tempChildEventsMap[messageId]?.event.eventData.createdAt??0); @@ -175,8 +183,12 @@ class Channel extends ScrollableMessages { Channel(this.channelId, this.internalChatRoomName, this.about, this.picture, List messageIds, this.participants, this.lastUpdated) : super ( internalChatRoomName.isEmpty? channelId: internalChatRoomName + "( " + channelId + " )" , - messageIds); + messageIds, + lastUpdated); + String getChannelId() { + return channelId; + } String get chatRoomName { return internalChatRoomName; @@ -214,11 +226,16 @@ class Channel extends ScrollableMessages { class DirectMessageRoom extends ScrollableMessages{ String otherPubkey; // id of user this DM is happening + int createdAt; - DirectMessageRoom(this.otherPubkey, List messageIds): - super ( "${getAuthorName(otherPubkey)} ($otherPubkey)", messageIds) { + DirectMessageRoom(this.otherPubkey, List messageIds, this.createdAt): + super ( "${getAuthorName(otherPubkey)} ($otherPubkey)", messageIds, createdAt) { } + String getChannelId() { + return otherPubkey; + } + bool isPrivateMessageRoom() { return false; @@ -757,7 +774,7 @@ class Store { } else { List temp = []; temp.add(eId); - DirectMessageRoom newDirectRoom= DirectMessageRoom(directRoomId, temp); + DirectMessageRoom newDirectRoom= DirectMessageRoom(directRoomId, temp, ce.eventData.createdAt); directRooms.add( newDirectRoom); if( ce.eventData.id == gCheckEventId && gDebug >= 0) print("Adding new message ${ce.eventData.id} to NEW direct room $directRoomId. sender pubkey = ${ce.eventData.pubkey}."); } @@ -1042,7 +1059,7 @@ class Store { List temp = []; temp.add(newTree.event.eventData.id); - directRooms.add(DirectMessageRoom(directRoomId, temp)); // TODO sort it + directRooms.add(DirectMessageRoom(directRoomId, temp, newTree.event.eventData.createdAt)); // TODO sort it break; @@ -1528,23 +1545,26 @@ class Store { if( lookedUpName.length == 1) { DirectMessageRoom? room = getDirectRoom(directRooms, lookedUpName.first); - if( room != null) { + if( room != null) {// room is already created, use it room.printDirectMessageRoom(this, page); return lookedUpName.first; } else { - if( isValidPubkey(lookedUpName.first)) { + if( isValidPubkey(lookedUpName.first)) { // in case the pubkey is valid and we have seen the pubkey in global author list, create new room print("Could not find a conversation or room with the given id. Creating one with ${lookedUpName.first}"); - createDirectRoom( directRoomId); + DirectMessageRoom room = createDirectRoom( directRoomId); + room.printDirectMessageRoom(this, page); return directRoomId; } } } else { - if( lookedUpName.length > 0) - print("Got more than one public id for the name given, which are: ${lookedUpName.length}"); - else { + if( lookedUpName.length > 0) { + print("Got more than one public id for the name given, which are: ${lookedUpName.length}"); + } + else { // in case the given id is not present in our global list of usernames, create new room for them if( isValidPubkey(directRoomId)) { print("Could not find a conversation or room with the given id. Creating one with $directRoomId"); - createDirectRoom(directRoomId); + DirectMessageRoom room = createDirectRoom(directRoomId); + room.printDirectMessageRoom(this, page); return directRoomId; } } @@ -1553,8 +1573,11 @@ class Store { return ""; } - void createDirectRoom(String directRoomId) { - directRooms.add(DirectMessageRoom(directRoomId, [])); + DirectMessageRoom createDirectRoom(String directRoomId) { + int createdAt = DateTime.now().millisecondsSinceEpoch ~/1000; + DirectMessageRoom room = DirectMessageRoom(directRoomId, [], createdAt); + directRooms.add(room); + return room; } // Write the tree's events to file as one event's json per line diff --git a/pubspec.yaml b/pubspec.yaml index 9561d0f..96662c7 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 -# spam +# improved fetching of events. and display for channels. environment: sdk: '>=2.17.3 <3.0.0'