@@ -3,7 +3,7 @@ import { ActivatedRoute, ParamMap, Router } from '@angular/router';
33import { MatLegacyDialog as MatDialog } from '@angular/material/legacy-dialog' ;
44import { UntypedFormBuilder } from '@angular/forms' ;
55import { Subject , forkJoin , iif , of , throwError } from 'rxjs' ;
6- import { takeUntil , finalize , switchMap , map , catchError , tap , debounceTime , distinctUntilChanged } from 'rxjs/operators' ;
6+ import { takeUntil , finalize , switchMap , map , catchError , tap , debounceTime , distinctUntilChanged , take } from 'rxjs/operators' ;
77import { StateService } from '../shared/state.service' ;
88import { NewsService } from '../news/news.service' ;
99import { DialogsFormService } from '../shared/dialogs/dialogs-form.service' ;
@@ -69,6 +69,7 @@ export class CommunityComponent implements OnInit, OnDestroy {
6969 availableLabels : string [ ] = [ ] ;
7070 selectedLabel = '' ;
7171 pinned = false ;
72+ attachmentMap : Record < string , any > = { } ;
7273
7374 get leadersTabLabel ( ) : string {
7475 return this . configuration . planetType === 'nation' ? $localize `Nation Leaders` : $localize `Community Leaders` ;
@@ -108,7 +109,6 @@ export class CommunityComponent implements OnInit, OnDestroy {
108109 const newsSortValue = ( item : any ) => item . sharedDate || item . doc . time ;
109110 this . newsService . newsUpdated$ . pipe ( takeUntil ( this . onDestroy$ ) ) . subscribe ( news => {
110111 this . news = news . sort ( ( a , b ) => newsSortValue ( b ) - newsSortValue ( a ) ) ;
111- this . news . forEach ( item => item . doc . messageLower = item . doc . message ?. toLowerCase ( ) || '' ) ;
112112 this . filteredNews = this . news ;
113113 this . availableLabels = this . getAvailableLabels ( this . news ) ;
114114 this . isLoading = false ;
@@ -291,9 +291,13 @@ export class CommunityComponent implements OnInit, OnDestroy {
291291
292292 getLinks ( planetCode ?) {
293293 return this . teamsService . getTeamMembers ( this . team || this . teamObject ( planetCode ) , true ) . pipe ( map ( ( docs ) => {
294- const { link : links , transaction : finances , report : reports } = docs . reduce ( ( docObject , doc ) => ( {
295- ...docObject , [ doc . docType ] : [ ...( docObject [ doc . docType ] || [ ] ) , doc ]
296- } ) , { link : [ ] , transaction : [ ] } ) ;
294+ const { link : links , transaction : finances , report : reports } = docs . reduce ( ( docObject , doc ) => {
295+ if ( ! docObject [ doc . docType ] ) {
296+ docObject [ doc . docType ] = [ ] ;
297+ }
298+ docObject [ doc . docType ] . push ( doc ) ;
299+ return docObject ;
300+ } , { link : [ ] , transaction : [ ] , report : [ ] } ) ;
297301 return { links, finances, reports } ;
298302 } ) ) ;
299303 }
@@ -312,21 +316,40 @@ export class CommunityComponent implements OnInit, OnDestroy {
312316
313317 setCouncillors ( users ) {
314318 const planetCode = this . planetCode ? this . planetCode : this . stateService . configuration . code ;
315- this . couchService . findAll ( 'attachments' ) . subscribe ( ( attachments : any [ ] ) => {
316- this . councillors = users
317- . filter ( user => planetCode === user . doc . planetCode && ( user . doc . isUserAdmin || user . doc . roles . indexOf ( 'leader' ) ) !== - 1 )
318- . map ( user => {
319- const { _id : userId , planetCode : userPlanetCode , name } = user . doc ;
320- const attachmentId = `org.couchdb.user:${ name } @${ userPlanetCode } ` ;
321- const attachmentDoc : any = attachments . find ( attachment => attachment . _id === attachmentId ) ;
322- const avatar = attachmentDoc ?
323- `${ environment . couchAddress } /attachments/${ attachmentId } /${ Object . keys ( attachmentDoc . _attachments ) [ 0 ] } ` :
324- ( user . imageSrc || 'assets/image.png' ) ;
325- return { avatar, userDoc : user , userId, name : user . doc . name , userPlanetCode : user . doc . planetCode , ...user } ;
326- } ) ;
319+ const councillorUsers = users
320+ . filter ( user => planetCode === user . doc . planetCode && ( user . doc . isUserAdmin || user . doc . roles . indexOf ( 'leader' ) ) !== - 1 ) ;
321+ const attachmentIds = councillorUsers
322+ . map ( user => `org.couchdb.user:${ user . doc . name } @${ user . doc . planetCode } ` )
323+ . filter ( id => ! ! id ) ;
324+
325+ this . fetchMissingAttachments ( attachmentIds ) . pipe ( take ( 1 ) ) . subscribe ( ( ) => {
326+ this . councillors = councillorUsers . map ( user => {
327+ const { _id : userId , planetCode : userPlanetCode , name } = user . doc ;
328+ const attachmentId = `org.couchdb.user:${ name } @${ userPlanetCode } ` ;
329+ const attachmentDoc : any = this . attachmentMap [ attachmentId ] ;
330+ const avatar = attachmentDoc ?
331+ `${ environment . couchAddress } /attachments/${ attachmentId } /${ Object . keys ( attachmentDoc . _attachments ) [ 0 ] } ` :
332+ ( user . imageSrc || 'assets/image.png' ) ;
333+ return { avatar, userDoc : user , userId, name : user . doc . name , userPlanetCode : user . doc . planetCode , ...user } ;
334+ } ) ;
327335 } ) ;
328336 }
329337
338+ private fetchMissingAttachments ( ids : string [ ] ) {
339+ const missing = ids . filter ( id => ! this . attachmentMap [ id ] ) ;
340+ if ( missing . length === 0 ) {
341+ return of ( undefined ) ;
342+ }
343+ return this . couchService . findAttachmentsByIds ( missing ) . pipe (
344+ tap ( ( attachments : any [ ] ) => {
345+ attachments . forEach ( attachment => {
346+ this . attachmentMap [ attachment . _id ] = attachment ;
347+ } ) ;
348+ } ) ,
349+ map ( ( ) => undefined )
350+ ) ;
351+ }
352+
330353 openAddLinkDialog ( ) {
331354 this . dialog . open ( CommunityLinkDialogComponent , {
332355 width : '50vw' ,
@@ -498,7 +521,12 @@ export class CommunityComponent implements OnInit, OnDestroy {
498521 }
499522 if ( this . voiceSearch ) {
500523 const lower = this . voiceSearch . toLowerCase ( ) ;
501- filtered = filtered . filter ( item => item . doc . messageLower . includes ( lower ) ) ;
524+ filtered = filtered . filter ( item => {
525+ if ( typeof item . doc . messageLower !== 'string' ) {
526+ item . doc . messageLower = ( item . doc . message || '' ) . toLowerCase ( ) ;
527+ }
528+ return item . doc . messageLower . includes ( lower ) ;
529+ } ) ;
502530 }
503531 this . filteredNews = filtered ;
504532 }
0 commit comments