diff --git a/lib/community_landing/data_fetcher.rb b/lib/community_landing/data_fetcher.rb index 0a82a1f..580e7c8 100644 --- a/lib/community_landing/data_fetcher.rb +++ b/lib/community_landing/data_fetcher.rb @@ -7,42 +7,57 @@ module CommunityLanding data = {} # Top contributors - data[:contributors] = if s.contributors_enabled - User - .joins(:posts) - .where(posts: { created_at: s.contributors_days.days.ago.. }) - .where.not(username: %w[system discobot]) - .where(active: true, staged: false) - .group("users.id") - .order("COUNT(posts.id) DESC") - .limit(s.contributors_count) - .select("users.*, COUNT(posts.id) AS post_count") + data[:contributors] = begin + if s.contributors_enabled + User + .joins(:posts) + .where(posts: { created_at: s.contributors_days.days.ago.. }) + .where.not(username: %w[system discobot]) + .where(active: true, staged: false) + .group("users.id") + .order("COUNT(posts.id) DESC") + .limit(s.contributors_count) + .select("users.*, COUNT(posts.id) AS post_count") + end + rescue => e + Rails.logger.warn("[CommunityLanding] contributors fetch failed: #{e.message}") + nil end # Public groups — optionally filtered by selected names - data[:groups] = if s.groups_enabled - selected = s.groups_selected.presence - scope = Group - .where(visibility_level: Group.visibility_levels[:public]) - .where(automatic: false) + data[:groups] = begin + if s.groups_enabled + selected = s.groups_selected.presence + scope = Group + .where(visibility_level: Group.visibility_levels[:public]) + .where(automatic: false) - if selected - names = selected.split("|").map(&:strip).reject(&:empty?) - scope = scope.where(name: names) if names.any? + if selected + names = selected.split("|").map(&:strip).reject(&:empty?) + scope = scope.where(name: names) if names.any? + end + + scope.limit(s.groups_count) end - - scope.limit(s.groups_count) + rescue => e + Rails.logger.warn("[CommunityLanding] groups fetch failed: #{e.message}") + nil end # Trending topics - data[:topics] = if s.topics_enabled - Topic - .listable_topics - .where(visible: true) - .where("topics.created_at > ?", 30.days.ago) - .order(posts_count: :desc) - .limit(s.topics_count) - .includes(:category, :user) + data[:topics] = begin + if s.topics_enabled + Topic + .listable_topics + .where(visible: true) + .where("topics.created_at > ?", 30.days.ago) + .order(posts_count: :desc) + .limit(s.topics_count) + .includes(:category, :user) + end + rescue => e + Rails.logger.warn("[CommunityLanding] topics fetch failed: #{e.message}") + nil end # Aggregate stats @@ -53,13 +68,18 @@ module CommunityLanding chat_count = 0 end - data[:stats] = { - members: User.real.count, - topics: Topic.listable_topics.count, - posts: Post.where(user_deleted: false).count, - likes: Post.sum(:like_count), - chats: chat_count, - } + data[:stats] = begin + { + members: User.real.count, + topics: Topic.listable_topics.count, + posts: Post.where(user_deleted: false).count, + likes: Post.sum(:like_count), + chats: chat_count, + } + rescue => e + Rails.logger.warn("[CommunityLanding] stats fetch failed: #{e.message}") + { members: 0, topics: 0, posts: 0, likes: 0, chats: 0 } + end data end diff --git a/lib/community_landing/page_builder.rb b/lib/community_landing/page_builder.rb index 24c61ec..ab739c6 100644 --- a/lib/community_landing/page_builder.rb +++ b/lib/community_landing/page_builder.rb @@ -128,7 +128,7 @@ module CommunityLanding def render_hero hero_card = @s.hero_card_enabled rescue true hero_img_first = @s.hero_image_first rescue false - hero_bg_img = @s.hero_background_image_url.presence + hero_bg_img = (@s.hero_background_image_url.presence rescue nil) hero_border = @s.hero_border_style rescue "none" hero_min_h = @s.hero_min_height rescue 0 site_name = @s.title @@ -197,7 +197,7 @@ module CommunityLanding html << "