Skip to content

Commit

Permalink
Merge pull request RocketChat#282 from assistify/feature/RocketChat#280
Browse files Browse the repository at this point in the history
…-thread-navigation

Rename-safe navigation in threads + attachment support
  • Loading branch information
mrsimpson authored Mar 22, 2018
2 parents fcbb88b + 8bfeeed commit 5bcb7e2
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 888 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export class CreateRequestBase {
}
});
}
_postMessage(room, user, msg) {
const msgObject = {_id: Random.id(), rid: room.rid, msg};
return RocketChat.sendMessage(user, msgObject, room);
_postMessage(room, user, message, attachments) {
const newMessage = { _id: Random.id(), rid: room.rid, msg: message, attachments: attachments || [] };
return RocketChat.sendMessage(user, newMessage, room);
}

//abstract method must be implemented.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RocketChat } from 'meteor/rocketchat:lib';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { CreateRequestFactory } from './createRequestFactory';
import { CreateRequestBase} from './createRequestBase';
import { CreateRequestBase } from './createRequestBase';

/*
* When a message is eligible to be answered as a independent question then it can be threaded into a new channel.
Expand All @@ -15,9 +15,7 @@ export class CreateRequestFromRoomId extends CreateRequestBase {
}

_getMessageUrl(msgId) {
const message = RocketChat.models.Messages.findOneById(msgId);
const room = RocketChat.models.Rooms.findOneById(message.rid);
return FlowRouter.path(RocketChat.roomTypes.getRouteLink(room.t, room), '', {msg: msgId});
return FlowRouter.path('message', { id: msgId });
}

_linkMessages(roomCreated, parentRoom) {
Expand All @@ -37,7 +35,14 @@ export class CreateRequestFromRoomId extends CreateRequestBase {
_id: Meteor.user()._id, // Thread Initiator
name: Meteor.user().username
}]
});
}
);

// The follwing lines would have the attachment remain at the original message.
// if (this._openingQuestion.attachments) {
// RocketChat.models.Messages.addMessageAttachments(this._openingQuestion._id, this._openingQuestion.attachments);
// }

/*
* Child Room
*/
Expand All @@ -48,15 +53,15 @@ export class CreateRequestFromRoomId extends CreateRequestBase {
name: Meteor.user().username // Use @Name field for navigation
}],
channels: [{
_id: parentRoom._id, // Parent Room ID
name: parentRoom.fname || parentRoom.name
_id: parentRoom._id // Parent Room ID
}]
});
// Re-post message in the new room
const msgAuthor = RocketChat.models.Users.findOneByUsername(this._openingQuestion.u.username); // Search with the technical username
const msgRePosted = this._postMessage(roomCreated, msgAuthor, this._openingQuestion.msg);
const msgRePosted = this._postMessage(roomCreated, msgAuthor, this._openingQuestion.msg, this._openingQuestion.attachments);

if (msgRePosted) {
/* Attach the child room */
/* Add a reference to the original message which can be rendered for navigation */
RocketChat.models.Messages.setMessageAttachments(this._openingQuestion._id, [{
author_name: this._openingQuestion.u.username || this._openingQuestion.u.name,
author_icon: `/avatar/${ this._openingQuestion.u.username }?_dc=0`,
Expand All @@ -75,7 +80,7 @@ export class CreateRequestFromRoomId extends CreateRequestBase {
const parentRoom = CreateRequestBase.getRoom(this._parentRoomId);
// Generate RoomName for the new room to be created.
this.name = `${ parentRoom.name }-${ CreateRequestBase.getNextId() }`;
const roomCreateResult = RocketChat.createRoom('r', this.name, Meteor.user() && Meteor.user().username, parentRoom.usernames, false, {parentRoomId: this._parentRoomId});
const roomCreateResult = RocketChat.createRoom('r', this.name, Meteor.user() && Meteor.user().username, parentRoom.usernames, false, { parentRoomId: this._parentRoomId });
if (parentRoom.t === 'e') {
parentRoom.usernames.concat([Meteor.user().username]);
}
Expand All @@ -96,7 +101,7 @@ export class CreateRequestFromRoomId extends CreateRequestBase {
Meteor.methods({
createRequestFromRoomId(parentRoomId, openingQuestion) {
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {method: 'createRequestFromRoomId'});
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'createRequestFromRoomId' });
}
const config = {
parentRoomId,
Expand Down
18 changes: 18 additions & 0 deletions packages/rocketchat-lib/lib/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* globals RocketChat */

import {FlowRouter} from 'meteor/kadira:flow-router';

/**
* in order to provide links to messages which are stable (e. g. survive renaming a room or changing its type)
* we introduce an explicit route. Since this needs to be executable on the client, we provide a method along with it
*/
FlowRouter.route('/message/:id', {
name: 'message',
action(params) {
Meteor.call('getCurrentMessageRouteLink', params.id, (err, res) => {
if (!err && res) {
FlowRouter.go(res);
}
});
}
});
4 changes: 4 additions & 0 deletions packages/rocketchat-lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ Package.onUse(function(api) {
api.addFiles('server/methods/unblockUser.js', 'server');
api.addFiles('server/methods/updateMessage.js', 'server');
api.addFiles('server/methods/globalAnnouncement.js', 'server');
api.addFiles('server/methods/getCurrentMessageRouteLink.js', 'server');

// SERVER STARTUP
api.addFiles('server/startup/settingsOnLoadCdnPrefix.js', 'server');
Expand All @@ -208,6 +209,9 @@ Package.onUse(function(api) {
api.addFiles('client/lib/userRoles.js', 'client');
api.addFiles('client/lib/Layout.js', 'client');

// COMMON ROUTES
api.addFiles('lib/routes.js'); // those routes might be used on the server to calculate a URL

// CLIENT METHODS
api.addFiles('client/methods/sendMessage.js', 'client');
api.addFiles('client/AdminBox.js', 'client');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Meteor.methods({
getCurrentMessageRouteLink(messageId) {
if (Meteor.isServer) {
const message = RocketChat.models.Messages.findOneById(messageId);

if (message && message.rid) {
const room = RocketChat.models.Rooms.findOneById(message.rid);
return FlowRouter.path(RocketChat.roomTypes.getRouteLink(room.t, room), '', {msg: message._id});
} else {
throw new Meteor.Error('invalid message');
}
}
}
});
10 changes: 10 additions & 0 deletions packages/rocketchat-lib/server/models/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,16 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base {
return this.update(query, update);
}

addMessageAttachments(_id, attachments) {
const query = {_id};

const update = {
$addToSet: { tags: { $each: attachments } }
};

return this.update(query, update);
}

setSlackBotIdAndSlackTs(_id, slackBotId, slackTs) {
const query = {_id};

Expand Down
Loading

0 comments on commit 5bcb7e2

Please sign in to comment.