|
| 1 | +import { validateSchema, customDomainSchema } from '@/lib/validate' |
| 2 | +import { GqlAuthenticationError, GqlInputError } from '@/lib/error' |
| 3 | +import { randomBytes } from 'node:crypto' |
| 4 | +import { getDomainMapping } from '@/lib/domains' |
| 5 | + |
| 6 | +export default { |
| 7 | + Query: { |
| 8 | + customDomain: async (parent, { subName }, { models }) => { |
| 9 | + return models.customDomain.findUnique({ where: { subName } }) |
| 10 | + }, |
| 11 | + domainMapping: async (parent, { domain }, { models }) => { |
| 12 | + const mapping = await getDomainMapping(domain) |
| 13 | + return mapping |
| 14 | + } |
| 15 | + }, |
| 16 | + Mutation: { |
| 17 | + setCustomDomain: async (parent, { subName, domain }, { me, models }) => { |
| 18 | + if (!me) { |
| 19 | + throw new GqlAuthenticationError() |
| 20 | + } |
| 21 | + |
| 22 | + const sub = await models.sub.findUnique({ where: { name: subName } }) |
| 23 | + if (!sub) { |
| 24 | + throw new GqlInputError('sub not found') |
| 25 | + } |
| 26 | + |
| 27 | + if (sub.userId !== me.id) { |
| 28 | + throw new GqlInputError('you do not own this sub') |
| 29 | + } |
| 30 | + |
| 31 | + domain = domain.trim() // protect against trailing spaces |
| 32 | + if (domain && !validateSchema(customDomainSchema, { domain })) { |
| 33 | + throw new GqlInputError('invalid domain format') |
| 34 | + } |
| 35 | + |
| 36 | + const existing = await models.customDomain.findUnique({ where: { subName } }) |
| 37 | + |
| 38 | + if (domain) { |
| 39 | + if (existing && existing.domain === domain && existing.status !== 'HOLD') { |
| 40 | + throw new GqlInputError('domain already set') |
| 41 | + } |
| 42 | + |
| 43 | + const initializeDomain = { |
| 44 | + domain, |
| 45 | + createdAt: new Date(), |
| 46 | + status: 'PENDING', |
| 47 | + verification: { |
| 48 | + dns: { |
| 49 | + state: 'PENDING', |
| 50 | + cname: 'stacker.news', |
| 51 | + // generate a random txt record only if it's a new domain |
| 52 | + txt: existing?.domain === domain && existing.verification.dns.txt |
| 53 | + ? existing.verification.dns.txt |
| 54 | + : randomBytes(32).toString('base64') |
| 55 | + }, |
| 56 | + ssl: { |
| 57 | + state: 'WAITING', |
| 58 | + arn: null, |
| 59 | + cname: null, |
| 60 | + value: null |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + const updatedDomain = await models.customDomain.upsert({ |
| 66 | + where: { subName }, |
| 67 | + update: { |
| 68 | + ...initializeDomain |
| 69 | + }, |
| 70 | + create: { |
| 71 | + ...initializeDomain, |
| 72 | + sub: { |
| 73 | + connect: { name: subName } |
| 74 | + } |
| 75 | + } |
| 76 | + }) |
| 77 | + |
| 78 | + // schedule domain verification in 30 seconds |
| 79 | + await models.$executeRaw` |
| 80 | + INSERT INTO pgboss.job (name, data, retrylimit, retrydelay, startafter, keepuntil) |
| 81 | + VALUES ('domainVerification', |
| 82 | + jsonb_build_object('domainId', ${updatedDomain.id}::INTEGER), |
| 83 | + 3, |
| 84 | + 30, |
| 85 | + now() + interval '30 seconds', |
| 86 | + now() + interval '2 days')` |
| 87 | + |
| 88 | + return updatedDomain |
| 89 | + } else { |
| 90 | + try { |
| 91 | + // delete any existing domain verification jobs |
| 92 | + await models.$queryRaw` |
| 93 | + DELETE FROM pgboss.job |
| 94 | + WHERE name = 'domainVerification' |
| 95 | + AND data->>'domainId' = ${existing.id}::TEXT` |
| 96 | + |
| 97 | + return await models.customDomain.delete({ where: { subName } }) |
| 98 | + } catch (error) { |
| 99 | + console.error(error) |
| 100 | + throw new GqlInputError('failed to delete domain') |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments