@@ -4,9 +4,9 @@ import { FlowRouter } from 'meteor/kadira:flow-router';
4
4
import { Meteor } from 'meteor/meteor' ;
5
5
6
6
/*
7
- * When a message is eligible to be answered as a independent question then it can be threaded into a new channel.
7
+ * When a repostedMessage is eligible to be answered as a independent question then it can be threaded into a new channel.
8
8
* When threading, the question is re-posted into a new room. To leave origin traces between the messages we update
9
- * the original message with system message to allow user to navigate to the message created in the new Room and vice verse.
9
+ * the original repostedMessage with system repostedMessage to allow user to navigate to the repostedMessage created in the new Room and vice verse.
10
10
*/
11
11
export class ThreadBuilder {
12
12
constructor ( parentRoomId , openingQuestion ) {
@@ -37,46 +37,72 @@ export class ThreadBuilder {
37
37
return RocketChat . models . Rooms . findOne ( roomId ) ;
38
38
}
39
39
40
- _postMessage ( room , user , message , attachments , channels , mentions ) {
40
+ _postMessage ( room , user , repostedMessage , attachments , channels , mentions ) {
41
41
attachments = attachments || [ ] ;
42
42
43
43
//sendMessage expects the attachments timestamp to be a string, => serialize it
44
44
attachments . forEach ( attachment =>
45
45
attachment . ts = attachment . ts ? attachment . ts . toISOString ( ) : ''
46
46
) ;
47
- const newMessage = { _id : Random . id ( ) , rid : room . rid , msg : message , attachments, channels, mentions } ;
47
+ const newMessage = { _id : Random . id ( ) , rid : room . rid , msg : repostedMessage , attachments, channels, mentions } ;
48
48
return RocketChat . sendMessage ( user , newMessage , room ) ;
49
49
}
50
50
51
51
_getMessageUrl ( msgId ) {
52
- return FlowRouter . path ( 'message' , { id : msgId } ) ;
52
+ const siteUrl = RocketChat . settings . get ( 'Site_Url' ) ;
53
+ return `${ siteUrl } ${ siteUrl . endsWith ( '/' ) ? '' : '/' } ?msg=${ msgId } ` ;
53
54
}
54
55
55
- _linkMessages ( roomCreated , parentRoom , message ) {
56
+ _linkMessages ( roomCreated , parentRoom , repostedMessage ) {
56
57
const rocketCatUser = RocketChat . models . Users . findOneByUsername ( 'rocket.cat' ) ;
57
58
if ( rocketCatUser && Meteor . userId ( ) ) {
58
- /* Add link in parent Room
59
- */
60
- RocketChat . models . Messages . createWithTypeRoomIdMessageAndUser ( 'create-thread' , parentRoom . _id , this . _getMessageUrl ( message . _id ) , rocketCatUser ,
61
- {
62
- mentions : [ {
63
- _id : Meteor . user ( ) . _id , // Thread Initiator
64
- name : Meteor . user ( ) . username // Use @Name field for navigation
65
- } ] ,
66
- channels : [ {
67
- _id : roomCreated . _id , // Parent Room ID
68
- name : roomCreated . name ,
69
- initialMessage : {
70
- _id : message . _id ,
71
- text : message . msg
72
- }
73
- } ]
74
- } ) ;
59
+ /* Add link in parent Room */
60
+
61
+ const linkMessage = Object . assign ( { } , this . _openingQuestion ) ; // shallow copy of the original message
62
+ delete linkMessage . _id ;
63
+
64
+ const repostingUser = Meteor . user ( ) ;
65
+ linkMessage . u = {
66
+ _id : repostingUser . _id ,
67
+ username : repostingUser . username ,
68
+ name : repostingUser . name
69
+ } ;
70
+
71
+ linkMessage . mentions = [ {
72
+ _id : repostingUser . _id , // Thread Initiator
73
+ name : repostingUser . username // Use @Name field for navigation
74
+ } ] . concat ( this . _openingQuestion . mentions || [ ] ) ;
75
+
76
+ linkMessage . channels = [ {
77
+ _id : roomCreated . _id , // Parent Room ID
78
+ name : roomCreated . name ,
79
+ initialMessage : {
80
+ _id : repostedMessage . _id ,
81
+ text : repostedMessage . msg
82
+ }
83
+ } ] ;
84
+
85
+ const messageQuoteAttachment = { // @see pinMessage.js
86
+ message_link : FlowRouter . path ( 'message' , { id : repostedMessage . _id } ) ,
87
+ text : this . _openingQuestion . msg ,
88
+ ts : this . _openingQuestion . ts
89
+ } ;
90
+
91
+ if ( repostingUser . _id !== this . _openingQuestion . u . _id ) {
92
+ messageQuoteAttachment . author_name = this . _openingQuestion . u . username ;
93
+ messageQuoteAttachment . author_icon = getAvatarUrlFromUsername ( this . _openingQuestion . u . username ) ;
94
+ }
95
+
96
+ linkMessage . attachments = [ messageQuoteAttachment ] . concat ( this . _openingQuestion . attachments || [ ] ) ;
97
+
98
+ linkMessage . urls = [ { url : this . _getMessageUrl ( repostedMessage . _id ) } ] ;
99
+
100
+ return RocketChat . models . Messages . createWithTypeRoomIdMessageAndUser ( 'create-thread' , parentRoom . _id , this . _getMessageUrl ( repostedMessage . _id ) , rocketCatUser , linkMessage , { ts : this . _openingQuestion . ts } ) ;
75
101
}
76
102
}
77
103
78
104
_getMembers ( ) {
79
- const checkRoles = [ 'owner' , 'moderator' ] ;
105
+ const checkRoles = [ 'owner' , 'moderator' , 'leader' ] ;
80
106
const members = [ ] ;
81
107
const users = RocketChat . models . Subscriptions . findByRoomIdWhenUsernameExists ( this . _parentRoomId , {
82
108
fields : {
@@ -111,7 +137,7 @@ export class ThreadBuilder {
111
137
112
138
create ( ) {
113
139
const parentRoom = ThreadBuilder . getRoom ( this . _parentRoomId ) ;
114
- // Generate RoomName for xthe new room to be created.
140
+ // Generate RoomName for the new room to be created.
115
141
this . name = `${ parentRoom . name || parentRoom . usernames . join ( '-' ) } -${ ThreadBuilder . getNextId ( ) } ` ;
116
142
const threadRoomType = parentRoom . t === 'd' ? 'p' : parentRoom . t ;
117
143
const threadRoom = RocketChat . createRoom ( threadRoomType , this . name , Meteor . user ( ) && Meteor . user ( ) . username , this . _getMembers ( ) , false ,
@@ -125,14 +151,14 @@ export class ThreadBuilder {
125
151
const room = RocketChat . models . Rooms . findOneById ( threadRoom . rid ) ;
126
152
if ( room && parentRoom ) {
127
153
// Post message
128
- const message = this . _postMessage (
154
+ const repostedMessage = this . _postMessage (
129
155
room ,
130
156
this . _openingQuestion . u ,
131
157
this . _openingQuestion . msg ,
132
158
this . _openingQuestion . attachments ? this . _openingQuestion . attachments . filter ( attachment => attachment . type && attachment . type === 'file' ) : [ ]
133
159
) ;
134
160
// Link messages
135
- this . _linkMessages ( room , parentRoom , message ) ;
161
+ this . _linkMessages ( room , parentRoom , repostedMessage ) ;
136
162
}
137
163
138
164
return threadRoom ;
@@ -148,11 +174,11 @@ Meteor.methods({
148
174
149
175
return new ThreadBuilder ( parentRoomId , openingQuestion ) . create ( ) ;
150
176
} ,
151
- createThreadFromMessage ( message ) {
152
- const thread = Meteor . call ( 'createThread' , message . rid , message ) ;
177
+ createThreadFromMessage ( openingQuestion ) {
178
+ const thread = Meteor . call ( 'createThread' , openingQuestion . rid , openingQuestion ) ;
153
179
if ( thread ) {
154
- //remove the original message from the display
155
- RocketChat . models . Messages . setHiddenById ( message . _id ) ;
180
+ //remove the original repostedMessage from the display
181
+ RocketChat . models . Messages . setHiddenById ( openingQuestion . _id ) ;
156
182
return thread ;
157
183
}
158
184
}
0 commit comments