Replace function for cleaning up user seriesHideFromContinueListening to not use old model

This commit is contained in:
advplyr 2024-08-03 15:08:03 -05:00
parent 4a5345dd5d
commit 164b4525c4

View File

@ -383,31 +383,24 @@ class Server {
} }
// Remove series from hide from continue listening that no longer exist // Remove series from hide from continue listening that no longer exist
const users = await Database.userModel.getOldUsers() try {
for (const _user of users) { const users = await Database.sequelize.query(`SELECT u.id, u.username, u.extraData, json_group_array(value) AS seriesIdsToRemove FROM users u, json_each(u.extraData->"seriesHideFromContinueListening") LEFT JOIN series se ON se.id = value WHERE se.id IS NULL GROUP BY u.id;`, {
let hasUpdated = false model: Database.userModel,
if (_user.seriesHideFromContinueListening.length) { type: Sequelize.QueryTypes.SELECT
const seriesHiding = ( })
await Database.seriesModel.findAll({ for (const user of users) {
where: { const extraData = JSON.parse(user.extraData)
id: _user.seriesHideFromContinueListening const existingSeriesIds = extraData.seriesHideFromContinueListening
}, const seriesIdsToRemove = JSON.parse(user.dataValues.seriesIdsToRemove)
attributes: ['id'], Logger.info(`[Server] Found ${seriesIdsToRemove.length} non-existent series in seriesHideFromContinueListening for user "${user.username}" - Removing (${seriesIdsToRemove.join(',')})`)
raw: true const newExtraData = {
}) ...extraData,
).map((se) => se.id) seriesHideFromContinueListening: existingSeriesIds.filter((s) => !seriesIdsToRemove.includes(s))
_user.seriesHideFromContinueListening = _user.seriesHideFromContinueListening.filter((seriesId) => { }
if (!seriesHiding.includes(seriesId)) { await user.update({ extraData: newExtraData })
// Series removed
hasUpdated = true
return false
}
return true
})
}
if (hasUpdated) {
await Database.updateUser(_user)
} }
} catch (error) {
Logger.error(`[Server] Failed to cleanup users seriesHideFromContinueListening`, error)
} }
} }