code improved for getting events together

no function change
This commit is contained in:
Vishal 2022-12-01 23:39:53 +05:30
parent eb9a666376
commit 80c780cdd8
4 changed files with 40 additions and 38 deletions

View File

@ -5,6 +5,7 @@ import 'package:nostr_console/tree_ds.dart';
import 'package:nostr_console/relays.dart';
import 'package:nostr_console/console_ui.dart';
import 'package:nostr_console/settings.dart';
import 'package:nostr_console/utils.dart';
import 'package:args/args.dart';
import 'package:logging/logging.dart';
@ -292,7 +293,7 @@ Future<void> main(List<String> arguments) async {
// get event for user
if( userPublicKey!= "") {
getUserEvents(gListRelayUrls1, userPublicKey, limitPerSubscription, getSecondsDaysAgo(limitSelfEvents));
getMentionEvents(gListRelayUrls2, userPublicKey, limitPerSubscription, getSecondsDaysAgo(limitSelfEvents), "#p"); // from relay group 2
getMentionEvents(gListRelayUrls2, {userPublicKey}, limitPerSubscription, getSecondsDaysAgo(limitSelfEvents), "#p"); // from relay group 2
}
// get other user events
@ -352,6 +353,7 @@ Future<void> main(List<String> arguments) async {
Store node = getTree(initialEvents);
gStore = node;
clearEvents();
mainMenuUi(node);
});

View File

