REFACTOR: Use core `default_results` function (#111)

This significantly reduces the amount of logic we need to carry in the discourse-assign plugin. One side effect is that I had to rename the 'q' parameter to 'search', so that it matches core's implementation.
This commit is contained in:
David Taylor 2020-09-16 12:18:14 +01:00 committed by GitHub
parent f6fc09ff13
commit c4aefd5917
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 41 additions and 93 deletions

View File

@ -9,20 +9,20 @@ export default UserTopicsList.extend({
taskActions: Ember.inject.service(),
order: "",
ascending: false,
q: "",
search: "",
bulkSelectEnabled: false,
selected: [],
canBulkSelect: alias("currentUser.staff"),
queryParams: ["order", "ascending", "q"],
queryParams: ["order", "ascending", "search"],
@discourseComputed("q")
searchTerm(q) {
return q;
@discourseComputed("search")
searchTerm(search) {
return search;
},
_setSearchTerm(searchTerm) {
this.set("q", searchTerm);
this.set("search", searchTerm);
this.refreshModel();
},
@ -34,7 +34,7 @@ export default UserTopicsList.extend({
params: {
order: this.order,
ascending: this.ascending,
q: this.q,
search: this.search,
},
})
.then((result) => this.set("model", result))

View File

@ -24,9 +24,9 @@ export default Controller.extend({
return ascending || false;
},
@discourseComputed("router.currentRoute.queryParams.q")
q(q) {
return q || "";
@discourseComputed("router.currentRoute.queryParams.search")
search(search) {
return search || "";
},
@discourseComputed("site.mobileView")

View File

@ -6,18 +6,18 @@ import { INPUT_DELAY } from "discourse-common/config/environment";
export default UserTopicsList.extend({
user: Ember.inject.controller(),
taskActions: Ember.inject.service(),
queryParams: ["order", "ascending", "q"],
queryParams: ["order", "ascending", "search"],
order: "",
ascending: false,
q: "",
search: "",
@discourseComputed("q")
searchTerm(q) {
return q;
@discourseComputed("search")
searchTerm(search) {
return search;
},
_setSearchTerm(searchTerm) {
this.set("q", searchTerm);
this.set("search", searchTerm);
this.refreshModel();
},
@ -29,7 +29,7 @@ export default UserTopicsList.extend({
params: {
order: this.order,
ascending: this.ascending,
q: this.q,
search: this.search,
},
})
.then((result) => this.set("model", result))

View File

@ -28,7 +28,7 @@ export default DiscourseRoute.extend({
params: {
order: params.order,
ascending: params.ascending,
q: params.q,
search: params.search,
},
});
},
@ -36,7 +36,7 @@ export default DiscourseRoute.extend({
setupController(controller, model) {
controller.setProperties({
model,
searchTerm: this.currentModel.params.q,
searchTerm: this.currentModel.params.search,
});
},

View File

@ -15,7 +15,7 @@ export default UserTopicListRoute.extend({
exclude_category_ids: [-1],
order: params.order,
ascending: params.ascending,
q: params.q,
search: params.search,
},
});
},

View File

@ -1,5 +1,5 @@
{{#if show-avatar}}
{{#link-to "group.assigned.show" filter.username_lower (query-params order=order ascending=ascending q=q)}}
{{#link-to "group.assigned.show" filter.username_lower (query-params order=order ascending=ascending search=search)}}
<div class="assign-image">
<a href={{filter.userPath}} data-user-card={{filter.username}}>{{avatar filter imageSize="large"}}</a>
</div>
@ -14,7 +14,7 @@
</div>
{{/link-to}}
{{else}}
{{#link-to "group.assigned.show" filter (query-params order=order ascending=ascending q=q)}}
{{#link-to "group.assigned.show" filter (query-params order=order ascending=ascending search=search)}}
<div class="assign-everyone">
{{i18n "discourse_assign.group_everyone"}}
</div>

View File

@ -17,7 +17,7 @@
filter="everyone"
routeType=route_type
assignmentCount=group.assignment_count
q=q
search=search
ascending=ascending
order=order}}
{{#each members as |member|}}
@ -25,7 +25,7 @@
show-avatar=true
filter=member
routeType=route_type
q=q
search=search
ascending=ascending
order=order}}
{{/each}}

View File

@ -20,8 +20,8 @@ load File.expand_path('../lib/discourse_assign/helpers.rb', __FILE__)
Discourse::Application.routes.append do
mount ::DiscourseAssign::Engine, at: "/assign"
get "topics/private-messages-assigned/:username" => "list#private_messages_assigned", as: "topics_private_messages_assigned", constraints: { username: ::RouteFormat.username }
get "/topics/messages-assigned/:username" => "list#messages_assigned", constraints: { username: ::RouteFormat.username }
get "/topics/group-topics-assigned/:groupname" => "list#group_topics_assigned", constraints: { username: ::RouteFormat.username }
get "/topics/messages-assigned/:username" => "list#messages_assigned", constraints: { username: ::RouteFormat.username }, as: "messages_assigned"
get "/topics/group-topics-assigned/:groupname" => "list#group_topics_assigned", constraints: { username: ::RouteFormat.username }, as: "group_topics_assigned"
get "/g/:id/assigned" => "groups#index"
get "/g/:id/assigned/:route_type" => "groups#index"
end
@ -233,92 +233,46 @@ after_initialize do
end
add_to_class(:topic_query, :list_messages_assigned) do |user|
secure = Topic.listable_topics.secured(@guardian).or(Topic.private_messages_for_user(@user))
list = joined_topic_user.where("
list = default_results(include_pms: true)
list = list.where("
topics.id IN (
SELECT topic_id FROM topic_custom_fields
WHERE name = 'assigned_to_id'
AND value = ?)
", user.id.to_s)
.includes(:tags)
list = apply_ordering(list, options)
list = list.merge(secure)
if options[:q].present?
term = options[:q]
ts_query = Search.ts_query(term: term)
list = list
.joins("LEFT JOIN topic_search_data ON topic_search_data.topic_id=topics.id")
.where(
"#{ts_query} @@ topic_search_data.search_data"
)
end
list = list.offset(per_page_setting * options[:page])
.limit(per_page_setting)
create_list(:assigned, { unordered: true }, list)
end
add_to_class(:list_controller, :messages_assigned) do
page = (params[:page].to_i || 0).to_i
user = User.find_by_username(params[:username])
raise Discourse::NotFound unless user
raise Discourse::InvalidAccess unless current_user.can_assign?
list_opts = build_topic_list_options
list_opts[:page] = page
list_opts[:ascending] = params[:ascending]
list_opts[:order] = params[:order]
list_opts[:q] = params[:q] if params[:q]
list = generate_list_for("messages_assigned", user, list_opts)
more_topics_url = "/topics/messages-assigned/#{params[:username]}.json?page=#{page + 1}"
more_topics_url += "&ascending=#{params[:ascending]}&order=#{params[:order]}" if params[:order]
list.more_topics_url = construct_url_with(:next, list_opts)
list.prev_topics_url = construct_url_with(:prev, list_opts)
list.more_topics_url = more_topics_url
respond_with_list(list)
end
add_to_class(:topic_query, :list_group_topics_assigned) do |group|
secure = Topic.listable_topics.secured(@guardian).or(Topic.private_messages_for_user(@user))
list = joined_topic_user.where("
list = default_results(include_pms: true)
list = list.where("
topics.id IN (
SELECT topic_id FROM topic_custom_fields
WHERE name = 'assigned_to_id'
AND value IN (SELECT user_id::varchar(255) from group_users where group_id = ?))
", group.id.to_s)
.includes(:tags)
list = apply_ordering(list, options)
list = list.merge(secure)
if options[:q].present?
term = options[:q]
ts_query = Search.ts_query(term: term)
list = list
.joins("LEFT JOIN topic_search_data ON topic_search_data.topic_id=topics.id")
.where(
"#{ts_query} @@ topic_search_data.search_data"
)
end
list = list.offset(per_page_setting * options[:page])
.limit(per_page_setting)
create_list(:assigned, { unordered: true }, list)
end
add_to_class(:list_controller, :group_topics_assigned) do
page = (params[:page].to_i || 0).to_i
group = Group.find_by("name = ?", params[:groupname])
guardian.ensure_can_see_group_members!(group)
@ -327,17 +281,11 @@ after_initialize do
raise Discourse::InvalidAccess unless group.can_show_assigned_tab?
list_opts = build_topic_list_options
list_opts[:page] = page
list_opts[:ascending] = params[:ascending]
list_opts[:order] = params[:order]
list_opts[:q] = params[:q] if params[:q]
list = generate_list_for("group_topics_assigned", group, list_opts)
more_topics_url = "/topics/group-topics-assigned/#{params[:groupname]}.json?page=#{page + 1}"
more_topics_url += "&ascending=#{params[:ascending]}&order=#{params[:order]}" if params[:order]
list.more_topics_url = construct_url_with(:next, list_opts)
list.prev_topics_url = construct_url_with(:prev, list_opts)
list.more_topics_url = more_topics_url
respond_with_list(list)
end

View File

@ -228,13 +228,13 @@ describe ListController do
topic2.save!
topic3.save!
get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { q: 'Testing' }
get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { search: 'Testing' }
expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic1.id, topic2.id, topic3.id])
get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { q: 'RSpec' }
get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { search: 'RSpec' }
expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic2.id])
get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { q: 'love' }
get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { search: 'love' }
expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic1.id])
end
@ -247,10 +247,10 @@ describe ListController do
topic2.save!
topic3.save!
get "/topics/messages-assigned/#{user.username}.json", params: { q: 'Testing' }
get "/topics/messages-assigned/#{user.username}.json", params: { search: 'Testing' }
expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic1.id, topic3.id])
get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { q: 'love' }
get "/topics/group-topics-assigned/#{get_assigned_allowed_group_name}.json", params: { search: 'love' }
expect(JSON.parse(response.body)['topic_list']['topics'].map { |t| t['id'] }).to match_array([topic1.id])
end
end