added since in request to relays, for 120 days before now.

comments created in last 120 days are requested only.
This commit is contained in:
Vishal 2022-08-28 06:38:38 +05:30
parent 01b5b82228
commit cb24f7e2bf
5 changed files with 34 additions and 21 deletions

View File

@ -181,7 +181,7 @@ Future<void> main(List<String> arguments) async {
return;
}
getUserEvents(gListRelayUrls, userPublicKey, 3000, 0);
getUserEvents(gListRelayUrls, userPublicKey, 3000, getSecondsDaysAgo(gEventsSinceDays));
// the default in case no arguments are given is:
// get a user's events, then from its type 3 event, gets events of its follows,
@ -201,7 +201,7 @@ Future<void> main(List<String> arguments) async {
List<String> contactList = [];
if (contactEvent != null ) {
if(gDebug > 0) print("In main: found contact list: \n ${contactEvent.originalJson}");
contactList = getContactFeed(gListRelayUrls, contactEvent.eventData.contactList, 4000);
contactList = getContactFeed(gListRelayUrls, contactEvent.eventData.contactList, 4000, getSecondsDaysAgo(gEventsSinceDays));
if( !gContactLists.containsKey(userPublicKey)) {
gContactLists[userPublicKey] = contactEvent.eventData.contactList;
@ -220,7 +220,7 @@ Future<void> main(List<String> arguments) async {
// get mentioned ptags, and then get the events for those users
List<String> pTags = getpTags(getRecievedEvents(), 300);
getMultiUserEvents(gListRelayUrls, pTags, 5000);
getMultiUserEvents(gListRelayUrls, pTags, 5000, getSecondsDaysAgo(gEventsSinceDays));
stdout.write('Waiting for rest of posts to come in.....');
Future.delayed(const Duration(milliseconds: numWaitSeconds * 2), () {

View File

@ -639,3 +639,9 @@ Set<String> getPublicKeyFromName(String userName) {
return pubkeys;
}
// returns the seconds since eponch N days ago
int getSecondsDaysAgo( int N) {
return DateTime.now().subtract(Duration(days: N)).millisecondsSinceEpoch ~/ 1000;
}

View File

@ -83,7 +83,7 @@ class Relays {
* @connect Connect to given relay and get all events for multiple users/publicKey and insert the
* received events in the given List<Event>
*/
void getMultiUserEvents(String relayUrl, List<String> publicKeys, int numEventsToGet) {
void getMultiUserEvents(String relayUrl, List<String> publicKeys, int numEventsToGet, int sinceWhen) {
List<String> reqKeys = [];
if( relays.containsKey(relayUrl)) {
@ -105,7 +105,7 @@ class Relays {
String subscriptionId = "multiple_user" + (relays[relayUrl]?.numRequestsSent??"").toString();
String request = getMultiUserRequest( subscriptionId, reqKeys, numEventsToGet);
String request = getMultiUserRequest( subscriptionId, reqKeys, numEventsToGet, sinceWhen);
sendRequest(relayUrl, request);
}
@ -207,9 +207,14 @@ String getUserRequest(String subscriptionId, String publicKey, int numUserEvents
return strSubscription1 + publicKey + strSubscription2;
}
String getMultiUserRequest(String subscriptionId, List<String> publicKeys, int numUserEvents) {
String getMultiUserRequest(String subscriptionId, List<String> publicKeys, int numUserEvents, int sinceWhen) {
String strTime = "";
if( sinceWhen != 0) {
strTime = ', "since": ${sinceWhen.toString()}';
}
var strSubscription1 = '["REQ","$subscriptionId",{ "authors": [';
var strSubscription2 ='], "limit": $numUserEvents } ]';
var strSubscription2 ='], "limit": $numUserEvents $strTime } ]';
String s = "";
for(int i = 0; i < publicKeys.length; i++) {
@ -221,7 +226,7 @@ String getMultiUserRequest(String subscriptionId, List<String> publicKeys, int n
return strSubscription1 + s + strSubscription2;
}
List<String> getContactFeed(List<String> relayUrls, List<Contact> contacts, numEventsToGet) {
List<String> getContactFeed(List<String> relayUrls, List<Contact> contacts, int numEventsToGet, int sinceWhen) {
// maps from relay url to list of users that it supplies events for
Map<String, List<String> > mContacts = {};
@ -240,10 +245,10 @@ List<String> getContactFeed(List<String> relayUrls, List<Contact> contacts, numE
// send request for the users events to the relays
mContacts.forEach((key, value) {
relays.getMultiUserEvents(key, value, numEventsToGet);
relays.getMultiUserEvents(key, value, numEventsToGet, sinceWhen);
relayUrls.forEach((relayUrl) {
relays.getMultiUserEvents(relayUrl, value, numEventsToGet);
relays.getMultiUserEvents(relayUrl, value, numEventsToGet, sinceWhen);
});
});
@ -252,13 +257,13 @@ List<String> getContactFeed(List<String> relayUrls, List<Contact> contacts, numE
return contactList;
}
void getUserEvents(List<String> serverUrls, publicKey, numUserEvents, sinceWhen) {
void getUserEvents(List<String> serverUrls, String publicKey, int numUserEvents, int sinceWhen) {
serverUrls.forEach((serverUrl) {
relays.getUserEvents(serverUrl, publicKey, numUserEvents, sinceWhen);
});
}
void getMultiUserEvents(List<String> serverUrls, List<String> publicKeys, numUserEvents) {
void getMultiUserEvents(List<String> serverUrls, List<String> publicKeys, int numUserEvents, int sinceWhen) {
if( gDebug > 0) print("Sending multi user request for ${publicKeys.length} users");
const int numMaxUserRequests = 15;
@ -270,7 +275,7 @@ void getMultiUserEvents(List<String> serverUrls, List<String> publicKeys, numUse
}
//print(" sending request form $i to ${i + getUserRequests} ");
List<String> partialList = publicKeys.sublist(i, i + getUserRequests);
relays.getMultiUserEvents(serverUrl, partialList, numUserEvents);
relays.getMultiUserEvents(serverUrl, partialList, numUserEvents, sinceWhen);
}
}
}

View File

@ -1,4 +1,6 @@
const int gEventsSinceDays = 120;
// for debugging
String gCheckEventId = "a4479de655094679cdfb10f347521aa58f24717cdc5ddba89fb346453a8a99ed";

View File

@ -42,7 +42,7 @@ class Tree {
List<String> tempWithoutParent = [];
Map<String, ChatRoom> rooms = {};
if( gDebug > 0) print("In from Events: size of tempChildEventsMap = ${tempChildEventsMap.length} ");
if( gDebug > 0) print("In Tree from Events: size of tempChildEventsMap = ${tempChildEventsMap.length} ");
tempChildEventsMap.forEach((key, value) {
String eId = value.e.eventData.id;
@ -52,20 +52,20 @@ class Tree {
String chatRoomId = value.e.eventData.getChatRoomId();
if( chatRoomId != "") {
if( rooms.containsKey(chatRoomId)) {
if( gDebug > 0) print("Adding new message $key to a chat room $chatRoomId. ");
//if( gDebug > 0) print("Adding new message $key to a chat room $chatRoomId. ");
addMessageToChannel(chatRoomId, eId, tempChildEventsMap, rooms);
if( gDebug > 0) print("Added new message to a chat room $chatRoomId. ");
//if( gDebug > 0) print("Added new message to a chat room $chatRoomId. ");
} else {
List<String> temp = [];
temp.add(eId);
//String name = json['name'];
ChatRoom room = ChatRoom(chatRoomId, "", "", "", temp);
rooms[chatRoomId] = room;
if( gDebug > 0) print("Added new chat room object $chatRoomId and added message to it. ");
//if( gDebug > 0) print("Added new chat room object $chatRoomId and added message to it. ");
}
} else {
if( gDebug > 0) print("Could not get chat room id for event $eId, its original json: ");
if( gDebug > 0) print(value.e.originalJson);
//if( gDebug > 0) print(value.e.originalJson);
}
}
@ -76,7 +76,7 @@ class Tree {
dynamic json = jsonDecode(value.e.eventData.content);
if( rooms.containsKey(chatRoomId)) {
if( rooms[chatRoomId]?.name == "") {
if( gDebug > 0) print('Added room name = ${json['name']} for $chatRoomId' );
//if( gDebug > 0) print('Added room name = ${json['name']} for $chatRoomId' );
rooms[chatRoomId]?.name = json['name'];
}
} else {
@ -926,11 +926,11 @@ void addMessageToChannel(String channelId, String messageId, var tempChildEvents
ChatRoom? room = chatRooms[channelId];
if( room != null ) {
if( room.messageIds.isEmpty) {
if(gDebug> 0) print("room is empty. adding new message and returning. ");
//if(gDebug> 0) print("room is empty. adding new message and returning. ");
room.messageIds.add(messageId);
return;
}
if(gDebug> 0) print("room has ${room.messageIds.length} messages already. adding new one to it. ");
//if(gDebug> 0) print("room has ${room.messageIds.length} messages already. adding new one to it. ");
for(int i = 0; i < room.messageIds.length; i++) {
int eventTime = (tempChildEventsMap[room.messageIds[i]]?.e.eventData.createdAt??0);