Skip to content

Commit

Permalink
Filter scripts by author
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonBarnabe committed Aug 22, 2024
1 parent e0056c1 commit fe9b7c4
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 15 deletions.
20 changes: 14 additions & 6 deletions app/controllers/concerns/script_listings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,11 @@ def code_search
return
end

script_ids = ScriptCodeSearch.search(params[:c])
limit_to_ids = User.find_by(id: params[:by])&.script_ids if params[:by].presence

script_ids = ScriptCodeSearch.search(params[:c], limit_to_ids:)
@scripts = Script.order(self.class.get_sort(params)).includes(:users, :localized_attributes).where(id: script_ids)
@scripts = @scripts.where(language: (params[:language] == 'css') ? 'css' : 'js') unless params[:language] == 'all'
include_deleted = current_user&.moderator? && params[:include_deleted] == '1'
@scripts = @scripts.listable(script_subset) unless include_deleted
@scripts = @scripts.paginate(page: page_number, per_page:)
Expand All @@ -157,14 +160,16 @@ def code_search
format.html do
@bots = 'noindex,nofollow'
@title = t('scripts.listing_title_for_code_search', search_string: params[:c])
@page_description = t('scripts.listing_description_for_code_search_html', search_string: params[:c])
@canonical_params = [:c, :sort]
@include_script_sets = false
if current_user&.moderator?
@page_description = if include_deleted
view_context.link_to('Exclude deleted scripts', { c: params[:c], include_deleted: nil })
else
view_context.link_to('Include deleted scripts', { c: params[:c], include_deleted: '1' })
end
@page_description += view_context.content_tag(:p,
if include_deleted
view_context.link_to('Exclude deleted scripts', { c: params[:c], include_deleted: nil })
else
view_context.link_to('Include deleted scripts', { c: params[:c], include_deleted: '1' })
end)
end

