chore(contacts): optimise `getContactsByGroup`

iterates: #10886
This commit is contained in:
Patryk Osmaczko 2023-06-06 12:50:31 +02:00 committed by osmaczko
parent 54709cb1c5
commit 0169901fa2
1 changed files with 41 additions and 34 deletions

View File

@ -252,39 +252,46 @@ QtObject:
# Having this logic here we ensure that the same contact group in each part of the app will have the same list # Having this logic here we ensure that the same contact group in each part of the app will have the same list
# of contacts. Be sure when you change any condition here. # of contacts. Be sure when you change any condition here.
let myPubKey = singletonInstance.userProfile.getPubKey() let myPubKey = singletonInstance.userProfile.getPubKey()
let contacts = toSeq(self.contacts.values).map(cd => cd.dto)
if (group == ContactsGroup.IncomingPendingContactRequests): var contacts: seq[ContactsDto] = @[]
return contacts.filter(x => x.id != myPubKey and for cd in self.contacts.values:
x.isContactRequestReceived() and let dto = cd.dto
not x.isContactRequestSent() and if dto.id == myPubKey:
not x.isContactRemoved() and continue
not x.isReceivedContactRequestRejected() and case group
not x.isBlocked()) of ContactsGroup.AllKnownContacts:
elif (group == ContactsGroup.OutgoingPendingContactRequests): contacts.add(dto)
return contacts.filter(x => x.id != myPubKey and of ContactsGroup.IncomingPendingContactRequests:
x.isContactRequestSent() and if dto.isContactRequestReceived() and
not x.isContactRequestReceived() and not dto.isContactRequestSent() and
# not x.isSentContactRequestRejected() and not dto.isContactRemoved() and
not x.isContactRemoved() and not dto.isReceivedContactRequestRejected() and
not x.isBlocked()) not dto.isBlocked():
elif (group == ContactsGroup.IncomingRejectedContactRequests): contacts.add(dto)
return contacts.filter(x => x.id != myPubKey and of ContactsGroup.OutgoingPendingContactRequests:
x.isContactRequestReceived() and if dto.isContactRequestSent() and
x.isReceivedContactRequestRejected() and not dto.isContactRequestReceived() and
not x.isBlocked()) not dto.isContactRemoved() and
# elif (group == ContactsGroup.OutgoingRejectedContactRequests): not dto.isBlocked():
# return contacts.filter(x => x.id != myPubKey and contacts.add(dto)
# x.isContactRequestSent() and of ContactsGroup.IncomingRejectedContactRequests:
# x.isSentContactRequestRejected() and if dto.isContactRequestReceived() and
# not x.isBlocked()) dto.isReceivedContactRequestRejected() and
elif (group == ContactsGroup.BlockedContacts): not dto.isBlocked():
return contacts.filter(x => x.id != myPubKey and contacts.add(dto)
x.isBlocked()) of ContactsGroup.OutgoingRejectedContactRequests:
elif (group == ContactsGroup.MyMutualContacts): # if dto.isContactRequestSent() and
# we need to revise this when we introduce "identity verification" feature # dto.isSentContactRequestRejected() and
return contacts.filter(x => x.id != myPubKey and # not dto.isBlocked():
x.isContact()) # contacts.add(dto)
elif (group == ContactsGroup.AllKnownContacts): discard
of ContactsGroup.BlockedContacts:
if dto.isBlocked():
contacts.add(dto)
of ContactsGroup.MyMutualContacts:
if dto.isContact():
contacts.add(dto)
return contacts return contacts
proc fetchContact(self: Service, id: string): ContactDetails = proc fetchContact(self: Service, id: string): ContactDetails =