diff --git a/app.js b/app.js index 929fe52..1d821cc 100644 --- a/app.js +++ b/app.js @@ -118,6 +118,7 @@ if (config.Email.enabled) { } const notifQueue = [] +const notifHistory = {} function now() { return Math.floor(Date.now() / 1000) @@ -131,11 +132,19 @@ async function sendEmailPush(channel) { if (!emailTransport) { return } + + // Don't re-send notifications within an hour if a channel happens to go live again + if (notifHistory[channel] && notifHistory[channel] > now() - 3600) { + return + } + notifHistory[channel] = now() + const db = await dbPromise - const data = await db.get('SELECT name FROM channels WHERE user_uuid = ?', channel) + const data = await db.get('SELECT name FROM channels WHERE id = ?', channel) if (!data) { return; } + const subs = await db.all('SELECT email,unsubkey FROM emailsub WHERE uuid = ? AND active = 1', channel); for (const sub of subs) { const unsubURL = config.Email.baseURL + 'unsubscribe/' + sub.unsubkey @@ -160,12 +169,12 @@ async function subscribeToChannel(channel, email) { } const db = await dbPromise - const data = await db.get('SELECT user_uuid FROM channels WHERE name = ?', channel) + const data = await db.get('SELECT id FROM channels WHERE name = ?', channel) if (!data) { throw new Error('Invalid channel!') } - const exists = await db.get('SELECT * FROM emailsub WHERE email = ? AND uuid = ?', [email, data.user_uuid]) + const exists = await db.get('SELECT * FROM emailsub WHERE email = ? AND uuid = ?', [email, data.id]) if (exists) { throw new Error('A subscription already exists for this email address.') } @@ -175,7 +184,7 @@ async function subscribeToChannel(channel, email) { const unsubKey = key() const activateURL = config.Email.baseURL + 'email/' + activateKey await db.run('INSERT INTO emailsub (unsubkey, activatekey, email, uuid, active, created_at) VALUES ' - + '(?, ?, ?, ?, 0, ?)', [unsubKey, activateKey, email, data.user_uuid, now()]) + + '(?, ?, ?, ?, 0, ?)', [unsubKey, activateKey, email, data.id, now()]) await emailTransport.sendMail({ from: config.Email.from, @@ -328,7 +337,7 @@ app.post('/publish', async (req, res) => { // Set channel streaming status db.run('UPDATE channels SET live_at=? WHERE id=?', Date.now(), streamer.id) - cache.live.push(streamer.name) + cache.live.push(streamer.id) // Send notifications if (!notifQueue.includes(streamer.user_uuid)) {