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

[FEATURE] Rest API upload file returns message object #13821

Merged
merged 8 commits into from
Oct 19, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 7 additions & 3 deletions app/api/server/v1/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Meteor } from 'meteor/meteor';
import Busboy from 'busboy';

import { FileUpload } from '../../../file-upload';
import { Rooms } from '../../../models';
import { Rooms, Messages } from '../../../models';
import { API } from '../api';

function findRoomByIdOrName({ params, checkedArchived = true }) {
Expand Down Expand Up @@ -73,7 +73,7 @@ API.v1.addRoute('rooms.upload/:rid', { authRequired: true }, {
Meteor.wrapAsync((callback) => {
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
if (fieldname !== 'file') {
return files.push(new Meteor.Error('invalid-field'));
return callback(new Meteor.Error('invalid-field'));
}

const fileDate = [];
Expand Down Expand Up @@ -111,6 +111,8 @@ API.v1.addRoute('rooms.upload/:rid', { authRequired: true }, {
userId: this.userId,
};

let fileData = {};

Meteor.runAsUser(this.userId, () => {
const uploadedFile = Meteor.wrapAsync(fileStore.insert.bind(fileStore))(details, file.fileBuffer);

Expand All @@ -119,9 +121,11 @@ API.v1.addRoute('rooms.upload/:rid', { authRequired: true }, {
delete fields.description;

API.v1.success(Meteor.call('sendFileMessage', this.urlParams.rid, null, uploadedFile, fields));

fileData = uploadedFile;
});

return API.v1.success();
return API.v1.success({ message: Messages.getMessageByFileIdAndUsername(fileData._id, this.userId) });
},
});

Expand Down
18 changes: 18 additions & 0 deletions app/models/server/models/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,24 @@ export class Messages extends Base {
return this.findOne({ 'file._id': fileID });
}

getMessageByFileIdAndUsername(fileID, userId) {
const query = {
'file._id': fileID,
'u._id': userId,
};

const options = {
fields: {
unread: 0,
mentions: 0,
channels: 0,
groupable: 0,
},
};

return this.findOne(query, options);
}

setAsRead(rid, until) {
return this.update({
rid,
Expand Down
67 changes: 67 additions & 0 deletions tests/end-to-end/api/09-rooms.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getCredentials, api, request, credentials } from '../../data/api-data.js';
import { password } from '../../data/user';
import { closeRoom, createRoom } from '../../data/rooms.helper';
import { imgURL } from '../../data/interactions.js';
import { updatePermission, updateSetting } from '../../data/permissions.helper';
import { sendSimpleMessage } from '../../data/chat.helper';

Expand Down Expand Up @@ -69,6 +70,72 @@ describe('[Rooms]', function() {
});
});

describe('/rooms.upload', () => {
let testChannel;
const testChannelName = `channel.test.upload.${ Date.now() }`;
it('create an channel', (done) => {
createRoom({ type: 'c', name: testChannelName })
.end((err, res) => {
testChannel = res.body.channel;
done();
});
});
it('don\'t upload a file to room with file field other than file', (done) => {
request.post(api(`rooms.upload/${ testChannel._id }`))
.set(credentials)
.attach('test', imgURL)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('error', '[invalid-field]');
expect(res.body).to.have.property('errorType', 'invalid-field');
})
.end(done);
});
it('don\'t upload a file to room with empty file', (done) => {
request.post(api(`rooms.upload/${ testChannel._id }`))
.set(credentials)
.attach('file', '')
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('error', res.body.error);
})
.end(done);
});
it('don\'t upload a file to room with more than 1 file', (done) => {
request.post(api(`rooms.upload/${ testChannel._id }`))
.set(credentials)
.attach('file', imgURL)
.attach('file', imgURL)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('error', 'Just 1 file is allowed');
})
.end(done);
});
it('upload a file to room', (done) => {
request.post(api(`rooms.upload/${ testChannel._id }`))
.set(credentials)
.attach('file', imgURL)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
const { message } = res.body;
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.nested.property('message._id', message._id);
expect(res.body).to.have.nested.property('message.rid', testChannel._id);
expect(res.body).to.have.nested.property('message.file._id', message.file._id);
expect(res.body).to.have.nested.property('message.file.type', message.file.type);
})
.end(done);
});
});

describe('/rooms.favorite', () => {
let testChannel;
const testChannelName = `channel.test.${ Date.now() }`;
Expand Down