From 313ded040f5a8e4e1bec7956b9e9902696899da9 Mon Sep 17 00:00:00 2001 From: Hudell Date: Mon, 16 Apr 2018 17:18:13 -0300 Subject: [PATCH 1/4] Added missing userId --- packages/rocketchat-api/server/v1/rooms.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/rocketchat-api/server/v1/rooms.js b/packages/rocketchat-api/server/v1/rooms.js index 3dd8eebc4d9c..1afcf2a313e4 100644 --- a/packages/rocketchat-api/server/v1/rooms.js +++ b/packages/rocketchat-api/server/v1/rooms.js @@ -76,7 +76,8 @@ RocketChat.API.v1.addRoute('rooms.upload/:rid', { authRequired: true }, { name: file.filename, size: file.fileBuffer.length, type: file.mimetype, - rid: this.urlParams.rid + rid: this.urlParams.rid, + userId: this.userId }; Meteor.runAsUser(this.userId, () => { From 0a2e18b9abcf6cf5a857f93b91a441fe993d82db Mon Sep 17 00:00:00 2001 From: Hudell Date: Mon, 16 Apr 2018 17:56:33 -0300 Subject: [PATCH 2/4] Fixed roomFiles method to return files without users too --- server/publications/roomFiles.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/publications/roomFiles.js b/server/publications/roomFiles.js index f19e6c560b24..dc500d16ac1d 100644 --- a/server/publications/roomFiles.js +++ b/server/publications/roomFiles.js @@ -7,11 +7,11 @@ Meteor.publish('roomFiles', function(rid, limit = 50) { const cursorFileListHandle = RocketChat.models.Uploads.findNotHiddenFilesOfRoom(rid, limit).observeChanges({ added(_id, record) { - const {username, name} = RocketChat.models.Users.findOneById(record.userId); + const {username, name} = record.userId ? RocketChat.models.Users.findOneById(record.userId) : {}; return pub.added('room_files', _id, {...record, user:{username, name}}); }, changed(_id, record) { - const {username, name} = RocketChat.models.Users.findOneById(record.userId); + const {username, name} = record.userId ? RocketChat.models.Users.findOneById(record.userId) : {}; return pub.changed('room_files', _id, {...record, user:{username, name}}); }, removed(_id, record) { From 979fa775fdcd258b868cc974901703babdf46ebb Mon Sep 17 00:00:00 2001 From: Hudell Date: Fri, 20 Apr 2018 15:49:13 -0300 Subject: [PATCH 3/4] Added migration to add userId on orphan files --- server/startup/migrations/v113.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 server/startup/migrations/v113.js diff --git a/server/startup/migrations/v113.js b/server/startup/migrations/v113.js new file mode 100644 index 000000000000..30512e6f6335 --- /dev/null +++ b/server/startup/migrations/v113.js @@ -0,0 +1,29 @@ +RocketChat.Migrations.add({ + version: 113, + up() { + if (RocketChat && RocketChat.models && RocketChat.models.Uploads && RocketChat.models.Messages) { + const fileQuery = { + userId: null + }; + + const filesToUpdate = RocketChat.models.Uploads.find(fileQuery); + filesToUpdate.forEach((file) => { + const messageQuery = { + 'file._id' : file._id + }; + const message = RocketChat.models.Messages.findOne(messageQuery); + const filter = { + _id: file._id + }; + + const update = { + $set: { + userId: message.u._id + } + }; + + RocketChat.models.Uploads.model.direct.update(filter, update); + }); + } + } +}); From 703adca13bf7dfccaa16731ffe50d4511fa85373 Mon Sep 17 00:00:00 2001 From: Hudell Date: Fri, 20 Apr 2018 16:56:40 -0300 Subject: [PATCH 4/4] Changed migration code to make sure the message exist before trying to use it's data --- server/startup/migrations/v113.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/server/startup/migrations/v113.js b/server/startup/migrations/v113.js index 30512e6f6335..dafc3166ec8e 100644 --- a/server/startup/migrations/v113.js +++ b/server/startup/migrations/v113.js @@ -12,17 +12,19 @@ RocketChat.Migrations.add({ 'file._id' : file._id }; const message = RocketChat.models.Messages.findOne(messageQuery); - const filter = { - _id: file._id - }; + if (message) { + const filter = { + _id: file._id + }; - const update = { - $set: { - userId: message.u._id - } - }; + const update = { + $set: { + userId: message.u._id + } + }; - RocketChat.models.Uploads.model.direct.update(filter, update); + RocketChat.models.Uploads.model.direct.update(filter, update); + } }); } }