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] code to get the updated messages #8857

Merged
merged 2 commits into from
Nov 16, 2017
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
25 changes: 25 additions & 0 deletions packages/rocketchat-api/server/v1/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,31 @@ RocketChat.API.v1.addRoute('chat.delete', { authRequired: true }, {
}
});

RocketChat.API.v1.addRoute('chat.syncMessages', { authRequired: true }, {
get() {
const { rid } = this.queryParams;
let lastUpdate = this.queryParams;
lastUpdate = lastUpdate ? new Date(lastUpdate) : lastUpdate;
if (!rid) {
return RocketChat.API.v1.failure('The "rid" query parameter must be provided.');
}
if (!lastUpdate) {
return RocketChat.API.v1.failure('The "lastUpdate" query parameter must be provided.');
}

let result;
Meteor.runAsUser(this.userId, () => {
result = Meteor.call('messages/get', rid, { lastUpdate });
});

if (!result) {
return RocketChat.API.v1.failure();
}

return RocketChat.API.v1.success({result});
}
});

RocketChat.API.v1.addRoute('chat.getMessage', { authRequired: true }, {
get() {
if (!this.queryParams.msgId) {
Expand Down
13 changes: 13 additions & 0 deletions packages/rocketchat-lib/server/models/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base {
return this.find(query, options);
}

findForUpdates(roomId, timestamp, options) {
const query = {
_hidden: {
$ne: true
},
rid: roomId,
_updatedAt: {
$gt: timestamp
}
};
return this.find(query, options);
}

findVisibleByRoomIdBeforeTimestamp(roomId, timestamp, options) {
const query = {
_hidden: {
Expand Down
36 changes: 36 additions & 0 deletions server/publications/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,39 @@ Meteor.publish('messages', function(rid/*, start*/) {
return cursorDeleteHandle.stop();
});
});

Meteor.methods({
'messages/get'(rid, {lastUpdate, latestDate = new Date(), oldestDate, inclusive = false, count = 20, unreads= false}) {
check(rid, String);

const fromId = Meteor.userId();

if (!fromId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'messages/get'
});
}

if (!Meteor.call('canAccessRoom', rid, fromId)) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
method: 'messages/get'
});
}

const options = {
sort: {
ts: -1
}
};

if (lastUpdate instanceof Date) {
return {
updated: RocketChat.models.Messages.findForUpdates(rid, lastUpdate, options).fetch(),
deleted: RocketChat.models.Messages.trashFindDeletedAfter(lastUpdate, {rid}, { ...options, fields: { _id: 1, _deletedAt: 1 }}).fetch()
};
}

return Meteor.call('getChannelHistory', { rid, latest: latestDate, oldest: oldestDate, inclusive, count, unreads });

}
});