Commit and return priorities in topics and posts

This commit is contained in:
Nat 2022-04-20 02:15:36 +08:00
parent ad45c6703f
commit 4489b9d4fe
No known key found for this signature in database
GPG Key ID: C095C891379A319E
8 changed files with 87 additions and 3 deletions

View File

@ -48,6 +48,7 @@ module DiscourseAssign
target_type = params.require(:target_type) target_type = params.require(:target_type)
username = params.permit(:username)['username'] username = params.permit(:username)['username']
group_name = params.permit(:group_name)['group_name'] 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 assign_to = username.present? ? User.find_by(username_lower: username.downcase) : Group.where("LOWER(name) = ?", group_name.downcase).first
@ -56,7 +57,7 @@ module DiscourseAssign
target = target_type.constantize.where(id: target_id).first target = target_type.constantize.where(id: target_id).first
raise Discourse::NotFound unless target 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] if assign[:success]
render json: success_json render json: success_json

View File

@ -8,6 +8,13 @@ class Assignment < ActiveRecord::Base
belongs_to :assigned_by_user, class_name: "User" belongs_to :assigned_by_user, class_name: "User"
belongs_to :target, polymorphic: true 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") } 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) def self.valid_type?(type)
@ -37,6 +44,7 @@ end
# target_id :integer not null # target_id :integer not null
# target_type :string not null # target_type :string not null
# active :boolean default(TRUE) # active :boolean default(TRUE)
# priority :integer
# #
# Indexes # Indexes
# #

View File

@ -199,7 +199,7 @@ class ::Assigner
end end
end end
def assign(assign_to, silent: false) def assign(assign_to, priority: nil, silent: false)
type = assign_to.is_a?(User) ? "User" : "Group" type = assign_to.is_a?(User) ? "User" : "Group"
forbidden_reason = forbidden_reasons(assign_to: assign_to, type: type) forbidden_reason = forbidden_reasons(assign_to: assign_to, type: type)
@ -211,7 +211,7 @@ class ::Assigner
@target.assignment&.destroy! @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) first_post.publish_change_to_clients!(:revised, reload_topic: true)

View File

@ -484,6 +484,14 @@ after_initialize do
(SiteSetting.assigns_public || scope.can_assign?) && object.topic.indirectly_assigned_to.present? (SiteSetting.assigns_public || scope.can_assign?) && object.topic.indirectly_assigned_to.present?
end 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 # SuggestedTopic serializer
add_to_serializer(:suggested_topic, :assigned_to_user, false) do add_to_serializer(:suggested_topic, :assigned_to_user, false) do
DiscourseAssign::Helpers.build_assigned_to_user(object.assigned_to, object) DiscourseAssign::Helpers.build_assigned_to_user(object.assigned_to, object)
@ -631,6 +639,14 @@ after_initialize do
(SiteSetting.assigns_public || scope.can_assign?) && object.assignment&.assigned_to&.is_a?(Group) && object.assignment.active (SiteSetting.assigns_public || scope.can_assign?) && object.assignment&.assigned_to&.is_a?(Group) && object.assignment.active
end 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 # CurrentUser serializer
add_to_serializer(:current_user, :can_assign) do add_to_serializer(:current_user, :can_assign) do
object.can_assign? object.can_assign?

View File

@ -43,6 +43,12 @@ RSpec.describe Assigner do
.to eq(TopicUser.notification_levels[:tracking]) .to eq(TopicUser.notification_levels[:tracking])
end 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 it 'does not update notification level if already watching' do
TopicUser.change(moderator.id, topic.id, TopicUser.change(moderator.id, topic.id,
notification_level: TopicUser.notification_levels[:watching] notification_level: TopicUser.notification_levels[:watching]

View File

@ -116,6 +116,15 @@ RSpec.describe DiscourseAssign::AssignController do
expect(post.topic.reload.assignment.assigned_to_id).to eq(user2.id) expect(post.topic.reload.assignment.assigned_to_id).to eq(user2.id)
end 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 it 'assigns topic to a group' do
put '/assign/assign.json', params: { put '/assign/assign.json', params: {
target_id: post.topic_id, target_type: 'Topic', group_name: assign_allowed_group.name target_id: post.topic_id, target_type: 'Topic', group_name: assign_allowed_group.name

View File

@ -29,4 +29,10 @@ RSpec.describe PostSerializer do
expect(serializer.as_json[:post][:assigned_to_group].id).to eq(assign_allowed_group.id) 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 expect(serializer.as_json[:post][:assigned_to_user]).to be nil
end 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 end

View File

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