Skip to content

Commit 4489b9d

Browse files
committed
Commit and return priorities in topics and posts
1 parent ad45c67 commit 4489b9d

File tree

8 files changed

+87
-3
lines changed

8 files changed

+87
-3
lines changed

app/controllers/discourse_assign/assign_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def assign
4848
target_type = params.require(:target_type)
4949
username = params.permit(:username)['username']
5050
group_name = params.permit(:group_name)['group_name']
51+
priority = (params.permit(:priority)['priority'])&.to_i
5152

5253
assign_to = username.present? ? User.find_by(username_lower: username.downcase) : Group.where("LOWER(name) = ?", group_name.downcase).first
5354

@@ -56,7 +57,7 @@ def assign
5657
target = target_type.constantize.where(id: target_id).first
5758
raise Discourse::NotFound unless target
5859

59-
assign = Assigner.new(target, current_user).assign(assign_to)
60+
assign = Assigner.new(target, current_user).assign(assign_to, priority: priority)
6061

6162
if assign[:success]
6263
render json: success_json

app/models/assignment.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ class Assignment < ActiveRecord::Base
88
belongs_to :assigned_by_user, class_name: "User"
99
belongs_to :target, polymorphic: true
1010

11+
enum priority: {
12+
low: 4,
13+
medium: 3,
14+
high: 2,
15+
urgent: 1,
16+
}, _prefix: true
17+
1118
scope :joins_with_topics, -> { joins("INNER JOIN topics ON topics.id = assignments.target_id AND assignments.target_type = 'Topic' AND topics.deleted_at IS NULL") }
1219

1320
def self.valid_type?(type)
@@ -37,6 +44,7 @@ def assigned_to_group?
3744
# target_id :integer not null
3845
# target_type :string not null
3946
# active :boolean default(TRUE)
47+
# priority :integer
4048
#
4149
# Indexes
4250
#

lib/assigner.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def forbidden_reasons(assign_to:, type:)
199199
end
200200
end
201201

202-
def assign(assign_to, silent: false)
202+
def assign(assign_to, priority: nil, silent: false)
203203
type = assign_to.is_a?(User) ? "User" : "Group"
204204

205205
forbidden_reason = forbidden_reasons(assign_to: assign_to, type: type)
@@ -211,7 +211,7 @@ def assign(assign_to, silent: false)
211211

212212
@target.assignment&.destroy!
213213

214-
assignment = @target.create_assignment!(assigned_to_id: assign_to.id, assigned_to_type: type, assigned_by_user_id: @assigned_by.id, topic_id: topic.id)
214+
assignment = @target.create_assignment!(assigned_to_id: assign_to.id, assigned_to_type: type, assigned_by_user_id: @assigned_by.id, topic_id: topic.id, priority: priority)
215215

216216
first_post.publish_change_to_clients!(:revised, reload_topic: true)
217217

plugin.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,14 @@ class ::ListController
484484
(SiteSetting.assigns_public || scope.can_assign?) && object.topic.indirectly_assigned_to.present?
485485
end
486486

487+
add_to_serializer(:topic_view, :assignment_priority, false) do
488+
Assignment.priorities[object.topic.assignment.priority]
489+
end
490+
491+
add_to_serializer(:topic_view, :include_assignment_priority?, false) do
492+
(SiteSetting.assigns_public || scope.can_assign?) && object.topic.assignment.present?
493+
end
494+
487495
# SuggestedTopic serializer
488496
add_to_serializer(:suggested_topic, :assigned_to_user, false) do
489497
DiscourseAssign::Helpers.build_assigned_to_user(object.assigned_to, object)
@@ -631,6 +639,14 @@ class ::ListController
631639
(SiteSetting.assigns_public || scope.can_assign?) && object.assignment&.assigned_to&.is_a?(Group) && object.assignment.active
632640
end
633641

642+
add_to_serializer(:post, :assignment_priority, false) do
643+
Assignment.priorities[object.assignment.priority]
644+
end
645+
646+
add_to_serializer(:post, :include_assignment_priority?, false) do
647+
(SiteSetting.assigns_public || scope.can_assign?) && object.assignment.present?
648+
end
649+
634650
# CurrentUser serializer
635651
add_to_serializer(:current_user, :can_assign) do
636652
object.can_assign?

spec/lib/assigner_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
.to eq(TopicUser.notification_levels[:tracking])
4444
end
4545

46+
it "can assign with priority" do
47+
assigner.assign(moderator, priority: 2)
48+
49+
expect(topic.assignment.priority_high?).to eq true
50+
end
51+
4652
it 'does not update notification level if already watching' do
4753
TopicUser.change(moderator.id, topic.id,
4854
notification_level: TopicUser.notification_levels[:watching]

spec/requests/assign_controller_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@
116116
expect(post.topic.reload.assignment.assigned_to_id).to eq(user2.id)
117117
end
118118

119+
it 'assigns topic with priority to a user' do
120+
put '/assign/assign.json', params: {
121+
target_id: post.topic_id, target_type: 'Topic', username: user2.username, priority: 4
122+
}
123+
124+
topicPriority = post.topic.reload.assignment.priority
125+
expect(Assignment.priorities[topicPriority]).to eq(4)
126+
end
127+
119128
it 'assigns topic to a group' do
120129
put '/assign/assign.json', params: {
121130
target_id: post.topic_id, target_type: 'Topic', group_name: assign_allowed_group.name

spec/serializers/post_serializer_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@
2929
expect(serializer.as_json[:post][:assigned_to_group].id).to eq(assign_allowed_group.id)
3030
expect(serializer.as_json[:post][:assigned_to_user]).to be nil
3131
end
32+
33+
it "includes priority in serializer" do
34+
Assigner.new(post, user).assign(user, priority: 1)
35+
serializer = PostSerializer.new(post, scope: guardian)
36+
expect(serializer.as_json[:post][:assignment_priority]).to eq(1)
37+
end
3238
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
require_relative '../support/assign_allowed_group'
5+
6+
RSpec.describe TopicViewSerializer do
7+
fab!(:user) { Fabricate(:user) }
8+
fab!(:topic) { Fabricate(:topic) }
9+
fab!(:post) { Fabricate(:post, topic: topic) }
10+
let(:guardian) { Guardian.new(user) }
11+
12+
include_context 'A group that is allowed to assign'
13+
14+
before do
15+
SiteSetting.assign_enabled = true
16+
add_to_assign_allowed_group(user)
17+
end
18+
19+
it "includes assigned user in serializer" do
20+
Assigner.new(topic, user).assign(user)
21+
serializer = TopicViewSerializer.new(TopicView.new(topic), scope: guardian)
22+
expect(serializer.as_json[:topic_view][:assigned_to_user][:username]).to eq(user.username)
23+
expect(serializer.as_json[:topic_view][:assigned_to_group]).to be nil
24+
end
25+
26+
it "includes assigned group in serializer" do
27+
Assigner.new(topic, user).assign(assign_allowed_group)
28+
serializer = TopicViewSerializer.new(TopicView.new(topic), scope: guardian)
29+
expect(serializer.as_json[:topic_view][:assigned_to_group][:name]).to eq(assign_allowed_group.name)
30+
expect(serializer.as_json[:topic_view][:assigned_to_user]).to be nil
31+
end
32+
33+
it "includes priority in serializer" do
34+
Assigner.new(topic, user).assign(user, priority: 1)
35+
serializer = TopicViewSerializer.new(TopicView.new(topic), scope: guardian)
36+
expect(serializer.as_json[:topic_view][:assignment_priority]).to eq(1)
37+
end
38+
end

0 commit comments

Comments
 (0)