@@ -47,12 +47,6 @@ import type CardService from '../services/card-service';
47
47
import type CommandService from '../services/command-service' ;
48
48
import type MatrixService from '../services/matrix-service' ;
49
49
50
- interface SkillId {
51
- skillCardId : string ;
52
- skillEventId : string ;
53
- isActive : boolean ;
54
- }
55
-
56
50
interface Args {
57
51
named : {
58
52
roomId : string | undefined ;
@@ -62,6 +56,8 @@ interface Args {
62
56
63
57
export class RoomResource extends Resource < Args > {
64
58
private _messageCache : TrackedMap < string , Message > = new TrackedMap ( ) ;
59
+ private _skillEventIdToCardIdCache : TrackedMap < string , string > =
60
+ new TrackedMap ( ) ;
65
61
private _skillCardsCache : TrackedMap < string , SkillCard > = new TrackedMap ( ) ;
66
62
private _nameEventsCache : TrackedMap < string , RoomNameEvent > =
67
63
new TrackedMap ( ) ;
@@ -113,7 +109,11 @@ export class RoomResource extends Resource<Args> {
113
109
await this . loadRoomMemberEvent ( roomId , event ) ;
114
110
break ;
115
111
case 'm.room.message' :
116
- await this . loadRoomMessage ( { roomId, event, index } ) ;
112
+ if ( this . isCardFragmentEvent ( event ) ) {
113
+ await this . loadCardFragment ( event ) ;
114
+ } else {
115
+ await this . loadRoomMessage ( { roomId, event, index } ) ;
116
+ }
117
117
break ;
118
118
case APP_BOXEL_COMMAND_RESULT_EVENT_TYPE :
119
119
this . updateMessageCommandResult ( { roomId, event, index } ) ;
@@ -188,60 +188,42 @@ export class RoomResource extends Resource<Args> {
188
188
}
189
189
190
190
@cached
191
- get skillIds ( ) : SkillId [ ] {
191
+ get allSkillEventIds ( ) : Set < string > {
192
192
let skillsConfig = this . matrixRoom ?. skillsConfig ;
193
193
if ( ! skillsConfig ) {
194
- return [ ] ;
194
+ return new Set ( ) ;
195
195
}
196
- let result : SkillId [ ] = [ ] ;
197
- for ( let eventId of [
196
+ return new Set ( [
198
197
...skillsConfig . enabledEventIds ,
199
198
...skillsConfig . disabledEventIds ,
200
- ] ) {
201
- let cardDoc ;
202
- try {
203
- cardDoc = this . serializedCardFromFragments ( eventId ) ;
204
- } catch {
205
- // the skill card fragments might not be loaded yet
199
+ ] ) ;
200
+ }
201
+
202
+ @cached
203
+ get skills ( ) : Skill [ ] {
204
+ let skillsConfig = this . matrixRoom ?. skillsConfig ;
205
+ if ( ! skillsConfig ) {
206
+ return [ ] ;
207
+ }
208
+ let result : Skill [ ] = [ ] ;
209
+ for ( let skillEventId of this . allSkillEventIds ) {
210
+ let cardId = this . _skillEventIdToCardIdCache . get ( skillEventId ) ;
211
+ if ( ! cardId ) {
206
212
continue ;
207
213
}
208
- if ( ! cardDoc . data . id ) {
214
+ let skillCard = this . _skillCardsCache . get ( cardId ) ;
215
+ if ( ! skillCard ) {
209
216
continue ;
210
217
}
211
- let cardId = cardDoc . data . id ;
212
- if ( ! this . _skillCardsCache . has ( cardId ) ) {
213
- this . cardService
214
- . createFromSerialized ( cardDoc . data , cardDoc )
215
- . then ( ( skillsCard ) => {
216
- this . _skillCardsCache . set ( cardId , skillsCard as SkillCard ) ;
217
- } ) ;
218
- }
219
218
result . push ( {
220
- skillCardId : cardDoc . data . id ,
221
- skillEventId : eventId ,
222
- isActive : skillsConfig . enabledEventIds . includes ( eventId ) ,
219
+ card : skillCard ,
220
+ skillEventId,
221
+ isActive : skillsConfig . enabledEventIds . includes ( skillEventId ) ,
223
222
} ) ;
224
223
}
225
224
return result ;
226
225
}
227
226
228
- @cached
229
- get skills ( ) : Skill [ ] {
230
- return this . skillIds
231
- . map ( ( { skillCardId, skillEventId, isActive } ) => {
232
- let card = this . _skillCardsCache . get ( skillCardId ) ;
233
- if ( card ) {
234
- return {
235
- card,
236
- skillEventId,
237
- isActive,
238
- } ;
239
- }
240
- return null ;
241
- } )
242
- . filter ( Boolean ) as Skill [ ] ;
243
- }
244
-
245
227
@cached
246
228
get created ( ) {
247
229
if ( this . _createEvent ) {
@@ -320,24 +302,48 @@ export class RoomResource extends Resource<Args> {
320
302
} ) ;
321
303
}
322
304
323
- private loadRoomMessage ( {
324
- roomId,
325
- event,
326
- index,
327
- } : {
328
- roomId : string ;
329
- event : MessageEvent | CommandEvent | CardMessageEvent ;
330
- index : number ;
331
- } ) {
332
- if ( event . content . msgtype === APP_BOXEL_CARDFRAGMENT_MSGTYPE ) {
333
- this . _fragmentCache . set ( event . event_id , event . content ) ;
305
+ private isCardFragmentEvent (
306
+ event : MessageEvent | CommandEvent | CardMessageEvent ,
307
+ ) : event is CardMessageEvent & {
308
+ content : { msgtype : typeof APP_BOXEL_CARDFRAGMENT_MSGTYPE } ;
309
+ } {
310
+ return event . content . msgtype === APP_BOXEL_CARDFRAGMENT_MSGTYPE ;
311
+ }
312
+
313
+ private async loadSkillCardIntoCache ( eventId : string ) {
314
+ let cardDoc = this . serializedCardFromFragments ( eventId ) ;
315
+ if ( ! cardDoc . data . id ) {
316
+ console . warn (
317
+ `No card id found for skill event id ${ eventId } , this should not happen, this can happen if you add a skill card to a room without saving it, and without giving it an ID` ,
318
+ ) ;
334
319
return ;
335
320
}
321
+ let cardId = cardDoc . data . id ;
322
+ let skillCard = ( await this . cardService . createFromSerialized (
323
+ cardDoc . data ,
324
+ cardDoc ,
325
+ ) ) as SkillCard ;
326
+ this . _skillCardsCache . set ( cardId , skillCard ) ;
327
+ this . _skillEventIdToCardIdCache . set ( eventId , cardId ) ;
328
+ }
336
329
337
- this . upsertMessage ( { roomId, event, index } ) ;
330
+ private async loadCardFragment (
331
+ event : CardMessageEvent & {
332
+ content : { msgtype : typeof APP_BOXEL_CARDFRAGMENT_MSGTYPE } ;
333
+ } ,
334
+ ) {
335
+ let eventId = event . event_id ;
336
+ this . _fragmentCache . set ( eventId , event . content ) ;
337
+ if (
338
+ ! this . allSkillEventIds . has ( eventId ) ||
339
+ this . _skillEventIdToCardIdCache . has ( eventId )
340
+ ) {
341
+ return ;
342
+ }
343
+ await this . loadSkillCardIntoCache ( eventId ) ;
338
344
}
339
345
340
- private upsertMessage ( {
346
+ private loadRoomMessage ( {
341
347
roomId,
342
348
event,
343
349
index,
0 commit comments