From 8f4c2e85e7e11de9817a14629ff3a780dcf5e036 Mon Sep 17 00:00:00 2001 From: Kira Date: Wed, 1 May 2019 13:17:08 -0700 Subject: [PATCH] Use updated dtext and deferred posts --- Gemfile | 2 +- Gemfile.lock | 18 ++++++------- app/controllers/application_controller.rb | 3 +++ app/controllers/concerns/deferred_posts.rb | 14 ++++++++++ app/helpers/application_helper.rb | 7 +++-- app/logical/deferred_posts.rb | 31 ---------------------- app/views/static/_deferred_posts.html.erb | 2 +- 7 files changed, 33 insertions(+), 44 deletions(-) create mode 100644 app/controllers/concerns/deferred_posts.rb delete mode 100644 app/logical/deferred_posts.rb diff --git a/Gemfile b/Gemfile index 514860ab8..e3ff7a86d 100644 --- a/Gemfile +++ b/Gemfile @@ -32,7 +32,7 @@ gem 'twitter' gem 'aws-sdk', '~> 2' gem 'responders' gem 'highline' -gem 'dtext_rb', :git => "https://github.com/r888888888/dtext_rb.git", :require => "dtext" +gem 'dtext_rb', :git => "https://github.com/zwagoth/dtext_rb.git", :require => "dtext" gem 'google-api-client' gem 'cityhash' gem 'bigquery', :git => "https://github.com/abronte/BigQuery.git", :ref => "b92b4e0b54574e3fde7ad910f39a67538ed387ad" diff --git a/Gemfile.lock b/Gemfile.lock index 2ba1a91f6..ca4280d66 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,13 +7,6 @@ GIT google-api-client (~> 0.9.3) googleauth (~> 0.5.0) -GIT - remote: https://github.com/r888888888/dtext_rb.git - revision: 073b369bf90217ab86fdef3d0f88a96e10343d37 - specs: - dtext_rb (1.9.2) - nokogiri (~> 1.8) - GIT remote: https://github.com/r888888888/ruby-imagespec.git revision: 2dab9811f4abb4fbaeea66feb42e388ba545b2d8 @@ -21,6 +14,13 @@ GIT specs: ruby-imagespec (0.3.1) +GIT + remote: https://github.com/zwagoth/dtext_rb.git + revision: 197f3d66d11ebdf4876b6e15817d4f8aa6fca5da + specs: + dtext_rb (1.10.0) + nokogiri (~> 1.8) + GEM remote: https://rubygems.org/ specs: @@ -265,9 +265,9 @@ GEM net-ssh (4.2.0) newrelic_rpm (5.0.0.342) nio4r (2.3.1) - nokogiri (1.10.1) + nokogiri (1.10.3) mini_portile2 (~> 2.4.0) - nokogiri (1.10.1-x64-mingw32) + nokogiri (1.10.3-x64-mingw32) mini_portile2 (~> 2.4.0) nokogumbo (1.5.0) nokogiri diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 730c61ee9..ce560bd2f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -15,6 +15,9 @@ class ApplicationController < ActionController::Base helper_method :show_moderation_notice? before_action :enable_cors + include DeferredPosts + helper_method :deferred_post_ids, :deferred_posts + rescue_from Exception, :with => :rescue_exception rescue_from User::PrivilegeError, :with => :access_denied rescue_from SessionLoader::AuthenticationFailure, :with => :authentication_failed diff --git a/app/controllers/concerns/deferred_posts.rb b/app/controllers/concerns/deferred_posts.rb new file mode 100644 index 000000000..b64bbb89c --- /dev/null +++ b/app/controllers/concerns/deferred_posts.rb @@ -0,0 +1,14 @@ +module DeferredPosts + extend ActiveSupport::Concern + + def deferred_post_ids + @post_ids_set ||= Set.new + end + + def deferred_posts + Post.where(id: deferred_post_ids.to_a).find_each.reduce({}) do |post_hash, p| + post_hash[p.id] = p.minimal_attributes + post_hash + end + end +end \ No newline at end of file diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ffd76ba1c..b3ed74947 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -77,7 +77,10 @@ module ApplicationHelper end def format_text(text, **options) - raw DTextRagel.parse(text, **options) + parsed = DTextRagel.parse(text, **options) + return raw "" if parsed.nil? + deferred_post_ids.merge(parsed[1]) + raw parsed[0] rescue DTextRagel::Error => e raw "" end @@ -244,7 +247,7 @@ module ApplicationHelper return "" if user.nil? post_id = user.avatar_id return "" unless post_id - DeferredPosts.add(post_id) + deferred_post_ids.add(post_id) tag.div class: 'post-thumb placeholder', id: "tp-#{post_id}", 'data-id': post_id do tag.img class: 'thumb-img placeholder', src: '/images/thumb-preview.png', height: 100, width: 100 end diff --git a/app/logical/deferred_posts.rb b/app/logical/deferred_posts.rb deleted file mode 100644 index 0564e8625..000000000 --- a/app/logical/deferred_posts.rb +++ /dev/null @@ -1,31 +0,0 @@ -class DeferredPosts - KEY = :deferred_posts - - def self.add(post_id) - raise ArgumentError.new "post id must be a number" if post_id.nil? || !post_id.respond_to?(:to_i) - posts = RequestStore[KEY] || [] - posts << post_id.to_i - RequestStore[KEY] = posts - end - - def self.remove(post_id) - posts = RequestStore[KEY] || [] - RequestStore[KEY] = posts - [post_id] - end - - def self.clear - RequestStore[KEY] = [] - end - - def self.dump - post_ids = RequestStore[KEY] || [] - post_ids.uniq! - return {} if post_ids.size == 0 - post_hash = {} - posts = Post.where(id: post_ids) - posts.find_each do |p| - post_hash[p.id] = p.minimal_attributes - end - post_hash - end -end \ No newline at end of file diff --git a/app/views/static/_deferred_posts.html.erb b/app/views/static/_deferred_posts.html.erb index 9ec99c8c0..d2b889058 100644 --- a/app/views/static/_deferred_posts.html.erb +++ b/app/views/static/_deferred_posts.html.erb @@ -1,3 +1,3 @@ \ No newline at end of file