From 4a5b7fa7f7fd7704bdce180ce6c3ff08d571045e Mon Sep 17 00:00:00 2001 From: Michael Rose Date: Sun, 9 Sep 2018 17:43:08 -0600 Subject: [PATCH] story: update DLP threads on story update Uses the DLP forums API to post to DLP when the story is updated. --- app/models/story.rb | 10 +++++- app/models/story_update.rb | 29 ++++++++--------- app/views/dlp_posts/story_update.text.erb | 14 ++++++++ app/workers/dlp_thread_notification_worker.rb | 19 +++++++++++ config/initializers/scryer.rb | 1 + lib/scryer/forum_client.rb | 32 +++++++++++++++++++ 6 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 app/views/dlp_posts/story_update.text.erb create mode 100644 app/workers/dlp_thread_notification_worker.rb create mode 100644 lib/scryer/forum_client.rb diff --git a/app/models/story.rb b/app/models/story.rb index fe43b74..ac0e5ad 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -92,7 +92,7 @@ class Story < ActiveRecord::Base story.save! if !orig || story.updated > orig.updated - StoryUpdate.new( + update = StoryUpdate.new( story_id: story.id, story_title: story.title, author_id: author.id, @@ -100,11 +100,19 @@ class Story < ActiveRecord::Base fandoms: story.fandoms, update_contents: doc ).save! + + notify_dlp(update) end story end + def self.notify_dlp(update) + StoryToThread.where(:story_id => update.story_id).each do |dlp_thread| + DlpThreadNotificationWorker.perform_async(update.id, dlp_thread.thread_id) + end + end + def self.load_from_es(index = 'ffn_index') # Open the "view" of the index response = $elasticsearch.search index: index, search_type: 'scan', scroll: '5m', size: 100, body: { diff --git a/app/models/story_update.rb b/app/models/story_update.rb index 7017d6d..6f12e89 100644 --- a/app/models/story_update.rb +++ b/app/models/story_update.rb @@ -14,21 +14,18 @@ # class StoryUpdate < ActiveRecord::Base - def self.fix_json - id = 2300000 - StoryUpdate.all.find_in_batches(start: id, batch_size: 1000) do |updates| - updates.each do |update| - if update.update_contents.is_a?(String) - begin - update.update_contents = JSON.parse(update.update_contents) - update.save! - rescue => e - puts "Failed to update StoryUpdate(#{update.id}): #{e}" - end - end - end - id += 1000 - puts "Finished batch (#{id}/2748371)" - end + belongs_to :story + + def summary + update_contents['summary'] + end + + def updated + DateTime.parse(update_contents['updated']) + end + + # Returns previous updates in descending order + def previous_updates + StoryUpdate.where('story_id = ? AND id < ?', self.story_id, self.id).order(id: :desc) end end diff --git a/app/views/dlp_posts/story_update.text.erb b/app/views/dlp_posts/story_update.text.erb new file mode 100644 index 0000000..f4a1887 --- /dev/null +++ b/app/views/dlp_posts/story_update.text.erb @@ -0,0 +1,14 @@ +[URL="https://www.fanfiction.net<%= @update.update_contents['url_latest'] %>"]<%= @story.title %>[/URL] has been updated with a new chapter. + +[b]Story Stats[/b] +Chapters: <%= @story.chapters %> +Words: <%= number_with_delimiter(@story.words) %> +Updated: <%= @story.updated %> +Published: <%= @story.published %> +Previously updated: <%= distance_of_time_in_words(@update.previous_updates.first.updated, @update.updated) %> ago +<% if @update.previous_updates.first.summary != @story.summary -%> +New Summary: +<%= @update.previous_updates.first.summary %> +<% end %> + +[SIZE=2]Brought to you by [URL="https://scryer.darklordpotter.net"]Scryer[/URL] story thread updates.[/SIZE] \ No newline at end of file diff --git a/app/workers/dlp_thread_notification_worker.rb b/app/workers/dlp_thread_notification_worker.rb new file mode 100644 index 0000000..bb6e123 --- /dev/null +++ b/app/workers/dlp_thread_notification_worker.rb @@ -0,0 +1,19 @@ +class DlpThreadNotificationWorker + include Sidekiq::Worker + sidekiq_options :backtrace => true + + def perform(story_update_id, dlp_thread_id) + logger.info "DlpThreadNotifier(Story(#{story_update_id}), Thread(#{dlp_thread_id})): Notifying thread." + + update = StoryUpdate.find(story_update_id) + email_body = ApplicationController.render( + 'dlp_posts/story_update', + assigns: { + story: update.story, + update: update + } + ) + + $forums.reply(dlp_thread_id, email_body) + end +end diff --git a/config/initializers/scryer.rb b/config/initializers/scryer.rb index 75552fb..c8a886e 100644 --- a/config/initializers/scryer.rb +++ b/config/initializers/scryer.rb @@ -3,3 +3,4 @@ require 'scryer/sybill_client' $scryer = Scryer::Client.new $sybill = Scryer::SybillClient.new +$forums = Scryer::ForumClient.new(auth_token=ENV['DLP_FORUMS_AUTH_TOKEN']) diff --git a/lib/scryer/forum_client.rb b/lib/scryer/forum_client.rb new file mode 100644 index 0000000..008eb78 --- /dev/null +++ b/lib/scryer/forum_client.rb @@ -0,0 +1,32 @@ +require 'faraday' + +module Scryer + class ForumClient + def initialize(api_base='https://forums.darklordpotter.net', auth_token) + @conn = Faraday.new(api_base, { ssl: { verify: false } }) do |faraday| + # faraday.request :json + faraday.request :url_encoded + faraday.response :rashify + faraday.response :json + faraday.response :logger, ::Logger.new(STDOUT), bodies: true + faraday.use :instrumentation + faraday.adapter Faraday.default_adapter # make requests with Net::HTTP + faraday.headers['User-Agent'] = 'Scryer' + faraday.headers['Authorization'] = "Bearer #{auth_token}" + end + end + + def reply(thread_id, post_body) + body = { + thread_id: thread_id, + post_body: post_body + } + resp = @conn.post do |req| + req.url 'api/?posts' + req.body = body + end + + resp.body + end + end +end -- 2.18.1