Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add logging for invitees #560

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/invite-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { InviteResponse_Decision } from './generated/rpc.js'
import { assert, keyToId, noop } from './utils.js'
import HashMap from './lib/hashmap.js'
import timingSafeEqual from './lib/timing-safe-equal.js'
import { Logger } from './logger.js'

// There are three slightly different invite types:
//
Expand Down Expand Up @@ -161,16 +162,21 @@ export class InviteApi extends TypedEmitter {
#isMember
#addProject
#pendingInvites = new PendingInvites()
#l

/**
* @param {Object} options
* @param {import('./local-peers.js').LocalPeers} options.rpc
* @param {object} options.queries
* @param {(projectId: string) => boolean} options.queries.isMember
* @param {(projectDetails: Pick<ProjectJoinDetails, 'projectKey' | 'encryptionKeys'> & { projectName: string }) => Promise<unknown>} options.queries.addProject
* @param {Logger} [options.logger]
*/
constructor({ rpc, queries }) {
constructor({ rpc, queries, logger }) {
super()

this.#l = Logger.create('InviteApi', logger)

this.rpc = rpc
this.#isMember = queries.isMember
this.#addProject = queries.addProject
Expand Down Expand Up @@ -199,8 +205,11 @@ export class InviteApi extends TypedEmitter {
#handleInviteRpcMessage(peerId, inviteRpcMessage) {
const invite = { ...inviteRpcMessage, receivedAt: Date.now() }

this.#l.log('Received invite %h from %S', invite.inviteId, peerId)

const isAlreadyMember = this.#isMember(invite.projectPublicId)
if (isAlreadyMember) {
this.#l.log('Invite %h: already in project', invite.inviteId)
this.rpc
.sendInviteResponse(peerId, {
decision: InviteResponse_Decision.ALREADY,
Expand All @@ -214,6 +223,7 @@ export class InviteApi extends TypedEmitter {
invite.inviteId
)
if (hasAlreadyReceivedThisInvite) {
this.#l.log('Invite %h: already received this invite', invite.inviteId)
return
}

Expand All @@ -227,13 +237,23 @@ export class InviteApi extends TypedEmitter {
#handleInviteCancel(inviteCancel) {
const { inviteId } = inviteCancel

this.#l.log('Received invite cancel for invite ID %h', inviteId)

const pendingInvite = this.#pendingInvites.getByInviteId(inviteId)
if (!pendingInvite) {
this.#l.log(
'Received invite cancel for %h but no such invite exists',
inviteId
)
return
}
const { invite, isAccepting } = pendingInvite

if (isAccepting) {
this.#l.log(
"Received invite cancel for %h but we're already accepting",
inviteId
)
return
}

Expand Down Expand Up @@ -286,6 +306,10 @@ export class InviteApi extends TypedEmitter {
// to join a project while an invite is pending, so we need to check this.
const isAlreadyMember = this.#isMember(projectPublicId)
if (isAlreadyMember) {
this.#l.log(
"Went to accept invite %h but we're already in the project",
inviteId
)
const pendingInvitesDeleted =
this.#pendingInvites.deleteByProjectPublicId(projectPublicId)
for (const pendingInvite of pendingInvitesDeleted) {
Expand Down Expand Up @@ -328,6 +352,8 @@ export class InviteApi extends TypedEmitter {
.then((args) => args?.[1])
.catch(noop)

this.#l.log('Sending accept response for invite %h', inviteId)

try {
await this.rpc.sendInviteResponse(peerId, {
decision: InviteResponse_Decision.ACCEPT,
Expand Down Expand Up @@ -359,6 +385,12 @@ export class InviteApi extends TypedEmitter {
inviteId.equals(pendingInvite.invite.inviteId)
if (isPendingInviteWeJustAccepted) continue

this.#l.log(
'Sending "already" response for invite %h to %S',
inviteId,
pendingInvite.peerId
)

this.rpc
.sendInviteResponse(pendingInvite.peerId, {
decision: InviteResponse_Decision.ALREADY,
Expand Down Expand Up @@ -389,6 +421,8 @@ export class InviteApi extends TypedEmitter {

assert(!isAccepting, `Cannot reject ${inviteIdString}`)

this.#l.log('Rejecting invite %h', inviteId)

this.rpc
.sendInviteResponse(peerId, {
decision: InviteResponse_Decision.REJECT,
Expand Down
1 change: 1 addition & 0 deletions src/mapeo-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export class MapeoManager extends TypedEmitter {
await this.addProject(projectDetails)
},
},
logger,
})

if (typeof coreStorage === 'string') {
Expand Down
Loading