@@ -582,82 +582,94 @@ final class ProposalServiceImpl implements ProposalService {
582582
583583 @override
584584 Stream <List <DetailProposal >> watchUserProposals () async * {
585- yield * _userService.watchUser.distinct ().switchMap ((user) {
586- final authorId = user.activeAccount? .catalystId;
587- if (! _isProposer (user) || authorId == null ) {
588- return const Stream .empty ();
589- }
590-
591- return _proposalRepository
592- .watchUserProposals (authorId: authorId)
593- .distinct ()
594- .switchMap <List <DetailProposal >>((documents) async * {
595- if (documents.isEmpty) {
596- yield [];
597- return ;
598- }
599- final proposalsDataStreams = await Future .wait (
600- documents.map (_createProposalDataStream).toList (),
601- );
602-
603- yield * Rx .combineLatest (
604- proposalsDataStreams,
605- (List <ProposalData ?> proposalsData) async {
606- // Note. one is null and two versions of same id.
607- final validProposalsData = proposalsData.whereType <ProposalData >().toList ();
585+ yield * _userService //
586+ .watchUser
587+ .distinct ()
588+ .switchMap (_userWhenUnlockedStream)
589+ .switchMap ((user) {
590+ if (user == null ) return const Stream .empty ();
591+
592+ final authorId = user.activeAccount? .catalystId;
593+ if (! _isProposer (user) || authorId == null ) {
594+ return const Stream .empty ();
595+ }
608596
609- final groupedProposals = groupBy (
610- validProposalsData,
611- (data) => data.document.metadata.selfRef.id,
597+ return _proposalRepository
598+ .watchUserProposals (authorId: authorId)
599+ .distinct ()
600+ .switchMap <List <DetailProposal >>((documents) async * {
601+ if (documents.isEmpty) {
602+ yield [];
603+ return ;
604+ }
605+ final proposalsDataStreams = await Future .wait (
606+ documents.map (_createProposalDataStream).toList (),
612607 );
613608
614- final filteredProposalsData = groupedProposals.values
615- .map ((group) {
616- if (group.any (
617- (p) => p.publish != ProposalPublish .localDraft,
618- )) {
619- return group.where (
620- (p) => p.publish != ProposalPublish .localDraft,
621- );
622- }
623- return group;
624- })
625- .expand ((group) => group)
626- .toList ();
627-
628- final proposalsWithVersions = await Future .wait (
629- filteredProposalsData.map ((proposalData) async {
630- final versions = await _getDetailVersionsOfProposal (proposalData);
631- return DetailProposal .fromData (proposalData, versions);
632- }),
633- );
634- return proposalsWithVersions;
635- },
636- ).switchMap (Stream .fromFuture);
637- });
638- });
609+ yield * Rx .combineLatest (
610+ proposalsDataStreams,
611+ (List <ProposalData ?> proposalsData) async {
612+ // Note. one is null and two versions of same id.
613+ final validProposalsData = proposalsData.whereType <ProposalData >().toList ();
614+
615+ final groupedProposals = groupBy (
616+ validProposalsData,
617+ (data) => data.document.metadata.selfRef.id,
618+ );
619+
620+ final filteredProposalsData = groupedProposals.values
621+ .map ((group) {
622+ if (group.any (
623+ (p) => p.publish != ProposalPublish .localDraft,
624+ )) {
625+ return group.where (
626+ (p) => p.publish != ProposalPublish .localDraft,
627+ );
628+ }
629+ return group;
630+ })
631+ .expand ((group) => group)
632+ .toList ();
633+
634+ final proposalsWithVersions = await Future .wait (
635+ filteredProposalsData.map ((proposalData) async {
636+ final versions = await _getDetailVersionsOfProposal (proposalData);
637+ return DetailProposal .fromData (proposalData, versions);
638+ }),
639+ );
640+ return proposalsWithVersions;
641+ },
642+ ).switchMap (Stream .fromFuture);
643+ });
644+ });
639645 }
640646
641647 @override
642648 Stream <ProposalsCount > watchUserProposalsCount () {
643- return _userService.watchUser.distinct ().switchMap ((user) {
644- final authorId = user.activeAccount? .catalystId;
645- if (! _isProposer (user) || authorId == null ) {
646- // user is not eligible for creating proposals
647- return const Stream .empty ();
648- }
649-
650- final activeCampaign = _activeCampaignObserver.campaign;
651- final categoriesIds = activeCampaign? .categories.map ((e) => e.selfRef.id).toList ();
652-
653- final filters = ProposalsCountFilters (
654- author: authorId,
655- onlyAuthor: true ,
656- campaign: categoriesIds != null ? CampaignFilters (categoriesIds: categoriesIds) : null ,
657- );
649+ return _userService //
650+ .watchUser
651+ .distinct ()
652+ .switchMap (_userWhenUnlockedStream)
653+ .switchMap ((user) {
654+ if (user == null ) return const Stream .empty ();
655+
656+ final authorId = user.activeAccount? .catalystId;
657+ if (! _isProposer (user) || authorId == null ) {
658+ // user is not eligible for creating proposals
659+ return const Stream .empty ();
660+ }
658661
659- return watchProposalsCount (filters: filters);
660- });
662+ final activeCampaign = _activeCampaignObserver.campaign;
663+ final categoriesIds = activeCampaign? .categories.map ((e) => e.selfRef.id).toList ();
664+
665+ final filters = ProposalsCountFilters (
666+ author: authorId,
667+ onlyAuthor: true ,
668+ campaign: categoriesIds != null ? CampaignFilters (categoriesIds: categoriesIds) : null ,
669+ );
670+
671+ return watchProposalsCount (filters: filters);
672+ });
661673 }
662674
663675 // TODO(damian-molinski): Remove this when voteBy is implemented.
@@ -788,4 +800,13 @@ final class ProposalServiceImpl implements ProposalService {
788800
789801 return page.copyWithItems (proposals);
790802 }
803+
804+ Stream <User ?> _userWhenUnlockedStream (User user) {
805+ final activeAccount = user.activeAccount;
806+
807+ if (activeAccount == null ) return Stream .value (null );
808+
809+ final isUnlockedStream = activeAccount.keychain.watchIsUnlocked;
810+ return isUnlockedStream.map ((isUnlocked) => isUnlocked ? user : null );
811+ }
791812}
0 commit comments