Use updated dtext and deferred posts

This commit is contained in:
Kira 2019-05-01 13:17:08 -07:00
parent f7e5eee844
commit 8f4c2e85e7
7 changed files with 33 additions and 44 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,3 @@
<script>
window.___deferred_posts = <%= raw DeferredPosts.dump.to_json %>;
window.___deferred_posts = <%= raw deferred_posts.to_json %>;
</script>