@ -83,16 +83,16 @@ class Relays {
sendRequest(relayUrl, request);
}
void getMentionEvents(String relayUrl, String publicKey, int limit, int sinceWhen, String tagToGet) {
void getMentionEvents(String relayUrl, Set<String> ids, int limit, int sinceWhen, String tagToGet) {
for(int i = 0; i < gBots.length; i++) { // ignore bots
if( publicKey == gBots[i]) {
if( ids == gBots[i]) {
return;
}
}
String subscriptionId = "mention" + (relays[relayUrl]?.numRequestsSent??"").toString() + "_" + relayUrl.substring(6);
String request = getMentionRequest(subscriptionId, publicKey, limit, sinceWhen, tagToGet);
String request = getMentionRequest(subscriptionId, ids, limit, sinceWhen, tagToGet);
sendRequest(relayUrl, request);
}
@ -102,7 +102,7 @@ class Relays {
*/
void getMultiUserEvents(String relayUrl, List<String> publicKeys, int limit, int sinceWhen) {
List<String> reqKeys = [];
Set<String> reqKeys = {};
if( relays.containsKey(relayUrl)) {
List<String>? users = relays[relayUrl]?.users;
if( users != null) {
@ -250,9 +250,9 @@ void getUserEvents(List<String> serverUrls, String publicKey, int numUserEvents,
});
}
void getMentionEvents(List<String> serverUrls, String publicKey, int numUserEvents, int sinceWhen, String tagToGet) {
void getMentionEvents(List<String> serverUrls, Set<String> ids, int numUserEvents, int sinceWhen, String tagToGet) {
serverUrls.forEach((serverUrl) {
relays.getMentionEvents(serverUrl, publicKey, numUserEvents, sinceWhen, tagToGet);
relays.getMentionEvents(serverUrl, ids, numUserEvents, sinceWhen, tagToGet);
});
}
@ -283,19 +283,12 @@ void sendEventsRequest(List<String> serverUrls, Set<String> eventIds) {
if( eventIds.length == 0)
return;
String eventIdsStr = "";
int i = 0;
eventIds.forEach((event) {
String comma = ",";
if( i == 0)
comma = "";
eventIdsStr = '$eventIdsStr$comma"${event}"';
i++;
});
String eventIdsStr = getJsonList(eventIds);;
String getEventRequest = '["REQ","event_${eventIds.length}",{"ids":[$eventIdsStr]}]';
if( gDebug > 0) log.info("sending $getEventRequest");
//print("send event req: $getEventRequest\n");
for(int i = 0; i < serverUrls.length; i++) {
relays.sendRequest(serverUrls[i], getEventRequest);
}

View File

@ -1120,7 +1120,7 @@ class Store {
secretEvent.eventData.TranslateAndDecryptGroupInvite();
String? newEncryptedChannelId = createEncryptedRoomFromInvite(allEncryptedGroupInviteIds, encryptedChannels, tempChildEventsMap, secretEvent);
if( newEncryptedChannelId != null) {
usersEncryptedChannelIds.add(secretEventId); // is later used so a request can be sent for this
usersEncryptedChannelIds.add(newEncryptedChannelId); // is later used so a request can be sent for this
}
}
});
@ -1141,17 +1141,14 @@ class Store {
if(gDebug != 0) print("In Tree FromEvents: number of events without parent in fromEvents = ${tempWithoutParent.length}");
// get dummy events
sendEventsRequest(gListRelayUrls1, dummyEventIds);
// get dummy events and encryped channel create events
sendEventsRequest(gListRelayUrls1, dummyEventIds.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.
for(String newEncryptedGroup in usersEncryptedChannelIds) {
getMentionEvents(gListRelayUrls2, newEncryptedGroup, gLimitFollowPosts, getSecondsDaysAgo(gDefaultNumLastDays), "#e"); // from relay group 2
}
getMentionEvents(gListRelayUrls2, usersEncryptedChannelIds, gLimitFollowPosts, getSecondsDaysAgo(gDefaultNumLastDays), "#e"); // from relay group 2
sendEventsRequest(gListRelayUrls1, usersEncryptedChannelIds);
//sendEventsRequest(gListRelayUrls1, usersEncryptedChannelIds);
myWait(200);
/*
print("allEncryptedGroupInviteIds = ${allEncryptedGroupInviteIds.length} $allEncryptedGroupInviteIds");
print("usersEncryptedGroupIds = ${usersEncryptedGroupIds.length} $usersEncryptedGroupIds");

View File

@ -16,7 +16,7 @@ class HistogramEntry {
}
}
void myWait(int ms) async {
Future<void> myWait(int ms) async {
Future<void> foo1() async {
await Future.delayed(Duration(milliseconds: ms));
return;
@ -281,6 +281,22 @@ String getStringFromUser(String prompt, [String defaultValue=""] ) {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// relay related functions
// returns list in form ( if 3 sized list)
// "pubkey1","pubkey2","pubkey3"
String getJsonList(Set<String> publicKeys) {
String s = "";
int i = 0;
for(String pubkey in publicKeys) {
s += "\"${pubkey.toLowerCase()}\"";
if( i < publicKeys.length - 1) {
s += ",";
}
i++;
}
return s;
}
String getKindRequest(String subscriptionId, List<int> kind, int limit, int sinceWhen) {
String strTime = "";
if( sinceWhen != 0) {
@ -312,17 +328,17 @@ String getUserRequest(String subscriptionId, String publicKey, int numUserEvents
return strSubscription1 + publicKey.toLowerCase() + strSubscription2;
}
String getMentionRequest(String subscriptionId, String publicKey, int numUserEvents, int sinceWhen, String tagToGet) {
String getMentionRequest(String subscriptionId, Set<String> ids, int numUserEvents, int sinceWhen, String tagToGet) {
String strTime = "";
if( sinceWhen != 0) {
strTime = ', "since": ${sinceWhen.toString()}';
}
var strSubscription1 = '["REQ","$subscriptionId",{ "$tagToGet": ["';
var strSubscription2 ='"], "limit": $numUserEvents $strTime } ]';
return strSubscription1 + publicKey.toLowerCase() + strSubscription2;
var strSubscription1 = '["REQ","$subscriptionId",{ "$tagToGet": [';
var strSubscription2 ='], "limit": $numUserEvents $strTime } ]';
return strSubscription1 + getJsonList(ids) + strSubscription2;
}
String getMultiUserRequest(String subscriptionId, List<String> publicKeys, int numUserEvents, int sinceWhen) {
String getMultiUserRequest(String subscriptionId, Set<String> publicKeys, int numUserEvents, int sinceWhen) {
String strTime = "";
if( sinceWhen != 0) {
strTime = ', "since": ${sinceWhen.toString()}';
@ -331,12 +347,6 @@ String getMultiUserRequest(String subscriptionId, List<String> publicKeys, int n
var strSubscription1 = '["REQ","$subscriptionId",{ "authors": [';
var strSubscription2 ='], "limit": $numUserEvents $strTime } ]';
String s = "";
for(int i = 0; i < publicKeys.length; i++) {
s += "\"${publicKeys[i].toLowerCase()}\"";
if( i < publicKeys.length - 1) {
s += ",";
}
}
s = getJsonList(publicKeys);
return strSubscription1 + s + strSubscription2;
}