Skip to content

Commit

Permalink
- do transforms locally
Browse files Browse the repository at this point in the history
- check plan on auth
  • Loading branch information
piascikj committed Mar 20, 2018
1 parent 525718c commit 37be6b4
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 21 deletions.
22 changes: 14 additions & 8 deletions lib/services/imdoneio-client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,15 @@ class ImdoneioClient extends Emitter

onAuthSuccess: (user, cb) ->
return cb null, user if @authenticated
@authenticated = true
@authRetryCount = 0
@emit 'authenticated'
@saveCredentials (err) =>
@storageAuthFailed = false
cb(null, user)
log 'onAuthSuccess'
@handlePushEvents()
@getPlan (err, @plan) =>
@authenticated = true
@authRetryCount = 0
@emit 'authenticated'
@saveCredentials (err) =>
@storageAuthFailed = false
cb(null, user)
log 'onAuthSuccess'
@handlePushEvents()

onAuthFailure: (err, res, cb) ->
status = err.imdone_status = if err && (err.code == 'ECONNREFUSED' || _.get(err, 'response.err.status') == 404) then 'unavailable' else 'failed'
Expand Down Expand Up @@ -285,6 +286,11 @@ class ImdoneioClient extends Emitter
getProjectName: (repo) -> _.get repo, 'config.sync.name'
setProjectName: (repo, name) -> _.set repo, 'config.sync.name', name

getPlan: (cb) ->
@doGet("/plan").end (err, res) =>
return cb(err, res) if err || !res.ok
cb(null, res.body)

getTransformableTasks: (tasks) ->
tasks.filter (task) =>
transformable = false
Expand Down
28 changes: 15 additions & 13 deletions lib/services/imdoneio-store.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = (repo) ->
Client = require('./imdoneio-client')
imdoneioClient = client = Client.instance
log = require('debug') 'imdoneio-store'
transform = require('./transform')
Task = require 'imdone-core/lib/task'
fs = require 'fs'
_ = require 'lodash'
Expand Down Expand Up @@ -86,20 +87,21 @@ module.exports = (repo) ->

repo.transformTasks = (tasks, cb) =>
return cb(null, repo.getTasks()) unless client.isAuthenticated()
return cb(null, repo.getTasks()) if !client.plan || client.plan.free
repo.pause()
client.transformTasks repo.config, tasks, (err, tasks) =>
async.mapSeries tasks, (task, cb) =>
repo.modifyTask task, false, (err, updatedTask) =>
return cb(null, updatedTask) unless err
cb err
, (err, results) ->
if err
repo.resume()
return cb err
repo.saveModifiedFiles (err) =>
repo.resume()
return cb err if err
cb(null, repo.getTasks())
transformed = transform.transformTasks repo.config, tasks
async.mapSeries transformed, (task, cb) =>
repo.modifyTask task, false, (err, updatedTask) =>
return cb(null, updatedTask) unless err
cb err
, (err, results) ->
if err
repo.resume()
return cb err
repo.saveModifiedFiles (err) =>
repo.resume()
return cb err if err
cb(null, repo.getTasks())

syncDone = (tasks) ->
return (err) ->
Expand Down
105 changes: 105 additions & 0 deletions lib/services/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const moment = require('moment')
const chrono = require('chrono-node')
const Task = require('imdone-core/lib/task')
const _ = require('lodash')

class Transformer {
constructor (opts) {
Object.assign(this, opts)
}

toJSON() {
const pattern = this.pattern && this.pattern.toString()
const clone = Object.assign({}, this)
delete clone.config
return Object.assign(clone,{pattern})
}

parseDate(text, ref) {
if (ref) ref = moment(ref)
const results = new chrono.parse(text, ref)
return moment(results[0].start.date())
}
}

const getTransformers = function (config) {
return [
new Transformer({
config,
name: "variable",
pattern: /\$(\w+)/,
vars: {
now: (instance) => instance.parseDate('now').format(),
today: (instance) => instance.parseDate('now').format('YYYY-MM-DD')
},
exec: function (task) {
return task.text.replace(this.pattern, (match, varName) => {
if (!this.vars[varName]) return varName
return this.vars[varName](this)
})
}
}),
new Transformer({
config,
name: "plain language due",
pattern: /due\s.*?\./i,
exec: function (task) {
return task.text.replace(this.pattern, (match) => {
const due = this.parseDate(match).format()
return `due:${due}`
})
}
}),
new Transformer({
config,
name: "plain language remind",
pattern: /remind me\s.*?(before)?\./i,
exec: function (task) {
return task.text.replace(this.pattern, (match, before) => {
let dueDate
if (before && task.meta.due) {
dueDate = task.meta.due[0]
match = match.replace('before', 'ago')
}
const remind = this.parseDate(match, dueDate).format()
return `remind:${remind}`
})
}
}),
new Transformer({
config,
name: "autocomplete",
exec: function (task) {
const list = _.get(this.config, 'transformers.autocomplete.list')
if (!list) return task.text
if (task.meta.completed) return task.text
if (task.list !== list) return task.text
task.addMetaData("completed", this.parseDate('now').format())
task.updateMetaData()
return task.text
},
pattern: /^((?!\scompleted:).)*$/,
list: _.get(config, 'transformers.autocomplete.list')
})
]
}

const transformTask = function (task, transformers) {
transformers.forEach(transformer => {
task.text = transformer.exec(task)
task.parseTodoTxt()
})
return task
}

const transformTasks = function (config, tasks) {
const transformers = getTransformers(config)
return tasks.map(task => transformTask(new Task(task), transformers))
}

module.exports = {
getTransformers,
transformTask,
transformTasks,
Transformer
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"async": "^1.5.2",
"atom-space-pen-views": "^2.0.5",
"chokidar": "^1.7.0",
"chrono-node": "^1.3.5",
"debug": "^2.2.0",
"engine.io": "^1.5.4",
"engine.io-client": "^1.6.4",
Expand Down

0 comments on commit 37be6b4

Please sign in to comment.