diff options
author | Wu Cheng-Han | 2017-01-12 17:18:24 +0800 |
---|---|---|
committer | Wu Cheng-Han | 2017-01-12 17:18:24 +0800 |
commit | 7e191acbde048b4ce274d1dbe49fb63a627a3def (patch) | |
tree | fcb57fc48a484b5520a5428460a4bf539f2a8ed3 /lib | |
parent | ffa14cfeefb9afcc6f177a9ecfe710ffb3227242 (diff) |
Fix author creation in operationCallback might cause unique constraint validation error
Diffstat (limited to 'lib')
-rw-r--r-- | lib/realtime.js | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/lib/realtime.js b/lib/realtime.js index a3c56c41..21390607 100644 --- a/lib/realtime.js +++ b/lib/realtime.js @@ -652,17 +652,25 @@ function operationCallback(socket, operation) { if (!user) return; userId = socket.request.user.id; if (!note.authors[userId]) { - models.Author.create({ - noteId: noteId, - userId: userId, - color: user.color - }).then(function (author) { - note.authors[author.userId] = { - userid: author.userId, - color: author.color, - photo: user.photo, - name: user.name - }; + models.Author.findOrCreate({ + where: { + noteId: noteId, + userId: userId + }, + defaults: { + noteId: noteId, + userId: userId, + color: user.color + } + }).spread(function (author, created) { + if (author) { + note.authors[author.userId] = { + userid: author.userId, + color: author.color, + photo: user.photo, + name: user.name + }; + } }).catch(function (err) { return logger.error('operation callback failed: ' + err); }); |