Skip to content

Commit 7e191ac

Browse files
committed
Fix author creation in operationCallback might cause unique constraint validation error
1 parent ffa14cf commit 7e191ac

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

lib/realtime.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -652,17 +652,25 @@ function operationCallback(socket, operation) {
652652
if (!user) return;
653653
userId = socket.request.user.id;
654654
if (!note.authors[userId]) {
655-
models.Author.create({
656-
noteId: noteId,
657-
userId: userId,
658-
color: user.color
659-
}).then(function (author) {
660-
note.authors[author.userId] = {
661-
userid: author.userId,
662-
color: author.color,
663-
photo: user.photo,
664-
name: user.name
665-
};
655+
models.Author.findOrCreate({
656+
where: {
657+
noteId: noteId,
658+
userId: userId
659+
},
660+
defaults: {
661+
noteId: noteId,
662+
userId: userId,
663+
color: user.color
664+
}
665+
}).spread(function (author, created) {
666+
if (author) {
667+
note.authors[author.userId] = {
668+
userid: author.userId,
669+
color: author.color,
670+
photo: user.photo,
671+
name: user.name
672+
};
673+
}
666674
}).catch(function (err) {
667675
return logger.error('operation callback failed: ' + err);
668676
});

0 commit comments

Comments
 (0)