mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-12-17 19:12:33 -05:00
Refactored sending of HipChat room messages.
This commit is contained in:
parent
8f996a44b0
commit
a189e32948
8 changed files with 61 additions and 58 deletions
|
@ -461,8 +461,9 @@ module.exports = class Handler
|
||||||
sendwithus.api.send context, (err, result) ->
|
sendwithus.api.send context, (err, result) ->
|
||||||
|
|
||||||
sendChangedHipChatMessage: (options) ->
|
sendChangedHipChatMessage: (options) ->
|
||||||
message = "#{options.creator.get('name')} saved a change to <a href=\"#{options.docLink}\">#{options.target.get('name')}</a>: #{options.target.get('commitMessage')}"
|
message = "#{options.creator.get('name')} saved a change to <a href=\"#{options.docLink}\">#{options.target.get('name')}</a>: #{options.target.get('commitMessage') or '(no commit message)'}"
|
||||||
hipchat.sendHipChatMessage message
|
rooms = if /Diplomat submission/.test(message) then ['main'] else ['main', 'artisans']
|
||||||
|
hipchat.sendHipChatMessage message, rooms
|
||||||
|
|
||||||
makeNewInstance: (req) ->
|
makeNewInstance: (req) ->
|
||||||
model = new @modelClass({})
|
model = new @modelClass({})
|
||||||
|
|
|
@ -2,32 +2,34 @@ config = require '../server_config'
|
||||||
request = require 'request'
|
request = require 'request'
|
||||||
log = require 'winston'
|
log = require 'winston'
|
||||||
|
|
||||||
module.exports.sendHipChatMessage = sendHipChatMessage = (message) ->
|
roomIDMap =
|
||||||
return unless key = config.hipchatAPIKey
|
main: 254598
|
||||||
return unless config.isProduction
|
artisans: 1146994
|
||||||
roomID = 254598
|
tower: 318356
|
||||||
form =
|
|
||||||
color: 'yellow'
|
|
||||||
notify: false
|
|
||||||
message: message
|
|
||||||
messageFormat: 'html'
|
|
||||||
url = "https://api.hipchat.com/v2/room/#{roomID}/notification?auth_token=#{key}"
|
|
||||||
request.post {uri: url, json: form}, (err, res, body) ->
|
|
||||||
return log.error 'Error sending HipChat patch request:', err or body if err or /error/i.test body
|
|
||||||
#log.info "Got HipChat patch response:", body
|
|
||||||
|
|
||||||
module.exports.sendTowerHipChatMessage = sendTowerHipChatMessage = (message) ->
|
module.exports.sendHipChatMessage = sendHipChatMessage = (message, rooms, options) ->
|
||||||
secondsFromEpoch = Math.floor(new Date().getTime() / 1000)
|
|
||||||
link = "<a href=\"https://papertrailapp.com/groups/488214/events?time=#{secondsFromEpoch}\">PaperTrail</a>"
|
|
||||||
message = "#{message} #{link}"
|
|
||||||
return unless key = config.hipchatTowerAPIKey
|
|
||||||
return unless config.isProduction
|
return unless config.isProduction
|
||||||
roomID = 318356
|
rooms ?= ['main']
|
||||||
form =
|
options ?= {}
|
||||||
color: 'red'
|
for room in rooms
|
||||||
notify: true
|
unless roomID = roomIDMap[room]
|
||||||
message: message
|
log.error "Unknown HipChat room #{room}."
|
||||||
messageFormat: 'html'
|
continue
|
||||||
url = "https://api.hipchat.com/v2/room/#{roomID}/notification?auth_token=#{key}"
|
unless key = config.hipchat[room]
|
||||||
request.post {uri: url, json: form}, (err, res, body) ->
|
log.info "No HipChat API key for room #{room}."
|
||||||
return log.error 'Error sending HipChat Tower message:', err or body if err or /error/i.test body
|
continue
|
||||||
|
form =
|
||||||
|
color: options.color or 'yellow'
|
||||||
|
notify: false
|
||||||
|
message: message
|
||||||
|
messageFormat: 'html'
|
||||||
|
if options.papertrail
|
||||||
|
secondsFromEpoch = Math.floor(new Date().getTime() / 1000)
|
||||||
|
link = "<a href=\"https://papertrailapp.com/groups/488214/events?time=#{secondsFromEpoch}\">PaperTrail</a>"
|
||||||
|
form.message = "#{message} #{link}"
|
||||||
|
form.color = options.color or 'red'
|
||||||
|
form.notify = true
|
||||||
|
url = "https://api.hipchat.com/v2/room/#{roomID}/notification?auth_token=#{key}"
|
||||||
|
request.post {uri: url, json: form}, (err, res, body) ->
|
||||||
|
return log.error 'Error sending HipChat message:', err or body if err or /error/i.test body
|
||||||
|
#log.info "Got HipChat message response:", body
|
||||||
|
|
|
@ -101,6 +101,6 @@ PatchHandler = class PatchHandler extends Handler
|
||||||
|
|
||||||
sendPatchCreatedHipChatMessage: (options) ->
|
sendPatchCreatedHipChatMessage: (options) ->
|
||||||
message = "#{options.creator.get('name')} submitted a patch to <a href=\"#{options.docLink}\">#{options.target.get('name')}</a>: #{options.patch.get('commitMessage')}"
|
message = "#{options.creator.get('name')} submitted a patch to <a href=\"#{options.docLink}\">#{options.target.get('name')}</a>: #{options.patch.get('commitMessage')}"
|
||||||
hipchat.sendHipChatMessage message
|
hipchat.sendHipChatMessage message, ['main']
|
||||||
|
|
||||||
module.exports = new PatchHandler()
|
module.exports = new PatchHandler()
|
||||||
|
|
|
@ -35,7 +35,7 @@ PaymentHandler = class PaymentHandler extends Handler
|
||||||
editableProperties: []
|
editableProperties: []
|
||||||
postEditableProperties: ['purchased']
|
postEditableProperties: ['purchased']
|
||||||
jsonSchema: require '../../app/schemas/models/payment.schema'
|
jsonSchema: require '../../app/schemas/models/payment.schema'
|
||||||
|
|
||||||
get: (req, res) ->
|
get: (req, res) ->
|
||||||
return res.send([]) unless req.user
|
return res.send([]) unless req.user
|
||||||
q = Payment.find({recipient:req.user._id})
|
q = Payment.find({recipient:req.user._id})
|
||||||
|
@ -43,7 +43,7 @@ PaymentHandler = class PaymentHandler extends Handler
|
||||||
return @sendDatabaseError(res, err) if err
|
return @sendDatabaseError(res, err) if err
|
||||||
res.send(payments)
|
res.send(payments)
|
||||||
)
|
)
|
||||||
|
|
||||||
logPaymentError: (req, msg) ->
|
logPaymentError: (req, msg) ->
|
||||||
console.warn "Payment Error: #{req.user.get('slug')} (#{req.user._id}): '#{msg}'"
|
console.warn "Payment Error: #{req.user.get('slug')} (#{req.user._id}): '#{msg}'"
|
||||||
|
|
||||||
|
@ -57,10 +57,10 @@ PaymentHandler = class PaymentHandler extends Handler
|
||||||
post: (req, res, pathName) ->
|
post: (req, res, pathName) ->
|
||||||
if pathName is 'check-stripe-charges'
|
if pathName is 'check-stripe-charges'
|
||||||
return @checkStripeCharges(req, res)
|
return @checkStripeCharges(req, res)
|
||||||
|
|
||||||
if (not req.user) or req.user.isAnonymous()
|
if (not req.user) or req.user.isAnonymous()
|
||||||
return @sendForbiddenError(res)
|
return @sendForbiddenError(res)
|
||||||
|
|
||||||
appleReceipt = req.body.apple?.rawReceipt
|
appleReceipt = req.body.apple?.rawReceipt
|
||||||
appleTransactionID = req.body.apple?.transactionID
|
appleTransactionID = req.body.apple?.transactionID
|
||||||
appleLocalPrice = req.body.apple?.localPrice
|
appleLocalPrice = req.body.apple?.localPrice
|
||||||
|
@ -146,7 +146,7 @@ PaymentHandler = class PaymentHandler extends Handler
|
||||||
if validation.valid is false
|
if validation.valid is false
|
||||||
@logPaymentError(req, 'Invalid apple payment object.')
|
@logPaymentError(req, 'Invalid apple payment object.')
|
||||||
return @sendBadInputError(res, validation.errors)
|
return @sendBadInputError(res, validation.errors)
|
||||||
|
|
||||||
payment.save((err) =>
|
payment.save((err) =>
|
||||||
if err
|
if err
|
||||||
@logPaymentError(req, 'Apple payment save error.'+err)
|
@logPaymentError(req, 'Apple payment save error.'+err)
|
||||||
|
@ -170,24 +170,24 @@ PaymentHandler = class PaymentHandler extends Handler
|
||||||
# First, make sure we save the payment info as a Customer object, if we haven't already.
|
# First, make sure we save the payment info as a Customer object, if we haven't already.
|
||||||
if token
|
if token
|
||||||
customerID = req.user.get('stripe')?.customerID
|
customerID = req.user.get('stripe')?.customerID
|
||||||
|
|
||||||
if customerID
|
if customerID
|
||||||
# old customer, new token. Save it.
|
# old customer, new token. Save it.
|
||||||
stripe.customers.update customerID, { card: token }, (err, customer) =>
|
stripe.customers.update customerID, { card: token }, (err, customer) =>
|
||||||
@beginStripePayment(req, res, timestamp, productID)
|
@beginStripePayment(req, res, timestamp, productID)
|
||||||
|
|
||||||
else
|
else
|
||||||
newCustomer = {
|
newCustomer = {
|
||||||
card: token
|
card: token
|
||||||
email: req.user.get('email')
|
email: req.user.get('email')
|
||||||
metadata: { id: req.user._id + '', slug: req.user.get('slug') }
|
metadata: { id: req.user._id + '', slug: req.user.get('slug') }
|
||||||
}
|
}
|
||||||
|
|
||||||
stripe.customers.create newCustomer, (err, customer) =>
|
stripe.customers.create newCustomer, (err, customer) =>
|
||||||
if err
|
if err
|
||||||
@logPaymentError(req, 'Stripe customer creation error. '+err)
|
@logPaymentError(req, 'Stripe customer creation error. '+err)
|
||||||
return @sendDatabaseError(res, err)
|
return @sendDatabaseError(res, err)
|
||||||
|
|
||||||
stripeInfo = _.cloneDeep(req.user.get('stripe') ? {})
|
stripeInfo = _.cloneDeep(req.user.get('stripe') ? {})
|
||||||
stripeInfo.customerID = customer.id
|
stripeInfo.customerID = customer.id
|
||||||
req.user.set('stripe', stripeInfo)
|
req.user.set('stripe', stripeInfo)
|
||||||
|
@ -223,7 +223,7 @@ PaymentHandler = class PaymentHandler extends Handler
|
||||||
((err, results) =>
|
((err, results) =>
|
||||||
if err
|
if err
|
||||||
@logPaymentError(req, 'Stripe async load db error. '+err)
|
@logPaymentError(req, 'Stripe async load db error. '+err)
|
||||||
return @sendDatabaseError(res, err)
|
return @sendDatabaseError(res, err)
|
||||||
[payment, charge] = results
|
[payment, charge] = results
|
||||||
|
|
||||||
if not (payment or charge)
|
if not (payment or charge)
|
||||||
|
@ -285,7 +285,7 @@ PaymentHandler = class PaymentHandler extends Handler
|
||||||
timestamp: parseInt(charge.metadata.timestamp)
|
timestamp: parseInt(charge.metadata.timestamp)
|
||||||
chargeID: charge.id
|
chargeID: charge.id
|
||||||
}
|
}
|
||||||
|
|
||||||
validation = @validateDocumentInput(payment.toObject())
|
validation = @validateDocumentInput(payment.toObject())
|
||||||
if validation.valid is false
|
if validation.valid is false
|
||||||
@logPaymentError(req, 'Invalid stripe payment object.')
|
@logPaymentError(req, 'Invalid stripe payment object.')
|
||||||
|
@ -302,9 +302,9 @@ PaymentHandler = class PaymentHandler extends Handler
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
#- Confirm all Stripe charges are recorded on our server
|
#- Confirm all Stripe charges are recorded on our server
|
||||||
|
|
||||||
checkStripeCharges: (req, res) ->
|
checkStripeCharges: (req, res) ->
|
||||||
return @sendSuccess(res) unless customerID = req.user.get('stripe')?.customerID
|
return @sendSuccess(res) unless customerID = req.user.get('stripe')?.customerID
|
||||||
async.parallel([
|
async.parallel([
|
||||||
|
@ -366,7 +366,7 @@ PaymentHandler = class PaymentHandler extends Handler
|
||||||
sendPaymentHipChatMessage: (options) ->
|
sendPaymentHipChatMessage: (options) ->
|
||||||
try
|
try
|
||||||
message = "#{options.user?.get('name')} bought #{options.payment?.get('amount')} via #{options.payment?.get('service')}."
|
message = "#{options.user?.get('name')} bought #{options.payment?.get('amount')} via #{options.payment?.get('service')}."
|
||||||
hipchat.sendHipChatMessage message
|
hipchat.sendHipChatMessage message, ['tower']
|
||||||
catch e
|
catch e
|
||||||
log.error "Couldn't send HipChat message on payment because of error: #{e}"
|
log.error "Couldn't send HipChat message on payment because of error: #{e}"
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,6 @@ Handler = require '../commons/Handler'
|
||||||
{handlers} = require '../commons/mapping'
|
{handlers} = require '../commons/mapping'
|
||||||
mongoose = require 'mongoose'
|
mongoose = require 'mongoose'
|
||||||
log = require 'winston'
|
log = require 'winston'
|
||||||
sendwithus = require '../sendwithus'
|
|
||||||
hipchat = require '../hipchat'
|
|
||||||
|
|
||||||
PurchaseHandler = class PurchaseHandler extends Handler
|
PurchaseHandler = class PurchaseHandler extends Handler
|
||||||
modelClass: Purchase
|
modelClass: Purchase
|
||||||
|
@ -19,22 +17,22 @@ PurchaseHandler = class PurchaseHandler extends Handler
|
||||||
purchase.set 'recipient', req.user._id
|
purchase.set 'recipient', req.user._id
|
||||||
purchase.set 'created', new Date().toISOString()
|
purchase.set 'created', new Date().toISOString()
|
||||||
purchase
|
purchase
|
||||||
|
|
||||||
post: (req, res) ->
|
post: (req, res) ->
|
||||||
purchased = req.body.purchased
|
purchased = req.body.purchased
|
||||||
purchaser = req.user._id
|
purchaser = req.user._id
|
||||||
purchasedOriginal = purchased?.original
|
purchasedOriginal = purchased?.original
|
||||||
|
|
||||||
Handler = require '../commons/Handler'
|
Handler = require '../commons/Handler'
|
||||||
return @sendBadInputError(res) if not Handler.isID(purchasedOriginal)
|
return @sendBadInputError(res) if not Handler.isID(purchasedOriginal)
|
||||||
|
|
||||||
collection = purchased?.collection
|
collection = purchased?.collection
|
||||||
return @sendBadInputError(res) if not collection in @jsonSchema.properties.purchased.properties.collection.enum
|
return @sendBadInputError(res) if not collection in @jsonSchema.properties.purchased.properties.collection.enum
|
||||||
|
|
||||||
handler = require('../' + handlers[collection])
|
handler = require('../' + handlers[collection])
|
||||||
criteria = { 'original': purchasedOriginal }
|
criteria = { 'original': purchasedOriginal }
|
||||||
sort = { 'version.major': -1, 'version.minor': -1 }
|
sort = { 'version.major': -1, 'version.minor': -1 }
|
||||||
|
|
||||||
handler.modelClass.findOne(criteria).sort(sort).exec (err, purchasedItem) =>
|
handler.modelClass.findOne(criteria).sort(sort).exec (err, purchasedItem) =>
|
||||||
gemsOwned = req.user.get('earned')?.gems or 0
|
gemsOwned = req.user.get('earned')?.gems or 0
|
||||||
return @sendDatabaseError(res, err) if err
|
return @sendDatabaseError(res, err) if err
|
||||||
|
@ -51,7 +49,7 @@ PurchaseHandler = class PurchaseHandler extends Handler
|
||||||
if purchase
|
if purchase
|
||||||
@addPurchaseToUser(req, res)
|
@addPurchaseToUser(req, res)
|
||||||
return @sendSuccess(res, @formatEntity(req, purchase))
|
return @sendSuccess(res, @formatEntity(req, purchase))
|
||||||
|
|
||||||
else
|
else
|
||||||
super(req, res)
|
super(req, res)
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,7 @@ module.exports.setup = (app) ->
|
||||||
log.error(error.stack)
|
log.error(error.stack)
|
||||||
# TODO: Generally ignore this error: error: Error trying db method get route analytics.log.event from undefined: Error: Cannot find module '../undefined'
|
# TODO: Generally ignore this error: error: Error trying db method get route analytics.log.event from undefined: Error: Cannot find module '../undefined'
|
||||||
unless "#{parts}" in ['analytics.users.active']
|
unless "#{parts}" in ['analytics.users.active']
|
||||||
hipchat.sendTowerHipChatMessage errorMessage
|
hipchat.sendHipChatMessage errorMessage, ['tower'], papertrail: true
|
||||||
errors.notFound(res, "Route #{req?.path} not found.")
|
errors.notFound(res, "Route #{req?.path} not found.")
|
||||||
|
|
||||||
getSchema = (req, res, moduleName) ->
|
getSchema = (req, res, moduleName) ->
|
||||||
|
|
|
@ -249,7 +249,6 @@ UserHandler = class UserHandler extends Handler
|
||||||
|
|
||||||
sendOneTimeEmail: (req, res) ->
|
sendOneTimeEmail: (req, res) ->
|
||||||
# TODO: should this API be somewhere else?
|
# TODO: should this API be somewhere else?
|
||||||
# TODO: hipchat tower success message shows up as a misleading PaperTrail error message
|
|
||||||
return @sendForbiddenError(res) unless req.user
|
return @sendForbiddenError(res) unless req.user
|
||||||
email = req.query.email or req.body.email
|
email = req.query.email or req.body.email
|
||||||
type = req.query.type or req.body.type
|
type = req.query.type or req.body.type
|
||||||
|
@ -276,7 +275,7 @@ UserHandler = class UserHandler extends Handler
|
||||||
req.user.save (err) =>
|
req.user.save (err) =>
|
||||||
return @sendDatabaseError(res, err) if err
|
return @sendDatabaseError(res, err) if err
|
||||||
@sendSuccess(res, {result: 'success'})
|
@sendSuccess(res, {result: 'success'})
|
||||||
hipchat.sendTowerHipChatMessage "#{req.user.get('name') or req.user.get('email')} just submitted subscribe modal parent email #{email}."
|
hipchat.sendHipChatMessage "#{req.user.get('name') or req.user.get('email')} just submitted subscribe modal parent email #{email}.", ['tower']
|
||||||
AnalyticsLogEvent.logEvent req.user, 'Sent one time email', email: email, type: type
|
AnalyticsLogEvent.logEvent req.user, 'Sent one time email', email: email, type: type
|
||||||
|
|
||||||
agreeToCLA: (req, res) ->
|
agreeToCLA: (req, res) ->
|
||||||
|
@ -295,7 +294,7 @@ UserHandler = class UserHandler extends Handler
|
||||||
req.user.save (err) =>
|
req.user.save (err) =>
|
||||||
return @sendDatabaseError(res, err) if err
|
return @sendDatabaseError(res, err) if err
|
||||||
@sendSuccess(res, {result: 'success'})
|
@sendSuccess(res, {result: 'success'})
|
||||||
hipchat.sendHipChatMessage "#{req.body.githubUsername or req.user.get('name')} just signed the CLA."
|
hipchat.sendHipChatMessage "#{req.body.githubUsername or req.user.get('name')} just signed the CLA.", ['main']
|
||||||
|
|
||||||
avatar: (req, res, id) ->
|
avatar: (req, res, id) ->
|
||||||
@modelClass.findById(id).exec (err, document) =>
|
@modelClass.findById(id).exec (err, document) =>
|
||||||
|
|
|
@ -48,8 +48,11 @@ config.mail =
|
||||||
cronHandlerPublicIP: process.env.COCO_CRON_PUBLIC_IP or ''
|
cronHandlerPublicIP: process.env.COCO_CRON_PUBLIC_IP or ''
|
||||||
cronHandlerPrivateIP: process.env.COCO_CRON_PRIVATE_IP or ''
|
cronHandlerPrivateIP: process.env.COCO_CRON_PRIVATE_IP or ''
|
||||||
|
|
||||||
config.hipchatAPIKey = process.env.COCO_HIPCHAT_API_KEY or ''
|
config.hipchat =
|
||||||
config.hipchatTowerAPIKey = process.env.COCO_HIPCHAT_TOWER_API_KEY or ''
|
main: process.env.COCO_HIPCHAT_API_KEY or ''
|
||||||
|
tower: process.env.COCO_HIPCHAT_TOWER_API_KEY or ''
|
||||||
|
artisans: process.env.COCO_HIPCHAT_ARTISANS_API_KEY or ''
|
||||||
|
|
||||||
config.queue =
|
config.queue =
|
||||||
accessKeyId: process.env.COCO_AWS_ACCESS_KEY_ID or ''
|
accessKeyId: process.env.COCO_AWS_ACCESS_KEY_ID or ''
|
||||||
secretAccessKey: process.env.COCO_AWS_SECRET_ACCESS_KEY or ''
|
secretAccessKey: process.env.COCO_AWS_SECRET_ACCESS_KEY or ''
|
||||||
|
|
Loading…
Reference in a new issue