hook up new forum thread views

This commit is contained in:
Andrew Brown 2021-07-21 13:50:32 -07:00
parent 16d823f1fe
commit 01ffde664a
5 changed files with 52 additions and 19 deletions

View File

@ -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

View File

@ -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

View File

@ -1,8 +1,9 @@
<% if Thredded.show_topic_followers %>
<div class="thredded--topic-followers">
<% if topic.followers.present? %>
<%= t('thredded.topics.followed_by')%>
<%= pluralize topic.followers.count, 'user' %>
<%# t('thredded.topics.followed_by') %>
<i class="material-icons tiny" style="position: relative; top: 2px;">people_alt</i>
<%= pluralize topic.followers.count, 'follower' %>
<%# topic.followers.each do |user| %>
<%#= user_link(user) %>
<%# end %>

View File

@ -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 %>

View File

@ -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'