mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 03:51:07 +08:00
Make rubocop happy again.
This commit is contained in:
@ -8,102 +8,102 @@ class QuandoraQuestion
|
||||
@question = JSON.parse question_json
|
||||
end
|
||||
|
||||
def topic
|
||||
topic = {}
|
||||
topic[:id] = @question['uid']
|
||||
topic[:author_id] = @question['author']['uid']
|
||||
topic[:title] = unescape @question['title']
|
||||
topic[:raw] = unescape @question['content']
|
||||
topic[:created_at] = Time.parse @question['created']
|
||||
topic
|
||||
end
|
||||
def topic
|
||||
topic = {}
|
||||
topic[:id] = @question['uid']
|
||||
topic[:author_id] = @question['author']['uid']
|
||||
topic[:title] = unescape @question['title']
|
||||
topic[:raw] = unescape @question['content']
|
||||
topic[:created_at] = Time.parse @question['created']
|
||||
topic
|
||||
end
|
||||
|
||||
def users
|
||||
users = {}
|
||||
user = user_from_author @question['author']
|
||||
users[user[:id]] = user
|
||||
replies.each do |reply|
|
||||
user = user_from_author reply[:author]
|
||||
users[user[:id]] = user
|
||||
end
|
||||
users.values.to_a
|
||||
end
|
||||
def users
|
||||
users = {}
|
||||
user = user_from_author @question['author']
|
||||
users[user[:id]] = user
|
||||
replies.each do |reply|
|
||||
user = user_from_author reply[:author]
|
||||
users[user[:id]] = user
|
||||
end
|
||||
users.values.to_a
|
||||
end
|
||||
|
||||
def user_from_author(author)
|
||||
email = author['email']
|
||||
email = "#{author['uid']}@noemail.com" unless email
|
||||
def user_from_author(author)
|
||||
email = author['email']
|
||||
email = "#{author['uid']}@noemail.com" unless email
|
||||
|
||||
user = {}
|
||||
user[:id] = author['uid']
|
||||
user[:name] = "#{author['firstName']} #{author['lastName']}"
|
||||
user[:email] = email
|
||||
user[:staged] = true
|
||||
user
|
||||
end
|
||||
user = {}
|
||||
user[:id] = author['uid']
|
||||
user[:name] = "#{author['firstName']} #{author['lastName']}"
|
||||
user[:email] = email
|
||||
user[:staged] = true
|
||||
user
|
||||
end
|
||||
|
||||
def replies
|
||||
posts = []
|
||||
answers = @question['answersList']
|
||||
comments = @question['comments']
|
||||
comments.each_with_index do |comment, i|
|
||||
posts << post_from_comment(comment, i, @question)
|
||||
end
|
||||
answers.each do |answer|
|
||||
posts << post_from_answer(answer)
|
||||
comments = answer['comments']
|
||||
comments.each_with_index do |comment, i|
|
||||
posts << post_from_comment(comment, i, answer)
|
||||
end
|
||||
end
|
||||
order_replies posts
|
||||
end
|
||||
def replies
|
||||
posts = []
|
||||
answers = @question['answersList']
|
||||
comments = @question['comments']
|
||||
comments.each_with_index do |comment, i|
|
||||
posts << post_from_comment(comment, i, @question)
|
||||
end
|
||||
answers.each do |answer|
|
||||
posts << post_from_answer(answer)
|
||||
comments = answer['comments']
|
||||
comments.each_with_index do |comment, i|
|
||||
posts << post_from_comment(comment, i, answer)
|
||||
end
|
||||
end
|
||||
order_replies posts
|
||||
end
|
||||
|
||||
def order_replies(posts)
|
||||
posts = posts.sort_by { |p| p[:created_at] }
|
||||
posts.each_with_index do |p, i|
|
||||
p[:post_number] = i + 2
|
||||
end
|
||||
posts.each do |p|
|
||||
parent = posts.select { |pp| pp[:id] == p[:parent_id] }
|
||||
p[:reply_to_post_number] = parent[0][:post_number] if parent.size > 0
|
||||
end
|
||||
posts
|
||||
end
|
||||
def order_replies(posts)
|
||||
posts = posts.sort_by { |p| p[:created_at] }
|
||||
posts.each_with_index do |p, i|
|
||||
p[:post_number] = i + 2
|
||||
end
|
||||
posts.each do |p|
|
||||
parent = posts.select { |pp| pp[:id] == p[:parent_id] }
|
||||
p[:reply_to_post_number] = parent[0][:post_number] if parent.size > 0
|
||||
end
|
||||
posts
|
||||
end
|
||||
|
||||
def post_from_answer(answer)
|
||||
post = {}
|
||||
post[:id] = answer['uid']
|
||||
post[:parent_id] = @question['uid']
|
||||
post[:author] = answer['author']
|
||||
post[:author_id] = answer['author']['uid']
|
||||
post[:raw] = unescape answer['content']
|
||||
post[:created_at] = Time.parse answer['created']
|
||||
post
|
||||
end
|
||||
def post_from_answer(answer)
|
||||
post = {}
|
||||
post[:id] = answer['uid']
|
||||
post[:parent_id] = @question['uid']
|
||||
post[:author] = answer['author']
|
||||
post[:author_id] = answer['author']['uid']
|
||||
post[:raw] = unescape answer['content']
|
||||
post[:created_at] = Time.parse answer['created']
|
||||
post
|
||||
end
|
||||
|
||||
def post_from_comment(comment, index, parent)
|
||||
if comment['created']
|
||||
created_at = Time.parse comment['created']
|
||||
else
|
||||
created_at = Time.parse parent['created']
|
||||
end
|
||||
parent_id = parent['uid']
|
||||
parent_id = "#{parent['uid']}-#{index - 1}" if index > 0
|
||||
post = {}
|
||||
id = "#{parent['uid']}-#{index}"
|
||||
post[:id] = id
|
||||
post[:parent_id] = parent_id
|
||||
post[:author] = comment['author']
|
||||
post[:author_id] = comment['author']['uid']
|
||||
post[:raw] = unescape comment['text']
|
||||
post[:created_at] = created_at
|
||||
post
|
||||
end
|
||||
def post_from_comment(comment, index, parent)
|
||||
if comment['created']
|
||||
created_at = Time.parse comment['created']
|
||||
else
|
||||
created_at = Time.parse parent['created']
|
||||
end
|
||||
parent_id = parent['uid']
|
||||
parent_id = "#{parent['uid']}-#{index - 1}" if index > 0
|
||||
post = {}
|
||||
id = "#{parent['uid']}-#{index}"
|
||||
post[:id] = id
|
||||
post[:parent_id] = parent_id
|
||||
post[:author] = comment['author']
|
||||
post[:author_id] = comment['author']['uid']
|
||||
post[:raw] = unescape comment['text']
|
||||
post[:created_at] = created_at
|
||||
post
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def unescape(html)
|
||||
return nil unless html
|
||||
CGI.unescapeHTML html
|
||||
end
|
||||
def unescape(html)
|
||||
return nil unless html
|
||||
CGI.unescapeHTML html
|
||||
end
|
||||
end
|
||||
|
@ -18,81 +18,81 @@ class SocialcastMessage
|
||||
}
|
||||
}
|
||||
|
||||
def initialize(message_json)
|
||||
@parsed_json = JSON.parse message_json
|
||||
end
|
||||
def initialize(message_json)
|
||||
@parsed_json = JSON.parse message_json
|
||||
end
|
||||
|
||||
def topic
|
||||
topic = {}
|
||||
topic[:id] = @parsed_json['id']
|
||||
topic[:author_id] = @parsed_json['user']['id']
|
||||
topic[:title] = title
|
||||
topic[:raw] = @parsed_json['body']
|
||||
topic[:created_at] = Time.parse @parsed_json['created_at']
|
||||
topic[:tags] = tags
|
||||
topic[:category] = category
|
||||
topic
|
||||
end
|
||||
def topic
|
||||
topic = {}
|
||||
topic[:id] = @parsed_json['id']
|
||||
topic[:author_id] = @parsed_json['user']['id']
|
||||
topic[:title] = title
|
||||
topic[:raw] = @parsed_json['body']
|
||||
topic[:created_at] = Time.parse @parsed_json['created_at']
|
||||
topic[:tags] = tags
|
||||
topic[:category] = category
|
||||
topic
|
||||
end
|
||||
|
||||
def title
|
||||
CreateTitle.from_body @parsed_json['body']
|
||||
end
|
||||
def title
|
||||
CreateTitle.from_body @parsed_json['body']
|
||||
end
|
||||
|
||||
def tags
|
||||
tags = []
|
||||
if group
|
||||
if TAGS_AND_CATEGORIES[group]
|
||||
tags = TAGS_AND_CATEGORIES[group][:tags]
|
||||
else
|
||||
tags << group
|
||||
end
|
||||
end
|
||||
tags << DEFAULT_TAG
|
||||
tags
|
||||
end
|
||||
def tags
|
||||
tags = []
|
||||
if group
|
||||
if TAGS_AND_CATEGORIES[group]
|
||||
tags = TAGS_AND_CATEGORIES[group][:tags]
|
||||
else
|
||||
tags << group
|
||||
end
|
||||
end
|
||||
tags << DEFAULT_TAG
|
||||
tags
|
||||
end
|
||||
|
||||
def category
|
||||
category = DEFAULT_CATEGORY
|
||||
if group && TAGS_AND_CATEGORIES[group]
|
||||
category = TAGS_AND_CATEGORIES[group][:category]
|
||||
end
|
||||
category
|
||||
end
|
||||
def category
|
||||
category = DEFAULT_CATEGORY
|
||||
if group && TAGS_AND_CATEGORIES[group]
|
||||
category = TAGS_AND_CATEGORIES[group][:category]
|
||||
end
|
||||
category
|
||||
end
|
||||
|
||||
def group
|
||||
@parsed_json['group']['groupname'].downcase if @parsed_json['group'] && @parsed_json['group']['groupname']
|
||||
end
|
||||
def group
|
||||
@parsed_json['group']['groupname'].downcase if @parsed_json['group'] && @parsed_json['group']['groupname']
|
||||
end
|
||||
|
||||
def url
|
||||
@parsed_json['url']
|
||||
end
|
||||
def url
|
||||
@parsed_json['url']
|
||||
end
|
||||
|
||||
def message_type
|
||||
@parsed_json['message_type']
|
||||
end
|
||||
def message_type
|
||||
@parsed_json['message_type']
|
||||
end
|
||||
|
||||
def replies
|
||||
posts = []
|
||||
comments = @parsed_json['comments']
|
||||
comments.each do |comment|
|
||||
posts << post_from_comment(comment)
|
||||
end
|
||||
posts
|
||||
end
|
||||
def replies
|
||||
posts = []
|
||||
comments = @parsed_json['comments']
|
||||
comments.each do |comment|
|
||||
posts << post_from_comment(comment)
|
||||
end
|
||||
posts
|
||||
end
|
||||
|
||||
def post_from_comment(comment)
|
||||
post = {}
|
||||
post[:id] = comment['id']
|
||||
post[:author_id] = comment['user']['id']
|
||||
post[:raw] = comment['text']
|
||||
post[:created_at] = Time.parse comment['created_at']
|
||||
post
|
||||
end
|
||||
def post_from_comment(comment)
|
||||
post = {}
|
||||
post[:id] = comment['id']
|
||||
post[:author_id] = comment['user']['id']
|
||||
post[:raw] = comment['text']
|
||||
post[:created_at] = Time.parse comment['created_at']
|
||||
post
|
||||
end
|
||||
|
||||
private
|
||||
private
|
||||
|
||||
def unescape(html)
|
||||
return nil unless html
|
||||
CGI.unescapeHTML html
|
||||
end
|
||||
def unescape(html)
|
||||
return nil unless html
|
||||
CGI.unescapeHTML html
|
||||
end
|
||||
end
|
||||
|
@ -8,17 +8,17 @@ class SocialcastUser
|
||||
@parsed_json = JSON.parse user_json
|
||||
end
|
||||
|
||||
def user
|
||||
email = @parsed_json['contact_info']['email']
|
||||
email = "#{@parsed_json['id']}@noemail.com" unless email
|
||||
def user
|
||||
email = @parsed_json['contact_info']['email']
|
||||
email = "#{@parsed_json['id']}@noemail.com" unless email
|
||||
|
||||
user = {}
|
||||
user[:id] = @parsed_json['id']
|
||||
user[:name] = @parsed_json['name']
|
||||
user[:username] = @parsed_json['username']
|
||||
user[:email] = email
|
||||
user[:staged] = true
|
||||
user
|
||||
end
|
||||
user = {}
|
||||
user[:id] = @parsed_json['id']
|
||||
user[:name] = @parsed_json['name']
|
||||
user[:username] = @parsed_json['username']
|
||||
user[:email] = email
|
||||
user[:staged] = true
|
||||
user
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -28,226 +28,226 @@ class ImportScripts::Vanilla < ImportScripts::Base
|
||||
|
||||
private
|
||||
|
||||
def check_file_exist
|
||||
raise ArgumentError.new("File does not exist: #{@vanilla_file}") unless File.exist?(@vanilla_file)
|
||||
end
|
||||
def check_file_exist
|
||||
raise ArgumentError.new("File does not exist: #{@vanilla_file}") unless File.exist?(@vanilla_file)
|
||||
end
|
||||
|
||||
def parse_file
|
||||
puts "parsing file..."
|
||||
file = read_file
|
||||
def parse_file
|
||||
puts "parsing file..."
|
||||
file = read_file
|
||||
|
||||
# TODO: parse header & validate version number
|
||||
header = file.readline
|
||||
# TODO: parse header & validate version number
|
||||
header = file.readline
|
||||
|
||||
until file.eof?
|
||||
line = file.readline
|
||||
next if line.blank?
|
||||
next if line.start_with?("//")
|
||||
until file.eof?
|
||||
line = file.readline
|
||||
next if line.blank?
|
||||
next if line.start_with?("//")
|
||||
|
||||
if m = /^Table: (\w+)/.match(line)
|
||||
# extract table name
|
||||
table = m[1].underscore.pluralize
|
||||
# read the data until an empty line
|
||||
data = []
|
||||
# first line is the table definition, turn that into a proper csv header
|
||||
data << file.readline.split(",").map { |c| c.split(":")[0].underscore }.join(",")
|
||||
until (line = file.readline).blank?
|
||||
data << line.strip
|
||||
end
|
||||
# PERF: don't parse useless tables
|
||||
useless_tables = ["user_meta"]
|
||||
useless_tables << "activities" unless @use_lastest_activity_as_user_bio
|
||||
next if useless_tables.include?(table)
|
||||
# parse the data
|
||||
puts "parsing #{table}..."
|
||||
parsed_data = CSV.parse(data.join("\n"), headers: true, header_converters: :symbol).map { |row| row.to_hash }
|
||||
instance_variable_set("@#{table}".to_sym, parsed_data)
|
||||
if m = /^Table: (\w+)/.match(line)
|
||||
# extract table name
|
||||
table = m[1].underscore.pluralize
|
||||
# read the data until an empty line
|
||||
data = []
|
||||
# first line is the table definition, turn that into a proper csv header
|
||||
data << file.readline.split(",").map { |c| c.split(":")[0].underscore }.join(",")
|
||||
until (line = file.readline).blank?
|
||||
data << line.strip
|
||||
end
|
||||
# PERF: don't parse useless tables
|
||||
useless_tables = ["user_meta"]
|
||||
useless_tables << "activities" unless @use_lastest_activity_as_user_bio
|
||||
next if useless_tables.include?(table)
|
||||
# parse the data
|
||||
puts "parsing #{table}..."
|
||||
parsed_data = CSV.parse(data.join("\n"), headers: true, header_converters: :symbol).map { |row| row.to_hash }
|
||||
instance_variable_set("@#{table}".to_sym, parsed_data)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def read_file
|
||||
puts "reading file..."
|
||||
string = File.read(@vanilla_file).gsub("\\N", "")
|
||||
.gsub(/\\$\n/m, "\\n")
|
||||
.gsub("\\,", ",")
|
||||
.gsub(/(?<!\\)\\"/, '""')
|
||||
.gsub(/\\\\\\"/, '\\""')
|
||||
StringIO.new(string)
|
||||
end
|
||||
def read_file
|
||||
puts "reading file..."
|
||||
string = File.read(@vanilla_file).gsub("\\N", "")
|
||||
.gsub(/\\$\n/m, "\\n")
|
||||
.gsub("\\,", ",")
|
||||
.gsub(/(?<!\\)\\"/, '""')
|
||||
.gsub(/\\\\\\"/, '\\""')
|
||||
StringIO.new(string)
|
||||
end
|
||||
|
||||
def import_users
|
||||
puts "", "importing users..."
|
||||
def import_users
|
||||
puts "", "importing users..."
|
||||
|
||||
admin_role_id = @roles.select { |r| r[:name] == "Administrator" }.first[:role_id]
|
||||
moderator_role_id = @roles.select { |r| r[:name] == "Moderator" }.first[:role_id]
|
||||
admin_role_id = @roles.select { |r| r[:name] == "Administrator" }.first[:role_id]
|
||||
moderator_role_id = @roles.select { |r| r[:name] == "Moderator" }.first[:role_id]
|
||||
|
||||
activities = (@activities || []).reject { |a| a[:activity_user_id] != a[:regarding_user_id] }
|
||||
activities = (@activities || []).reject { |a| a[:activity_user_id] != a[:regarding_user_id] }
|
||||
|
||||
create_users(@users) do |user|
|
||||
next if user[:name] == "[Deleted User]"
|
||||
create_users(@users) do |user|
|
||||
next if user[:name] == "[Deleted User]"
|
||||
|
||||
if @use_lastest_activity_as_user_bio
|
||||
last_activity = activities.select { |a| user[:user_id] == a[:activity_user_id] }.last
|
||||
bio_raw = last_activity.try(:[], :story) || ""
|
||||
else
|
||||
bio_raw = user[:discovery_text]
|
||||
end
|
||||
|
||||
u = {
|
||||
id: user[:user_id],
|
||||
email: user[:email],
|
||||
username: user[:name],
|
||||
created_at: parse_date(user[:date_inserted]),
|
||||
bio_raw: clean_up(bio_raw),
|
||||
avatar_url: user[:photo],
|
||||
moderator: @user_roles.select { |ur| ur[:user_id] == user[:user_id] }.map { |ur| ur[:role_id] }.include?(moderator_role_id),
|
||||
admin: @user_roles.select { |ur| ur[:user_id] == user[:user_id] }.map { |ur| ur[:role_id] }.include?(admin_role_id),
|
||||
}
|
||||
|
||||
u
|
||||
if @use_lastest_activity_as_user_bio
|
||||
last_activity = activities.select { |a| user[:user_id] == a[:activity_user_id] }.last
|
||||
bio_raw = last_activity.try(:[], :story) || ""
|
||||
else
|
||||
bio_raw = user[:discovery_text]
|
||||
end
|
||||
end
|
||||
|
||||
def import_categories
|
||||
puts "", "importing categories..."
|
||||
|
||||
# save some information about the root category
|
||||
@root_category = @categories.select { |c| c[:category_id] == "-1" }.first
|
||||
@root_category_created_at = parse_date(@root_category[:date_inserted])
|
||||
|
||||
# removes root category
|
||||
@categories.reject! { |c| c[:category_id] == "-1" }
|
||||
|
||||
# adds root's child categories
|
||||
first_level_categories = @categories.select { |c| c[:parent_category_id] == "-1" }
|
||||
if first_level_categories.count > 0
|
||||
puts "", "importing first-level categories..."
|
||||
create_categories(first_level_categories) { |category| import_category(category) }
|
||||
|
||||
# adds other categories
|
||||
second_level_categories = @categories.select { |c| c[:parent_category_id] != "-1" }
|
||||
if second_level_categories.count > 0
|
||||
puts "", "importing second-level categories..."
|
||||
create_categories(second_level_categories) { |category| import_category(category) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def import_category(category)
|
||||
c = {
|
||||
id: category[:category_id],
|
||||
name: category[:name],
|
||||
user_id: user_id_from_imported_user_id(category[:insert_user_id]) || Discourse::SYSTEM_USER_ID,
|
||||
position: category[:sort].to_i,
|
||||
created_at: parse_category_date(category[:date_inserted]),
|
||||
description: clean_up(category[:description]),
|
||||
u = {
|
||||
id: user[:user_id],
|
||||
email: user[:email],
|
||||
username: user[:name],
|
||||
created_at: parse_date(user[:date_inserted]),
|
||||
bio_raw: clean_up(bio_raw),
|
||||
avatar_url: user[:photo],
|
||||
moderator: @user_roles.select { |ur| ur[:user_id] == user[:user_id] }.map { |ur| ur[:role_id] }.include?(moderator_role_id),
|
||||
admin: @user_roles.select { |ur| ur[:user_id] == user[:user_id] }.map { |ur| ur[:role_id] }.include?(admin_role_id),
|
||||
}
|
||||
if category[:parent_category_id] != "-1"
|
||||
c[:parent_category_id] = category_id_from_imported_category_id(category[:parent_category_id])
|
||||
end
|
||||
c
|
||||
|
||||
u
|
||||
end
|
||||
end
|
||||
|
||||
def parse_category_date(date)
|
||||
date == "0000-00-00 00:00:00" ? @root_category_created_at : parse_date(date)
|
||||
end
|
||||
def import_categories
|
||||
puts "", "importing categories..."
|
||||
|
||||
def import_topics
|
||||
puts "", "importing topics..."
|
||||
# save some information about the root category
|
||||
@root_category = @categories.select { |c| c[:category_id] == "-1" }.first
|
||||
@root_category_created_at = parse_date(@root_category[:date_inserted])
|
||||
|
||||
create_posts(@discussions) do |discussion|
|
||||
{
|
||||
id: "discussion#" + discussion[:discussion_id],
|
||||
user_id: user_id_from_imported_user_id(discussion[:insert_user_id]) || Discourse::SYSTEM_USER_ID,
|
||||
title: discussion[:name],
|
||||
category: category_id_from_imported_category_id(discussion[:category_id]),
|
||||
raw: clean_up(discussion[:body]),
|
||||
created_at: parse_date(discussion[:date_inserted]),
|
||||
}
|
||||
# removes root category
|
||||
@categories.reject! { |c| c[:category_id] == "-1" }
|
||||
|
||||
# adds root's child categories
|
||||
first_level_categories = @categories.select { |c| c[:parent_category_id] == "-1" }
|
||||
if first_level_categories.count > 0
|
||||
puts "", "importing first-level categories..."
|
||||
create_categories(first_level_categories) { |category| import_category(category) }
|
||||
|
||||
# adds other categories
|
||||
second_level_categories = @categories.select { |c| c[:parent_category_id] != "-1" }
|
||||
if second_level_categories.count > 0
|
||||
puts "", "importing second-level categories..."
|
||||
create_categories(second_level_categories) { |category| import_category(category) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def import_posts
|
||||
puts "", "importing posts..."
|
||||
|
||||
create_posts(@comments) do |comment|
|
||||
next unless t = topic_lookup_from_imported_post_id("discussion#" + comment[:discussion_id])
|
||||
|
||||
{
|
||||
id: "comment#" + comment[:comment_id],
|
||||
user_id: user_id_from_imported_user_id(comment[:insert_user_id]) || Discourse::SYSTEM_USER_ID,
|
||||
topic_id: t[:topic_id],
|
||||
raw: clean_up(comment[:body]),
|
||||
created_at: parse_date(comment[:date_inserted]),
|
||||
}
|
||||
end
|
||||
def import_category(category)
|
||||
c = {
|
||||
id: category[:category_id],
|
||||
name: category[:name],
|
||||
user_id: user_id_from_imported_user_id(category[:insert_user_id]) || Discourse::SYSTEM_USER_ID,
|
||||
position: category[:sort].to_i,
|
||||
created_at: parse_category_date(category[:date_inserted]),
|
||||
description: clean_up(category[:description]),
|
||||
}
|
||||
if category[:parent_category_id] != "-1"
|
||||
c[:parent_category_id] = category_id_from_imported_category_id(category[:parent_category_id])
|
||||
end
|
||||
c
|
||||
end
|
||||
|
||||
def import_private_topics
|
||||
puts "", "importing private topics..."
|
||||
def parse_category_date(date)
|
||||
date == "0000-00-00 00:00:00" ? @root_category_created_at : parse_date(date)
|
||||
end
|
||||
|
||||
create_posts(@conversations) do |conversation|
|
||||
next if conversation[:first_message_id].blank?
|
||||
def import_topics
|
||||
puts "", "importing topics..."
|
||||
|
||||
# list all other user ids in the conversation
|
||||
user_ids_in_conversation = @user_conversations.select { |uc| uc[:conversation_id] == conversation[:conversation_id] && uc[:user_id] != conversation[:insert_user_id] }
|
||||
.map { |uc| uc[:user_id] }
|
||||
# retrieve their emails
|
||||
user_emails_in_conversation = @users.select { |u| user_ids_in_conversation.include?(u[:user_id]) }
|
||||
.map { |u| u[:email] }
|
||||
# retrieve their usernames from the database
|
||||
target_usernames = User.where("email IN (?)", user_emails_in_conversation).pluck(:username).to_a
|
||||
|
||||
next if target_usernames.blank?
|
||||
|
||||
user = find_user_by_import_id(conversation[:insert_user_id]) || Discourse.system_user
|
||||
first_message = @conversation_messages.select { |cm| cm[:message_id] == conversation[:first_message_id] }.first
|
||||
|
||||
{
|
||||
archetype: Archetype.private_message,
|
||||
id: "conversation#" + conversation[:conversation_id],
|
||||
user_id: user.id,
|
||||
title: "Private message from #{user.username}",
|
||||
target_usernames: target_usernames,
|
||||
raw: clean_up(first_message[:body]),
|
||||
created_at: parse_date(conversation[:date_inserted]),
|
||||
}
|
||||
end
|
||||
create_posts(@discussions) do |discussion|
|
||||
{
|
||||
id: "discussion#" + discussion[:discussion_id],
|
||||
user_id: user_id_from_imported_user_id(discussion[:insert_user_id]) || Discourse::SYSTEM_USER_ID,
|
||||
title: discussion[:name],
|
||||
category: category_id_from_imported_category_id(discussion[:category_id]),
|
||||
raw: clean_up(discussion[:body]),
|
||||
created_at: parse_date(discussion[:date_inserted]),
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def import_private_posts
|
||||
puts "", "importing private posts..."
|
||||
def import_posts
|
||||
puts "", "importing posts..."
|
||||
|
||||
first_message_ids = Set.new(@conversations.map { |c| c[:first_message_id] }.to_a)
|
||||
@conversation_messages.reject! { |cm| first_message_ids.include?(cm[:message_id]) }
|
||||
create_posts(@comments) do |comment|
|
||||
next unless t = topic_lookup_from_imported_post_id("discussion#" + comment[:discussion_id])
|
||||
|
||||
create_posts(@conversation_messages) do |message|
|
||||
next unless t = topic_lookup_from_imported_post_id("conversation#" + message[:conversation_id])
|
||||
|
||||
{
|
||||
archetype: Archetype.private_message,
|
||||
id: "message#" + message[:message_id],
|
||||
user_id: user_id_from_imported_user_id(message[:insert_user_id]) || Discourse::SYSTEM_USER_ID,
|
||||
topic_id: t[:topic_id],
|
||||
raw: clean_up(message[:body]),
|
||||
created_at: parse_date(message[:date_inserted]),
|
||||
}
|
||||
end
|
||||
{
|
||||
id: "comment#" + comment[:comment_id],
|
||||
user_id: user_id_from_imported_user_id(comment[:insert_user_id]) || Discourse::SYSTEM_USER_ID,
|
||||
topic_id: t[:topic_id],
|
||||
raw: clean_up(comment[:body]),
|
||||
created_at: parse_date(comment[:date_inserted]),
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def parse_date(date)
|
||||
DateTime.strptime(date, "%Y-%m-%d %H:%M:%S")
|
||||
end
|
||||
def import_private_topics
|
||||
puts "", "importing private topics..."
|
||||
|
||||
def clean_up(raw)
|
||||
return "" if raw.blank?
|
||||
raw.gsub("\\n", "\n")
|
||||
.gsub(/<\/?pre\s*>/i, "\n```\n")
|
||||
.gsub(/<\/?code\s*>/i, "`")
|
||||
.gsub("<", "<")
|
||||
.gsub(">", ">")
|
||||
create_posts(@conversations) do |conversation|
|
||||
next if conversation[:first_message_id].blank?
|
||||
|
||||
# list all other user ids in the conversation
|
||||
user_ids_in_conversation = @user_conversations.select { |uc| uc[:conversation_id] == conversation[:conversation_id] && uc[:user_id] != conversation[:insert_user_id] }
|
||||
.map { |uc| uc[:user_id] }
|
||||
# retrieve their emails
|
||||
user_emails_in_conversation = @users.select { |u| user_ids_in_conversation.include?(u[:user_id]) }
|
||||
.map { |u| u[:email] }
|
||||
# retrieve their usernames from the database
|
||||
target_usernames = User.where("email IN (?)", user_emails_in_conversation).pluck(:username).to_a
|
||||
|
||||
next if target_usernames.blank?
|
||||
|
||||
user = find_user_by_import_id(conversation[:insert_user_id]) || Discourse.system_user
|
||||
first_message = @conversation_messages.select { |cm| cm[:message_id] == conversation[:first_message_id] }.first
|
||||
|
||||
{
|
||||
archetype: Archetype.private_message,
|
||||
id: "conversation#" + conversation[:conversation_id],
|
||||
user_id: user.id,
|
||||
title: "Private message from #{user.username}",
|
||||
target_usernames: target_usernames,
|
||||
raw: clean_up(first_message[:body]),
|
||||
created_at: parse_date(conversation[:date_inserted]),
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def import_private_posts
|
||||
puts "", "importing private posts..."
|
||||
|
||||
first_message_ids = Set.new(@conversations.map { |c| c[:first_message_id] }.to_a)
|
||||
@conversation_messages.reject! { |cm| first_message_ids.include?(cm[:message_id]) }
|
||||
|
||||
create_posts(@conversation_messages) do |message|
|
||||
next unless t = topic_lookup_from_imported_post_id("conversation#" + message[:conversation_id])
|
||||
|
||||
{
|
||||
archetype: Archetype.private_message,
|
||||
id: "message#" + message[:message_id],
|
||||
user_id: user_id_from_imported_user_id(message[:insert_user_id]) || Discourse::SYSTEM_USER_ID,
|
||||
topic_id: t[:topic_id],
|
||||
raw: clean_up(message[:body]),
|
||||
created_at: parse_date(message[:date_inserted]),
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def parse_date(date)
|
||||
DateTime.strptime(date, "%Y-%m-%d %H:%M:%S")
|
||||
end
|
||||
|
||||
def clean_up(raw)
|
||||
return "" if raw.blank?
|
||||
raw.gsub("\\n", "\n")
|
||||
.gsub(/<\/?pre\s*>/i, "\n```\n")
|
||||
.gsub(/<\/?code\s*>/i, "`")
|
||||
.gsub("<", "<")
|
||||
.gsub(">", ">")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user