Commit 4a5b7fa7 authored by Michael Rose's avatar Michael Rose

story: update DLP threads on story update

Uses the DLP forums API to post to DLP when the story is updated.
parent 187ea77f
......@@ -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: {
......
......@@ -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
[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
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
......@@ -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'])
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment