Skip to content

Commit

Permalink
[BREAK] Update lastMessage rooms property and convert the "starred"…
Browse files Browse the repository at this point in the history
… property, to the same format (#12266)

* Update room last message object with reaction, star, pin and snippet

* Add same field format: "starred" in all responses that includes "messages" or "lastMessage" objects

* Add tests cases for message format
  • Loading branch information
MarcosSpessatto authored and sampaiodiego committed Oct 20, 2018
1 parent cd86591 commit 63f7e8f
Show file tree
Hide file tree
Showing 26 changed files with 541 additions and 106 deletions.
1 change: 1 addition & 0 deletions packages/rocketchat-api/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Package.onUse(function(api) {
api.addFiles('server/settings.js', 'server');

// Register helpers
api.addFiles('server/helpers/composeRoomWithLastMessage.js', 'server');
api.addFiles('server/helpers/requestParams.js', 'server');
api.addFiles('server/helpers/getPaginationItems.js', 'server');
api.addFiles('server/helpers/getUserFromParams.js', 'server');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
RocketChat.API.helperMethods.set('composeRoomWithLastMessage', function _composeRoomWithLastMessage(room, userId) {
if (room.lastMessage) {
room.lastMessage = RocketChat.composeMessageObjectWithUser(room.lastMessage, userId);
}
return room;
});
55 changes: 27 additions & 28 deletions packages/rocketchat-api/server/v1/channels.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'underscore';

// Returns the channel IF found otherwise it will return the failure of why it didn't. Check the `statusCode` property
function findChannelByIdOrName({ params, checkedArchived = true }) {
function findChannelByIdOrName({ params, checkedArchived = true, userId }) {
if ((!params.roomId || !params.roomId.trim()) && (!params.roomName || !params.roomName.trim())) {
throw new Meteor.Error('error-roomid-param-not-provided', 'The parameter "roomId" or "roomName" is required');
}
Expand All @@ -22,6 +22,9 @@ function findChannelByIdOrName({ params, checkedArchived = true }) {
if (checkedArchived && room.archived) {
throw new Meteor.Error('error-room-archived', `The channel, ${ room.name }, is archived`);
}
if (userId && room.lastMessage) {
room.lastMessage = RocketChat.composeMessageObjectWithUser(room.lastMessage, userId);
}

return room;
}
Expand All @@ -35,7 +38,7 @@ RocketChat.API.v1.addRoute('channels.addAll', { authRequired: true }, {
});

return RocketChat.API.v1.success({
channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
channel: findChannelByIdOrName({ params: this.requestParams(), userId: this.userId }),
});
},
});
Expand Down Expand Up @@ -175,14 +178,10 @@ function createChannelValidator(params) {

function createChannel(userId, params) {
const readOnly = typeof params.readOnly !== 'undefined' ? params.readOnly : false;

let id;
Meteor.runAsUser(userId, () => {
id = Meteor.call('createChannel', params.name, params.members ? params.members : [], readOnly, params.customFields);
});
const id = Meteor.runAsUser(userId, () => Meteor.call('createChannel', params.name, params.members ? params.members : [], readOnly, params.customFields));

return {
channel: RocketChat.models.Rooms.findOneById(id.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
channel: findChannelByIdOrName({ params: { roomId: id.rid }, userId: this.userId }),
};
}

Expand Down Expand Up @@ -236,9 +235,7 @@ RocketChat.API.v1.addRoute('channels.delete', { authRequired: true }, {
Meteor.call('eraseRoom', findResult._id);
});

return RocketChat.API.v1.success({
channel: findResult,
});
return RocketChat.API.v1.success();
},
});

Expand Down Expand Up @@ -367,10 +364,12 @@ RocketChat.API.v1.addRoute('channels.history', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.info', { authRequired: true }, {
get() {
const findResult = findChannelByIdOrName({ params: this.requestParams(), checkedArchived: false });

return RocketChat.API.v1.success({
channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
channel: findChannelByIdOrName({
params: this.requestParams(),
checkedArchived: false,
userId: this.userId,
}),
});
},
});
Expand All @@ -386,7 +385,7 @@ RocketChat.API.v1.addRoute('channels.invite', { authRequired: true }, {
});

return RocketChat.API.v1.success({
channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
channel: findChannelByIdOrName({ params: this.requestParams(), userId: this.userId }),
});
},
});
Expand All @@ -400,7 +399,7 @@ RocketChat.API.v1.addRoute('channels.join', { authRequired: true }, {
});

return RocketChat.API.v1.success({
channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
channel: findChannelByIdOrName({ params: this.requestParams(), userId: this.userId }),
});
},
});
Expand All @@ -416,7 +415,7 @@ RocketChat.API.v1.addRoute('channels.kick', { authRequired: true }, {
});

return RocketChat.API.v1.success({
channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
channel: findChannelByIdOrName({ params: this.requestParams(), userId: this.userId }),
});
},
});
Expand All @@ -430,7 +429,7 @@ RocketChat.API.v1.addRoute('channels.leave', { authRequired: true }, {
});

return RocketChat.API.v1.success({
channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
channel: findChannelByIdOrName({ params: this.requestParams(), userId: this.userId }),
});
},
});
Expand Down Expand Up @@ -465,7 +464,7 @@ RocketChat.API.v1.addRoute('channels.list', { authRequired: true }, {
const rooms = cursor.fetch();

return RocketChat.API.v1.success({
channels: rooms,
channels: rooms.map((room) => this.composeRoomWithLastMessage(room, this.userId)),
count: rooms.length,
offset,
total,
Expand All @@ -491,7 +490,7 @@ RocketChat.API.v1.addRoute('channels.list.joined', { authRequired: true }, {
const rooms = cursor.fetch();

return RocketChat.API.v1.success({
channels: rooms,
channels: rooms.map((room) => this.composeRoomWithLastMessage(room, this.userId)),
offset,
count: rooms.length,
total: totalCount,
Expand Down Expand Up @@ -526,7 +525,7 @@ RocketChat.API.v1.addRoute('channels.members', { authRequired: true }, {

const users = RocketChat.models.Users.find({ _id: { $in: members } }, {
fields: { _id: 1, username: 1, name: 1, status: 1, utcOffset: 1 },
sort: { username: sort.username != null ? sort.username : 1 },
sort: { username: sort.username != null ? sort.username : 1 },
}).fetch();

return RocketChat.API.v1.success({
Expand Down Expand Up @@ -568,7 +567,7 @@ RocketChat.API.v1.addRoute('channels.messages', { authRequired: true }, {
const messages = cursor.fetch();

return RocketChat.API.v1.success({
messages,
messages: messages.map((record) => RocketChat.composeMessageObjectWithUser(record, this.userId)),
count: messages.length,
offset,
total,
Expand Down Expand Up @@ -703,7 +702,7 @@ RocketChat.API.v1.addRoute('channels.rename', { authRequired: true }, {
});

return RocketChat.API.v1.success({
channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
channel: findChannelByIdOrName({ params: { roomId: this.bodyParams.roomId }, userId: this.userId }),
});
},
});
Expand All @@ -721,7 +720,7 @@ RocketChat.API.v1.addRoute('channels.setCustomFields', { authRequired: true }, {
});

return RocketChat.API.v1.success({
channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
channel: findChannelByIdOrName({ params: this.requestParams(), userId: this.userId }),
});
},
});
Expand All @@ -743,7 +742,7 @@ RocketChat.API.v1.addRoute('channels.setDefault', { authRequired: true }, {
});

return RocketChat.API.v1.success({
channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
channel: findChannelByIdOrName({ params: this.requestParams(), userId: this.userId }),
});
},
});
Expand Down Expand Up @@ -783,7 +782,7 @@ RocketChat.API.v1.addRoute('channels.setJoinCode', { authRequired: true }, {
});

return RocketChat.API.v1.success({
channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
channel: findChannelByIdOrName({ params: this.requestParams(), userId: this.userId }),
});
},
});
Expand Down Expand Up @@ -827,7 +826,7 @@ RocketChat.API.v1.addRoute('channels.setReadOnly', { authRequired: true }, {
});

return RocketChat.API.v1.success({
channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
channel: findChannelByIdOrName({ params: this.requestParams(), userId: this.userId }),
});
},
});
Expand Down Expand Up @@ -889,7 +888,7 @@ RocketChat.API.v1.addRoute('channels.setType', { authRequired: true }, {
});

return RocketChat.API.v1.success({
channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
channel: this.composeRoomWithLastMessage(RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }), this.userId),
});
},
});
Expand Down
17 changes: 10 additions & 7 deletions packages/rocketchat-api/server/v1/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ RocketChat.API.v1.addRoute('chat.syncMessages', { authRequired: true }, {
}

return RocketChat.API.v1.success({
result,
result: {
updated: result.updated.map((message) => RocketChat.composeMessageObjectWithUser(message, this.userId)),
deleted: result.deleted.map((message) => RocketChat.composeMessageObjectWithUser(message, this.userId)),
},
});
},
});
Expand All @@ -79,7 +82,7 @@ RocketChat.API.v1.addRoute('chat.getMessage', { authRequired: true }, {
}

return RocketChat.API.v1.success({
message: msg,
message: RocketChat.composeMessageObjectWithUser(msg, this.userId),
});
},
});
Expand All @@ -100,7 +103,7 @@ RocketChat.API.v1.addRoute('chat.pinMessage', { authRequired: true }, {
Meteor.runAsUser(this.userId, () => pinnedMessage = Meteor.call('pinMessage', msg));

return RocketChat.API.v1.success({
message: pinnedMessage,
message: RocketChat.composeMessageObjectWithUser(pinnedMessage, this.userId),
});
},
});
Expand All @@ -116,7 +119,7 @@ RocketChat.API.v1.addRoute('chat.postMessage', { authRequired: true }, {
return RocketChat.API.v1.success({
ts: Date.now(),
channel: messageReturn.channel,
message: messageReturn.message,
message: RocketChat.composeMessageObjectWithUser(messageReturn.message, this.userId),
});
},
});
Expand All @@ -138,7 +141,7 @@ RocketChat.API.v1.addRoute('chat.search', { authRequired: true }, {
Meteor.runAsUser(this.userId, () => result = Meteor.call('messageSearch', searchText, roomId, count).message.docs);

return RocketChat.API.v1.success({
messages: result,
messages: result.map((message) => RocketChat.composeMessageObjectWithUser(message, this.userId)),
});
},
});
Expand All @@ -156,7 +159,7 @@ RocketChat.API.v1.addRoute('chat.sendMessage', { authRequired: true }, {
Meteor.runAsUser(this.userId, () => message = Meteor.call('sendMessage', this.bodyParams.message));

return RocketChat.API.v1.success({
message,
message: RocketChat.composeMessageObjectWithUser(message, this.userId),
});
},
});
Expand Down Expand Up @@ -248,7 +251,7 @@ RocketChat.API.v1.addRoute('chat.update', { authRequired: true }, {
});

