Skip to content

Commit

Permalink
FIX: Add plugin event to topic list user lookup (#14116)
Browse files Browse the repository at this point in the history
This can be used to change the list of topic posters. For example,
discourse-solved can use this to move the user who posted the solution
after the original poster.
  • Loading branch information
nbianca committed Aug 25, 2021
1 parent 5ae700e commit 197532d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
14 changes: 14 additions & 0 deletions app/models/topic_list.rb
Expand Up @@ -25,6 +25,19 @@ def self.preload(topics, object)
end
end

def self.on_preload_user_ids(&blk)
(@preload_user_ids ||= Set.new) << blk
end

def self.preload_user_ids(topics, user_ids, object)
if @preload_user_ids
@preload_user_ids.each do |preload_user_ids|
user_ids = preload_user_ids.call(topics, user_ids, object)
end
end
user_ids
end

attr_accessor(
:more_topics_url,
:prev_topics_url,
Expand Down Expand Up @@ -113,6 +126,7 @@ def load_topics
user_ids << ft.user_id << ft.last_post_user_id << ft.featured_user_ids << ft.allowed_user_ids
end

user_ids = TopicList.preload_user_ids(@topics, user_ids, self)
user_lookup = UserLookup.new(user_ids)

@topics.each do |ft|
Expand Down
4 changes: 2 additions & 2 deletions app/models/topic_posters_summary.rb
Expand Up @@ -41,10 +41,10 @@ def new_topic_poster_for(user)
topic_poster
end

def descriptions_by_id
def descriptions_by_id(ids: nil)
@descriptions_by_id ||= begin
result = {}
ids = user_ids
ids = ids || user_ids

if id = ids.shift
result[id] ||= []
Expand Down
10 changes: 10 additions & 0 deletions lib/plugin/instance.rb
Expand Up @@ -210,6 +210,16 @@ def register_topic_view_posts_filter(trigger, &block)
TopicView.add_custom_filter(trigger, &block)
end

# Allows to add more user IDs to the list of preloaded users. This can be
# useful to efficiently change the list of posters or participants.
# Example usage:
# register_topic_list_preload_user_ids do |topics, user_ids, topic_list|
# user_ids << Discourse::SYSTEM_USER_ID
# end
def register_topic_list_preload_user_ids(&block)
TopicList.on_preload_user_ids(&block)
end

# Allow to eager load additional tables in Search. Useful to avoid N+1 performance problems.
# Example usage:
# register_search_topic_eager_load do |opts|
Expand Down

0 comments on commit 197532d

Please sign in to comment.