2023-05-30 00:38:38 +02:00
|
|
|
const Logger = require('../../Logger')
|
|
|
|
const { areEquivalent, copyValue, isNullOrNaN } = require('../../utils')
|
|
|
|
|
2023-10-29 17:28:34 +01:00
|
|
|
/**
|
|
|
|
* @typedef EreaderDeviceObject
|
|
|
|
* @property {string} name
|
|
|
|
* @property {string} email
|
|
|
|
* @property {string} availabilityOption
|
|
|
|
* @property {string[]} users
|
|
|
|
*/
|
|
|
|
|
2023-05-30 00:38:38 +02:00
|
|
|
// REF: https://nodemailer.com/smtp/
|
|
|
|
class EmailSettings {
|
|
|
|
constructor(settings = null) {
|
|
|
|
this.id = 'email-settings'
|
|
|
|
this.host = null
|
|
|
|
this.port = 465
|
|
|
|
this.secure = true
|
|
|
|
this.user = null
|
|
|
|
this.pass = null
|
2023-06-12 06:53:51 +02:00
|
|
|
this.testAddress = null
|
2023-05-30 00:38:38 +02:00
|
|
|
this.fromAddress = null
|
|
|
|
|
2023-10-29 17:28:34 +01:00
|
|
|
/** @type {EreaderDeviceObject[]} */
|
2023-05-30 00:38:38 +02:00
|
|
|
this.ereaderDevices = []
|
|
|
|
|
|
|
|
if (settings) {
|
|
|
|
this.construct(settings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(settings) {
|
|
|
|
this.host = settings.host
|
|
|
|
this.port = settings.port
|
|
|
|
this.secure = !!settings.secure
|
|
|
|
this.user = settings.user
|
|
|
|
this.pass = settings.pass
|
2023-06-12 06:53:51 +02:00
|
|
|
this.testAddress = settings.testAddress
|
2023-05-30 00:38:38 +02:00
|
|
|
this.fromAddress = settings.fromAddress
|
|
|
|
this.ereaderDevices = settings.ereaderDevices?.map(d => ({ ...d })) || []
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
host: this.host,
|
|
|
|
port: this.port,
|
|
|
|
secure: this.secure,
|
|
|
|
user: this.user,
|
|
|
|
pass: this.pass,
|
2023-06-12 06:53:51 +02:00
|
|
|
testAddress: this.testAddress,
|
2023-05-30 00:38:38 +02:00
|
|
|
fromAddress: this.fromAddress,
|
|
|
|
ereaderDevices: this.ereaderDevices.map(d => ({ ...d }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update(payload) {
|
|
|
|
if (!payload) return false
|
|
|
|
|
|
|
|
if (payload.port !== undefined) {
|
|
|
|
if (isNullOrNaN(payload.port)) payload.port = 465
|
|
|
|
else payload.port = Number(payload.port)
|
|
|
|
}
|
|
|
|
if (payload.secure !== undefined) payload.secure = !!payload.secure
|
|
|
|
|
|
|
|
if (payload.ereaderDevices !== undefined && !Array.isArray(payload.ereaderDevices)) payload.ereaderDevices = undefined
|
|
|
|
|
2023-10-29 17:28:34 +01:00
|
|
|
if (payload.ereaderDevices?.length) {
|
|
|
|
// Validate ereader devices
|
|
|
|
payload.ereaderDevices = payload.ereaderDevices.map((device) => {
|
|
|
|
if (!device.name || !device.email) {
|
|
|
|
Logger.error(`[EmailSettings] Update ereader device is invalid`, device)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
if (!device.availabilityOption || !['adminOrUp', 'userOrUp', 'guestOrUp', 'specificUsers'].includes(device.availabilityOption)) {
|
|
|
|
device.availabilityOption = 'adminOrUp'
|
|
|
|
}
|
|
|
|
if (device.availabilityOption === 'specificUsers' && !device.users?.length) {
|
|
|
|
device.availabilityOption = 'adminOrUp'
|
|
|
|
}
|
|
|
|
if (device.availabilityOption !== 'specificUsers' && device.users?.length) {
|
|
|
|
device.users = []
|
|
|
|
}
|
|
|
|
return device
|
|
|
|
}).filter(d => d)
|
|
|
|
}
|
|
|
|
|
2023-05-30 00:38:38 +02:00
|
|
|
let hasUpdates = false
|
|
|
|
|
|
|
|
const json = this.toJSON()
|
|
|
|
for (const key in json) {
|
|
|
|
if (key === 'id') continue
|
|
|
|
|
|
|
|
if (payload[key] !== undefined && !areEquivalent(payload[key], json[key])) {
|
|
|
|
this[key] = copyValue(payload[key])
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
|
|
|
getTransportObject() {
|
|
|
|
const payload = {
|
|
|
|
host: this.host,
|
|
|
|
secure: this.secure
|
|
|
|
}
|
2024-04-26 01:04:02 +02:00
|
|
|
// Only set to true for port 465 (https://nodemailer.com/smtp/#tls-options)
|
|
|
|
if (this.port !== 465) {
|
|
|
|
payload.secure = false
|
|
|
|
}
|
2023-05-30 00:38:38 +02:00
|
|
|
if (this.port) payload.port = this.port
|
|
|
|
if (this.user && this.pass !== undefined) {
|
|
|
|
payload.auth = {
|
|
|
|
user: this.user,
|
|
|
|
pass: this.pass
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload
|
|
|
|
}
|
|
|
|
|
2023-10-29 17:28:34 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {EreaderDeviceObject} device
|
|
|
|
* @param {import('../user/User')} user
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
checkUserCanAccessDevice(device, user) {
|
|
|
|
let deviceAvailability = device.availabilityOption || 'adminOrUp'
|
|
|
|
if (deviceAvailability === 'adminOrUp' && user.isAdminOrUp) return true
|
|
|
|
if (deviceAvailability === 'userOrUp' && (user.isAdminOrUp || user.isUser)) return true
|
|
|
|
if (deviceAvailability === 'guestOrUp') return true
|
|
|
|
if (deviceAvailability === 'specificUsers') {
|
|
|
|
let deviceUsers = device.users || []
|
|
|
|
return deviceUsers.includes(user.id)
|
2023-05-30 00:38:38 +02:00
|
|
|
}
|
2023-10-29 17:28:34 +01:00
|
|
|
return false
|
|
|
|
}
|
2023-05-30 00:38:38 +02:00
|
|
|
|
2023-10-29 17:28:34 +01:00
|
|
|
/**
|
|
|
|
* Get ereader devices accessible to user
|
|
|
|
*
|
|
|
|
* @param {import('../user/User')} user
|
|
|
|
* @returns {EreaderDeviceObject[]}
|
|
|
|
*/
|
|
|
|
getEReaderDevices(user) {
|
|
|
|
return this.ereaderDevices.filter((device) => this.checkUserCanAccessDevice(device, user))
|
2023-05-30 00:38:38 +02:00
|
|
|
}
|
|
|
|
|
2023-10-29 17:28:34 +01:00
|
|
|
/**
|
|
|
|
* Get ereader device by name
|
|
|
|
*
|
|
|
|
* @param {string} deviceName
|
|
|
|
* @returns {EreaderDeviceObject}
|
|
|
|
*/
|
2023-05-30 00:38:38 +02:00
|
|
|
getEReaderDevice(deviceName) {
|
|
|
|
return this.ereaderDevices.find(d => d.name === deviceName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = EmailSettings
|