diff --git a/modules/services/osm.js b/modules/services/osm.js index 9afd91fb8..74e9a5cb8 100644 --- a/modules/services/osm.js +++ b/modules/services/osm.js @@ -861,7 +861,43 @@ export default { // Create a note // POST /api/0.6/notes?params postNoteCreate: function(note, callback) { - // todo + if (!this.authenticated()) { + return callback({ message: 'Not Authenticated', status: -3 }, note); + } + if (_noteCache.inflightPost[note.id]) { + return callback({ message: 'Note update already inflight', status: -2 }, note); + } + + if (!note.loc[0] || !note.loc[1] || !note.newComment) return; // location & description required + + var path = '/api/0.6/notes?' + + 'lat=' + note.loc[1] + + '&lon=' + note.loc[0] + + '&' + utilQsString({ text: note.newComment }); + _noteCache.inflightPost[note.id] = oauth.xhr( + { method: 'POST', path: path }, + wrapcb(this, done, _connectionID) + ); + + + function done(err, xml) { + delete _noteCache.inflightPost[note.id]; + if (err) { return callback(err); } + + // we get the updated note back, remove from caches and reparse.. + var item = encodeNoteRtree(note); + _noteCache.rtree.remove(item, function isEql(a, b) { return a.data.id === b.data.id; }); + delete _noteCache.note[note.id]; + + var options = { skipSeen: false }; + return parseXML(xml, function(err, results) { + if (err) { + return callback(err); + } else { + return callback(undefined, results[0]); + } + }, options); + } }, @@ -884,6 +920,7 @@ export default { action = 'reopen'; } else { action = 'comment'; + if (!note.newComment) return; // when commenting, comment required } var path = '/api/0.6/notes/' + note.id + '/' + action; diff --git a/modules/ui/note_editor.js b/modules/ui/note_editor.js index fd526076f..10c78ab9e 100644 --- a/modules/ui/note_editor.js +++ b/modules/ui/note_editor.js @@ -322,7 +322,7 @@ export function uiNoteEditor(context) { this.blur(); // avoid keeping focus on the button - #4641 var osm = services.osm; if (osm) { - osm.postNoteAdd(d, d.status, function(err, note) { + osm.postNoteCreate(d, function(err, note) { dispatch.call('change', note); }); }