Skip to content

Commit

Permalink
chore: invite API's membership check can be synchronous (#480)
Browse files Browse the repository at this point in the history
This change should have no user impact.

This function doesn't do anything async so we can make it synchronous.
I think this is a useful change on its own but will make upcoming
changes a bit simpler.
  • Loading branch information
EvanHahn authored Feb 25, 2024
1 parent c402e48 commit 964cb80
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
15 changes: 5 additions & 10 deletions src/invite-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class InviteApi extends TypedEmitter {
* @param {Object} options
* @param {import('./local-peers.js').LocalPeers} options.rpc
* @param {object} options.queries
* @param {(projectId: string) => Promise<boolean>} options.queries.isMember
* @param {(projectId: string) => boolean} options.queries.isMember
* @param {(invite: import('./generated/rpc.js').Invite) => Promise<void>} options.queries.addProject
*/
constructor({ rpc, queries }) {
Expand All @@ -58,21 +58,16 @@ export class InviteApi extends TypedEmitter {
this.#isMember = queries.isMember
this.#addProject = queries.addProject

this.rpc.on('invite', (peerId, invite) => {
this.#handleInvite(peerId, invite).catch(() => {
/* c8 ignore next */
// TODO: Log errors, but otherwise ignore them, but can't think of a reason there would be an error here
})
})
this.rpc.on('invite', this.#handleInvite)
}

/**
* @param {string} peerId
* @param {import('./generated/rpc.js').Invite} invite
*/
async #handleInvite(peerId, invite) {
#handleInvite = (peerId, invite) => {
const projectId = projectKeyToId(invite.projectKey)
const isAlreadyMember = await this.#isMember(projectId)
const isAlreadyMember = this.#isMember(projectId)

if (isAlreadyMember) {
this.#sendAlreadyResponse({ peerId, projectId })
Expand Down Expand Up @@ -107,7 +102,7 @@ export class InviteApi extends TypedEmitter {
async accept(projectPublicId) {
const projectId = this.#getProjectIdFromPublicId(projectPublicId)

const isAlreadyMember = await this.#isMember(projectId)
const isAlreadyMember = this.#isMember(projectId)
const peersToRespondTo = this.#peersToRespondTo.get(projectId)

if (isAlreadyMember) {
Expand Down
2 changes: 1 addition & 1 deletion src/mapeo-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export class MapeoManager extends TypedEmitter {
this.#invite = new InviteApi({
rpc: this.#localPeers,
queries: {
isMember: async (projectId) => {
isMember: (projectId) => {
const projectExists = this.#db
.select()
.from(projectKeysTable)
Expand Down
26 changes: 13 additions & 13 deletions tests/invite-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test('invite-received event has expected payload', async (t) => {
const inviteApi = new InviteApi({
rpc: r2,
queries: {
isMember: async (projectId) => {
isMember: (projectId) => {
return projects.has(projectId)
},
addProject: async (invite) => {
Expand Down Expand Up @@ -77,7 +77,7 @@ test('Accept invite', async (t) => {
const inviteApi = new InviteApi({
rpc: r2,
queries: {
isMember: async (projectId) => {
isMember: (projectId) => {
const projectKey = Buffer.from(projectId, 'hex')
return projects.has(projectKeyToPublicId(projectKey))
},
Expand Down Expand Up @@ -125,7 +125,7 @@ test('Reject invite', async (t) => {
const inviteApi = new InviteApi({
rpc: r2,
queries: {
isMember: async (projectId) => {
isMember: (projectId) => {
const projectKey = Buffer.from(projectId, 'hex')
return projects.has(projectKeyToPublicId(projectKey))
},
Expand Down Expand Up @@ -172,7 +172,7 @@ test('Receiving invite for project that peer already belongs to', async (t) => {
const inviteApi = new InviteApi({
rpc: r2,
queries: {
isMember: async () => {
isMember: () => {
return true
},
addProject: async () => {
Expand Down Expand Up @@ -220,7 +220,7 @@ test('Receiving invite for project that peer already belongs to', async (t) => {
const inviteApi = new InviteApi({
rpc: r2,
queries: {
isMember: async () => {
isMember: () => {
return isMember
},
addProject: async () => {
Expand Down Expand Up @@ -270,7 +270,7 @@ test('Receiving invite for project that peer already belongs to', async (t) => {
const inviteApi = new InviteApi({
rpc: r2,
queries: {
isMember: async (projectId) => {
isMember: (projectId) => {
return projects.has(projectId)
},
addProject: async (invite) => {
Expand Down Expand Up @@ -314,7 +314,7 @@ test('trying to accept or reject non-existent invite throws', async (t) => {
const inviteApi = new InviteApi({
rpc,
queries: {
isMember: async () => true,
isMember: () => true,
addProject: async () => {},
},
})
Expand All @@ -336,7 +336,7 @@ test('invitor disconnecting results in accept throwing', async (t) => {
const inviteApi = new InviteApi({
rpc: r2,
queries: {
isMember: async () => false,
isMember: () => false,
addProject: async () => {
t.fail('should not try to add project if could not accept')
},
Expand Down Expand Up @@ -378,7 +378,7 @@ test('invitor disconnecting results in invite reject response not throwing', asy
const inviteApi = new InviteApi({
rpc: r2,
queries: {
isMember: async () => false,
isMember: () => false,
addProject: async () => {},
},
})
Expand Down Expand Up @@ -420,7 +420,7 @@ test('invitor disconnecting results in invite already response not throwing', as
const inviteApi = new InviteApi({
rpc: r2,
queries: {
isMember: async () => {
isMember: () => {
return isMember
},
addProject: async () => {},
Expand Down Expand Up @@ -463,7 +463,7 @@ test('addProject throwing results in invite accept throwing', async (t) => {
const inviteApi = new InviteApi({
rpc: r2,
queries: {
isMember: async () => false,
isMember: () => false,
addProject: async () => {
throw new Error('Failed to add project')
},
Expand Down Expand Up @@ -503,7 +503,7 @@ test('Invite from multiple peers', async (t) => {
const inviteApi = new InviteApi({
rpc: invitee,
queries: {
isMember: async (projectId) => {
isMember: (projectId) => {
const projectKey = Buffer.from(projectId, 'hex')
return projects.has(projectKeyToPublicId(projectKey))
},
Expand Down Expand Up @@ -570,7 +570,7 @@ test.skip('Invite from multiple peers, first disconnects before accepted, receiv
const inviteApi = new InviteApi({
rpc: invitee,
queries: {
isMember: async (projectId) => {
isMember: (projectId) => {
return projects.has(projectId)
},
addProject: async (invite) => {
Expand Down

0 comments on commit 964cb80

Please sign in to comment.