A platform for community discussion. Free, open, simple.
DEV: Format .thor files (#23059)
Jarek Radosz8bebd8fd99f7
diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.ymlindex 0ded454e9e6..2140fb1809b 100644--- a/.github/workflows/linting.yml+++ b/.github/workflows/linting.yml@@ -73,7 +73,7 @@ jobs:if: ${{ !cancelled() }}run: |set -E- bundle exec stree check Gemfile $(git ls-files '*.rb') $(git ls-files '*.rake')+ bundle exec stree check Gemfile $(git ls-files '*.rb') $(git ls-files '*.rake') $(git ls-files '*.thor')- name: ESLint (core)if: ${{ !cancelled() }}diff --git a/lib/tasks/backfill.thor b/lib/tasks/backfill.thorindex 6690de54bac..f3026b830bb 100644--- a/lib/tasks/backfill.thor+++ b/lib/tasks/backfill.thor@@ -4,13 +4,11 @@ class Backfill < Thordesc "link_titles", "Backfills link titles"def link_titles- require './config/environment'+ require "./config/environment"topic_links = TopicLink.where(crawled_at: nil, internal: false)puts "Enqueueing Topic Links: #{topic_links.count} links found."- topic_links.pluck(:id).each do |tl|- Jobs.enqueue(:crawl_topic_link, topic_link_id: tl)- end+ topic_links.pluck(:id).each { |tl| Jobs.enqueue(:crawl_topic_link, topic_link_id: tl) }endenddiff --git a/lib/tasks/populate.thor b/lib/tasks/populate.thorindex 52ad939eb47..422de72ba4f 100644--- a/lib/tasks/populate.thor+++ b/lib/tasks/populate.thor@@ -21,14 +21,28 @@ class Populate < Thor> $ thor populate:posts -p 10 -n 5MD- method_option :num_posts, aliases: '-n', type: :numeric, required: true, desc: "Number of posts to make"- method_option :users, aliases: '-u', type: :array, desc: "Usernames of users who will make the posts"- method_option :title, aliases: '-t', desc: "The title of the topic, if making a new topic"- method_option :topic_id, aliases: '-i', type: :numeric, desc: "The id of the topic where the posts will be added"- method_option :num_topics, aliases: '-p', type: :numeric, default: 1, desc: "Number of topics to create"+ method_option :num_posts,+ aliases: "-n",+ type: :numeric,+ required: true,+ desc: "Number of posts to make"+ method_option :users,+ aliases: "-u",+ type: :array,+ desc: "Usernames of users who will make the posts"+ method_option :title, aliases: "-t", desc: "The title of the topic, if making a new topic"+ method_option :topic_id,+ aliases: "-i",+ type: :numeric,+ desc: "The id of the topic where the posts will be added"+ method_option :num_topics,+ aliases: "-p",+ type: :numeric,+ default: 1,+ desc: "Number of topics to create"def posts- require './config/environment'+ require "./config/environment"users = []if options[:users]options[:users].each do |u|@@ -49,10 +63,8 @@ class Populate < Thortopic = create_topic(users) unless topicputs "Adding posts to '#{topic.title}'"puts "Making #{options[:num_posts]} posts"- (start_post..options[:num_posts]).each do- create_post(users, topic)- end- puts ''+ (start_post..options[:num_posts]).each { create_post(users, topic) }+ puts ""puts "Done. Topic id = #{topic.id}"endensure@@ -65,7 +77,12 @@ class Populate < Thoruser = User.find_by_email(user_email)unless userputs "Creating new account: #{user_email}"- user = User.create!(email: user_email, password: SecureRandom.hex, username: UserNameSuggester.suggest(user_email))+ user =+ User.create!(+ email: user_email,+ password: SecureRandom.hex,+ username: UserNameSuggester.suggest(user_email),+ )enduser.active = trueuser.save!@@ -87,35 +104,158 @@ class Populate < Thorenddef create_post(users, topic)- print '.'- raw = rand(4) == 0 ? (rand(2) == 0 ? image_posts.sample : wikipedia_posts.sample) : generate_sentence(7)+ print "."+ raw =+ (+ if rand(4) == 0+ (rand(2) == 0 ? image_posts.sample : wikipedia_posts.sample)+ else+ generate_sentence(7)+ end+ )post_creator = PostCreator.new(users.sample, topic_id: topic.id, raw: raw)post = post_creator.create- unless post- puts post_creator.errors.full_messages, ""- end+ puts post_creator.errors.full_messages, "" unless postpostenddef hipster_words- @hipster_words ||= ['etsy', 'twee', 'hoodie', 'Banksy', 'retro', 'synth', 'single-origin', 'coffee', 'art', 'party', 'cliche', 'artisan', 'Williamsburg', 'squid', 'helvetica', 'keytar', 'American Apparel', 'craft beer', 'food truck', "you probably haven't heard of them", 'cardigan', 'aesthetic', 'raw denim', 'sartorial', 'gentrify', 'lomo', 'Vice', 'Pitchfork', 'Austin', 'sustainable', 'salvia', 'organic', 'thundercats', 'PBR', 'iPhone', 'lo-fi', 'skateboard', 'jean shorts', 'next level', 'beard', 'tattooed', 'trust fund', 'Four Loko', 'master cleanse', 'ethical', 'high life', 'wolf moon', 'fanny pack', 'Terry Richardson', '8-bit', 'Carles', 'Shoreditch', 'seitan', 'freegan', 'keffiyeh', 'biodiesel', 'quinoa', 'farm-to-table', 'fixie', 'viral', 'chambray', 'scenester', 'leggings', 'readymade', 'Brooklyn', 'Wayfarers', 'Marfa', 'put a bird on it', 'dreamcatcher', 'photo booth', 'tofu', 'mlkshk', 'vegan', 'vinyl', 'DIY', 'banh mi', 'bicycle rights', 'before they sold out', 'gluten-free', 'yr butcher blog', 'whatever', '+1', 'Cosby Sweater', 'VHS', 'messenger bag', 'cred', 'locavore', 'mustache', 'tumblr', 'Portland', 'mixtape', 'fap', 'letterpress', "McSweeney's", 'stumptown', 'brunch', 'Wes Anderson', 'irony', 'echo park']+ @hipster_words ||= [+ "etsy",+ "twee",+ "hoodie",+ "Banksy",+ "retro",+ "synth",+ "single-origin",+ "coffee",+ "art",+ "party",+ "cliche",+ "artisan",+ "Williamsburg",+ "squid",+ "helvetica",+ "keytar",+ "American Apparel",+ "craft beer",+ "food truck",+ "you probably haven't heard of them",+ "cardigan",+ "aesthetic",+ "raw denim",+ "sartorial",+ "gentrify",+ "lomo",+ "Vice",+ "Pitchfork",+ "Austin",+ "sustainable",+ "salvia",+ "organic",+ "thundercats",+ "PBR",+ "iPhone",+ "lo-fi",+ "skateboard",+ "jean shorts",+ "next level",+ "beard",+ "tattooed",+ "trust fund",+ "Four Loko",+ "master cleanse",+ "ethical",+ "high life",+ "wolf moon",+ "fanny pack",+ "Terry Richardson",+ "8-bit",+ "Carles",+ "Shoreditch",+ "seitan",+ "freegan",+ "keffiyeh",+ "biodiesel",+ "quinoa",+ "farm-to-table",+ "fixie",+ "viral",+ "chambray",+ "scenester",+ "leggings",+ "readymade",+ "Brooklyn",+ "Wayfarers",+ "Marfa",+ "put a bird on it",+ "dreamcatcher",+ "photo booth",+ "tofu",+ "mlkshk",+ "vegan",+ "vinyl",+ "DIY",+ "banh mi",+ "bicycle rights",+ "before they sold out",+ "gluten-free",+ "yr butcher blog",+ "whatever",+ "+1",+ "Cosby Sweater",+ "VHS",+ "messenger bag",+ "cred",+ "locavore",+ "mustache",+ "tumblr",+ "Portland",+ "mixtape",+ "fap",+ "letterpress",+ "McSweeney's",+ "stumptown",+ "brunch",+ "Wes Anderson",+ "irony",+ "echo park",+ ]enddef generate_sentence(num_words)- sentence = hipster_words.sample(num_words).join(' ').capitalize + '.'- sentence.force_encoding('UTF-8')+ sentence = hipster_words.sample(num_words).join(" ").capitalize + "."+ sentence.force_encoding("UTF-8")enddef generate_email- email = hipster_words.sample.delete(' ') + '@' + hipster_words.sample.delete(' ') + '.com'- email.delete("'").force_encoding('UTF-8')+ email = hipster_words.sample.delete(" ") + "@" + hipster_words.sample.delete(" ") + ".com"+ email.delete("'").force_encoding("UTF-8")enddef image_posts- @image_posts ||= ["http://i.imgur.com/CnRF48R.jpg\n\n", "http://i.imgur.com/2iaeK.png\n\n", "http://i.imgur.com/WSD5t61.jpg\n\n", "http://i.imgur.com/GUldmUd.jpg\n\n", "http://i.imgur.com/nJnb6Bj.jpg\n\n", "http://i.imgur.com/eljDYjm.jpg\n\n", "http://i.imgur.com/5yZMWyY.png\n\n", "http://i.imgur.com/2iCPGm2.jpg\n\n"]+ @image_posts ||= [+ "http://i.imgur.com/CnRF48R.jpg\n\n",+ "http://i.imgur.com/2iaeK.png\n\n",+ "http://i.imgur.com/WSD5t61.jpg\n\n",+ "http://i.imgur.com/GUldmUd.jpg\n\n",+ "http://i.imgur.com/nJnb6Bj.jpg\n\n",+ "http://i.imgur.com/eljDYjm.jpg\n\n",+ "http://i.imgur.com/5yZMWyY.png\n\n",+ "http://i.imgur.com/2iCPGm2.jpg\n\n",+ ]enddef wikipedia_posts- @wikipedia_posts ||= ["http://en.wikipedia.org/wiki/Dwarf_fortress\n\n", "http://en.wikipedia.org/wiki/Action_plan\n\n", "http://en.wikipedia.org/wiki/Chang%27e_3\n\n", "http://en.wikipedia.org/wiki/Carl_sagan\n\n", "http://en.wikipedia.org/wiki/Chasmosaurus\n\n", "http://en.wikipedia.org/wiki/Indian_Space_Research_Organisation\n\n", "http://en.wikipedia.org/wiki/Rockstar_Consortium\n\n", "http://en.wikipedia.org/wiki/Manitoulin_island\n\n"]+ @wikipedia_posts ||= [+ "http://en.wikipedia.org/wiki/Dwarf_fortress\n\n",+ "http://en.wikipedia.org/wiki/Action_plan\n\n",+ "http://en.wikipedia.org/wiki/Chang%27e_3\n\n",+ "http://en.wikipedia.org/wiki/Carl_sagan\n\n",+ "http://en.wikipedia.org/wiki/Chasmosaurus\n\n",+ "http://en.wikipedia.org/wiki/Indian_Space_Research_Organisation\n\n",+ "http://en.wikipedia.org/wiki/Rockstar_Consortium\n\n",+ "http://en.wikipedia.org/wiki/Manitoulin_island\n\n",+ ]endenddiff --git a/lib/tasks/typepad.thor b/lib/tasks/typepad.thorindex 4341fb04dc8..87312d47c57 100644--- a/lib/tasks/typepad.thor+++ b/lib/tasks/typepad.thor@@ -1,21 +1,21 @@# frozen_string_literal: true-require 'open-uri'+require "open-uri"class Typepad < Thor-desc "import", "Imports posts from a Disqus XML export"- method_option :file, aliases: '-f', required: true, desc: "The typepad file to import"- method_option :post_as, aliases: '-p', required: true, desc: "The Discourse username to post as"- method_option :google_api, aliases: '-g', required: false, desc: "The google plus API key to use to fetch usernames"+ method_option :file, aliases: "-f", required: true, desc: "The typepad file to import"+ method_option :post_as, aliases: "-p", required: true, desc: "The Discourse username to post as"+ method_option :google_api,+ aliases: "-g",+ required: false,+ desc: "The google plus API key to use to fetch usernames"def import- require './config/environment'+ require "./config/environment"backup_settings = {}- %w(blocked_email_domains).each do |s|- backup_settings[s] = SiteSetting.get(s)- end+ %w[blocked_email_domains].each { |s| backup_settings[s] = SiteSetting.get(s) }user = User.where(username_lower: options[:post_as].downcase).firstif user.nil?@@ -31,26 +31,24 @@ class Typepad < Thorinput = ""entries = []- File.open(options[:file]).each_line do |l|- l = l.scrub-- if l =~ /\A--------\z/- parsed_entry = process_entry(input)- if parsed_entry- puts "Parsed #{parsed_entry[:title]}"- entries << parsed_entry+ File+ .open(options[:file])+ .each_line do |l|+ l = l.scrub++ if l =~ /\A--------\z/+ parsed_entry = process_entry(input)+ if parsed_entry+ puts "Parsed #{parsed_entry[:title]}"+ entries << parsed_entry+ end+ input = ""+ else+ input << lend- input = ""- else- input << lend- end- entries.each_with_index do |e, i|- if e[:title] =~ /Head/- puts "#{i}: #{e[:title]}"- end- end+ entries.each_with_index { |e, i| puts "#{i}: #{e[:title]}" if e[:title] =~ /Head/ }RateLimiter.disableSiteSetting.blocked_email_domains = ""@@ -74,13 +72,23 @@ class Typepad < Thoremail = c[:email]post_user = User.where(email: email).firstif post_user.blank?- post_user = User.create!(name: c[:name], email: email, username: UserNameSuggester.suggest(username))+ post_user =+ User.create!(+ name: c[:name],+ email: email,+ username: UserNameSuggester.suggest(username),+ )endelsepost_user = User.where(username: username).firstif post_user.blank?suggested = UserNameSuggester.suggest(username)- post_user = User.create!(name: c[:name], email: "#{suggested}@no-email-found.com", username: suggested)+ post_user =+ User.create!(+ name: c[:name],+ email: "#{suggested}@no-email-found.com",+ username: suggested,+ )endend@@ -89,7 +97,7 @@ class Typepad < Thorraw: c[:body],cooked: c[:body],created_at: c[:date],- skip_validations: true+ skip_validations: true,}beginpost = PostCreator.new(post_user, attrs).create@@ -100,35 +108,34 @@ class Typepad < Thorendendend-ensureRateLimiter.enable- backup_settings.each do |s, v|- SiteSetting.set(s, v)- end+ backup_settings.each { |s, v| SiteSetting.set(s, v) }endprivatedef clean_type!(type)type.downcase!- type.gsub!(/ /, '_')+ type.gsub!(/ /, "_")typeenddef parse_meta_data(section)result = {}- section.split(/\n/).each do |l|- if l =~ /\A([A-Z\ ]+)\: (.*)\z/- key, value = Regexp.last_match[1], Regexp.last_match[2]- clean_type!(key)- value.strip!- result[key.to_sym] = value- else- result[:body] ||= ""- result[:body] << l << "\n"+ section+ .split(/\n/)+ .each do |l|+ if l =~ /\A([A-Z\ ]+)\: (.*)\z/+ key, value = Regexp.last_match[1], Regexp.last_match[2]+ clean_type!(key)+ value.strip!+ result[key.to_sym] = value+ else+ result[:body] ||= ""+ result[:body] << l << "\n"+ endend- endresultend@@ -159,35 +166,36 @@ class Typepad < Thorwhen :commentcomment = parse_comment(value).slice(:author, :email, :url, :body, :date)- if options[:google_api] && comment[:author] =~ /plus.google.com\/(\d+)/+ if options[:google_api] && comment[:author] =~ %r{plus.google.com/(\d+)}gplus_id = Regexp.last_match[1]from_redis = Discourse.redis.get("gplus:#{gplus_id}")if from_redis.blank?- json = ::JSON.parse(open("https://www.googleapis.com/plus/v1/people/#{gplus_id}?key=#{options[:google_api]}").read)- from_redis = json['displayName']+ json =+ ::JSON.parse(+ open(+ "https://www.googleapis.com/plus/v1/people/#{gplus_id}?key=#{options[:google_api]}",+ ).read,+ )+ from_redis = json["displayName"]Discourse.redis.set("gplus:#{gplus_id}", from_redis)endcomment[:author] = from_redisend- if comment[:author] =~ /([^\.]+)\.wordpress\.com/- comment[:author] = Regexp.last_match[1]- end+ comment[:author] = Regexp.last_match[1] if comment[:author] =~ /([^\.]+)\.wordpress\.com/- if comment[:author] =~ /([^\.]+)\.blogspot\.com/- comment[:author] = Regexp.last_match[1]- end+ comment[:author] = Regexp.last_match[1] if comment[:author] =~ /([^\.]+)\.blogspot\.com/- if comment[:author] =~ /twitter.com\/([a-zA-Z0-9]+)/+ if comment[:author] =~ %r{twitter.com/([a-zA-Z0-9]+)}comment[:author] = Regexp.last_match[1]end- if comment[:author] =~ /www.facebook.com\/profile.php\?id=(\d+)/+ if comment[:author] =~ %r{www.facebook.com/profile.php\?id=(\d+)}fb_id = Regexp.last_match[1]from_redis = Discourse.redis.get("fb:#{fb_id}")if from_redis.blank?json = ::JSON.parse(open("http://graph.facebook.com/#{fb_id}").read)- from_redis = json['username']+ from_redis = json["username"]Discourse.redis.set("fb:#{fb_id}", from_redis)endcomment[:author] = from_redis@@ -195,11 +203,11 @@ class Typepad < Thorcomment[:name] = comment[:author]if comment[:author]- comment[:author].gsub!(/\A[_\.]+/, '')- comment[:author].gsub!(/[_\.]+\z/, '')+ comment[:author].gsub!(/\A[_\.]+/, "")+ comment[:author].gsub!(/[_\.]+\z/, "")if comment[:author].size < 12- comment[:author].gsub!(/ /, '_')+ comment[:author].gsub!(/ /, "_")elsesegments = []current = ""@@ -225,14 +233,13 @@ class Typepad < Thorsegments << currentcomment[:author] = segments[0]- if segments.size > 1 && segments[1][0] =~ /[a-zA-Z]/- comment[:author] << segments[1][0]- end+ comment[:author] << segments[1][0] if segments.size > 1 && segments[1][0] =~ /[a-zA-Z]/endendcomment[:author] = "commenter" if comment[:author].blank?- comment[:author] = "codinghorror" if comment[:author] == "Jeff Atwood" || comment[:author] == "JeffAtwood" || comment[:author] == "Jeff_Atwood"+ comment[:author] = "codinghorror" if comment[:author] == "Jeff Atwood" ||+ comment[:author] == "JeffAtwood" || comment[:author] == "Jeff_Atwood"comment[:date] = comment[:date] ? DateTime.strptime(comment[:date], "%m/%d/%Y") : Time.nowentry[:comments] << comment if comment[:body].present?@@ -241,5 +248,4 @@ class Typepad < Thorentry[:title] && entry[:body] ? entry : nilend-end