Merge pull request #1009 from stackernews/refinv

Fix all known problems with invite and referral links
This commit is contained in:
Keyan 2024-04-03 10:10:16 -05:00 committed by GitHub
commit e1e512deb7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 36 additions and 37 deletions

View File

@ -111,7 +111,7 @@ export function getGetServerSideProps (
} }
if (error || !data || (notFound && notFound(data, vars, me))) { if (error || !data || (notFound && notFound(data, vars, me))) {
res.writeHead(301, { res.writeHead(302, {
Location: '/404' Location: '/404'
}).end() }).end()
} }

View File

@ -21,7 +21,7 @@ export default function Invite ({ invite, active }) {
<CopyInput <CopyInput
groupClassName='mb-1' groupClassName='mb-1'
size='sm' type='text' size='sm' type='text'
placeholder={`https://stacker.news/invites/${invite.id}`} readOnly noForm placeholder={`${process.env.NODE_ENV === 'development' ? 'http://localhost:3000' : 'https://stacker.news'}/invites/${invite.id}`} readOnly noForm
/> />
<div className={styles.other}> <div className={styles.other}>
<span>{invite.gift} sat gift</span> <span>{invite.gift} sat gift</span>

View File

@ -51,6 +51,17 @@ function getCallbacks (req) {
// token won't have an id on it for new logins, we add it // token won't have an id on it for new logins, we add it
// note: token is what's kept in the jwt // note: token is what's kept in the jwt
token.id = Number(user.id) token.id = Number(user.id)
// if referrer exists, set on user
// isNewUser doesn't work for nostr/lightning auth because we create the user before nextauth can
// this means users can update their referrer if they don't have one, which is fine
if (req.cookies.sn_referrer && user?.id) {
const referrer = await prisma.user.findUnique({ where: { name: req.cookies.sn_referrer } })
if (referrer) {
await prisma.user.updateMany({ where: { id: user.id, referrerId: null }, data: { referrerId: referrer.id } })
notifyReferral(referrer.id)
}
}
} }
if (token?.id) { if (token?.id) {
@ -60,33 +71,22 @@ function getCallbacks (req) {
token.sub = Number(token.id) token.sub = Number(token.id)
} }
if (isNewUser) { // sign them up for the newsletter
// if referrer exists, set on user if (isNewUser && user?.email && process.env.LIST_MONK_URL && process.env.LIST_MONK_AUTH) {
if (req.cookies.sn_referrer && user?.id) { fetch(process.env.LIST_MONK_URL + '/api/subscribers', {
const referrer = await prisma.user.findUnique({ where: { name: req.cookies.sn_referrer } }) method: 'POST',
if (referrer) { headers: {
await prisma.user.update({ where: { id: user.id }, data: { referrerId: referrer.id } }) 'Content-Type': 'application/json',
notifyReferral(referrer.id) Authorization: 'Basic ' + Buffer.from(process.env.LIST_MONK_AUTH).toString('base64')
} },
} body: JSON.stringify({
email: user.email,
// sign them up for the newsletter name: 'blank',
if (user?.email && process.env.LIST_MONK_URL && process.env.LIST_MONK_AUTH) { lists: [2],
fetch(process.env.LIST_MONK_URL + '/api/subscribers', { status: 'enabled',
method: 'POST', preconfirm_subscriptions: true
headers: { })
'Content-Type': 'application/json', }).then(async r => console.log(await r.json())).catch(console.log)
Authorization: 'Basic ' + Buffer.from(process.env.LIST_MONK_AUTH).toString('base64')
},
body: JSON.stringify({
email: user.email,
name: 'blank',
lists: [2],
status: 'enabled',
preconfirm_subscriptions: true
})
}).then(async r => console.log(await r.json())).catch(console.log)
}
} }
return token return token

View File

@ -26,9 +26,10 @@ export async function getServerSideProps ({ req, res, query: { id, error = null
}) })
if (!data?.invite) { if (!data?.invite) {
res.writeHead(301, { res.writeHead(302, {
Location: '/404' Location: '/404'
}).end() }).end()
return { props: {} }
} }
if (session && res) { if (session && res) {
@ -36,19 +37,17 @@ export async function getServerSideProps ({ req, res, query: { id, error = null
// attempt to send gift // attempt to send gift
// catch any errors and just ignore them for now // catch any errors and just ignore them for now
await serialize(models, await serialize(models,
models.$queryRawUnsafe('SELECT invite_drain($1::INTEGER, $2::INTEGER)', session.user.id, id)) models.$queryRawUnsafe('SELECT invite_drain($1::INTEGER, $2::TEXT)', session.user.id, id))
const invite = await models.invite.findUnique({ where: { id } }) const invite = await models.invite.findUnique({ where: { id } })
notifyInvite(invite.userId) notifyInvite(invite.userId)
} catch (e) { } catch (e) {
console.log(e) console.log(e)
} }
return { res.writeHead(302, {
redirect: { Location: '/'
destination: '/', }).end()
permanent: false return { props: {} }
}
}
} }
return { return {