Skip to content

Commit 362c515

Browse files
committed
FEATURE: compose a new pre-filled message via URL
1 parent 8e95c6c commit 362c515

File tree

7 files changed

+47
-3
lines changed

7 files changed

+47
-3
lines changed

app/assets/javascripts/discourse/mixins/open-composer.js.es6

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default Ember.Mixin.create({
1212
});
1313
},
1414

15-
openComposerWithParams(controller, topicTitle, topicBody, topicCategoryId, topicCategory) {
15+
openComposerWithTopicParams(controller, topicTitle, topicBody, topicCategoryId, topicCategory) {
1616
const Composer = require('discourse/models/composer').default;
1717
this.controllerFor('composer').open({
1818
action: Composer.CREATE_TOPIC,
@@ -23,6 +23,18 @@ export default Ember.Mixin.create({
2323
draftKey: controller.get('model.draft_key'),
2424
draftSequence: controller.get('model.draft_sequence')
2525
});
26+
},
27+
28+
openComposerWithMessageParams(usernames, topicTitle, topicBody) {
29+
const Composer = require('discourse/models/composer').default;
30+
this.controllerFor('composer').open({
31+
action: Composer.PRIVATE_MESSAGE,
32+
usernames,
33+
topicTitle,
34+
topicBody,
35+
archetypeId: 'private_message',
36+
draftKey: 'new_private_message'
37+
});
2638
}
2739

2840
});

app/assets/javascripts/discourse/routes/app-route-map.js.es6

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export default function() {
9393
this.route('guidelines', {path: '/guidelines'});
9494

9595
this.route('new-topic', {path: '/new-topic'});
96+
this.route('new-message', {path: '/new-message'});
9697

9798
this.resource('badges', function() {
9899
this.route('show', {path: '/:id/:slug'});

app/assets/javascripts/discourse/routes/application.js.es6

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ const ApplicationRoute = Discourse.Route.extend(OpenComposer, {
145145
},
146146

147147
createNewTopicViaParams(title, body, category_id, category) {
148-
this.openComposerWithParams(this.controllerFor('discovery/topics'), title, body, category_id, category);
148+
this.openComposerWithTopicParams(this.controllerFor('discovery/topics'), title, body, category_id, category);
149+
},
150+
151+
createNewMessageViaParams(username, title, body) {
152+
this.openComposerWithMessageParams(username, title, body);
149153
}
150154
},
151155

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export default Discourse.Route.extend({
2+
beforeModel: function(transition) {
3+
const self = this;
4+
if (Discourse.User.current()) {
5+
// User is logged in
6+
self.replaceWith('discovery.latest').then(function(e) {
7+
Discourse.User.findByUsername(transition.queryParams.username).then((user) => {
8+
if (user.can_send_private_message_to_user) {
9+
Ember.run.next(function() {
10+
e.send('createNewMessageViaParams', user.username, transition.queryParams.title, transition.queryParams.body);
11+
});
12+
} else {
13+
bootbox.alert(I18n.t("composer.cant_send_pm", {username: user.username}));
14+
}
15+
}).catch((error) => {
16+
bootbox.alert(I18n.t("generic_error"));
17+
});
18+
});
19+
} else {
20+
// User is not logged in
21+
self.session.set("shouldRedirectToUrl", window.location.href);
22+
self.replaceWith('login');
23+
}
24+
}
25+
});

app/controllers/static_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def enter
7575
uri.path !~ /\./
7676

7777
destination = uri.path
78-
destination = "#{uri.path}?#{uri.query}" if uri.path =~ /new-topic/
78+
destination = "#{uri.path}?#{uri.query}" if uri.path =~ /new-topic/ || uri.path =~ /new-message/
7979
end
8080
rescue URI::InvalidURIError
8181
# Do nothing if the URI is invalid

config/locales/client.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,7 @@ en:
907907
toggler: "hide or show the composer panel"
908908
modal_ok: "OK"
909909
modal_cancel: "Cancel"
910+
cant_send_pm: "Sorry, you can't send a message to %{username}."
910911

911912
admin_options_title: "Optional staff settings for this topic"
912913
auto_close:

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@
463463
get 'embed/info' => 'embed#info'
464464

465465
get "new-topic" => "list#latest"
466+
get "new-message" => "list#latest"
466467

467468
# Topic routes
468469
get "t/id_for/:slug" => "topics#id_for_slug"

0 commit comments

Comments
 (0)