use channel id instead of the user uuid, some easy anti spamming measure

This commit is contained in:
Evert Prants 2021-02-13 21:06:48 +02:00
parent b2a1e71e8d
commit 6769b371be
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
1 changed files with 14 additions and 5 deletions

19
app.js
View File

@ -118,6 +118,7 @@ if (config.Email.enabled) {
} }
const notifQueue = [] const notifQueue = []
const notifHistory = {}
function now() { function now() {
return Math.floor(Date.now() / 1000) return Math.floor(Date.now() / 1000)
@ -131,11 +132,19 @@ async function sendEmailPush(channel) {
if (!emailTransport) { if (!emailTransport) {
return 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 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) { if (!data) {
return; return;
} }
const subs = await db.all('SELECT email,unsubkey FROM emailsub WHERE uuid = ? AND active = 1', channel); const subs = await db.all('SELECT email,unsubkey FROM emailsub WHERE uuid = ? AND active = 1', channel);
for (const sub of subs) { for (const sub of subs) {
const unsubURL = config.Email.baseURL + 'unsubscribe/' + sub.unsubkey const unsubURL = config.Email.baseURL + 'unsubscribe/' + sub.unsubkey
@ -160,12 +169,12 @@ async function subscribeToChannel(channel, email) {
} }
const db = await dbPromise 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) { if (!data) {
throw new Error('Invalid channel!') 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) { if (exists) {
throw new Error('A subscription already exists for this email address.') throw new Error('A subscription already exists for this email address.')
} }
@ -175,7 +184,7 @@ async function subscribeToChannel(channel, email) {
const unsubKey = key() const unsubKey = key()
const activateURL = config.Email.baseURL + 'email/' + activateKey const activateURL = config.Email.baseURL + 'email/' + activateKey
await db.run('INSERT INTO emailsub (unsubkey, activatekey, email, uuid, active, created_at) VALUES ' 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({ await emailTransport.sendMail({
from: config.Email.from, from: config.Email.from,
@ -328,7 +337,7 @@ app.post('/publish', async (req, res) => {
// Set channel streaming status // Set channel streaming status
db.run('UPDATE channels SET live_at=? WHERE id=?', Date.now(), streamer.id) 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 // Send notifications
if (!notifQueue.includes(streamer.user_uuid)) { if (!notifQueue.includes(streamer.user_uuid)) {