feat(email): allow unsub via POST request

This commit is contained in:
Sabe Jones
2024-02-07 16:09:23 -06:00
parent 67a6e6f8ac
commit 91795875b5

View File

@@ -7,6 +7,36 @@ import {
const api = {}; const api = {};
async function emailUnsubscribe (req, res) {
req.checkQuery({
code: {
notEmpty: { errorMessage: res.t('missingUnsubscriptionCode') },
},
});
const validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
const data = JSON.parse(decrypt(req.query.code));
if (data._id) {
const userUpdated = await User.updateOne(
{ _id: data._id },
{ $set: { 'preferences.emailNotifications.unsubscribeFromAll': true } },
).exec();
if (userUpdated.modifiedCount !== 1) throw new NotFound(res.t('userNotFound'));
res.send(`<h1>${res.t('unsubscribedSuccessfully')}</h1> ${res.t('unsubscribedTextUsers')}`);
} else {
const unsubscribedEmail = await EmailUnsubscription
.findOne({ email: data.email.toLowerCase() }).exec();
if (!unsubscribedEmail) await EmailUnsubscription.create({ email: data.email.toLowerCase() });
const okResponse = `<h1>${res.t('unsubscribedSuccessfully')}</h1> ${res.t('unsubscribedTextOthers')}`;
res.send(okResponse);
}
}
/** /**
* @api {get} /email/unsubscribe Unsubscribe an email address or user from email notifications * @api {get} /email/unsubscribe Unsubscribe an email address or user from email notifications
* @apiName UnsubscribeEmail * @apiName UnsubscribeEmail
@@ -27,33 +57,32 @@ api.unsubscribe = {
method: 'GET', method: 'GET',
url: '/email/unsubscribe', url: '/email/unsubscribe',
async handler (req, res) { async handler (req, res) {
req.checkQuery({ await emailUnsubscribe(req, res);
code: { },
notEmpty: { errorMessage: res.t('missingUnsubscriptionCode') }, };
},
});
const validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
const data = JSON.parse(decrypt(req.query.code)); /**
* @api {post} /email/unsubscribe Unsubscribe an email address or user from email notifications
if (data._id) { * @apiName OneClickUnsubscribe
const userUpdated = await User.updateOne( * @apiGroup Unsubscribe
{ _id: data._id }, * @apiDescription This is a POST method for compliance with RFC 8058. It works identically to the
{ $set: { 'preferences.emailNotifications.unsubscribeFromAll': true } }, * GET method on the same URI, allowing the user to unsubscribe from emails either via visiting a
).exec(); * hyperlink or activating a one-click Unsubscribe button in their email client.
* Does not require authentication.
if (userUpdated.modifiedCount !== 1) throw new NotFound(res.t('userNotFound')); *
* @apiParam (Query) {String} code An unsubscription code that contains an encrypted User ID or
res.send(`<h1>${res.t('unsubscribedSuccessfully')}</h1> ${res.t('unsubscribedTextUsers')}`); * email address
} else { *
const unsubscribedEmail = await EmailUnsubscription * @apiSuccess {String} Webpage An html success message
.findOne({ email: data.email.toLowerCase() }).exec(); *
if (!unsubscribedEmail) await EmailUnsubscription.create({ email: data.email.toLowerCase() }); * @apiError (400) {BadRequest} missingUnsubscriptionCode The unsubscription code is missing.
* @apiUse UserNotFound
const okResponse = `<h1>${res.t('unsubscribedSuccessfully')}</h1> ${res.t('unsubscribedTextOthers')}`; */
res.send(okResponse); api.oneClickUnsubscribe = {
} method: 'POST',
url: '/email/unsubscribe',
async handler (req, res) {
await emailUnsubscribe(req, res);
}, },
}; };