codecombat/server/models/Patch.coffee
Scott Erickson 7bab895dee Generalize new I18N view system
Previously, when diplomats submit translations, the system
would try to figure out whether it should be a 'patch' or a 'change',
and then would either create a patch for an admin or artisan to
review and accept or reject, or would apply the changes immediately
and they would be live. This was done as a compromise between
getting translations live quickly, but also preventing already-translated
text from getting overwritten without oversight.

But having the client handle this added logical complexity. So
this makes all diplomats submit patches, no matter what. The server
is then in charge of deciding if it should auto-accept the patch or not.
Either way, a patch is created.

There was also much refactoring. This commit includes:

* Update jsondiffpatch so changes within array items are handled correctly
* Refactor posting patches to use the new auto-accepting logic, and out of Patch model
* Refactor POST /db/patch/:handle/status so that it doesn't rely on handlers
* Refactor patch stat handling to ensure auto-accepted patches are counted
* Refactor User.incrementStat to use mongodb update commands, to avoid race conditions
* Refactor Patch tests
2016-09-09 10:59:26 -07:00

48 lines
1.9 KiB
CoffeeScript

mongoose = require('mongoose')
deltas = require '../../app/core/deltas'
log = require 'winston'
{handlers} = require '../commons/mapping'
config = require '../../server_config'
PatchSchema = new mongoose.Schema({status: String}, {strict: false,read:config.mongo.readpref})
PatchSchema.index({'target.original': 1, 'status': 1}, {name: 'target_status'})
PatchSchema.methods.isTranslationPatch = -> # Don't ever fat arrow bind this one
expanded = deltas.flattenDelta @get('delta')
_.some expanded, (delta) -> 'i18n' in delta.dataPath
PatchSchema.methods.isMiscPatch = ->
expanded = deltas.flattenDelta @get('delta')
_.some expanded, (delta) -> 'i18n' not in delta.dataPath
# Keep track of when a patch is pending and newly approved.
PatchSchema.path('status').set (newVal) ->
@set 'wasPending', @status is 'pending' and newVal isnt 'pending'
@set 'newlyAccepted', newVal is 'accepted' and not @get('newlyAccepted') # Only true on the first accept
newVal
PatchSchema.methods.isNewlyAccepted = -> @get('newlyAccepted')
PatchSchema.methods.wasPending = -> @get 'wasPending'
PatchSchema.pre 'save', (next) ->
User = require './User'
userID = @get('creator').toHexString()
collection = @get('target.collection')
# Increment patch submitter stats when the patch is "accepted".
# Does not handle if the patch is accepted multiple times, but that's an edge case.
if @get('status') is 'accepted'
User.incrementStat userID, 'stats.patchesContributed' # accepted patches
if @isTranslationPatch()
User.incrementStat(userID, 'stats.totalTranslationPatches')
User.incrementStat(userID, User.statsMapping.translations[collection])
if @isMiscPatch()
User.incrementStat(userID, 'stats.totalMiscPatches')
User.incrementStat(userID, User.statsMapping.misc[collection])
next()
jsonSchema = require '../../app/schemas/models/patch'
PatchSchema.statics.jsonSchema = jsonSchema
module.exports = mongoose.model('patch', PatchSchema)