Skip to content
Snippets Groups Projects
Commit bc548e7f authored by Pierre H. Lehnen's avatar Pierre H. Lehnen Committed by Rodrigo Nascimento
Browse files

[FIX] Missing user data on files uploaded through the API (#10473)

* Added missing userId

* Fixed roomFiles method to return files without users too

* Added migration to add userId on orphan files

* Changed migration code to make sure the message exist before trying to use it's data
parent 6f2702df
No related branches found
No related tags found
No related merge requests found
......@@ -99,7 +99,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, () => {
......
......@@ -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) {
......
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);
if (message) {
const filter = {
_id: file._id
};
const update = {
$set: {
userId: message.u._id
}
};
RocketChat.models.Uploads.model.direct.update(filter, update);
}
});
}
}
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment