Skip to content

Commit

Permalink
Commit and return priorities in topics and posts
Browse files Browse the repository at this point in the history
  • Loading branch information
nattsw committed Apr 19, 2022
1 parent ad45c67 commit 4489b9d
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/controllers/discourse_assign/assign_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def assign
target_type = params.require(:target_type)
username = params.permit(:username)['username']
group_name = params.permit(:group_name)['group_name']
priority = (params.permit(:priority)['priority'])&.to_i

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

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

assign = Assigner.new(target, current_user).assign(assign_to)
assign = Assigner.new(target, current_user).assign(assign_to, priority: priority)

if assign[:success]
render json: success_json
Expand Down
8 changes: 8 additions & 0 deletions app/models/assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ class Assignment < ActiveRecord::Base
belongs_to :assigned_by_user, class_name: "User"
belongs_to :target, polymorphic: true

enum priority: {
low: 4,
medium: 3,
high: 2,
urgent: 1,
}, _prefix: true

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") }

def self.valid_type?(type)
Expand Down Expand Up @@ -37,6 +44,7 @@ def assigned_to_group?
# target_id :integer not null
# target_type :string not null
# active :boolean default(TRUE)
# priority :integer
#
# Indexes
#
Expand Down
4 changes: 2 additions & 2 deletions lib/assigner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def forbidden_reasons(assign_to:, type:)
end
end

def assign(assign_to, silent: false)
def assign(assign_to, priority: nil, silent: false)
type = assign_to.is_a?(User) ? "User" : "Group"

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

@target.assignment&.destroy!

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)
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)

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

Expand Down
16 changes: 16 additions & 0 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,14 @@ class ::ListController
(SiteSetting.assigns_public || scope.can_assign?) && object.topic.indirectly_assigned_to.present?
end

add_to_serializer(:topic_view, :assignment_priority, false) do
Assignment.priorities[object.topic.assignment.priority]
end

add_to_serializer(:topic_view, :include_assignment_priority?, false) do
(SiteSetting.assigns_public || scope.can_assign?) && object.topic.assignment.present?
end

# SuggestedTopic serializer
add_to_serializer(:suggested_topic, :assigned_to_user, false) do
DiscourseAssign::Helpers.build_assigned_to_user(object.assigned_to, object)
Expand Down Expand Up @@ -631,6 +639,14 @@ class ::ListController
(SiteSetting.assigns_public || scope.can_assign?) && object.assignment&.assigned_to&.is_a?(Group) && object.assignment.active
end

add_to_serializer(:post, :assignment_priority, false) do
Assignment.priorities[object.assignment.priority]
end

add_to_serializer(:post, :include_assignment_priority?, false) do
(SiteSetting.assigns_public || scope.can_assign?) && object.assignment.present?
end

# CurrentUser serializer
add_to_serializer(:current_user, :can_assign) do
object.can_assign?
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/assigner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
.to eq(TopicUser.notification_levels[:tracking])
end

it "can assign with priority" do
assigner.assign(moderator, priority: 2)

expect(topic.assignment.priority_high?).to eq true
end

it 'does not update notification level if already watching' do
TopicUser.change(moderator.id, topic.id,
notification_level: TopicUser.notification_levels[:watching]
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/assign_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@
expect(post.topic.reload.assignment.assigned_to_id).to eq(user2.id)
end

it 'assigns topic with priority to a user' do
put '/assign/assign.json', params: {
target_id: post.topic_id, target_type: 'Topic', username: user2.username, priority: 4
}

topicPriority = post.topic.reload.assignment.priority
expect(Assignment.priorities[topicPriority]).to eq(4)
end

it 'assigns topic to a group' do
put '/assign/assign.json', params: {
target_id: post.topic_id, target_type: 'Topic', group_name: assign_allowed_group.name
Expand Down
6 changes: 6 additions & 0 deletions spec/serializers/post_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@
expect(serializer.as_json[:post][:assigned_to_group].id).to eq(assign_allowed_group.id)
expect(serializer.as_json[:post][:assigned_to_user]).to be nil
end

it "includes priority in serializer" do
Assigner.new(post, user).assign(user, priority: 1)
serializer = PostSerializer.new(post, scope: guardian)
expect(serializer.as_json[:post][:assignment_priority]).to eq(1)
end
end
38 changes: 38 additions & 0 deletions spec/serializers/topic_view_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require 'rails_helper'
require_relative '../support/assign_allowed_group'

RSpec.describe TopicViewSerializer do
fab!(:user) { Fabricate(:user) }
fab!(:topic) { Fabricate(:topic) }
fab!(:post) { Fabricate(:post, topic: topic) }
let(:guardian) { Guardian.new(user) }

include_context 'A group that is allowed to assign'

before do
SiteSetting.assign_enabled = true
add_to_assign_allowed_group(user)
end

it "includes assigned user in serializer" do
Assigner.new(topic, user).assign(user)
serializer = TopicViewSerializer.new(TopicView.new(topic), scope: guardian)
expect(serializer.as_json[:topic_view][:assigned_to_user][:username]).to eq(user.username)
expect(serializer.as_json[:topic_view][:assigned_to_group]).to be nil
end

it "includes assigned group in serializer" do
Assigner.new(topic, user).assign(assign_allowed_group)
serializer = TopicViewSerializer.new(TopicView.new(topic), scope: guardian)
expect(serializer.as_json[:topic_view][:assigned_to_group][:name]).to eq(assign_allowed_group.name)
expect(serializer.as_json[:topic_view][:assigned_to_user]).to be nil
end

it "includes priority in serializer" do
Assigner.new(topic, user).assign(user, priority: 1)
serializer = TopicViewSerializer.new(TopicView.new(topic), scope: guardian)
expect(serializer.as_json[:topic_view][:assignment_priority]).to eq(1)
end
end

0 comments on commit 4489b9d

Please sign in to comment.