2016-04-06 13:56:06 -04:00
|
|
|
mongoose = require('mongoose')
|
|
|
|
deltas = require '../../app/core/deltas'
|
|
|
|
log = require 'winston'
|
|
|
|
{handlers} = require '../commons/mapping'
|
|
|
|
config = require '../../server_config'
|
2015-12-16 20:09:22 -05:00
|
|
|
|
2016-04-06 13:56:06 -04:00
|
|
|
PatchSchema = new mongoose.Schema({status: String}, {strict: false,read:config.mongo.readpref})
|
2016-08-16 12:24:34 -04:00
|
|
|
PatchSchema.index({'target.original': 1, 'status': 1}, {name: 'target_status'})
|
2016-04-06 13:56:06 -04:00
|
|
|
|
|
|
|
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()
|
2016-09-07 19:15:54 -04:00
|
|
|
collection = @get('target.collection')
|
2016-04-06 13:56:06 -04:00
|
|
|
|
2016-09-07 19:15:54 -04:00
|
|
|
# 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.
|
2016-04-06 13:56:06 -04:00
|
|
|
if @get('status') is 'accepted'
|
|
|
|
User.incrementStat userID, 'stats.patchesContributed' # accepted patches
|
2016-09-07 19:15:54 -04:00
|
|
|
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])
|
2016-04-06 13:56:06 -04:00
|
|
|
|
|
|
|
next()
|
|
|
|
|
2016-08-16 12:24:34 -04:00
|
|
|
jsonSchema = require '../../app/schemas/models/patch'
|
|
|
|
PatchSchema.statics.jsonSchema = jsonSchema
|
|
|
|
|
2016-04-06 13:56:06 -04:00
|
|
|
module.exports = mongoose.model('patch', PatchSchema)
|