# frozen_string_literal: true namespace :booru do task recount_votes: :environment do puts 'Recounting votes for all posts (be patient!)' i = 0 Post.includes(:votes).find_each do |p| p.recount_votes! i += 1 if (i % 10000) == 0 puts "Done #{i} posts..." end end end namespace :reindex do desc 'Rebuild all indices' task all: :environment do puts 'Rebuild Reports...' Report.__elasticsearch__.import(force: true) puts 'Rebuild Galleries...' Gallery.__elasticsearch__.import(force: true) puts 'Rebuild Forum Posts...' ForumPost.__elasticsearch__.import(force: true) puts 'Rebuild Filters...' Filter.__elasticsearch__.import(force: true) puts 'Rebuild Comments...' Comment.__elasticsearch__.import(force: true) puts 'Rebuild Posts (this can take hours)...' Post.__elasticsearch__.import(force: true) end desc 'Rebuild Post indices' task posts: :environment do puts 'Rebuild Posts (this can take hours)...' Post.__elasticsearch__.import(force: true) end end namespace :forums do desc 'Build topic positions' task build_topic_positions: :environment do count = Topic.count c = 0 Topic.each do |topic| puts "Building topic positions for topic '#{topic.title}' (#{c} of #{count})" i = 0 c += 1 topic.posts.order(created_at: :asc).select([:id, :topic_position]).each do |post| puts "Setting #{post.id} to #{i}" post.set(topic_position: i) i += 1 end end end desc 'Build topic post counts' task build_topic_post_counts: :environment do Topic.each do |topic| topic.set(post_count: topic.posts.where(hidden_from_users: false).count) end Forum.each do |forum| forum.set(post_count: forum.topics.where(hidden_from_users: false).sum(:post_count)) forum.set(topic_count: forum.topics.where(hidden_from_users: false).size) end end end end