@link_alternates = [
Expand Down Expand Up @@ -201,6 +206,7 @@ def apply_filters(scripts, params, script_subset, default_sort: nil)
scripts = scripts.where(id: set_script_ids)
end
scripts = scripts.where(language: (params[:language] == 'css') ? 'css' : 'js') unless params[:language] == 'all'
scripts = scripts.joins(:authors).where(authors: { user_id: params[:by] }) if params[:by].presence
scripts = scripts.order(get_sort(params, set:, default_sort:))
return scripts
end
Expand Down Expand Up @@ -311,6 +317,8 @@ def load_scripts_for_index_with_es
with[:available_as_js] = true
end

with[:author_ids] = params[:by] if params[:by]

@scripts = Script.search(
params[:q].presence || '*',
fields: ['name^10', 'description^5', 'author^5', 'additional_info^1'],
Expand Down
10 changes: 5 additions & 5 deletions app/helpers/scripts_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
module ScriptsHelper
extend Memoist

def script_list_link(label, sort: nil, site: nil, set: nil, default_sort: nil, language: nil, filter_locale: nil, rel: nil, new: nil)
def script_list_link(label, sort: nil, site: nil, set: nil, default_sort: nil, language: nil, filter_locale: nil, rel: nil, by: nil)
is_link = true
is_minified = action_name == 'minified'
is_code_search = action_name == 'code_search'
is_libraries = action_name == 'libraries'
# sets can have a different default
sort_param_to_use = (sort == default_sort) ? nil : sort
rel ||= (set.present? || filter_locale.present?) ? :nofollow : nil
if sort == params[:sort] && site == params[:site] && set == params[:set] && language == params[:language]
if sort == params[:sort] && site == params[:site] && set == params[:set] && language == params[:language] && by == params[:by]
l = label
is_link = false
elsif is_libraries
l = link_to(label, libraries_scripts_path(sort: sort_param_to_use, q: params[:q], set:), rel:)
elsif is_minified
l = link_to(label, minified_scripts_path(sort: sort_param_to_use), rel:)
elsif is_code_search
l = link_to(label, code_search_scripts_path(sort: sort_param_to_use, c: params[:c]), rel:)
l = link_to(label, code_search_scripts_path(sort: sort_param_to_use, c: params[:c], language:, by:), rel:)
elsif site.nil?
l = link_to(label, { sort: sort_param_to_use, site: nil, set:, q: params[:q], language:, filter_locale:, new: }, rel:)
l = link_to(label, { sort: sort_param_to_use, site: nil, set:, q: params[:q], language:, filter_locale:, by: }, rel:)
elsif params[:controller] == 'users'
l = link_to(label, { sort: sort_param_to_use, site:, set:, language:, filter_locale: }, rel:)
else
l = link_to label, by_site_scripts_path(sort: sort_param_to_use, site:, set:, q: params[:q], language:, filter_locale:, new:), rel:
l = link_to label, by_site_scripts_path(sort: sort_param_to_use, site:, set:, q: params[:q], language:, filter_locale:, by:), rel:
end
tag.li(class: "list-option#{is_link ? '' : ' list-current'}") { l }
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/concerns/script_indexing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ module ScriptIndexing
author: {
type: 'keyword',
},
author_ids: {
type: 'integer',
},
created_at: {
type: 'date',
},
Expand Down Expand Up @@ -88,6 +91,7 @@ def search_data
description: search_value_from_localized_attributes('description'),
additional_info: search_value_from_localized_attributes('additional_info'),
author: users.map(&:name).join(' '),
author_ids: users.map(&:id),
created_at:,
code_updated_at:,
total_installs:,
Expand Down
10 changes: 8 additions & 2 deletions app/services/script_code_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ def index_all
end
end

def search(text)
content, _stderr, _status = Open3.capture3('grep', '-R', '-F', '-l', text, BASE_PATH)
def search(text, limit_to_ids: nil)
content, _stderr, _status = if limit_to_ids.nil?
Open3.capture3('grep', '-R', '-F', '-l', text, BASE_PATH)
elsif limit_to_ids.none?
['', nil, nil]
else
Open3.capture3('grep', '-R', '-F', '-l', text, *limit_to_ids.map { |id| "#{BASE_PATH}/#{id}" })
end
content.split("\n").map { |line| line.delete_prefix("#{BASE_PATH}/") }.map(&:to_i)
end

Expand Down
19 changes: 18 additions & 1 deletion app/views/scripts/_list_options.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ libraries = false if !defined?(libraries) || libraries.nil?
default_sort = @set&.default_sort || (params[:q].present? ? 'relevance' : (libraries ? 'created' : ScriptListings::DEFAULT_SORT))
rel = nil unless defined?(rel)

current_options = params.to_unsafe_h.slice(:sort, :site, :set, :language, :filter_locale,).map {|k, v| [k.to_sym, v] }.to_h.merge(default_sort: default_sort)
current_options = params.to_unsafe_h.slice(:sort, :site, :set, :language, :filter_locale, :by).map {|k, v| [k.to_sym, v] }.to_h.merge(default_sort: default_sort)
current_options[:rel] = rel if rel
skip_all_sites_link = false unless defined?(skip_all_sites_link)
%>
Expand Down Expand Up @@ -81,6 +81,23 @@ skip_all_sites_link = false unless defined?(skip_all_sites_link)
</div>
<% end %>

<% if (current_user || params[:by].presence) && controller_name != 'users' %>
<div id="script-list-author" class="list-option-group">Authorship:
<ul>
<%= script_list_link(t('scripts.listing_author.all'), **current_options.merge(by: nil)) %>
<% if current_user %>
<%= script_list_link(t('scripts.listing_author.me'), **current_options.merge(by: current_user.id.to_s)) %>
<% end %>
<% if current_user.nil? || current_user.id != params[:by].to_i %>
<% by = User.find_by(id: params[:by]) %>
<% if by %>
<%= script_list_link(t('scripts.listing_author.user', author_name: by.name), **current_options.merge(by: by.id.to_s)) %>
<% end %>
<% end %>
</ul>
</div>
<% end %>

<% # This doesn't work for search
if include_script_sets && (action_name != 'index' || params[:q].blank?) && user_signed_in? && !current_user.script_sets.empty? %>
<div id="script-list-set" class="list-option-group">Script set:
Expand Down
2 changes: 1 addition & 1 deletion app/views/scripts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<div class="sidebar-title"><%= t('common.listing_options_title') %></div>
<div></div>
</div>
<%= render partial: 'list_options', locals: {by_sites: @by_sites, scripts: @scripts, sort_options: @sort_options, include_script_sets: @include_script_sets.nil? ? true : @include_script_sets, include_search: true} %>
<%= render partial: 'list_options', locals: {by_sites: @by_sites, scripts: @scripts, sort_options: @sort_options, include_script_sets: @include_script_sets.nil? ? true : @include_script_sets, include_search: action_name != 'code_search'} %>
</div>
<% end %>
</div>
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ en:
listing_title_for_search: "'%{search_string}' user scripts"
# html title for script list page for code search results.
listing_title_for_code_search: "'%{search_string}' user scripts"
listing_description_for_code_search_html: "Code search for <code>%{search_string}</code>"
# html title for script list page when no other situation applies
listing_title_generic: "User scripts"
# html title for script list page for a user's favorites. set_name is script_sets.favorites_name
Expand Down Expand Up @@ -283,6 +284,10 @@ en:
listing_language_all: "All"
listing_language_js: "JavaScript"
listing_language_css: "CSS"
listing_author:
all: All authors
me: My scripts
user: Scripts by %{author_name}
search_failed: Something went wrong loading your results. Some search functionality may not be working. We've been notified of the issue.
# link to start the process of importing (creating from an external place) scripts
import: "Import scripts"
Expand Down

0 comments on commit fe9b7c4

Please sign in to comment.