Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor contacts models to have a single model, remove useless properties and improve updating #16667

Merged
merged 12 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ method toggleReactionFromOthers*(self: Module, messageId: string, emojiId: int,
if(item.shouldAddReaction(emojiIdAsEnum, reactionFrom)):
let userWhoAddedThisReaction = self.controller.getContactById(reactionFrom)
self.view.pinnedModel().addReaction(messageId, emojiIdAsEnum, didIReactWithThisEmoji = false,
userWhoAddedThisReaction.id, userWhoAddedThisReaction.userDefaultDisplayName(), reactionId)
userWhoAddedThisReaction.id, userWhoAddedThisReaction.userDefaultDisplayName(), reactionId)
else:
self.view.pinnedModel().removeReaction(messageId, emojiIdAsEnum, reactionId, didIRemoveThisReaction = false)
else:
Expand Down
3 changes: 0 additions & 3 deletions src/app/modules/main/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1188,8 +1188,6 @@ method getContactDetailsAsJson*[T](self: Module[T], publicKey: string, getVerifi

let jsonObj = %* {
# contact details props
"defaultDisplayName": contactDetails.defaultDisplayName,
"optionalName": contactDetails.optionalName,
"icon": contactDetails.icon,
"isCurrentUser": contactDetails.isCurrentUser,
"colorId": contactDetails.colorId,
Expand All @@ -1210,7 +1208,6 @@ method getContactDetailsAsJson*[T](self: Module[T], publicKey: string, getVerifi
"isBlocked": contactDetails.dto.isBlocked,
"isContactRequestReceived": contactDetails.dto.isContactRequestReceived,
"isContactRequestSent": contactDetails.dto.isContactRequestSent,
"isSyncing": contactDetails.dto.isSyncing,
"removed": contactDetails.dto.removed,
"trustStatus": contactDetails.dto.trustStatus.int,
"contactRequestState": contactDetails.dto.contactRequestState.int,
Expand Down
10 changes: 5 additions & 5 deletions src/app/modules/main/profile_section/contacts/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ proc init*(self: Controller) =

self.events.on(SIGNAL_CONTACT_ADDED) do(e: Args):
var args = ContactArgs(e)
self.delegate.contactAdded(args.contactId)
self.delegate.addOrUpdateContactItem(args.contactId)

self.events.on(SIGNAL_CONTACT_BLOCKED) do(e: Args):
var args = ContactArgs(e)
self.delegate.contactBlocked(args.contactId)
self.delegate.addOrUpdateContactItem(args.contactId)

self.events.on(SIGNAL_CONTACT_UNBLOCKED) do(e: Args):
var args = ContactArgs(e)
self.delegate.contactUnblocked(args.contactId)
self.delegate.addOrUpdateContactItem(args.contactId)

self.events.on(SIGNAL_CONTACT_REMOVED) do(e: Args):
var args = ContactArgs(e)
self.delegate.contactRemoved(args.contactId)
self.delegate.addOrUpdateContactItem(args.contactId)

self.events.on(SIGNAL_CONTACT_NICKNAME_CHANGED) do(e: Args):
var args = ContactArgs(e)
Expand All @@ -67,7 +67,7 @@ proc init*(self: Controller) =

self.events.on(SIGNAL_CONTACT_UPDATED) do(e: Args):
var args = ContactArgs(e)
self.delegate.contactUpdated(args.contactId)
self.delegate.addOrUpdateContactItem(args.contactId)

self.events.on(SIGNAL_CONTACTS_STATUS_UPDATED) do(e: Args):
let args = ContactsStatusUpdatedArgs(e)
Expand Down
14 changes: 1 addition & 13 deletions src/app/modules/main/profile_section/contacts/io_interface.nim
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,7 @@ method removeContact*(self: AccessInterface, publicKey: string) {.base.} =

# Controller Delegate Interface

method contactAdded*(self: AccessInterface, publicKey: string) {.base.} =
raise newException(ValueError, "No implementation available")

method contactBlocked*(self: AccessInterface, publicKey: string) {.base.} =
raise newException(ValueError, "No implementation available")

method contactUnblocked*(self: AccessInterface, publicKey: string) {.base.} =
raise newException(ValueError, "No implementation available")

method contactRemoved*(self: AccessInterface, publicKey: string) {.base.} =
method addOrUpdateContactItem*(self: AccessInterface, publicKey: string) {.base.} =
raise newException(ValueError, "No implementation available")

method contactNicknameChanged*(self: AccessInterface, publicKey: string) {.base.} =
Expand All @@ -80,9 +71,6 @@ method contactNicknameChanged*(self: AccessInterface, publicKey: string) {.base.
method contactTrustStatusChanged*(self: AccessInterface, publicKey: string, trustStatus: TrustStatus) {.base.} =
raise newException(ValueError, "No implementation available")

method contactUpdated*(self: AccessInterface, publicKey: string) {.base.} =
raise newException(ValueError, "No implementation available")

method contactsStatusUpdated*(self: AccessInterface, statusUpdates: seq[StatusUpdateDto]) {.base.} =
raise newException(ValueError, "No implementation available")

Expand Down
116 changes: 37 additions & 79 deletions src/app/modules/main/profile_section/contacts/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import io_interface, view, controller, json
import ../../../shared_models/user_item
import ../../../shared_models/user_model
import ../io_interface as delegate_interface
import ../../../../global/global_singleton

import ../../../../core/eventemitter
import app_service/common/types
Expand Down Expand Up @@ -87,16 +86,13 @@ proc createItemFromPublicKey(self: Module, publicKey: string): UserItem =
isBlocked = contactDetails.dto.isBlocked(),
isCurrentUser = contactDetails.isCurrentUser,
contactRequest = toContactStatus(contactDetails.dto.contactRequestState),
defaultDisplayName = contactDetails.defaultDisplayName,
optionalName = contactDetails.optionalName,
lastUpdated = contactDetails.dto.lastUpdated,
lastUpdatedLocally = contactDetails.dto.lastUpdatedLocally,
bio = contactDetails.dto.bio,
thumbnailImage = contactDetails.dto.image.thumbnail,
largeImage = contactDetails.dto.image.large,
isContactRequestReceived = contactDetails.dto.isContactRequestReceived,
isContactRequestSent = contactDetails.dto.isContactRequestSent,
isSyncing = contactDetails.dto.isSyncing,
isRemoved = contactDetails.dto.removed,
trustStatus = contactDetails.dto.trustStatus,
)
Expand All @@ -105,8 +101,7 @@ proc buildModel(self: Module, model: Model, group: ContactsGroup) =
var items: seq[UserItem]
let contacts = self.controller.getContacts(group)
for c in contacts:
let item = self.createItemFromPublicKey(c.id)
items.add(item)
items.add(self.createItemFromPublicKey(c.id))

model.addItems(items)

Expand All @@ -123,13 +118,6 @@ method viewDidLoad*(self: Module) =

method onContactsLoaded*(self: Module) =
self.buildModel(self.view.contactsModel(), ContactsGroup.AllKnownContacts)
self.buildModel(self.view.myMutualContactsModel(), ContactsGroup.MyMutualContacts)
self.buildModel(self.view.blockedContactsModel(), ContactsGroup.BlockedContacts)
self.buildModel(self.view.receivedContactRequestsModel(), ContactsGroup.IncomingPendingContactRequests)
self.buildModel(self.view.sentContactRequestsModel(), ContactsGroup.OutgoingPendingContactRequests)
# Temporary commented until we provide appropriate flags on the `status-go` side to cover all sections.
# self.buildModel(self.view.receivedButRejectedContactRequestsModel(), ContactsGroup.IncomingRejectedContactRequests)
# self.buildModel(self.view.sentButRejectedContactRequestsModel(), ContactsGroup.IncomingRejectedContactRequests)

method getModuleAsVariant*(self: Module): QVariant =
return self.viewVariant
Expand Down Expand Up @@ -169,81 +157,51 @@ method removeContact*(self: Module, publicKey: string) =
method changeContactNickname*(self: Module, publicKey: string, nickname: string) =
self.controller.changeContactNickname(publicKey, nickname)

proc addItemToAppropriateModel(self: Module, item: UserItem) =
if(singletonInstance.userProfile.getPubKey() == item.pubKey):
return
let contact = self.controller.getContact(item.pubKey())

self.view.contactsModel().addItem(item)
if contact.isBlocked():
self.view.blockedContactsModel().addItem(item)
return

case contact.contactRequestState:
of ContactRequestState.Received:
self.view.receivedContactRequestsModel().addItem(item)
of ContactRequestState.Sent:
self.view.sentContactRequestsModel().addItem(item)
of ContactRequestState.Mutual:
self.view.myMutualContactsModel().addItem(item)
else:
return

proc removeItemWithPubKeyFromAllModels(self: Module, publicKey: string) =
self.view.contactsModel().removeItemById(publicKey)
self.view.myMutualContactsModel().removeItemById(publicKey)
self.view.receivedContactRequestsModel().removeItemById(publicKey)
self.view.sentContactRequestsModel().removeItemById(publicKey)
# Temporary commented until we provide appropriate flags on the `status-go` side to cover all sections.
# self.view.receivedButRejectedContactRequestsModel().removeItemById(publicKey)
# self.view.sentButRejectedContactRequestsModel().removeItemById(publicKey)
self.view.blockedContactsModel().removeItemById(publicKey)

proc removeIfExistsAndAddToAppropriateModel(self: Module, publicKey: string) =
self.removeItemWithPubKeyFromAllModels(publicKey)
method addOrUpdateContactItem*(self: Module, publicKey: string) =
let ind = self.view.contactsModel().findIndexByPubKey(publicKey)
let item = self.createItemFromPublicKey(publicKey)
self.addItemToAppropriateModel(item)

method contactAdded*(self: Module, publicKey: string) =
self.removeIfExistsAndAddToAppropriateModel(publicKey)

method contactBlocked*(self: Module, publicKey: string) =
self.removeIfExistsAndAddToAppropriateModel(publicKey)

method contactUnblocked*(self: Module, publicKey: string) =
self.removeIfExistsAndAddToAppropriateModel(publicKey)

method contactRemoved*(self: Module, publicKey: string) =
self.removeIfExistsAndAddToAppropriateModel(publicKey)

method contactUpdated*(self: Module, publicKey: string) =
self.removeIfExistsAndAddToAppropriateModel(publicKey)
if ind == -1:
self.view.contactsModel().addItem(item)
return
self.view.contactsModel().updateItem(
publicKey,
item.displayName,
item.ensName,
item.isEnsVerified,
item.localNickname,
item.alias,
item.icon,
item.trustStatus,
item.onlineStatus,
item.isContact,
item.isBlocked,
item.contactRequest,
item.lastUpdated,
item.lastUpdatedLocally,
item.bio,
item.thumbnailImage,
item.largeImage,
item.isContactRequestReceived,
item.isContactRequestSent,
item.isRemoved,
)

method contactsStatusUpdated*(self: Module, statusUpdates: seq[StatusUpdateDto]) =
for s in statusUpdates:
let status = toOnlineStatus(s.statusType)
self.view.myMutualContactsModel().setOnlineStatus(s.publicKey, status)
self.view.contactsModel().setOnlineStatus(s.publicKey, status)
self.view.contactsModel().setOnlineStatus(s.publicKey, toOnlineStatus(s.statusType))

method contactNicknameChanged*(self: Module, publicKey: string) =
let contactDetails = self.controller.getContactDetails(publicKey)
let displayName = contactDetails.dto.displayName
let ensName = contactDetails.dto.name
let localNickname = contactDetails.dto.localNickname

self.view.contactsModel().setName(publicKey, displayName, ensName, localNickname)
self.view.myMutualContactsModel().setName(publicKey, displayName, ensName, localNickname)
self.view.receivedContactRequestsModel().setName(publicKey, displayName, ensName, localNickname)
self.view.sentContactRequestsModel().setName(publicKey, displayName, ensName, localNickname)
# Temporary commented until we provide appropriate flags on the `status-go` side to cover all sections.
# self.view.receivedButRejectedContactRequestsModel().setName(publicKey, displayName, ensName, localNickname)
# self.view.sentButRejectedContactRequestsModel().setName(publicKey, displayName, ensName, localNickname)
self.view.blockedContactsModel().setName(publicKey, displayName, ensName, localNickname)

self.view.contactsModel().setName(
publicKey,
contactDetails.dto.displayName,
contactDetails.dto.name,
contactDetails.dto.localNickname,
)

method contactTrustStatusChanged*(self: Module, publicKey: string, trustStatus: TrustStatus) =
self.view.contactsModel().updateTrustStatus(publicKey, trustStatus)
self.view.myMutualContactsModel().updateTrustStatus(publicKey, trustStatus)
self.view.blockedContactsModel().updateTrustStatus(publicKey, trustStatus)

method markAsTrusted*(self: Module, publicKey: string): void =
self.controller.markAsTrusted(publicKey)
Expand All @@ -259,7 +217,7 @@ method requestContactInfo*(self: Module, publicKey: string) =

method onContactInfoRequestFinished*(self: Module, publicKey: string, ok: bool) =
if ok:
self.removeIfExistsAndAddToAppropriateModel(publicKey)
self.addOrUpdateContactItem(publicKey)
self.view.onContactInfoRequestFinished(publicKey, ok)

method shareUserUrlWithData*(self: Module, pubkey: string): string =
Expand Down
Loading