Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/rocketchat-api/server/v1/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down
4 changes: 2 additions & 2 deletions server/publications/roomFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 29 additions & 0 deletions server/startup/migrations/v113.js
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you test if the message's object exists to prevent an exception here?

}
};

RocketChat.models.Uploads.model.direct.update(filter, update);
});
}
}
});