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

[NEW] Rest threads #14045

Merged
merged 6 commits into from
Apr 17, 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
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