mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 17:06:31 -06:00
DEV: Improve support for plugins. (#7332)
* DEV: Allow NewPostManager handlers handle PMs. * DEV: Add custom fields topic option to PostCreator. * DEV: Add topic_id to serializer data. * DEV: Wrap topic title from notification item in a span.
This commit is contained in:
parent
6398ded798
commit
da39d66e83
@ -724,6 +724,7 @@ const Composer = RestModel.extend({
|
||||
}
|
||||
|
||||
const props = {
|
||||
topic_id: this.topic.id,
|
||||
raw: this.reply,
|
||||
raw_old: this.editConflict ? null : this.originalText,
|
||||
edit_reason: opts.editReason,
|
||||
|
@ -85,6 +85,11 @@ createWidget("notification-item", {
|
||||
}
|
||||
|
||||
if (this.attrs.fancy_title) {
|
||||
if (this.attrs.topic_id) {
|
||||
return `<span data-topic-id="${this.attrs.topic_id}">${
|
||||
this.attrs.fancy_title
|
||||
}</span>`;
|
||||
}
|
||||
return this.attrs.fancy_title;
|
||||
}
|
||||
|
||||
|
@ -169,7 +169,7 @@
|
||||
display: none;
|
||||
}
|
||||
|
||||
span {
|
||||
span:first-child {
|
||||
color: $primary;
|
||||
}
|
||||
|
||||
|
@ -671,7 +671,7 @@
|
||||
margin-right: 0.5em;
|
||||
}
|
||||
|
||||
span {
|
||||
span:first-child {
|
||||
color: $primary;
|
||||
}
|
||||
|
||||
|
@ -102,7 +102,7 @@
|
||||
p {
|
||||
display: inline-block;
|
||||
|
||||
span {
|
||||
span:first-child {
|
||||
color: $primary;
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ class NewPostManager
|
||||
end
|
||||
|
||||
def self.clear_handlers!
|
||||
@sorted_handlers = [{ priority: 0, proc: method(:default_handler) }]
|
||||
@sorted_handlers = []
|
||||
end
|
||||
|
||||
def self.add_handler(priority = 0, &block)
|
||||
@ -178,22 +178,17 @@ class NewPostManager
|
||||
return result
|
||||
end
|
||||
|
||||
# We never queue private messages
|
||||
return perform_create_post if @args[:archetype] == Archetype.private_message
|
||||
|
||||
if args[:topic_id] && Topic.where(id: args[:topic_id], archetype: Archetype.private_message).exists?
|
||||
return perform_create_post
|
||||
end
|
||||
|
||||
# Perform handlers until one returns a result
|
||||
handled = NewPostManager.handlers.any? do |handler|
|
||||
NewPostManager.handlers.any? do |handler|
|
||||
result = handler.call(self)
|
||||
return result if result
|
||||
|
||||
false
|
||||
end
|
||||
|
||||
perform_create_post unless handled
|
||||
# We never queue private messages
|
||||
return perform_create_post if @args[:archetype] == Archetype.private_message ||
|
||||
(args[:topic_id] && Topic.where(id: args[:topic_id], archetype: Archetype.private_message).exists?)
|
||||
|
||||
NewPostManager.default_handler(self) || perform_create_post
|
||||
end
|
||||
|
||||
# Enqueue this post
|
||||
|
@ -56,6 +56,7 @@ class PostCreator
|
||||
# pinned_at - Topic pinned time (optional)
|
||||
# pinned_globally - Is the topic pinned globally (optional)
|
||||
# shared_draft - Is the topic meant to be a shared draft
|
||||
# topic_opts - Options to be overwritten for topic
|
||||
#
|
||||
def initialize(user, opts)
|
||||
# TODO: we should reload user in case it is tainted, should take in a user_id as opposed to user
|
||||
@ -410,7 +411,8 @@ class PostCreator
|
||||
def create_topic
|
||||
return if @topic
|
||||
begin
|
||||
topic_creator = TopicCreator.new(@user, guardian, @opts)
|
||||
opts = @opts[:topic_opts] ? @opts.merge(@opts[:topic_opts]) : @opts
|
||||
topic_creator = TopicCreator.new(@user, guardian, opts)
|
||||
@topic = topic_creator.create
|
||||
rescue ActiveRecord::Rollback
|
||||
rollback_from_errors!(topic_creator)
|
||||
|
@ -37,6 +37,9 @@ class TopicCreator
|
||||
def create
|
||||
topic = Topic.new(setup_topic_params)
|
||||
setup_tags(topic)
|
||||
if fields = @opts[:custom_fields]
|
||||
topic.custom_fields = fields
|
||||
end
|
||||
|
||||
DiscourseEvent.trigger(:before_create_topic, topic, self)
|
||||
|
||||
|
@ -212,7 +212,7 @@ describe NewPostManager do
|
||||
handler = -> { nil }
|
||||
|
||||
NewPostManager.add_handler(&handler)
|
||||
expect(NewPostManager.handlers).to eq([default_handler, handler])
|
||||
expect(NewPostManager.handlers).to eq([handler])
|
||||
end
|
||||
|
||||
it "can be added in high priority" do
|
||||
@ -223,7 +223,7 @@ describe NewPostManager do
|
||||
NewPostManager.add_handler(100, &a)
|
||||
NewPostManager.add_handler(50, &b)
|
||||
NewPostManager.add_handler(101, &c)
|
||||
expect(NewPostManager.handlers).to eq([c, a, b, default_handler])
|
||||
expect(NewPostManager.handlers).to eq([c, a, b])
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -54,6 +54,11 @@ describe PostCreator do
|
||||
expect { creator.create }.to raise_error(Discourse::InvalidAccess)
|
||||
end
|
||||
|
||||
it "can be created with custom fields" do
|
||||
post = PostCreator.create(user, basic_topic_params.merge(topic_opts: { custom_fields: { hello: "world" } }))
|
||||
expect(post.topic.custom_fields).to eq("hello" => "world")
|
||||
end
|
||||
|
||||
context "reply to post number" do
|
||||
it "omits reply to post number if received on a new topic" do
|
||||
p = PostCreator.new(user, basic_topic_params.merge(reply_to_post_number: 3)).create
|
||||
|
Loading…
Reference in New Issue
Block a user