mirror of
https://github.com/indentlabs/notebook.git
synced 2025-10-26 11:19:22 +00:00
add service for saving threads to documents
This commit is contained in:
parent
5b29f1bdcc
commit
3b1fd41b1e
43
app/services/forums_prosify_service.rb
Normal file
43
app/services/forums_prosify_service.rb
Normal file
@ -0,0 +1,43 @@
|
||||
class ForumsProsifyService < Service
|
||||
ENDLINE = "\r\n"
|
||||
|
||||
def self.prosify_text(thredded_topic_id, strip_parentheticals=true)
|
||||
topic = Thredded::Topic.find_by(id: thredded_topic_id)
|
||||
prose = ""
|
||||
|
||||
topic.posts.find_each do |post|
|
||||
paragraphs = post.content.split(ENDLINE)
|
||||
|
||||
paragraphs.each do |paragraph|
|
||||
prose += "\t#{paragraph}#{ENDLINE}" unless strip_parentheticals && post.content.start_with?('(') && post.content.end_with?(')')
|
||||
end
|
||||
end
|
||||
|
||||
prose
|
||||
end
|
||||
|
||||
def self.prosify_html(thredded_topic_id, strip_parentheticals=true)
|
||||
topic = Thredded::Topic.find_by(id: thredded_topic_id)
|
||||
prose = ""
|
||||
|
||||
user_display_name_cache = {}
|
||||
|
||||
topic.posts.find_each do |post|
|
||||
user_display_name_cache[post.user_id] = post.user.try(:display_name) || "Anonymous user" unless user_display_name_cache.key?(post.user_id)
|
||||
|
||||
tooltip = "authored by #{user_display_name_cache[post.user_id]}"
|
||||
prose += "<p class='tooltipped' data-tooltip='#{tooltip}' data-position='right'>#{post.content}</p>" unless strip_parentheticals && post.content.start_with?('(') && post.content.end_with?(')')
|
||||
end
|
||||
|
||||
prose
|
||||
end
|
||||
|
||||
def self.save_to_document(user, thredded_topic_id, strip_parentheticals=true)
|
||||
topic = Thredded::Topic.find_by(id: thredded_topic_id)
|
||||
|
||||
user.documents.create!(
|
||||
title: "#{topic.title} (forums post)",
|
||||
body: prosify_html(thredded_topic_id, strip_parentheticals)
|
||||
)
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user