Skip to content

Commit

Permalink
[NEW] Rest threads (#14045)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosSpessatto authored and rodrigok committed Apr 17, 2019
1 parent f6f507d commit 05c14e8
Show file tree
Hide file tree
Showing 4 changed files with 872 additions and 8 deletions.
137 changes: 136 additions & 1 deletion app/api/server/v1/chat.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { Messages } from '../../../models';
import { hasPermission } from '../../../authorization';
import { canAccessRoom, hasPermission } from '../../../authorization';
import { composeMessageObjectWithUser } from '../../../utils';
import { processWebhookMessage } from '../../../lib';
import { API } from '../api';
import Rooms from '../../../models/server/models/Rooms';
import Users from '../../../models/server/models/Users';
import { settings } from '../../../settings';

API.v1.addRoute('chat.delete', { authRequired: true }, {
post() {
Expand Down Expand Up @@ -378,3 +381,135 @@ API.v1.addRoute('chat.getDeletedMessages', { authRequired: true }, {
});
},
});

API.v1.addRoute('chat.getThreadsList', { authRequired: true }, {
get() {
const { rid } = this.queryParams;
const { offset, count } = this.getPaginationItems();

if (!rid) {
throw new Meteor.Error('The required "rid" query param is missing.');
}
const threads = Meteor.runAsUser(this.userId, () => Meteor.call('getThreadsList', {
rid,
limit: count,
skip: offset,
}));
return API.v1.success({ threads });
},
});

API.v1.addRoute('chat.syncThreadsList', { authRequired: true }, {
get() {
const { rid } = this.queryParams;
const { query, fields, sort } = this.parseJsonQuery();
const { updatedSince } = this.queryParams;
let updatedSinceDate;
if (!settings.get('Threads_enabled')) {
throw new Meteor.Error('error-not-allowed', 'Threads Disabled');
}
if (!rid) {
throw new Meteor.Error('error-room-id-param-not-provided', 'The required "rid" query param is missing.');
}
if (!updatedSince) {
throw new Meteor.Error('error-updatedSince-param-invalid', 'The required param "updatedSince" is missing.');
}
if (isNaN(Date.parse(updatedSince))) {
throw new Meteor.Error('error-updatedSince-param-invalid', 'The "updatedSince" query parameter must be a valid date.');
} else {
updatedSinceDate = new Date(updatedSince);
}
const user = Users.findOneById(this.userId, { fields: { _id: 1 } });
const room = Rooms.findOneById(rid, { fields: { t: 1, _id: 1 } });
if (!canAccessRoom(room, user)) {
throw new Meteor.Error('error-not-allowed', 'Not Allowed');
}
const threadQuery = Object.assign({}, query, { rid, tcount: { $exists: true } });
return API.v1.success({
threads: {
update: Messages.find({ ...threadQuery, _updatedAt: { $gt: updatedSinceDate } }, { fields, sort }).fetch(),
remove: Messages.trashFindDeletedAfter(updatedSinceDate, threadQuery, { fields, sort }).fetch(),
},
});
},
});

API.v1.addRoute('chat.getThreadMessages', { authRequired: true }, {
get() {
const { tmid } = this.queryParams;
const { offset, count } = this.getPaginationItems();

if (!tmid) {
throw new Meteor.Error('The required "tmid" query param is missing.');
}
const messages = Meteor.runAsUser(this.userId, () => Meteor.call('getThreadMessages', {
tmid,
limit: count,
skip: offset,
}));
return API.v1.success({ messages });
},
});

API.v1.addRoute('chat.syncThreadMessages', { authRequired: true }, {
get() {
const { tmid } = this.queryParams;
const { query, fields, sort } = this.parseJsonQuery();
const { updatedSince } = this.queryParams;
let updatedSinceDate;
if (!settings.get('Threads_enabled')) {
throw new Meteor.Error('error-not-allowed', 'Threads Disabled');
}
if (!tmid) {
throw new Meteor.Error('error-invalid-params', 'The required "tmid" query param is missing.');
}
if (!updatedSince) {
throw new Meteor.Error('error-updatedSince-param-invalid', 'The required param "updatedSince" is missing.');
}
if (isNaN(Date.parse(updatedSince))) {
throw new Meteor.Error('error-updatedSince-param-invalid', 'The "updatedSince" query parameter must be a valid date.');
} else {
updatedSinceDate = new Date(updatedSince);
}
const thread = Messages.findOneById(tmid, { fields: { rid: 1 } });
if (!thread.rid) {
throw new Meteor.Error('error-invalid-message', 'Invalid Message');
}
const user = Users.findOneById(this.userId, { fields: { _id: 1 } });
const room = Rooms.findOneById(thread.rid, { fields: { t: 1, _id: 1 } });

if (!canAccessRoom(room, user)) {
throw new Meteor.Error('error-not-allowed', 'Not Allowed');
}
return API.v1.success({
messages: {
update: Messages.find({ ...query, tmid, _updatedAt: { $gt: updatedSinceDate } }, { fields, sort }).fetch(),
remove: Messages.trashFindDeletedAfter(updatedSinceDate, { ...query, tmid }, { fields, sort }).fetch(),
},
});
},
});

API.v1.addRoute('chat.followMessage', { authRequired: true }, {
post() {
const { mid } = this.bodyParams;

if (!mid) {
throw new Meteor.Error('The required "mid" body param is missing.');
}
Meteor.runAsUser(this.userId, () => Meteor.call('followMessage', { mid }));
return API.v1.success();
},
});

API.v1.addRoute('chat.unfollowMessage', { authRequired: true }, {
post() {
const { mid } = this.bodyParams;

if (!mid) {
throw new Meteor.Error('The required "mid" body param is missing.');
}
Meteor.runAsUser(this.userId, () => Meteor.call('unfollowMessage', { mid }));
return API.v1.success();
},
});
16 changes: 9 additions & 7 deletions tests/data/chat.helper.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { api, credentials, request } from './api-data';

export const sendSimpleMessage = ({ roomId, text = 'test message' }) => {
export const sendSimpleMessage = ({ roomId, text = 'test message', tmid }) => {
if (!roomId) {
throw new Error('"roomId" is required in "sendSimpleMessage" test helper');
}
const message = {
rid: roomId,
text,
};
if (tmid) {
message.tmid = tmid;
}

return request.post(api('chat.sendMessage'))
.set(credentials)
.send({
message: {
rid: roomId,
text,
},
});
.send({ message });
};

export const deleteMessage = ({ roomId, msgId }) => {
Expand Down
25 changes: 25 additions & 0 deletions tests/data/users.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { api, credentials, request } from './api-data';
import { password } from './user';

export const createUser = () => new Promise((resolve) => {
const username = `user.test.${ Date.now() }`;
const email = `${ username }@rocket.chat`;
request.post(api('users.create'))
.set(credentials)
.send({ email, name: username, username, password })
.end((err, res) => resolve(res.body.user));
});

export const login = (username, password) => new Promise((resolve) => {
request.post(api('login'))
.send({
user: username,
password,
})
.end((err, res) => {
const userCredentials = {};
userCredentials['X-Auth-Token'] = res.body.data.authToken;
userCredentials['X-User-Id'] = res.body.data.userId;
resolve(userCredentials);
});
});
Loading

0 comments on commit 05c14e8

Please sign in to comment.