fetched 14x only after receiving an invite

This commit is contained in:
Vishal 2022-11-29 11:02:31 +05:30
parent 2fd5bb0d68
commit 7c065f18e5
6 changed files with 62 additions and 57 deletions

View File

@ -27,12 +27,6 @@ const String colorArg = "color";
const String overWriteFlag = "overwrite";
Future<void> main(List<String> arguments) async {
Logger.root.level = Level.ALL; // defaults to Level.INFO
DateTime appStartTime = DateTime.now();
print("app start time: $appStartTime");
Logger.root.onRecord.listen((record) {
print('${record.level.name}: ${record.time.difference(appStartTime)}: ${record.message}');
});
final parser = ArgParser()..addOption(requestArg, abbr: 'q') ..addOption(pubkeyArg, abbr:"p")..addOption(prikeyArg, abbr:"k")
..addOption(lastdaysArg, abbr:"d") ..addOption(relayArg, abbr:"r")
@ -58,6 +52,13 @@ Future<void> main(List<String> arguments) async {
return;
}
Logger.root.level = Level.ALL; // defaults to Level.INFO
DateTime appStartTime = DateTime.now();
print("app start time: $appStartTime");
Logger.root.onRecord.listen((record) {
print('${record.level.name}: ${record.time.difference(appStartTime)}: ${record.message}');
});
// start application
printIntro("Nostr");
@ -291,7 +292,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)); // from relay group 2
getMentionEvents(gListRelayUrls2, userPublicKey, limitPerSubscription, getSecondsDaysAgo(limitSelfEvents), "#p"); // from relay group 2
}
// get other user events
@ -299,12 +300,12 @@ Future<void> main(List<String> arguments) async {
// get group and meta info events
getKindEvents([0, 3, 40, 41], gListRelayUrls1, 3 * limitPerSubscription, getSecondsDaysAgo(limitMetaInfoEvents)); // get all type 3 etc
getKindEvents([140, 141], gListRelayUrls1, 3 * limitPerSubscription, getSecondsDaysAgo(1)); // get all type 3 etc
//getKindEvents([140, 141], gListRelayUrls1, 3 * limitPerSubscription, getSecondsDaysAgo(1)); // get all type 3 etc
getKindEvents([42], gListRelayUrls1, limitPerSubscription * 3, getSecondsDaysAgo( limitFollowPosts)); // get all type 3 etc
getKindEvents([142], gListRelayUrls1, limitPerSubscription * 3, getSecondsDaysAgo( 1)); // get all type 3 etc
//getKindEvents([142], gListRelayUrls1, limitPerSubscription * 3, getSecondsDaysAgo( 1)); // get all type 3 etc
getKindEvents([gSecretMessageKind], gListRelayUrls1, limitPerSubscription * 3, getSecondsDaysAgo( 1)); // get all type 3 etc
getKindEvents([gSecretMessageKind], gListRelayUrls1, limitPerSubscription * 3, getSecondsDaysAgo( 3)); // get all type 3 etc
// TODO get all 40 events, and then get all #e for them ( responses to them)

View File

@ -466,13 +466,11 @@ class EventData {
return;
}
if( createdAt < getSecondsDaysAgo(1)) {
if( createdAt < getSecondsDaysAgo(3)) {
//print("old 142. not decrypting");
return;
//return;
}
switch(kind) {
case 142:
//print("in translateAndDecrypt14x");

View File

@ -83,7 +83,7 @@ class Relays {
sendRequest(relayUrl, request);
}
void getMentionEvents(String relayUrl, String publicKey, int limit, int sinceWhen) {
void getMentionEvents(String relayUrl, String publicKey, int limit, int sinceWhen, String tagToGet) {
for(int i = 0; i < gBots.length; i++) { // ignore bots
if( publicKey == gBots[i]) {
return;
@ -91,20 +91,8 @@ class Relays {
}
String subscriptionId = "mention" + (relays[relayUrl]?.numRequestsSent??"").toString() + "_" + relayUrl.substring(6);
if( relays.containsKey(relayUrl)) {
List<String>? users = relays[relayUrl]?.users;
if( users != null) { // get a user only if it has not already been requested
// following is too restrictive casuse changed sinceWhen is not considered. TODO improve it
for(int i = 0; i < users.length; i++) {
if( users[i] == publicKey) {
return;
}
}
users.add(publicKey);
}
}
String request = getMentionRequest(subscriptionId, publicKey, limit, sinceWhen);
String request = getMentionRequest(subscriptionId, publicKey, limit, sinceWhen, tagToGet);
sendRequest(relayUrl, request);
}
@ -262,9 +250,9 @@ void getUserEvents(List<String> serverUrls, String publicKey, int numUserEvents,
});
}
void getMentionEvents(List<String> serverUrls, String publicKey, int numUserEvents, int sinceWhen) {
void getMentionEvents(List<String> serverUrls, String publicKey, int numUserEvents, int sinceWhen, String tagToGet) {
serverUrls.forEach((serverUrl) {
relays.getMentionEvents(serverUrl, publicKey, numUserEvents, sinceWhen);
relays.getMentionEvents(serverUrl, publicKey, numUserEvents, sinceWhen, tagToGet);
});
}

View File

@ -11,7 +11,7 @@ int gSpecificDebug = 0;
final log = Logger('ExampleLogger');
// for debugging
String gCheckEventId = "0082a613c9f4d43f796a427d15db74f10ac64212e28f465870ceaf099f488087";
String gCheckEventId = "5924e987416a16df0cbc000ce264f65d133ba352589138ee3a7784f5bae75641";
int gDefaultNumWaitSeconds = 8000; // is used in main()

View File

@ -201,14 +201,18 @@ int scrollableCompareTo(ScrollableMessages a, ScrollableMessages b) {
}
}
enum enumRoomType { kind4, kind40, kind140}
class ScrollableMessages {
String topHeader;
List<String> messageIds;
int createdAt;
enumRoomType roomType;
ScrollableMessages(this.topHeader, this.messageIds, this.createdAt);
ScrollableMessages(this.topHeader, this.messageIds, this.createdAt, this.roomType);
void addMessageToRoom(String messageId, Map<String, Tree> tempChildEventsMap) {
if( gSpecificDebug > 0 && roomType == enumRoomType.kind140) print("in addMessageToRoom for enc");
int newEventTime = (tempChildEventsMap[messageId]?.event.eventData.createdAt??0);
if(gDebug> 0) print("Room has ${messageIds.length} messages already. adding new one to it. ");
@ -217,12 +221,12 @@ class ScrollableMessages {
int eventTime = (tempChildEventsMap[messageIds[i]]?.event.eventData.createdAt??0);
if( newEventTime < eventTime) {
// shift current i and rest one to the right, and put event Time here
if(gDebug> 0) print("In addMessageToRoom: inserted in middle to room ");
if(gSpecificDebug > 0 && roomType == enumRoomType.kind140) print("In addMessageToRoom: inserted enc message in middle to room with name ${topHeader}");
messageIds.insert(i, messageId);
return;
}
}
if(gDebug> 0) print("In addMessageToRoom: added to room ");
if(gSpecificDebug > 0 && roomType == enumRoomType.kind140) print("In addMessageToRoom: inserted enc message in end of room with name ${topHeader}");
// insert at end
messageIds.add(messageId);
@ -297,10 +301,13 @@ class Channel extends ScrollableMessages {
Set<String> participants; // pubkey of all participants - only for encrypted channels
String creatorPubkey; // creator of the channel, if event is known
Channel(this.channelId, this.internalChatRoomName, this.about, this.picture, List<String> messageIds, this.participants, this.lastUpdated, [this.creatorPubkey=""]) :
enumRoomType roomType;
Channel(this.channelId, this.internalChatRoomName, this.about, this.picture, List<String> messageIds, this.participants, this.lastUpdated, this.roomType, [this.creatorPubkey=""] ) :
super ( internalChatRoomName.isEmpty? channelId: "Channel Name: $internalChatRoomName (id: $channelId)" ,
messageIds,
lastUpdated);
lastUpdated,
roomType);
String getChannelId() {
return channelId;
@ -346,7 +353,7 @@ class DirectMessageRoom extends ScrollableMessages{
int createdAt;
DirectMessageRoom(this.otherPubkey, List<String> messageIds, this.createdAt):
super ( "${getAuthorName(otherPubkey)} ($otherPubkey)", messageIds, createdAt) {
super ( "${getAuthorName(otherPubkey)} ($otherPubkey)", messageIds, createdAt, enumRoomType.kind4) {
}
String getChannelId() {
@ -682,7 +689,7 @@ class Store {
}
List<String> emptyMessageList = [];
Channel room = Channel(chatRoomId, roomName, roomAbout, "", emptyMessageList, {}, ce.eventData.createdAt);
Channel room = Channel(chatRoomId, roomName, roomAbout, "", emptyMessageList, {}, ce.eventData.createdAt, enumRoomType.kind40);
rooms.add( room);
}
} on Exception catch(e) {
@ -707,7 +714,7 @@ class Store {
channel.addMessageToRoom(eId, tempChildEventsMap);
} else {
Channel newChannel = Channel(channelId, "", "", "", [eId], {}, 0);
Channel newChannel = Channel(channelId, "", "", "", [eId], {}, 0, enumRoomType.kind40);
// message added in above line
rooms.add( newChannel);
}
@ -738,9 +745,10 @@ class Store {
/**
* Will create a entry in encryptedChannels ( if one does not already exist)
* Returns id of channel if one is created, null otherwise.
*
*/
static void createEncryptedRoomFromInvite( List<String> secretMessageIds, List<Channel> encryptedChannels, Map<String, Tree> tempChildEventsMap, Event eventSecretMessage) {
static String? createEncryptedRoomFromInvite( List<String> secretMessageIds, List<Channel> encryptedChannels, Map<String, Tree> tempChildEventsMap, Event eventSecretMessage) {
String? event140Id = getEncryptedChannelIdFromSecretMessage(secretMessageIds, tempChildEventsMap, eventSecretMessage);
Event? event140 = tempChildEventsMap[event140Id]?.event;
@ -755,17 +763,9 @@ class Store {
dynamic json = jsonDecode(event140.eventData.content);
Channel? channel = getChannel(encryptedChannels, chatRoomId);
if( channel != null) {
// if channel entry already exists, then update its participants info, and name info
if( channel.chatRoomName == "" && json.containsKey('name')) {
channel.chatRoomName = json['name'];
}
if( channel.lastUpdated == 0) { // == 0 only when it was created using a 142 msg. otherwise, don't update it if it was created using 141
channel.participants = participants;
channel.lastUpdated = event140.eventData.createdAt;
}
channel.creatorPubkey = event140.eventData.pubkey;
// if channel entry already exists, then do nothing, cause we've already processed this channel create event
} else {
// create new encrypted channel
String roomName = "", roomAbout = "";
if( json.containsKey('name') ) {
roomName = json['name']??"";
@ -775,8 +775,9 @@ class Store {
roomAbout = json['about'];
}
List<String> emptyMessageList = [];
Channel room = Channel(chatRoomId, roomName, roomAbout, "", emptyMessageList, participants, event140.eventData.createdAt, event140.eventData.pubkey);
Channel room = Channel(chatRoomId, roomName, roomAbout, "", emptyMessageList, participants, event140.eventData.createdAt, enumRoomType.kind140, event140.eventData.pubkey);
encryptedChannels.add( room);
return chatRoomId;
}
} on Exception catch(e) {
if( gDebug > 0) print("In From Event. Event type 140. Json Decode error for event id ${event140.eventData.id}. error = $e");
@ -785,25 +786,30 @@ class Store {
else {
//printWarning("could not find event 140 from event $gSecretMessageKind ${eventSecretMessage.eventData.id}");
}
return null;
}
static void handleEncryptedChannelEvent( List<String> secretMessageIds, List<Channel> encryptedChannels, Map<String, Tree> tempChildEventsMap, Event ce) {
String eId = ce.eventData.id;
int eKind = ce.eventData.kind;
if( ce.eventData.createdAt < getSecondsDaysAgo(1)) {
return; // dont process old 142/141 messages cause they're different format
if( ce.eventData.createdAt < getSecondsDaysAgo(3)) {
//return; // dont process old 142/141 messages cause they're different format
}
switch(eKind) {
case 142:
//if( gSpecificDebug > 0 && eId == gCheckEventId) printWarning("Got ${eId}");
if( gSpecificDebug > 0) print("got kind 142 message. total number of encrypted channels: ${encryptedChannels.length}. event e tags ${ce.eventData.eTags}");
String channelId = ce.eventData.getChannelIdForMessage();
if( channelId.length == 64) { // sometimes people may forget to give e tags or give wrong tags like #e
Channel? channel = getChannel(encryptedChannels, channelId);
if( channel != null) {
channel.addMessageToRoom(eId, tempChildEventsMap);
}
} else {
if( gSpecificDebug > 0) print("could not get channel");
}
} else {
// could not get channel id of message.
printWarning("---Could not get encryptd channel for message id ${ce.eventData.id} got channelId : ${channelId} its len ${channelId.length}");
@ -1115,12 +1121,16 @@ class Store {
// tempEncrytedSecretMessageIds has been created above
// now create encrypted rooms
Set<String> usersEncryptedGroups = {};
tempEncryptedSecretMessageIds.forEach((secretEventId) {
Event? secretEvent = tempChildEventsMap[secretEventId]?.event;
if( secretEvent != null) {
secretEvent.eventData.TranslateAndDecryptSecretMessage(tempChildEventsMap);
createEncryptedRoomFromInvite(tempEncryptedSecretMessageIds, encryptedChannels, tempChildEventsMap, secretEvent);
String? newEncryptedChannelId = createEncryptedRoomFromInvite(tempEncryptedSecretMessageIds, encryptedChannels, tempChildEventsMap, secretEvent);
if( newEncryptedChannelId != null) {
usersEncryptedGroups.add(newEncryptedChannelId);
}
}
});
@ -1143,6 +1153,14 @@ class Store {
// get dummy events
sendEventsRequest(gListRelayUrls1, dummyEventIds);
// 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 usersEncryptedGroups) {
getMentionEvents(gListRelayUrls2, newEncryptedGroup, gLimitFollowPosts, getSecondsDaysAgo(gDefaultNumLastDays), "#e"); // from relay group 2
}
sendEventsRequest(gListRelayUrls1, usersEncryptedGroups);
// create a dummy top level tree and then create the main Tree object
return Store( topLevelTrees, tempChildEventsMap, tempWithoutParent, channels, encryptedChannels, tempDirectRooms, tempEncryptedSecretMessageIds);
} // end fromEvents()

View File

@ -309,12 +309,12 @@ String getUserRequest(String subscriptionId, String publicKey, int numUserEvents
return strSubscription1 + publicKey.toLowerCase() + strSubscription2;
}
String getMentionRequest(String subscriptionId, String publicKey, int numUserEvents, int sinceWhen) {
String getMentionRequest(String subscriptionId, String publicKey, int numUserEvents, int sinceWhen, String tagToGet) {
String strTime = "";
if( sinceWhen != 0) {
strTime = ', "since": ${sinceWhen.toString()}';
}
var strSubscription1 = '["REQ","$subscriptionId",{ "#p": ["';
var strSubscription1 = '["REQ","$subscriptionId",{ "$tagToGet": ["';
var strSubscription2 ='"], "limit": $numUserEvents $strTime } ]';
return strSubscription1 + publicKey.toLowerCase() + strSubscription2;
}