return RocketChat.API.v1.success({
message: RocketChat.models.Messages.findOneById(msg._id),
message: RocketChat.composeMessageObjectWithUser(RocketChat.models.Messages.findOneById(msg._id), this.userId),
});
},
});
Expand Down
26 changes: 12 additions & 14 deletions packages/rocketchat-api/server/v1/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ RocketChat.API.v1.addRoute('groups.addAll', { authRequired: true }, {
});

return RocketChat.API.v1.success({
group: RocketChat.models.Rooms.findOneById(findResult.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
group: this.composeRoomWithLastMessage(RocketChat.models.Rooms.findOneById(findResult.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }), this.userId),
});
},
});
Expand Down Expand Up @@ -202,7 +202,7 @@ RocketChat.API.v1.addRoute('groups.create', { authRequired: true }, {
});

return RocketChat.API.v1.success({
group: RocketChat.models.Rooms.findOneById(id.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
group: this.composeRoomWithLastMessage(RocketChat.models.Rooms.findOneById(id.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }), this.userId),
});
},
});
Expand All @@ -215,9 +215,7 @@ RocketChat.API.v1.addRoute('groups.delete', { authRequired: true }, {
Meteor.call('eraseRoom', findResult.rid);
});

return RocketChat.API.v1.success({
group: RocketChat.models.Rooms.findOneById(findResult.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
});
return RocketChat.API.v1.success();
},
});

