invites
This commit is contained in:
parent
69290f4c7a
commit
15217dc77a
3 changed files with 131 additions and 107 deletions
|
|
@ -1,4 +1,8 @@
|
|||
const invitesService = require('../services/invitesService');
|
||||
const nostrService = require('../services/nostrService');
|
||||
const InvitesServiceProvider = require('../services/invitesService');
|
||||
const invitesService = new InvitesServiceProvider({
|
||||
nostrService,
|
||||
}).provide();
|
||||
|
||||
module.exports = async function createAppInvite(inviterNpub) {
|
||||
const appInvite = await invitesService.createAppInvite(inviterNpub);
|
||||
|
|
|
|||
|
|
@ -2,10 +2,14 @@ class ServicesProvider {
|
|||
constructor() {}
|
||||
|
||||
provide() {
|
||||
const invitesService = require('../services/invitesService');
|
||||
const nostrService = require('../services/nostrService');
|
||||
const loginService = require('../services/loginService');
|
||||
|
||||
const InvitesServiceProvider = require('../services/invitesService');
|
||||
const invitesService = new InvitesServiceProvider({
|
||||
nostrService,
|
||||
}).provide();
|
||||
|
||||
const SessionServiceProvider = require('../services/sessionService');
|
||||
const sessionService = new SessionServiceProvider({
|
||||
invitesService,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,13 @@ const nostrService = require('./nostrService');
|
|||
const models = require('../models');
|
||||
const errors = require('../errors');
|
||||
|
||||
async function appInviteExists(inviteUuid) {
|
||||
class InvitesServiceProvider {
|
||||
constructor({ nostrService }) {
|
||||
this.nostrService = nostrService;
|
||||
}
|
||||
|
||||
provide() {
|
||||
async function appInviteExists(inviteUuid) {
|
||||
const invite = await models.AppInviteCreated.findOne({
|
||||
where: { uuid: inviteUuid },
|
||||
});
|
||||
|
|
@ -12,16 +18,16 @@ async function appInviteExists(inviteUuid) {
|
|||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function getAppInvite(inviteUuid) {
|
||||
async function getAppInvite(inviteUuid) {
|
||||
const invite = await models.AppInviteCreated.findOne({
|
||||
where: { uuid: inviteUuid },
|
||||
});
|
||||
return invite;
|
||||
}
|
||||
}
|
||||
|
||||
async function isAppInviteSpent(appInviteUuid) {
|
||||
async function isAppInviteSpent(appInviteUuid) {
|
||||
const signUpChallengeCompleted =
|
||||
await models.SignUpChallengeCompleted.findOne({
|
||||
where: {
|
||||
|
|
@ -33,17 +39,17 @@ async function isAppInviteSpent(appInviteUuid) {
|
|||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function createAppInvite(inviterPubKey) {
|
||||
async function createAppInvite(inviterPubKey) {
|
||||
return await models.AppInviteCreated.create({
|
||||
uuid: uuid.v7(),
|
||||
inviter_pub_key: inviterPubKey,
|
||||
created_at: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function createSignUpChallenge(appInviteUuid) {
|
||||
async function createSignUpChallenge(appInviteUuid) {
|
||||
if (!(await appInviteExists(appInviteUuid))) {
|
||||
throw new errors.NotFoundError("Invite doesn't exist.");
|
||||
}
|
||||
|
|
@ -60,13 +66,18 @@ async function createSignUpChallenge(appInviteUuid) {
|
|||
app_invite_uuid: appInviteUuid,
|
||||
created_at: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function verifySignUpChallenge(signedEvent) {
|
||||
const challengeTag = signedEvent.tags.find((tag) => tag[0] === 'challenge');
|
||||
async function verifySignUpChallenge(signedEvent) {
|
||||
const challengeTag = signedEvent.tags.find(
|
||||
(tag) => tag[0] === 'challenge'
|
||||
);
|
||||
const challenge = challengeTag[1];
|
||||
|
||||
const nostrChallenge = await nostrService.getNostrChallenge(null, challenge);
|
||||
const nostrChallenge = await nostrService.getNostrChallenge(
|
||||
null,
|
||||
challenge
|
||||
);
|
||||
|
||||
const signUpChallenge = await models.SignUpChallengeCreated.findOne({
|
||||
where: {
|
||||
|
|
@ -75,26 +86,27 @@ async function verifySignUpChallenge(signedEvent) {
|
|||
});
|
||||
|
||||
if (await nostrService.hasNostrChallengeBeenCompleted(challenge)) {
|
||||
throw new errors.AlreadyUsedError('This challenge has already been used.');
|
||||
throw new errors.AlreadyUsedError(
|
||||
'This challenge has already been used.'
|
||||
);
|
||||
}
|
||||
|
||||
const completedNostrChallenge =
|
||||
await nostrService.verifyNostrChallenge(signedEvent);
|
||||
|
||||
const completedSignUpChallenge = await models.SignUpChallengeCompleted.create(
|
||||
{
|
||||
const completedSignUpChallenge =
|
||||
await models.SignUpChallengeCompleted.create({
|
||||
uuid: uuid.v7(),
|
||||
nostr_challenge_completed_uuid: completedNostrChallenge.uuid,
|
||||
app_invite_uuid: signUpChallenge.app_invite_uuid,
|
||||
public_key: completedNostrChallenge.public_key,
|
||||
created_at: new Date().toISOString(),
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
return completedSignUpChallenge;
|
||||
}
|
||||
}
|
||||
|
||||
async function isPublicKeySignedUp(publicKey) {
|
||||
async function isPublicKeySignedUp(publicKey) {
|
||||
const signUpChallengeCompleted =
|
||||
await models.SignUpChallengeCompleted.findOne({
|
||||
where: {
|
||||
|
|
@ -107,9 +119,9 @@ async function isPublicKeySignedUp(publicKey) {
|
|||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
return {
|
||||
appInviteExists,
|
||||
getAppInvite,
|
||||
isAppInviteSpent,
|
||||
|
|
@ -117,4 +129,8 @@ module.exports = {
|
|||
createSignUpChallenge,
|
||||
verifySignUpChallenge,
|
||||
isPublicKeySignedUp,
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = InvitesServiceProvider;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue