discourse/app/models/topic_list.rb

77 lines
1.8 KiB
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
require_dependency 'avatar_lookup'
class TopicList
include ActiveModel::Serialization
attr_accessor :more_topics_url, :draft, :draft_key, :draft_sequence
def initialize(current_user, topics)
@current_user = current_user
@topics_input = topics
end
# Lazy initialization
def topics
return @topics if @topics.present?
@topics = @topics_input.to_a
# Attach some data for serialization to each topic
@topic_lookup = TopicUser.lookup_for(@current_user, @topics) if @current_user.present?
# Create a lookup for all the user ids we need
user_ids = []
2013-02-07 10:45:24 -05:00
@topics.each do |ft|
2013-02-05 14:16:51 -05:00
user_ids << ft.user_id << ft.last_post_user_id << ft.featured_user_ids
end
avatar_lookup = AvatarLookup.new(user_ids)
2013-02-07 10:45:24 -05:00
@topics.each do |ft|
2013-02-05 14:16:51 -05:00
ft.user_data = @topic_lookup[ft.id] if @topic_lookup.present?
ft.posters = ft.posters_summary(ft.user_data, @current_user, avatar_lookup: avatar_lookup)
end
return @topics
end
def filter_summary
2013-02-07 10:45:24 -05:00
@filter_summary ||= get_summary
2013-02-05 14:16:51 -05:00
end
def attributes
{'more_topics_url' => page}
end
2013-02-07 10:45:24 -05:00
protected
2013-02-05 14:16:51 -05:00
def get_summary
s = {}
return s unless @current_user
split = SiteSetting.top_menu.split("|")
split.each do |i|
name, filter = i.split(",")
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
exclude = nil
if filter && filter[0] == "-"
exclude = filter[1..-1]
end
query = TopicQuery.new(@current_user, exclude_category: exclude)
s["unread"] = query.unread_count if name == 'unread'
s["new"] = query.new_count if name == 'new'
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
catSplit = name.split("/")
2013-02-07 10:45:24 -05:00
if catSplit[0] == "category" && catSplit.length == 2 && @current_user
2013-02-05 14:16:51 -05:00
query = TopicQuery.new(@current_user, only_category: catSplit[1], limit: false)
s[name] = query.unread_count + query.new_count
end
end
s
end
end