discourse/script/import_scripts/quandora/quandora_question.rb

110 lines
2.8 KiB
Ruby
Raw Normal View History

2016-01-27 01:51:23 -05:00
require 'json'
require 'cgi'
require 'time'
class QuandoraQuestion
2017-07-27 21:20:09 -04:00
def initialize(question_json)
@question = JSON.parse question_json
end
2016-01-27 01:51:23 -05:00
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]
2017-07-27 21:20:09 -04:00
users[user[:id]] = user
2016-01-27 01:51:23 -05:00
end
users.values.to_a
end
2017-07-27 21:20:09 -04:00
def user_from_author(author)
2016-01-27 01:51:23 -05:00
email = author['email']
email = "#{author['uid']}@noemail.com" unless email
user = {}
user[:id] = author['uid']
user[:name] = "#{author['firstName']} #{author['lastName']}"
2017-07-27 21:20:09 -04:00
user[:email] = email
2016-01-27 01:51:23 -05:00
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
2017-07-27 21:20:09 -04:00
order_replies posts
2016-01-27 01:51:23 -05:00
end
2017-07-27 21:20:09 -04:00
def order_replies(posts)
2016-01-27 01:51:23 -05:00
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
2017-07-27 21:20:09 -04:00
def post_from_answer(answer)
2016-01-27 01:51:23 -05:00
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
2017-07-27 21:20:09 -04:00
def post_from_comment(comment, index, parent)
2016-01-27 01:51:23 -05:00
if comment['created']
created_at = Time.parse comment['created']
else
created_at = Time.parse parent['created']
end
parent_id = parent['uid']
2017-07-27 21:20:09 -04:00
parent_id = "#{parent['uid']}-#{index - 1}" if index > 0
2016-01-27 01:51:23 -05:00
post = {}
id = "#{parent['uid']}-#{index}"
2017-07-27 21:20:09 -04:00
post[:id] = id
2016-01-27 01:51:23 -05:00
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
2017-07-27 21:20:09 -04:00
def unescape(html)
2016-01-27 01:51:23 -05:00
return nil unless html
CGI.unescapeHTML html
end
end