Expand Down Expand Up @@ -331,7 +329,7 @@ RocketChat.API.v1.addRoute('groups.info', { authRequired: true }, {
const findResult = findPrivateGroupByIdOrName({ params: this.requestParams(), userId: this.userId, checkedArchived: false });

return RocketChat.API.v1.success({
group: RocketChat.models.Rooms.findOneById(findResult.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
group: this.composeRoomWithLastMessage(RocketChat.models.Rooms.findOneById(findResult.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }), this.userId),
});
},
});
Expand All @@ -355,7 +353,7 @@ RocketChat.API.v1.addRoute('groups.invite', { authRequired: true }, {
Meteor.runAsUser(this.userId, () => Meteor.call('addUserToRoom', { rid, username }));

return RocketChat.API.v1.success({
group: RocketChat.models.Rooms.findOneById(rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
group: this.composeRoomWithLastMessage(RocketChat.models.Rooms.findOneById(rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }), this.userId),
});
},
});
Expand Down Expand Up @@ -405,7 +403,7 @@ RocketChat.API.v1.addRoute('groups.list', { authRequired: true }, {


return RocketChat.API.v1.success({
groups: rooms,
groups: rooms.map((room) => this.composeRoomWithLastMessage(room, this.userId)),
offset,
count: rooms.length,
total: totalCount,
Expand Down Expand Up @@ -434,7 +432,7 @@ RocketChat.API.v1.addRoute('groups.listAll', { authRequired: true }, {
});

return RocketChat.API.v1.success({
groups: rooms,
groups: rooms.map((room) => this.composeRoomWithLastMessage(room, this.userId)),
offset,
count: rooms.length,
total: totalCount,
Expand Down Expand Up @@ -495,7 +493,7 @@ RocketChat.API.v1.addRoute('groups.messages', { authRequired: true }, {
}).fetch();

return RocketChat.API.v1.success({
messages,
messages: messages.map((message) => RocketChat.composeMessageObjectWithUser(message, this.userId)),
count: messages.length,
offset,
total: RocketChat.models.Messages.find(ourQuery).count(),
Expand Down Expand Up @@ -608,7 +606,7 @@ RocketChat.API.v1.addRoute('groups.rename', { authRequired: true }, {
});

return RocketChat.API.v1.success({
group: RocketChat.models.Rooms.findOneById(findResult.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
group: this.composeRoomWithLastMessage(RocketChat.models.Rooms.findOneById(findResult.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }), this.userId),
});
},
});
Expand All @@ -626,7 +624,7 @@ RocketChat.API.v1.addRoute('groups.setCustomFields', { authRequired: true }, {
});

return RocketChat.API.v1.success({
group: RocketChat.models.Rooms.findOneById(findResult.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
group: this.composeRoomWithLastMessage(RocketChat.models.Rooms.findOneById(findResult.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }), this.userId),
});
},
});
Expand Down Expand Up @@ -684,7 +682,7 @@ RocketChat.API.v1.addRoute('groups.setReadOnly', { authRequired: true }, {
});

return RocketChat.API.v1.success({
group: RocketChat.models.Rooms.findOneById(findResult.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
group: this.composeRoomWithLastMessage(RocketChat.models.Rooms.findOneById(findResult.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }), this.userId),
});
},
});
Expand Down Expand Up @@ -724,7 +722,7 @@ RocketChat.API.v1.addRoute('groups.setType', { authRequired: true }, {
});

return RocketChat.API.v1.success({
group: RocketChat.models.Rooms.findOneById(findResult.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
group: this.composeRoomWithLastMessage(RocketChat.models.Rooms.findOneById(findResult.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude }), this.userId),
});
},
});
Expand Down
Loading

0 comments on commit 63f7e8f

Please sign in to comment.