-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathmodule.nim
1701 lines (1471 loc) · 73.6 KB
/
module.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import NimQml, tables, json, sugar, sequtils, stew/shims/strformat, marshal, times, chronicles, stint, browsers
import io_interface, view, controller, chat_search_item, chat_search_model
import ephemeral_notification_item, ephemeral_notification_model
import ./communities/models/[pending_request_item, pending_request_model]
import ../shared_models/[user_item, member_item, member_model, section_item, section_model, section_details]
import ../shared_models/[color_hash_item, color_hash_model]
import ../shared_modules/keycard_popup/module as keycard_shared_module
import ../../global/app_sections_config as conf
import ../../global/app_signals
import ../../global/global_singleton
import ../../global/utils as utils
import ../../../constants
import chat_section/model as chat_model
import chat_section/item as chat_item
import chat_section/module as chat_section_module
import wallet_section/module as wallet_section_module
import profile_section/module as profile_section_module
import app_search/module as app_search_module
import stickers/module as stickers_module
import gifs/module as gifs_module
import activity_center/module as activity_center_module
import communities/module as communities_module
import node_section/module as node_section_module
import communities/tokens/models/token_item
import communities/tokens/models/token_model
import network_connection/module as network_connection_module
import shared_urls/module as shared_urls_module
import ../../../app_service/service/contacts/dto/contacts
import ../../../app_service/service/community_tokens/community_collectible_owner
import ../../../app_service/service/keychain/service as keychain_service
import ../../../app_service/service/chat/service as chat_service
import ../../../app_service/service/community/service as community_service
import ../../../app_service/service/message/service as message_service
import ../../../app_service/service/token/service as token_service
import ../../../app_service/service/collectible/service as collectible_service
import ../../../app_service/service/currency/service as currency_service
import ../../../app_service/service/transaction/service as transaction_service
import ../../../app_service/service/wallet_account/service as wallet_account_service
import ../../../app_service/service/provider/service as provider_service
import ../../../app_service/service/profile/service as profile_service
import ../../../app_service/service/accounts/service as accounts_service
import ../../../app_service/service/settings/service as settings_service
import ../../../app_service/service/contacts/service as contacts_service
import ../../../app_service/service/about/service as about_service
import ../../../app_service/service/language/service as language_service
import ../../../app_service/service/privacy/service as privacy_service
import ../../../app_service/service/stickers/service as stickers_service
import ../../../app_service/service/activity_center/service as activity_center_service
import ../../../app_service/service/saved_address/service as saved_address_service
import ../../../app_service/service/node/service as node_service
import ../../../app_service/service/node_configuration/service as node_configuration_service
import ../../../app_service/service/devices/service as devices_service
import ../../../app_service/service/mailservers/service as mailservers_service
import ../../../app_service/service/gif/service as gif_service
import ../../../app_service/service/ens/service as ens_service
import ../../../app_service/service/community_tokens/service as community_tokens_service
import ../../../app_service/service/network/service as network_service
import ../../../app_service/service/general/service as general_service
import ../../../app_service/service/keycard/service as keycard_service
import ../../../app_service/service/shared_urls/service as urls_service
import ../../../app_service/service/network_connection/service as network_connection_service
import ../../../app_service/service/visual_identity/service as procs_from_visual_identity_service
import ../../../app_service/common/types
import ../../../app_service/common/utils as common_utils
import app_service/service/network/network_item
import ../../core/notifications/details
import ../../core/eventemitter
import ../../core/custom_urls/urls_manager
export io_interface
import app/core/tasks/threadpool
const TOAST_MESSAGE_VISIBILITY_DURATION_IN_MS = 5000 # 5 seconds
const STATUS_URL_ENS_RESOLVE_REASON = "StatusUrl"
type
SpectateRequest = object
communityId*: string
channelUuid*: string
Module*[T: io_interface.DelegateInterface] = ref object of io_interface.AccessInterface
delegate: T
view: View
viewVariant: QVariant
controller: Controller
chatSectionModules: OrderedTable[string, chat_section_module.AccessInterface]
events: EventEmitter
urlsManager: UrlsManager
keycardService: keycard_service.Service
settingsService: settings_service.Service
networkService: network_service.Service
privacyService: privacy_service.Service
accountsService: accounts_service.Service
walletAccountService: wallet_account_service.Service
keychainService: keychain_service.Service
networkConnectionService: network_connection_service.Service
walletSectionModule: wallet_section_module.AccessInterface
profileSectionModule: profile_section_module.AccessInterface
stickersModule: stickers_module.AccessInterface
gifsModule: gifs_module.AccessInterface
activityCenterModule: activity_center_module.AccessInterface
communitiesModule: communities_module.AccessInterface
appSearchModule: app_search_module.AccessInterface
nodeSectionModule: node_section_module.AccessInterface
keycardSharedModuleForAuthenticationOrSigning: keycard_shared_module.AccessInterface
keycardSharedModuleKeycardSyncPurpose: keycard_shared_module.AccessInterface
keycardSharedModule: keycard_shared_module.AccessInterface
networkConnectionModule: network_connection_module.AccessInterface
sharedUrlsModule: shared_urls_module.AccessInterface
moduleLoaded: bool
chatsLoaded: bool
communityDataLoaded: bool
pendingSpectateRequest: SpectateRequest
statusDeepLinkToActivate: string
{.push warning[Deprecated]: off.}
# Forward declaration
method calculateProfileSectionHasNotification*[T](self: Module[T]): bool
proc switchToContactOrDisplayUserProfile[T](self: Module[T], publicKey: string)
method activateStatusDeepLink*[T](self: Module[T], statusDeepLink: string)
proc checkIfWeHaveNotifications[T](self: Module[T])
proc newModule*[T](
delegate: T,
events: EventEmitter,
urlsManager: UrlsManager,
keychainService: keychain_service.Service,
accountsService: accounts_service.Service,
chatService: chat_service.Service,
communityService: community_service.Service,
messageService: message_service.Service,
tokenService: token_service.Service,
collectibleService: collectible_service.Service,
currencyService: currency_service.Service,
transactionService: transaction_service.Service,
walletAccountService: wallet_account_service.Service,
profileService: profile_service.Service,
settingsService: settings_service.Service,
contactsService: contacts_service.Service,
aboutService: about_service.Service,
languageService: language_service.Service,
privacyService: privacy_service.Service,
providerService: provider_service.Service,
stickersService: stickers_service.Service,
activityCenterService: activity_center_service.Service,
savedAddressService: saved_address_service.Service,
nodeConfigurationService: node_configuration_service.Service,
devicesService: devices_service.Service,
mailserversService: mailservers_service.Service,
nodeService: node_service.Service,
gifService: gif_service.Service,
ensService: ens_service.Service,
communityTokensService: community_tokens_service.Service,
networkService: network_service.Service,
generalService: general_service.Service,
keycardService: keycard_service.Service,
networkConnectionService: network_connection_service.Service,
sharedUrlsService: urls_service.Service,
threadpool: ThreadPool
): Module[T] =
result = Module[T]()
result.delegate = delegate
result.view = view.newView(result)
result.viewVariant = newQVariant(result.view)
result.controller = controller.newController(
result,
events,
settingsService,
nodeConfigurationService,
accountsService,
chatService,
communityService,
contactsService,
messageService,
gifService,
privacyService,
mailserversService,
nodeService,
communityTokensService,
walletAccountService,
tokenService,
networkService,
sharedUrlsService,
)
result.moduleLoaded = false
result.chatsLoaded = false
result.communityDataLoaded = false
result.events = events
result.urlsManager = urlsManager
result.keycardService = keycardService
result.settingsService = settingsService
result.networkService = networkService
result.privacyService = privacyService
result.accountsService = accountsService
result.walletAccountService = walletAccountService
result.keychainService = keychainService
# Submodules
result.chatSectionModules = initOrderedTable[string, chat_section_module.AccessInterface]()
result.walletSectionModule = wallet_section_module.newModule(
result, events, tokenService, collectibleService, currencyService,
transactionService, walletAccountService,
settingsService, savedAddressService, networkService, accountsService,
keycardService, nodeService, networkConnectionService, devicesService,
communityTokensService, threadpool
)
result.profileSectionModule = profile_section_module.newModule(
result, events, accountsService, settingsService, stickersService,
profileService, contactsService, aboutService, languageService, privacyService, nodeConfigurationService,
devicesService, mailserversService, chatService, ensService, walletAccountService, generalService, communityService,
networkService, keycardService, keychainService, tokenService, nodeService
)
result.stickersModule = stickers_module.newModule(result, events, stickersService, settingsService, walletAccountService,
networkService, tokenService, keycardService)
result.gifsModule = gifs_module.newModule(result, events, gifService)
result.activityCenterModule = activity_center_module.newModule(result, events, activityCenterService, contactsService,
messageService, chatService, communityService)
result.communitiesModule = communities_module.newModule(result, events, communityService, contactsService, communityTokensService,
networkService, transactionService, tokenService, chatService, walletAccountService, keycardService)
result.appSearchModule = app_search_module.newModule(result, events, contactsService, chatService, communityService,
messageService)
result.nodeSectionModule = node_section_module.newModule(result, events, settingsService, nodeService, nodeConfigurationService)
result.networkConnectionModule = network_connection_module.newModule(result, events, networkConnectionService)
result.sharedUrlsModule = shared_urls_module.newModule(result, events, sharedUrlsService)
method delete*[T](self: Module[T]) =
self.controller.delete
self.profileSectionModule.delete
self.stickersModule.delete
self.gifsModule.delete
self.activityCenterModule.delete
self.communitiesModule.delete
for cModule in self.chatSectionModules.values:
cModule.delete
self.chatSectionModules.clear
self.walletSectionModule.delete
self.appSearchModule.delete
self.nodeSectionModule.delete
if not self.keycardSharedModuleForAuthenticationOrSigning.isNil:
self.keycardSharedModuleForAuthenticationOrSigning.delete
if not self.keycardSharedModuleKeycardSyncPurpose.isNil:
self.keycardSharedModuleKeycardSyncPurpose.delete
if not self.keycardSharedModule.isNil:
self.keycardSharedModule.delete
self.networkConnectionModule.delete
self.sharedUrlsModule.delete
self.view.delete
self.viewVariant.delete
method getAppNetwork*[T](self: Module[T]): NetworkItem =
return self.controller.getAppNetwork()
method onAppNetworkChanged*[T](self: Module[T]) =
self.view.emitAppNetworkChangedSignal()
proc createTokenItem[T](self: Module[T], tokenDto: CommunityTokenDto) : token_item.TokenItem =
let network = self.controller.getNetworkByChainId(tokenDto.chainId)
let tokenOwners = self.controller.getCommunityTokenOwners(tokenDto.communityId, tokenDto.chainId, tokenDto.address)
let ownerAddressName = if len(tokenDto.deployer) > 0: self.controller.getCommunityTokenOwnerName(tokenDto.deployer) else: ""
let remainingSupply = if tokenDto.infiniteSupply: stint.parse("0", Uint256) else: self.controller.getRemainingSupply(tokenDto.chainId, tokenDto.address)
let burnState = self.controller.getCommunityTokenBurnState(tokenDto.chainId, tokenDto.address)
let remoteDestructedAddresses = self.controller.getRemoteDestructedAddresses(tokenDto.chainId, tokenDto.address)
let destructedAmount = self.controller.getRemoteDestructedAmount(tokenDto.chainId, tokenDto.address)
result = initTokenItem(tokenDto, network, tokenOwners, ownerAddressName, burnState, remoteDestructedAddresses, remainingSupply, destructedAmount)
proc createTokenItemImproved[T](self: Module[T], tokenDto: CommunityTokenDto, communityTokenJsonItems: JsonNode) : token_item.TokenItem =
# These 3 values come from local caches so they can be done sync
let network = self.controller.getNetworkByChainId(tokenDto.chainId)
let tokenOwners = self.controller.getCommunityTokenOwners(tokenDto.communityId, tokenDto.chainId, tokenDto.address)
let ownerAddressName = if len(tokenDto.deployer) > 0: self.controller.getCommunityTokenOwnerName(tokenDto.deployer) else: ""
var tokenDetails: JsonNode
for details in communityTokenJsonItems.items:
if details["address"].getStr == tokenDto.address:
tokenDetails = details
break
if tokenDetails.kind == JNull:
error "Token details not found for token", name = tokenDto.name, address = tokenDto.address
return
let remainingSupply = tokenDetails["remainingSupply"].getStr
let burnState = tokenDetails["burnState"].getInt
let remoteDestructedAddresses = map(tokenDetails["remoteDestructedAddresses"].getElems(),
proc(remoteDestructAddress: JsonNode): string = remoteDestructAddress.getStr)
let destructedAmount = tokenDetails["destructedAmount"].getStr
result = initTokenItem(
tokenDto,
network,
tokenOwners,
ownerAddressName,
ContractTransactionStatus(burnState),
remoteDestructedAddresses,
stint.parse(remainingSupply, Uint256),
stint.parse(destructedAmount, Uint256),
)
method onCommunityTokensDetailsLoaded[T](self: Module[T], communityId: string,
communityTokens: seq[CommunityTokenDto], communityTokenJsonItems: JsonNode) =
let communityTokensItems = communityTokens.map(proc(tokenDto: CommunityTokenDto): TokenItem =
result = self.createTokenItemImproved(tokenDto, communityTokenJsonItems)
)
self.view.model().setTokenItems(communityId, communityTokensItems)
proc createCommunitySectionItem[T](self: Module[T], communityDetails: CommunityDto): SectionItem =
var communityTokensItems: seq[TokenItem]
if communityDetails.memberRole == MemberRole.Owner or communityDetails.memberRole == MemberRole.TokenMaster:
self.controller.getCommunityTokensDetailsAsync(communityDetails.id)
# Get community members' revealed accounts
# We will update the model later when we finish loading the accounts
self.controller.asyncGetRevealedAccountsForAllMembers(communityDetails.id)
# If there are tokens already in the model, we should keep the existing community tokens, until
# getCommunityTokensDetailsAsync will trigger onCommunityTokensDetailsLoaded
let existingCommunity = self.view.model().getItemById(communityDetails.id)
if not existingCommunity.isEmpty() and not existingCommunity.communityTokens.isNil:
communityTokensItems = existingCommunity.communityTokens.items
let (unviewedCount, notificationsCount) = self.controller.sectionUnreadMessagesAndMentionsCount(
communityDetails.id,
communityDetails.muted,
)
let hasNotification = unviewedCount > 0 or notificationsCount > 0
let active = self.getActiveSectionId() == communityDetails.id # We must pass on if the current item section is currently active to keep that property as it is
# Add members who were kicked from the community after the ownership change for auto-rejoin after they share addresses
var members = communityDetails.members
for requestForAutoRejoin in communityDetails.waitingForSharedAddressesRequestsToJoin:
var chatMember = ChatMember()
chatMember.id = requestForAutoRejoin.publicKey
chatMember.joined = false
chatMember.role = MemberRole.None
members.add(chatMember)
var bannedMembers = newSeq[MemberItem]()
for memberId, memberState in communityDetails.pendingAndBannedMembers.pairs:
let state = memberState.toMembershipRequestState()
case state:
of MembershipRequestState.Banned, MembershipRequestState.BannedWithAllMessagesDelete, MembershipRequestState.UnbannedPending:
bannedMembers.add(self.createMemberItem(memberId, "", state, MemberRole.None))
else:
discard
result = initItem(
communityDetails.id,
sectionType = SectionType.Community,
communityDetails.name,
communityDetails.memberRole,
communityDetails.isControlNode,
communityDetails.description,
communityDetails.introMessage,
communityDetails.outroMessage,
communityDetails.images.thumbnail,
communityDetails.images.banner,
icon = "",
communityDetails.color,
communityDetails.tags,
hasNotification,
notificationsCount,
active,
enabled = true,
communityDetails.joined,
communityDetails.canJoin,
communityDetails.spectated,
communityDetails.canManageUsers,
communityDetails.canRequestAccess,
communityDetails.isMember,
communityDetails.permissions.access,
communityDetails.permissions.ensOnly,
communityDetails.muted,
# members
members.map(proc(member: ChatMember): MemberItem =
var state = MembershipRequestState.Accepted
if member.id in communityDetails.pendingAndBannedMembers:
let memberState = communityDetails.pendingAndBannedMembers[member.id].toMembershipRequestState()
if memberState == MembershipRequestState.BannedPending or memberState == MembershipRequestState.KickedPending:
state = memberState
elif not member.joined:
state = MembershipRequestState.AwaitingAddress
result = self.createMemberItem(member.id, "", state, member.role)
),
# pendingRequestsToJoin
communityDetails.pendingRequestsToJoin.map(x => pending_request_item.initItem(
x.id,
x.publicKey,
x.chatId,
x.communityId,
x.state,
x.our
)),
communityDetails.settings.historyArchiveSupportEnabled,
communityDetails.adminSettings.pinMessageAllMembersEnabled,
bannedMembers,
# pendingMemberRequests
communityDetails.pendingRequestsToJoin.map(proc(requestDto: CommunityMembershipRequestDto): MemberItem =
result = self.createMemberItem(requestDto.publicKey, requestDto.id, MembershipRequestState(requestDto.state), MemberRole.None)
),
# declinedMemberRequests
communityDetails.declinedRequestsToJoin.map(proc(requestDto: CommunityMembershipRequestDto): MemberItem =
result = self.createMemberItem(requestDto.publicKey, requestDto.id, MembershipRequestState(requestDto.state), MemberRole.None)
),
communityDetails.encrypted,
communityTokensItems,
communityDetails.pubsubTopic,
communityDetails.pubsubTopicKey,
communityDetails.shard.index,
)
proc connectForNotificationsOnly[T](self: Module[T]) =
self.events.on(SIGNAL_WALLET_ACCOUNT_SAVED) do(e:Args):
let args = AccountArgs(e)
self.view.showToastAccountAdded(args.account.name)
self.events.on(SIGNAL_WALLET_ACCOUNT_DELETED) do(e:Args):
let args = AccountArgs(e)
self.view.showToastAccountRemoved(args.account.name)
self.events.on(SIGNAL_KEYPAIR_NAME_CHANGED) do(e: Args):
let args = KeypairArgs(e)
self.view.showToastKeypairRenamed(args.oldKeypairName, args.keypair.name)
self.events.on(SIGNAL_NETWORK_ENDPOINT_UPDATED) do(e: Args):
let args = NetworkEndpointUpdatedArgs(e)
self.view.showNetworkEndpointUpdated(args.networkName, args.isTest, args.revertedToDefault)
self.events.on(SIGNAL_KEYPAIR_DELETED) do(e: Args):
let args = KeypairArgs(e)
self.view.showToastKeypairRemoved(args.keyPairName)
self.events.on(SIGNAL_IMPORTED_KEYPAIRS) do(e:Args):
let args = KeypairsArgs(e)
var kpName: string
if args.keypairs.len > 0:
kpName = args.keypairs[0].name
self.view.showToastKeypairsImported(kpName, args.keypairs.len, args.error)
self.events.on(SIGNAL_TRANSACTION_SENT) do(e:Args):
let args = TransactionSentArgs(e)
self.view.showToastTransactionSent(args.chainId, args.txHash, args.uuid, args.error,
ord(args.txType), args.fromAddress, args.toAddress, args.fromTokenKey, args.fromAmount,
args.toTokenKey, args.toAmount)
self.events.on(MARK_WALLET_ADDRESSES_AS_SHOWN) do(e:Args):
let args = WalletAddressesArgs(e)
for address in args.addresses:
self.addressWasShown(address)
self.events.on(SIGNAL_TRANSACTION_SENDING_COMPLETE) do(e:Args):
let args = TransactionMinedArgs(e)
self.view.showToastTransactionSendingComplete(args.chainId, args.transactionHash, args.data, args.success,
ord(args.txType), args.fromAddress, args.toAddress, args.fromTokenKey, args.fromAmount, args.toTokenKey, args.toAmount)
method load*[T](
self: Module[T],
events: EventEmitter,
settingsService: settings_service.Service,
nodeConfigurationService: node_configuration_service.Service,
contactsService: contacts_service.Service,
chatService: chat_service.Service,
communityService: community_service.Service,
messageService: message_service.Service,
mailserversService: mailservers_service.Service,
) =
singletonInstance.engine.setRootContextProperty("mainModule", self.viewVariant)
self.controller.init()
self.view.load()
self.connectForNotificationsOnly()
var activeSection: SectionItem
var activeSectionId = singletonInstance.localAccountSensitiveSettings.getActiveSection()
if (activeSectionId == ""):
activeSectionId = singletonInstance.userProfile.getPubKey()
# Communities Portal Section
let communitiesPortalSectionItem = initItem(
conf.COMMUNITIESPORTAL_SECTION_ID,
SectionType.CommunitiesPortal,
conf.COMMUNITIESPORTAL_SECTION_NAME,
memberRole = MemberRole.Owner,
description = "",
image = "",
icon = conf.COMMUNITIESPORTAL_SECTION_ICON,
color = "",
hasNotification = false,
notificationsCount = 0,
active = false,
enabled = true,
)
self.view.model().addItem(communitiesPortalSectionItem)
if(activeSectionId == communitiesPortalSectionItem.id):
activeSection = communitiesPortalSectionItem
# Wallet Section
let walletSectionItem = initItem(
conf.WALLET_SECTION_ID,
SectionType.Wallet,
conf.WALLET_SECTION_NAME,
memberRole = MemberRole.Owner,
description = "",
introMessage = "",
outroMessage = "",
image = "",
icon = conf.WALLET_SECTION_ICON,
color = "",
hasNotification = false,
notificationsCount = 0,
active = false,
enabled = WALLET_ENABLED,
)
self.view.model().addItem(walletSectionItem)
if(activeSectionId == walletSectionItem.id):
activeSection = walletSectionItem
# Node Management Section
let nodeManagementSectionItem = initItem(
conf.NODEMANAGEMENT_SECTION_ID,
SectionType.NodeManagement,
conf.NODEMANAGEMENT_SECTION_NAME,
memberRole = MemberRole.Owner,
description = "",
introMessage = "",
outroMessage = "",
image = "",
icon = conf.NODEMANAGEMENT_SECTION_ICON,
color = "",
hasNotification = false,
notificationsCount = 0,
active = false,
enabled = singletonInstance.localAccountSensitiveSettings.getNodeManagementEnabled(),
)
self.view.model().addItem(nodeManagementSectionItem)
if(activeSectionId == nodeManagementSectionItem.id):
activeSection = nodeManagementSectionItem
# Profile Section
let profileSettingsSectionItem = initItem(
conf.SETTINGS_SECTION_ID,
SectionType.ProfileSettings,
conf.SETTINGS_SECTION_NAME,
memberRole = MemberRole.Owner,
description = "",
introMessage = "",
outroMessage = "",
image = "",
icon = conf.SETTINGS_SECTION_ICON,
color = "",
hasNotification = self.calculateProfileSectionHasNotification(),
notificationsCount = 0,
active = false,
enabled = true,
)
self.view.model().addItem(profileSettingsSectionItem)
if(activeSectionId == profileSettingsSectionItem.id):
activeSection = profileSettingsSectionItem
self.profileSectionModule.load()
self.stickersModule.load()
self.gifsModule.load()
self.activityCenterModule.load()
self.communitiesModule.load()
self.appSearchModule.load()
self.nodeSectionModule.load()
# Load wallet last as it triggers events that are listened by other modules
self.walletSectionModule.load()
self.networkConnectionModule.load()
self.sharedUrlsModule.load()
# Set active section on app start
# If section is empty or profile then open the loading section until chats are loaded
if activeSection.isEmpty() or activeSection.sectionType == SectionType.ProfileSettings:
# Set bogus Item as active until the chat is loaded
let loadingItem = initItem(
LOADING_SECTION_ID,
SectionType.LoadingSection,
name = "",
memberRole = MemberRole.Owner,
description = "",
image = "",
icon = "",
color = "",
hasNotification = false,
notificationsCount = 0,
active = false,
enabled = true,
)
self.view.model().addItem(loadingItem)
self.setActiveSection(loadingItem, skipSavingInSettings = true)
else:
self.setActiveSection(activeSection)
method onChatsLoaded*[T](
self: Module[T],
events: EventEmitter,
settingsService: settings_service.Service,
nodeConfigurationService: node_configuration_service.Service,
contactsService: contacts_service.Service,
chatService: chat_service.Service,
communityService: community_service.Service,
messageService: message_service.Service,
mailserversService: mailservers_service.Service,
walletAccountService: wallet_account_service.Service,
tokenService: token_service.Service,
communityTokensService: community_tokens_service.Service,
sharedUrlsService: urls_service.Service,
networkService: network_service.Service,
) =
self.chatsLoaded = true
if not self.communityDataLoaded:
return
let myPubKey = singletonInstance.userProfile.getPubKey()
var activeSection: SectionItem
var activeSectionId = singletonInstance.localAccountSensitiveSettings.getActiveSection()
if activeSectionId == "" or activeSectionId == conf.SETTINGS_SECTION_ID:
activeSectionId = myPubKey
# Create personal chat section
self.chatSectionModules[myPubKey] = chat_section_module.newModule(
self,
events,
sectionId = myPubKey,
isCommunity = false,
settingsService,
nodeConfigurationService,
contactsService,
chatService,
communityService,
messageService,
mailserversService,
walletAccountService,
tokenService,
communityTokensService,
sharedUrlsService,
networkService
)
let (unviewedMessagesCount, unviewedMentionsCount) = self.controller.sectionUnreadMessagesAndMentionsCount(
myPubKey,
sectionIsMuted = false
)
let personalChatSectionItem = initItem(
myPubKey,
sectionType = SectionType.Chat,
name = conf.CHAT_SECTION_NAME,
icon = conf.CHAT_SECTION_ICON,
hasNotification = unviewedMessagesCount > 0 or unviewedMentionsCount > 0,
notificationsCount = unviewedMentionsCount,
active = self.getActiveSectionId() == myPubKey,
enabled = true,
joined = true,
canJoin = true,
canRequestAccess = true,
isMember = true,
muted = false,
)
self.view.model().addItem(personalChatSectionItem)
if activeSectionId == personalChatSectionItem.id:
activeSection = personalChatSectionItem
self.chatSectionModules[myPubKey].load()
let communities = self.controller.getJoinedAndSpectatedCommunities()
# Create Community sections
for community in communities:
self.chatSectionModules[community.id] = chat_section_module.newModule(
self,
events,
community.id,
isCommunity = true,
settingsService,
nodeConfigurationService,
contactsService,
chatService,
communityService,
messageService,
mailserversService,
walletAccountService,
tokenService,
communityTokensService,
sharedUrlsService,
networkService
)
let communitySectionItem = self.createCommunitySectionItem(community)
self.view.model().addItem(communitySectionItem)
if activeSectionId == communitySectionItem.id:
activeSection = communitySectionItem
self.chatSectionModules[community.id].load()
# Set active section if it is one of the channel sections
if not activeSection.isEmpty():
self.setActiveSection(activeSection)
# Remove old loading section
self.view.model().removeItem(LOADING_SECTION_ID)
self.view.sectionsLoaded()
if self.statusDeepLinkToActivate != "":
self.activateStatusDeepLink(self.statusDeepLinkToActivate)
self.checkIfWeHaveNotifications()
method onCommunityDataLoaded*[T](
self: Module[T],
events: EventEmitter,
settingsService: settings_service.Service,
nodeConfigurationService: node_configuration_service.Service,
contactsService: contacts_service.Service,
chatService: chat_service.Service,
communityService: community_service.Service,
messageService: message_service.Service,
mailserversService: mailservers_service.Service,
walletAccountService: wallet_account_service.Service,
tokenService: token_service.Service,
communityTokensService: community_tokens_service.Service,
sharedUrlsService: urls_service.Service,
networkService: network_service.Service,
) =
self.communityDataLoaded = true
if not self.chatsLoaded:
return
self.onChatsLoaded(
events,
settingsService,
nodeConfigurationService,
contactsService,
chatService,
communityService,
messageService,
mailserversService,
walletAccountService,
tokenService,
communityTokensService,
sharedUrlsService,
networkService,
)
method onChatsLoadingFailed*[T](self: Module[T]) =
self.view.chatsLoadingFailed()
proc checkIfModuleDidLoad [T](self: Module[T]) =
if self.moduleLoaded:
return
for cModule in self.chatSectionModules.values:
if(not cModule.isLoaded()):
return
# if (not self.communitiesPortalSectionModule.isLoaded()):
# return
if (not self.walletSectionModule.isLoaded()):
return
if(not self.nodeSectionModule.isLoaded()):
return
if(not self.profileSectionModule.isLoaded()):
return
if(not self.stickersModule.isLoaded()):
return
if not self.gifsModule.isLoaded():
return
if(not self.activityCenterModule.isLoaded()):
return
if(not self.communitiesModule.isLoaded()):
return
if(not self.appSearchModule.isLoaded()):
return
if(not self.networkConnectionModule.isLoaded()):
return
self.moduleLoaded = true
self.delegate.mainDidLoad()
method chatSectionDidLoad*[T](self: Module[T]) =
self.checkIfModuleDidLoad()
method communitySectionDidLoad*[T](self: Module[T]) =
self.checkIfModuleDidLoad()
method appSearchDidLoad*[T](self: Module[T]) =
self.checkIfModuleDidLoad()
method stickersDidLoad*[T](self: Module[T]) =
self.checkIfModuleDidLoad()
method gifsDidLoad*[T](self: Module[T]) =
self.checkIfModuleDidLoad()
method activityCenterDidLoad*[T](self: Module[T]) =
self.checkIfModuleDidLoad()
method communitiesModuleDidLoad*[T](self: Module[T]) =
self.checkIfModuleDidLoad()
#method communitiesPortalSectionDidLoad*[T](self: Module[T]) =
# self.checkIfModuleDidLoad()
method walletSectionDidLoad*[T](self: Module[T]) =
self.checkIfModuleDidLoad()
method profileSectionDidLoad*[T](self: Module[T]) =
self.checkIfModuleDidLoad()
method nodeSectionDidLoad*[T](self: Module[T]) =
self.checkIfModuleDidLoad()
method networkConnectionModuleDidLoad*[T](self: Module[T]) =
self.checkIfModuleDidLoad()
method viewDidLoad*[T](self: Module[T]) =
self.checkIfModuleDidLoad()
method emitMailserverWorking*[T](self: Module[T]) =
self.view.emitMailserverWorking()
method emitMailserverNotWorking*[T](self: Module[T]) =
self.view.emitMailserverNotWorking()
method setCommunityIdToSpectate*[T](self: Module[T], communityId: string) =
self.pendingSpectateRequest.communityId = communityId
self.pendingSpectateRequest.channelUuid = ""
method getActiveSectionId*[T](self: Module[T]): string =
return self.controller.getActiveSectionId()
method setActiveSection*[T](self: Module[T], item: SectionItem, skipSavingInSettings: bool = false) =
if(item.isEmpty()):
echo "section is empty and cannot be made as active one"
return
self.controller.setActiveSectionId(item.id)
self.activeSectionSet(item.id, skipSavingInSettings)
method setActiveSectionById*[T](self: Module[T], id: string) =
let item = self.view.model().getItemById(id)
if item.isEmpty():
discard self.communitiesModule.spectateCommunity(id)
else:
self.setActiveSection(item)
proc notifySubModulesAboutChange[T](self: Module[T], sectionId: string) =
for cModule in self.chatSectionModules.values:
cModule.onActiveSectionChange(sectionId)
# If there is a need other section may be notified the same way from here...
method activeSectionSet*[T](self: Module[T], sectionId: string, skipSavingInSettings: bool = false) =
if self.view.activeSection.getId() == sectionId:
return
let item = self.view.model().getItemById(sectionId)
if(item.isEmpty()):
# should never be here
echo "main-module, incorrect section id: ", sectionId
return
case sectionId:
of conf.COMMUNITIESPORTAL_SECTION_ID:
self.communitiesModule.onActivated()
self.view.model().setActiveSection(sectionId)
self.view.activeSectionSet(item)
if not skipSavingInSettings:
singletonInstance.localAccountSensitiveSettings.setActiveSection(sectionId)
self.notifySubModulesAboutChange(sectionId)
proc setSectionAvailability[T](self: Module[T], sectionType: SectionType, available: bool) =
if(available):
self.view.model().enableSection(sectionType)
else:
self.view.model().disableSection(sectionType)
method toggleSection*[T](self: Module[T], sectionType: SectionType) =
if (sectionType == SectionType.NodeManagement):
let enabled = singletonInstance.localAccountSensitiveSettings.getNodeManagementEnabled()
self.setSectionAvailability(sectionType, not enabled)
singletonInstance.localAccountSensitiveSettings.setNodeManagementEnabled(not enabled)
method setCurrentUserStatus*[T](self: Module[T], status: StatusType) =
self.controller.setCurrentUserStatus(status)
proc getChatSectionModule*[T](self: Module[T]): chat_section_module.AccessInterface =
return self.chatSectionModules[singletonInstance.userProfile.getPubKey()]
method getChatSectionModuleAsVariant*[T](self: Module[T]): QVariant =
return self.getChatSectionModule().getModuleAsVariant()
method getCommunitySectionModule*[T](self: Module[T], communityId: string): QVariant =
if(not self.chatSectionModules.contains(communityId)):
echo "main-module, unexisting community key: ", communityId
return
return self.chatSectionModules[communityId].getModuleAsVariant()
method rebuildChatSearchModel*[T](self: Module[T]) =
var items: seq[chat_search_item.Item] = @[]
for chat in self.controller.getAllChats():
var chatName = chat.name
var chatImage = chat.icon
var colorHash: ColorHashDto = @[]
var colorId: int = 0
var sectionId = self.view.model().getItemBySectionType(SectionType.Chat).id()
var sectionName = self.view.model().getItemBySectionType(SectionType.Chat).name()
if chat.chatType == ChatType.OneToOne:
let contactDetails = self.controller.getContactDetails(chat.id)
chatName = contactDetails.defaultDisplayName
chatImage = contactDetails.icon
if not contactDetails.dto.ensVerified:
colorHash = self.controller.getColorHash(chat.id)
colorId = self.controller.getColorId(chat.id)
elif chat.chatType == ChatType.CommunityChat:
sectionId = chat.communityId
sectionName = self.view.model().getItemById(sectionId).name()
items.add(chat_search_item.initItem(
chat.id,
chatName,
chat.color,
colorId,
chatImage,
colorHash.toJson(),
sectionId,
sectionName,
))
self.view.chatSearchModel().setItems(items)
method switchTo*[T](self: Module[T], sectionId, chatId: string) =
self.controller.switchTo(sectionId, chatId, "")
method onActiveChatChange*[T](self: Module[T], sectionId: string, chatId: string) =
self.appSearchModule.onActiveChatChange(sectionId, chatId)
method onChatLeft*[T](self: Module[T], chatId: string) =
self.appSearchModule.updateSearchLocationIfPointToChatWithId(chatId)
proc checkIfWeHaveNotifications[T](self: Module[T]) =
let sectionWithUnread = self.view.model().isThereASectionWithUnreadMessages()
let activtyCenterNotifications = self.activityCenterModule.unreadActivityCenterNotificationsCountFromView() > 0
self.view.setNotificationAvailable(sectionWithUnread or activtyCenterNotifications)
method onActivityNotificationsUpdated[T](self: Module[T]) =
self.checkIfWeHaveNotifications()
method onNotificationsUpdated[T](self: Module[T], sectionId: string, sectionHasUnreadMessages: bool,
sectionNotificationCount: int) =
self.view.model().updateNotifications(sectionId, sectionHasUnreadMessages, sectionNotificationCount)
self.checkIfWeHaveNotifications()
method onNetworkConnected[T](self: Module[T]) =
self.view.setConnected(true)
method onNetworkDisconnected[T](self: Module[T]) =
self.view.setConnected(false)
method isConnected[T](self: Module[T]): bool =
self.controller.isConnected()
method getAppSearchModule*[T](self: Module[T]): QVariant =
self.appSearchModule.getModuleAsVariant()
method communitySpectated*[T](self: Module[T], communityId: string) =
if self.pendingSpectateRequest.communityId != communityId:
return
self.pendingSpectateRequest.communityId = ""
if self.pendingSpectateRequest.channelUuid == "":
return
let chatId = communityId & self.pendingSpectateRequest.channelUuid
self.pendingSpectateRequest.channelUuid = ""
self.controller.switchTo(communityId, chatId, "")
method communityJoined*[T](
self: Module[T],
community: CommunityDto,
events: EventEmitter,