mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-24 21:12:38 -04:00
e681c6322f
refresh user features on editor load when out of date GitOrigin-RevId: ef39b5626cfdc6ed611137a6f6eca3417d3ce73f
61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
const { User } = require('../../models/User')
|
|
const { promisifyAll } = require('../../util/promises')
|
|
const Settings = require('@overleaf/settings')
|
|
|
|
function _featuresChanged(newFeatures, featuresBefore) {
|
|
for (const feature in newFeatures) {
|
|
if (featuresBefore[feature] !== newFeatures[feature]) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
module.exports = {
|
|
updateFeatures(userId, features, callback) {
|
|
const update = {
|
|
featuresUpdatedAt: new Date(),
|
|
}
|
|
// record the system-wide features epoch, if defined
|
|
if (Settings.featuresEpoch) {
|
|
update.featuresEpoch = Settings.featuresEpoch
|
|
}
|
|
for (const key in features) {
|
|
const value = features[key]
|
|
update[`features.${key}`] = value
|
|
}
|
|
User.findByIdAndUpdate(userId, update, (err, docBeforeUpdate) => {
|
|
let featuresChanged = false
|
|
if (docBeforeUpdate) {
|
|
featuresChanged = _featuresChanged(features, docBeforeUpdate.features)
|
|
}
|
|
|
|
return callback(err, features, featuresChanged)
|
|
})
|
|
},
|
|
|
|
overrideFeatures(userId, features, callback) {
|
|
const update = { features, featuresUpdatedAt: new Date() }
|
|
User.findByIdAndUpdate(userId, update, (err, docBeforeUpdate) => {
|
|
let featuresChanged = false
|
|
if (docBeforeUpdate) {
|
|
featuresChanged = _featuresChanged(features, docBeforeUpdate.features)
|
|
}
|
|
return callback(err, featuresChanged)
|
|
})
|
|
},
|
|
|
|
createFeaturesOverride(userId, featuresOverride, callback) {
|
|
User.updateOne(
|
|
{ _id: userId },
|
|
{ $push: { featuresOverrides: featuresOverride } },
|
|
callback
|
|
)
|
|
},
|
|
}
|
|
|
|
module.exports.promises = promisifyAll(module.exports, {
|
|
multiResult: {
|
|
updateFeatures: ['features', 'featuresChanged'],
|
|
},
|
|
})
|