discourse/script/import_scripts/quandora/quandora_question.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

112 lines
2.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
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
2018-06-07 01:28:18 -04: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
2016-01-27 01:51:23 -05:00
2018-06-07 01:28:18 -04:00
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
2016-01-27 01:51:23 -05:00
2018-06-07 01:28:18 -04:00
def user_from_author(author)
email = author['email']
email = "#{author['uid']}@noemail.com" unless email
2016-01-27 01:51:23 -05:00
2018-06-07 01:28:18 -04:00
user = {}
user[:id] = author['uid']
user[:name] = "#{author['firstName']} #{author['lastName']}"
user[:email] = email
user[:staged] = true
user
end
2016-01-27 01:51:23 -05:00
2018-06-07 01:28:18 -04:00
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
2016-01-27 01:51:23 -05:00
2018-06-07 01:28:18 -04:00
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
2016-01-27 01:51:23 -05:00
2018-06-07 01:28:18 -04:00
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
2016-01-27 01:51:23 -05:00
2018-06-07 01:28:18 -04:00
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
2016-01-27 01:51:23 -05:00
private
2018-06-07 01:28:18 -04:00
def unescape(html)
return nil unless html
CGI.unescapeHTML html
end
2016-01-27 01:51:23 -05:00
end