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
const users = await Database.userModel.getOldUsers()
for (const _user of users) {
let hasUpdated = false
if (_user.seriesHideFromContinueListening.length) {
const seriesHiding = (
await Database.seriesModel.findAll({
where: {
id: _user.seriesHideFromContinueListening
},
attributes: ['id'],
raw: true
})
).map((se) => se.id)
_user.seriesHideFromContinueListening = _user.seriesHideFromContinueListening.filter((seriesId) => {
if (!seriesHiding.includes(seriesId)) {
// Series removed
hasUpdated = true
return false
}
return true
})
}
if (hasUpdated) {
await Database.updateUser(_user)
try {
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;`, {
model: Database.userModel,
type: Sequelize.QueryTypes.SELECT
})
for (const user of users) {
const extraData = JSON.parse(user.extraData)
const existingSeriesIds = extraData.seriesHideFromContinueListening
const seriesIdsToRemove = JSON.parse(user.dataValues.seriesIdsToRemove)
Logger.info(`[Server] Found ${seriesIdsToRemove.length} non-existent series in seriesHideFromContinueListening for user "${user.username}" - Removing (${seriesIdsToRemove.join(',')})`)
const newExtraData = {
...extraData,
seriesHideFromContinueListening: existingSeriesIds.filter((s) => !seriesIdsToRemove.includes(s))
}
await user.update({ extraData: newExtraData })
}
} catch (error) {
Logger.error(`[Server] Failed to cleanup users seriesHideFromContinueListening`, error)
}
}