2015-12-05 14:09:25 +08:00
|
|
|
{ComposerExtension} = require 'nylas-exports'
|
2015-10-03 07:06:56 +08:00
|
|
|
request = require 'request'
|
2015-12-24 09:25:30 +08:00
|
|
|
post = Promise.promisify(request.post, multiArgs: true)
|
2015-10-03 07:06:56 +08:00
|
|
|
|
2015-12-05 14:09:25 +08:00
|
|
|
class AvailabilityComposerExtension extends ComposerExtension
|
2015-10-03 07:06:56 +08:00
|
|
|
|
2015-12-05 14:09:25 +08:00
|
|
|
# When subclassing the ComposerExtension, you can add your own custom logic
|
2016-03-18 08:20:30 +08:00
|
|
|
# to execute before a draft is sent in the @applyTransformsToDraft
|
2015-10-03 07:06:56 +08:00
|
|
|
# method. Here, we're registering the events before we send the draft.
|
2016-03-18 08:20:30 +08:00
|
|
|
@applyTransformsToDraft: ({draft}) ->
|
|
|
|
matches = (/data-quick-schedule="(.*)?" style/).exec(draft.body)
|
|
|
|
return draft unless matches
|
|
|
|
|
|
|
|
json = atob(matches[1])
|
|
|
|
data = JSON.parse(json)
|
|
|
|
data.attendees = []
|
|
|
|
data.attendees = draft.participants().map (p) ->
|
|
|
|
name: p.name, email: p.email, isSender: p.isMe()
|
|
|
|
serverUrl = "https://quickschedule.herokuapp.com/register-events"
|
|
|
|
|
|
|
|
return post({
|
|
|
|
url: serverUrl,
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
})
|
|
|
|
.then (args) =>
|
|
|
|
if args[0].statusCode != 200
|
|
|
|
throw new Error()
|
|
|
|
data = args[1]
|
|
|
|
return Promise.resolve(draft)
|
|
|
|
.catch (error) ->
|
|
|
|
dialog = require('remote').require('dialog')
|
|
|
|
dialog.showErrorBox('Error creating QuickSchedule event',
|
|
|
|
"There was a problem connecting to the QuickSchedule server. Make sure you're connected to the internet and "+
|
|
|
|
"try sending again. If problems persist, contact the N1 team (using the blue question icon at the bottom right "+
|
|
|
|
"of your inbox) and we'll get right on it!")
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
|
|
|
@unapplyTransformsToDraft: ({draft}) ->
|
|
|
|
return draft
|
2015-10-03 07:06:56 +08:00
|
|
|
|
|
|
|
|
2015-12-05 14:09:25 +08:00
|
|
|
module.exports = AvailabilityComposerExtension
|