diff --git a/app/controllers/thredded_proxy_controller.rb b/app/controllers/thredded_proxy_controller.rb index da905480..4c408ffe 100644 --- a/app/controllers/thredded_proxy_controller.rb +++ b/app/controllers/thredded_proxy_controller.rb @@ -1,4 +1,6 @@ class ThreddedProxyController < ApplicationController + before_action :authenticate_user!, only: [:view_as_plaintext, :view_as_irc_log, :save_to_document] + def topic topic = Thredded::Topic.find_by(slug: params[:slug]) if topic.present? @@ -10,4 +12,20 @@ class ThreddedProxyController < ApplicationController redirect_back(fallback_location: root_path, notice: "The link you tried to visit is invalid or no longer exists.") end end + + def view_as_plaintext + topic = Thredded::Topic.find_by(slug: params[:slug]) + render plain: ForumsProsifyService.prosify_text(topic) + end + + def view_as_irc_log + topic = Thredded::Topic.find_by(slug: params[:slug]) + render plain: ForumsProsifyService.prosify_irc_log(topic) + end + + def save_to_document + topic = Thredded::Topic.find_by(slug: params[:slug]) + document = ForumsProsifyService.save_to_document(current_user, topic) + redirect_to document, notice: "Thread saved to document!" + end end diff --git a/app/services/forums_prosify_service.rb b/app/services/forums_prosify_service.rb index 6fd9f243..0eab6d12 100644 --- a/app/services/forums_prosify_service.rb +++ b/app/services/forums_prosify_service.rb @@ -1,11 +1,10 @@ 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) + def self.prosify_text(thredded_topic, strip_parentheticals=true) prose = "" - topic.posts.find_each do |post| + thredded_topic.posts.find_each do |post| paragraphs = post.content.split(ENDLINE) paragraphs.each do |paragraph| @@ -16,13 +15,27 @@ class ForumsProsifyService < Service prose end - def self.prosify_html(thredded_topic_id, strip_parentheticals=true) - topic = Thredded::Topic.find_by(id: thredded_topic_id) + def self.prosify_irc_log(thredded_topic, strip_parentheticals=true) prose = "" - user_display_name_cache = {} - topic.posts.find_each do |post| + thredded_topic.posts.find_each do |post| + paragraphs = post.content.split(ENDLINE) + user_display_name_cache[post.user_id] = post.user.try(:display_name) || "Anonymous" unless user_display_name_cache.key?(post.user_id) + + paragraphs.each do |paragraph| + prose += "<#{user_display_name_cache[post.user_id]}> #{paragraph}#{ENDLINE}" unless strip_parentheticals && post.content.start_with?('(') && post.content.end_with?(')') + end + end + + prose + end + + def self.prosify_html(thredded_topic, strip_parentheticals=true) + prose = "" + user_display_name_cache = {} + + thredded_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]}" @@ -32,12 +45,10 @@ class ForumsProsifyService < Service prose end - def self.save_to_document(user, thredded_topic_id, strip_parentheticals=true) - topic = Thredded::Topic.find_by(id: thredded_topic_id) - + def self.save_to_document(user, thredded_topic, strip_parentheticals=true) user.documents.create!( - title: "#{topic.title} (forums post)", - body: prosify_html(thredded_topic_id, strip_parentheticals) + title: "#{thredded_topic.title} (forums post)", + body: prosify_html(thredded_topic, strip_parentheticals) ) end end \ No newline at end of file diff --git a/app/views/thredded/topics/_followers.html.erb b/app/views/thredded/topics/_followers.html.erb index 683dc031..572b784f 100644 --- a/app/views/thredded/topics/_followers.html.erb +++ b/app/views/thredded/topics/_followers.html.erb @@ -1,8 +1,9 @@ <% if Thredded.show_topic_followers %>
<% if topic.followers.present? %> - <%= t('thredded.topics.followed_by')%> - <%= pluralize topic.followers.count, 'user' %> + <%# t('thredded.topics.followed_by') %> + people_alt + <%= pluralize topic.followers.count, 'follower' %> <%# topic.followers.each do |user| %> <%#= user_link(user) %> <%# end %> diff --git a/app/views/thredded/topics/_header.html.erb b/app/views/thredded/topics/_header.html.erb index a44c7d56..cb3b0d94 100644 --- a/app/views/thredded/topics/_header.html.erb +++ b/app/views/thredded/topics/_header.html.erb @@ -22,11 +22,11 @@ class: 'thredded--post--dropdown--actions--item', rel: 'nofollow' %> <% end %> - <% if user_signed_in? %> - <%= link_to '#', class: 'thredded--post--dropdown--actions--item' do %> + <% if user_signed_in? && params.key?(:id) %> + <%= link_to main_app.documentize_topic_path(params[:id]), class: 'thredded--post--dropdown--actions--item' do %> Save to document <% end %> - <%= link_to '#', class: 'thredded--post--dropdown--actions--item', rel: 'nofollow' do %> + <%= link_to main_app.plaintext_topic_path(params[:id]), class: 'thredded--post--dropdown--actions--item', rel: 'nofollow' do %> View as plaintext <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 6e45512b..36729525 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -393,8 +393,11 @@ Rails.application.routes.draw do # get '/forum', to: 'emergency#temporarily_disabled' # get '/forum/:wildcard', to: 'emergency#temporarily_disabled' # get '/forum/:wildcard/:another', to: 'emergency#temporarily_disabled' - mount Thredded::Engine, at: '/forum', as: :thredded - get '/topic/:slug', to: 'thredded_proxy#topic', as: :topic + mount Thredded::Engine, at: '/forum', as: :thredded + get '/topic/:slug', to: 'thredded_proxy#topic', as: :topic + get '/plaintext/:slug', to: 'thredded_proxy#view_as_plaintext', as: :plaintext_topic + get '/irc_log/:slug', to: 'thredded_proxy#view_as_irc_log', as: :irc_log_topic + get '/save/:slug', to: 'thredded_proxy#save_to_document', as: :documentize_topic mount StripeEvent::Engine, at: '/webhooks/stripe'