Skip to content

Commit

Permalink
Merge pull request #199 from airgap-it/fix/unknown-room
Browse files Browse the repository at this point in the history
fix(p2p): handle unknown rooms
  • Loading branch information
AndreasGassmann authored Apr 26, 2021
2 parents f731121 + a82a5d6 commit c30848b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@airgap/beacon-sdk",
"version": "2.2.4",
"version": "2.2.5",
"description": "The beacon-sdk allows you to easily connect DApps with Wallets through P2P communication or a chrome extension.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const SDK_VERSION: string = '2.2.4'
export const SDK_VERSION: string = '2.2.5'
export const BEACON_VERSION: string = '2'
19 changes: 15 additions & 4 deletions src/transports/clients/P2PCommunicationClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export class P2PCommunicationClient extends CommunicationClient {

private readonly activeListeners: Map<string, (event: MatrixClientEvent<any>) => void> = new Map()

private readonly ignoredRooms: string[] = []

constructor(
private readonly name: string,
keyPair: sodium.KeyPair,
Expand Down Expand Up @@ -358,6 +360,11 @@ export class P2PCommunicationClient extends CommunicationClient {
}
)
await this.storage.set(StorageKey.MATRIX_PEER_ROOM_IDS, newRoomIds)

// TODO: We also need to delete the room from the sync state
// If we need to delete a room, we can assume the local state is not up to date anymore, so we can reset the state

this.ignoredRooms.push(roomId)
}

public async listenForChannelOpening(
Expand Down Expand Up @@ -524,12 +531,16 @@ export class P2PCommunicationClient extends CommunicationClient {

const joinedRooms = await this.client.joinedRooms
logger.log('checking joined rooms', joinedRooms, recipient)
const relevantRooms = joinedRooms.filter((roomElement: MatrixRoom) =>
roomElement.members.some((member: string) => member === recipient)
)
const relevantRooms = joinedRooms
.filter((roomElement: MatrixRoom) => !this.ignoredRooms.some((id) => roomElement.id === id))
.filter((roomElement: MatrixRoom) =>
roomElement.members.some((member: string) => member === recipient)
)

let room: MatrixRoom
if (relevantRooms.length === 0) {
// We always create a new room if one has been ignored. This is because if we ignore one, we know the server state changed.
// So we cannot trust the current sync state. This can be removed once we have a method to properly clear and refresh the sync state.
if (relevantRooms.length === 0 || this.ignoredRooms.length > 0) {
logger.log(`getRelevantJoinedRoom`, `no relevant rooms found, creating new one`)

const roomId = await this.client.createTrustedPrivateRoom(recipient)
Expand Down

0 comments on commit c30848b

Please sign in to comment.