Skip to content

Commit

Permalink
Combine roomId and cardDoc for cardHashes key (#1143)
Browse files Browse the repository at this point in the history
  • Loading branch information
FadhlanR authored Apr 5, 2024
1 parent 3b6fb8a commit 71d03aa
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
21 changes: 15 additions & 6 deletions packages/base/room.gts
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,19 @@ class PatchField extends FieldDef {
@field payload = contains(PatchObjectField);
}

// A map from card document hash to the first card fragment event id.
// A map from a hash of roomId + card document to the first card fragment event id.
// This map can be used to avoid sending the same version of the card more than once in a conversation.
// We can reuse exisiting eventId if user attached the same version of the card.
const cardHashes: Map<string, string> = new Map();
export function getEventIdForCard(cardDoc: LooseSingleCardDocument) {
return cardHashes.get(md5(JSON.stringify(cardDoc)));
function generateCardHashKey(roomId: string, cardDoc: LooseSingleCardDocument) {
return md5(roomId + JSON.stringify(cardDoc));
}

export function getEventIdForCard(
roomId: string,
cardDoc: LooseSingleCardDocument,
) {
return cardHashes.get(generateCardHashKey(roomId, cardDoc));
}

export class MessageField extends FieldDef {
Expand Down Expand Up @@ -623,9 +630,11 @@ export class RoomField extends FieldDef {
`Expected to find ${fragments[0].data.totalParts} fragments for fragment of event id ${eventId} but found ${fragments.length} fragments`,
);
}
let cardSource = fragments.map((f) => f.data.cardFragment).join('');
let cardDoc = JSON.parse(cardSource) as LooseSingleCardDocument;
cardHashes.set(md5(cardSource), eventId);

let cardDoc = JSON.parse(
fragments.map((f) => f.data.cardFragment).join(''),
) as LooseSingleCardDocument;
cardHashes.set(generateCardHashKey(this.roomId, cardDoc), eventId);
return cardDoc;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/host/app/services/matrix-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ export default class MatrixService extends Service {
let attachedCardsEventIds: string[] = [];
if (serializedAttachedCards.length > 0) {
for (let attachedCard of serializedAttachedCards) {
let eventId = roomModule.getEventIdForCard(attachedCard);
let eventId = roomModule.getEventIdForCard(roomId, attachedCard);
if (!eventId) {
let responses = await this.sendCardFragments(roomId, attachedCard);
eventId = responses[0].event_id; // we only care about the first fragment
Expand Down
3 changes: 1 addition & 2 deletions packages/matrix/tests/room-creation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,7 @@ test.describe('Room creation', () => {
expect(roomsAfterDeletionKeys.length).toEqual(
roomsBeforeDeletionKeys.length,
);
expect(roomsAfterDeletionKeys[0]).toEqual(roomsBeforeDeletionKeys[0]); // Existing room
expect(roomsAfterDeletionKeys[1]).not.toEqual(roomsBeforeDeletion[2]); // The new room after deletions
expect(roomsAfterDeletionKeys.includes(roomsBeforeDeletionKeys[1])).toBeFalsy(); // Deleted room
});

test('it can cancel deleting a room', async ({ page }) => {
Expand Down

0 comments on commit 71d03aa

Please sign in to comment.