@@ -68,29 +68,36 @@ void BaseChatMesh::bootstrapRTCfromContacts() {
6868}
6969
7070ContactInfo* BaseChatMesh::allocateContactSlot (bool transient_only) {
71- if (num_contacts < MAX_CONTACTS ) {
72- return &contacts[num_contacts++];
73- } else if (transient_only || shouldOverwriteWhenFull ()) {
74- // Find oldest non-favourite contact by oldest lastmod timestamp
75- int oldest_idx = -1 ;
76- uint32_t oldest_lastmod = 0xFFFFFFFF ;
77- for (int i = 0 ; i < num_contacts; i++) {
78- if (transient_only) {
79- if (contacts[i].type == ADV_TYPE_NONE && contacts[i].lastmod < oldest_lastmod) {
80- oldest_lastmod = contacts[i].lastmod ;
81- oldest_idx = i;
82- }
83- } else {
71+ int oldest_idx = -1 ;
72+ uint32_t oldest_lastmod = 0xFFFFFFFF ;
73+ if (transient_only) {
74+ // only allocate from first N
75+ for (int i = 0 ; i < MAX_ANON_CONTACTS ; i++) {
76+ if (contacts[i].type == ADV_TYPE_NONE && contacts[i].lastmod < oldest_lastmod) {
77+ oldest_lastmod = contacts[i].lastmod ;
78+ oldest_idx = i;
79+ }
80+ }
81+ if (oldest_idx >= 0 ) {
82+ // NOTE: do NOT call onContactOverwrite()
83+ return &contacts[oldest_idx];
84+ }
85+ } else {
86+ if (num_contacts < MAX_ANON_CONTACTS +MAX_CONTACTS ) {
87+ return &contacts[num_contacts++];
88+ } else if (shouldOverwriteWhenFull ()) {
89+ // Find oldest non-favourite contact by oldest lastmod timestamp
90+ for (int i = MAX_ANON_CONTACTS ; i < num_contacts; i++) {
8491 bool is_favourite = (contacts[i].flags & 0x01 ) != 0 ;
85- if (!is_favourite && contacts[i].lastmod < oldest_lastmod && contacts[i]. type != ADV_TYPE_NONE ) {
92+ if (!is_favourite && contacts[i].lastmod < oldest_lastmod) {
8693 oldest_lastmod = contacts[i].lastmod ;
8794 oldest_idx = i;
8895 }
8996 }
90- }
91- if (oldest_idx >= 0 ) {
92- onContactOverwrite ( contacts[oldest_idx]. id . pub_key ) ;
93- return &contacts[oldest_idx];
97+ if (oldest_idx >= 0 ) {
98+ onContactOverwrite (contacts[oldest_idx]. id . pub_key );
99+ return & contacts[oldest_idx];
100+ }
94101 }
95102 }
96103 return NULL ; // no space, no overwrite or all contacts are all favourites
@@ -139,6 +146,11 @@ void BaseChatMesh::onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id,
139146 packet->header = save;
140147 }
141148
149+ if (from && from->type == ADV_TYPE_NONE ) { // already in contacts, but from a temporary ANON_REQ ?
150+ memset (from, 0 , sizeof (*from)); // clear the anon/temp slot
151+ from = NULL ; // do normal 'add' flow
152+ }
153+
142154 bool is_new = false ; // true = not in contacts[], false = exists in contacts[]
143155 if (from == NULL ) {
144156 if (!shouldAutoAddContactType (parser.getType ())) {
@@ -930,11 +942,11 @@ bool BaseChatMesh::getContactByIdx(uint32_t idx, ContactInfo& contact) {
930942}
931943
932944ContactsIterator BaseChatMesh::startContactsIterator () {
933- return ContactsIterator ();
945+ return ContactsIterator (MAX_ANON_CONTACTS ); // start at offset, skip the anon entries
934946}
935947
936948bool ContactsIterator::hasNext (const BaseChatMesh* mesh, ContactInfo& dest) {
937- if (next_idx >= mesh->getNumContacts ()) return false ;
949+ if (next_idx >= mesh->getTotalContactSlots ()) return false ;
938950
939951 dest = mesh->contacts [next_idx++];
940952 return true ;
0 commit comments