Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/responders/link-responders.js
// @flow | // @flow | ||||
import t from 'tcomb'; | import t, { type TUnion, type TInterface } from 'tcomb'; | ||||
import type { | import type { | ||||
InviteLinkVerificationRequest, | InviteLinkVerificationRequest, | ||||
InviteLinkVerificationResponse, | InviteLinkVerificationResponse, | ||||
FetchInviteLinksResponse, | FetchInviteLinksResponse, | ||||
} from 'lib/types/link-types.js'; | } from 'lib/types/link-types.js'; | ||||
import { tShape } from 'lib/utils/validation-utils.js'; | import { tShape, tID } from 'lib/utils/validation-utils.js'; | ||||
import { | import { | ||||
fetchPrimaryInviteLinks, | fetchPrimaryInviteLinks, | ||||
verifyInviteLink, | verifyInviteLink, | ||||
} from '../fetchers/link-fetchers.js'; | } from '../fetchers/link-fetchers.js'; | ||||
import { Viewer } from '../session/viewer.js'; | import { Viewer } from '../session/viewer.js'; | ||||
import { validateInput } from '../utils/validation-utils.js'; | import { validateInput, validateOutput } from '../utils/validation-utils.js'; | ||||
const inviteLinkVerificationRequestInputValidator = tShape({ | const inviteLinkVerificationRequestInputValidator: TInterface<InviteLinkVerificationRequest> = | ||||
tShape({ | |||||
secret: t.String, | secret: t.String, | ||||
}); | }); | ||||
export const inviteLinkVerificationResponseValidator: TUnion<InviteLinkVerificationResponse> = | |||||
t.union([ | |||||
tShape({ | |||||
status: t.enums.of(['valid', 'already_joined']), | |||||
community: tShape({ | |||||
name: t.String, | |||||
id: tID, | |||||
}), | |||||
}), | |||||
tShape({ | |||||
status: t.enums.of(['invalid', 'expired']), | |||||
}), | |||||
]); | |||||
async function inviteLinkVerificationResponder( | async function inviteLinkVerificationResponder( | ||||
viewer: Viewer, | viewer: Viewer, | ||||
input: any, | input: any, | ||||
): Promise<InviteLinkVerificationResponse> { | ): Promise<InviteLinkVerificationResponse> { | ||||
const request: InviteLinkVerificationRequest = input; | const request = await validateInput( | ||||
await validateInput( | |||||
viewer, | viewer, | ||||
inviteLinkVerificationRequestInputValidator, | inviteLinkVerificationRequestInputValidator, | ||||
request, | input, | ||||
); | |||||
const response = await verifyInviteLink(viewer, request); | |||||
return validateOutput( | |||||
viewer.platformDetails, | |||||
inviteLinkVerificationResponseValidator, | |||||
response, | |||||
); | ); | ||||
return await verifyInviteLink(viewer, request); | |||||
} | } | ||||
async function fetchPrimaryInviteLinksResponder( | async function fetchPrimaryInviteLinksResponder( | ||||
viewer: Viewer, | viewer: Viewer, | ||||
): Promise<FetchInviteLinksResponse> { | ): Promise<FetchInviteLinksResponse> { | ||||
const primaryLinks = await fetchPrimaryInviteLinks(viewer); | const primaryLinks = await fetchPrimaryInviteLinks(viewer); | ||||
return { | return { | ||||
links: primaryLinks, | links: primaryLinks, | ||||
}; | }; | ||||
} | } | ||||
export { inviteLinkVerificationResponder, fetchPrimaryInviteLinksResponder }; | export { inviteLinkVerificationResponder, fetchPrimaryInviteLinksResponder }; |