@@ -30,7 +30,7 @@ import {
3030 SortAlgorithm
3131} from "./models" ;
3232import { FILTER_CHANGED , FilterPriority , IFilterCondition } from "../filters/IFilterCondition" ;
33- import { EffectiveMembership , splitRoomsByMembership } from "../membership" ;
33+ import { EffectiveMembership , getEffectiveMembership , splitRoomsByMembership } from "../membership" ;
3434import { OrderingAlgorithm } from "./list-ordering/OrderingAlgorithm" ;
3535import { getListAlgorithmInstance } from "./list-ordering" ;
3636
@@ -99,6 +99,14 @@ export class Algorithm extends EventEmitter {
9999 return this . _cachedRooms ;
100100 }
101101
102+ /**
103+ * Awaitable version of the sticky room setter.
104+ * @param val The new room to sticky.
105+ */
106+ public async setStickyRoomAsync ( val : Room ) {
107+ await this . updateStickyRoom ( val ) ;
108+ }
109+
102110 public getTagSorting ( tagId : TagID ) : SortAlgorithm {
103111 return this . sortAlgorithms [ tagId ] ;
104112 }
@@ -160,10 +168,13 @@ export class Algorithm extends EventEmitter {
160168 // It's possible to have no selected room. In that case, clear the sticky room
161169 if ( ! val ) {
162170 if ( this . _stickyRoom ) {
171+ const stickyRoom = this . _stickyRoom . room ;
172+ this . _stickyRoom = null ; // clear before we go to update the algorithm
173+
163174 // Lie to the algorithm and re-add the room to the algorithm
164- await this . handleRoomUpdate ( this . _stickyRoom . room , RoomUpdateCause . NewRoom ) ;
175+ await this . handleRoomUpdate ( stickyRoom , RoomUpdateCause . NewRoom ) ;
176+ return ;
165177 }
166- this . _stickyRoom = null ;
167178 return ;
168179 }
169180
@@ -289,6 +300,8 @@ export class Algorithm extends EventEmitter {
289300 }
290301
291302 protected recalculateFilteredRoomsForTag ( tagId : TagID ) : void {
303+ if ( ! this . hasFilters ) return ; // don't bother doing work if there's nothing to do
304+
292305 console . log ( `Recalculating filtered rooms for ${ tagId } ` ) ;
293306 delete this . filteredRooms [ tagId ] ;
294307 const rooms = this . cachedRooms [ tagId ] . map ( r => r ) ; // cheap clone
@@ -458,14 +471,7 @@ export class Algorithm extends EventEmitter {
458471
459472 // Now process all the joined rooms. This is a bit more complicated
460473 for ( const room of memberships [ EffectiveMembership . Join ] ) {
461- let tags = Object . keys ( room . tags || { } ) ;
462-
463- if ( tags . length === 0 ) {
464- // Check to see if it's a DM if it isn't anything else
465- if ( DMRoomMap . shared ( ) . getUserIdForRoomId ( room . roomId ) ) {
466- tags = [ DefaultTagID . DM ] ;
467- }
468- }
474+ const tags = this . getTagsOfJoinedRoom ( room ) ;
469475
470476 let inTag = false ;
471477 if ( tags . length > 0 ) {
@@ -496,6 +502,39 @@ export class Algorithm extends EventEmitter {
496502 this . updateTagsFromCache ( ) ;
497503 }
498504
505+ private getTagsForRoom ( room : Room ) : TagID [ ] {
506+ // XXX: This duplicates a lot of logic from setKnownRooms above, but has a slightly
507+ // different use case and therefore different performance curve
508+
509+ const tags : TagID [ ] = [ ] ;
510+
511+ const membership = getEffectiveMembership ( room . getMyMembership ( ) ) ;
512+ if ( membership === EffectiveMembership . Invite ) {
513+ tags . push ( DefaultTagID . Invite ) ;
514+ } else if ( membership === EffectiveMembership . Leave ) {
515+ tags . push ( DefaultTagID . Archived ) ;
516+ } else {
517+ tags . push ( ...this . getTagsOfJoinedRoom ( room ) ) ;
518+ }
519+
520+ if ( ! tags . length ) tags . push ( DefaultTagID . Untagged ) ;
521+
522+ return tags ;
523+ }
524+
525+ private getTagsOfJoinedRoom ( room : Room ) : TagID [ ] {
526+ let tags = Object . keys ( room . tags || { } ) ;
527+
528+ if ( tags . length === 0 ) {
529+ // Check to see if it's a DM if it isn't anything else
530+ if ( DMRoomMap . shared ( ) . getUserIdForRoomId ( room . roomId ) ) {
531+ tags = [ DefaultTagID . DM ] ;
532+ }
533+ }
534+
535+ return tags ;
536+ }
537+
499538 /**
500539 * Updates the roomsToTags map
501540 */
@@ -566,6 +605,19 @@ export class Algorithm extends EventEmitter {
566605 }
567606 }
568607
608+ if ( cause === RoomUpdateCause . NewRoom && ! this . roomIdsToTags [ room . roomId ] ) {
609+ console . log ( `[RoomListDebug] Updating tags for new room ${ room . roomId } (${ room . name } )` ) ;
610+
611+ // Get the tags for the room and populate the cache
612+ const roomTags = this . getTagsForRoom ( room ) . filter ( t => ! isNullOrUndefined ( this . cachedRooms [ t ] ) ) ;
613+
614+ // "This should never happen" condition - we specify DefaultTagID.Untagged in getTagsForRoom(),
615+ // which means we should *always* have a tag to go off of.
616+ if ( ! roomTags . length ) throw new Error ( `Tags cannot be determined for ${ room . roomId } ` ) ;
617+
618+ this . roomIdsToTags [ room . roomId ] = roomTags ;
619+ }
620+
569621 let tags = this . roomIdsToTags [ room . roomId ] ;
570622 if ( ! tags ) {
571623 console . warn ( `No tags known for "${ room . name } " (${ room . roomId } )` ) ;
0 commit comments