From 5cd1a34700fcda063441d78b9466cb7deb92db11 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Wed, 27 Jul 2016 17:20:48 -0700 Subject: [PATCH] add google pubsub integration --- Gemfile | 1 + Gemfile.lock | 36 +++++++++++++++++++++++++++++++ app/models/post.rb | 19 +++++++++++----- config/danbooru_default_config.rb | 4 ++++ 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index 8d53dccc4..16d8954cf 100644 --- a/Gemfile +++ b/Gemfile @@ -54,6 +54,7 @@ gem 'aws-sdk', '~> 2' gem 'responders' gem 'highline' gem 'dtext_rb', :git => "https://github.com/r888888888/dtext_rb.git", :require => "dtext" +gem 'google-api-client' # needed for looser jpeg header compat gem 'ruby-imagespec', :require => "image_spec", :git => "https://github.com/r888888888/ruby-imagespec.git", :branch => "exif-fixes" diff --git a/Gemfile.lock b/Gemfile.lock index 01b9c5d2c..5b386d31a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -138,6 +138,24 @@ GEM oauth2 (~> 1.0.0) globalid (0.3.6) activesupport (>= 4.1.0) + google-api-client (0.9.9) + addressable (~> 2.3) + googleauth (~> 0.5) + httpclient (~> 2.7) + hurley (~> 0.1) + memoist (~> 0.11) + mime-types (>= 1.6) + representable (~> 2.3.0) + retriable (~> 2.0) + thor (~> 0.19) + googleauth (0.5.1) + faraday (~> 0.9) + jwt (~> 1.4) + logging (~> 2.0) + memoist (~> 0.12) + multi_json (~> 1.11) + os (~> 0.9) + signet (~> 0.7) hashie (3.4.4) highline (1.7.8) hike (1.2.3) @@ -146,6 +164,8 @@ GEM http-cookie (1.0.2) domain_name (~> 0.5) http_parser.rb (0.6.0) + httpclient (2.8.0) + hurley (0.2) i18n (0.7.0) jeweler (2.1.1) builder @@ -163,6 +183,10 @@ GEM jwt (1.5.4) kgio (2.9.3) libv8 (3.16.14.11) + little-plugger (1.1.4) + logging (2.1.0) + little-plugger (~> 1.1) + multi_json (~> 1.10) loofah (2.0.3) nokogiri (>= 1.5.9) mail (2.6.3) @@ -177,6 +201,7 @@ GEM ntlm-http (~> 0.1, >= 0.1.1) webrobots (>= 0.0.9, < 0.2) memcache-client (1.8.5) + memoist (0.14.0) memoizable (0.4.2) thread_safe (~> 0.3, >= 0.3.1) metaclass (0.0.4) @@ -214,6 +239,7 @@ GEM multi_json (~> 1.3) multi_xml (~> 0.5) rack (~> 1.2) + os (0.9.6) pg (0.18.2) pg (0.18.2-x64-mingw32) pkg-config (1.1.7) @@ -263,6 +289,8 @@ GEM rdoc (4.2.2) json (~> 1.4) ref (2.0.0) + representable (2.3.0) + uber (~> 0.0.7) responders (2.1.0) railties (>= 4.2.0, < 5) rest-client (1.8.0) @@ -274,6 +302,7 @@ GEM http-cookie (>= 1.0.2, < 2.0) mime-types (>= 1.16, < 3.0) netrc (~> 0.7) + retriable (2.1.0) rmagick (2.15.4) ruby-prof (0.15.8) rubyzip (1.1.7) @@ -295,6 +324,11 @@ GEM shoulda-context (1.2.1) shoulda-matchers (2.8.0) activesupport (>= 3.0.0) + signet (0.7.2) + addressable (~> 2.3) + faraday (~> 0.9) + jwt (~> 1.5) + multi_json (~> 1.10) simple_form (3.1.0) actionpack (~> 4.0) activemodel (~> 4.0) @@ -348,6 +382,7 @@ GEM thread_safe (~> 0.1) tzinfo-data (1.2015.6) tzinfo (>= 1.0.0) + uber (0.0.15) uglifier (2.7.1) execjs (>= 0.3.0) json (>= 1.8.0) @@ -392,6 +427,7 @@ DEPENDENCIES factory_girl ffaker gctools + google-api-client highline kgio mechanize diff --git a/app/models/post.rb b/app/models/post.rb index bca459b71..a39cbdfe9 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,4 +1,5 @@ require 'danbooru/has_bit_flags' +require 'google/apis/pubsub_v1' class Post < ActiveRecord::Base class ApprovalError < Exception ; end @@ -14,7 +15,7 @@ class Post < ActiveRecord::Base after_save :apply_post_metatags after_save :expire_essential_tag_string_cache after_create :update_iqdb_async - after_commit :pg_notify + after_commit :notify_pubsub before_save :merge_old_changes before_save :normalize_tags before_save :update_tag_post_counts @@ -1373,8 +1374,16 @@ class Post < ActiveRecord::Base save! end - def pg_notify - execute_sql("notify changes_posts, '#{id}'") + def notify_pubsub + return unless Danbooru.config.google_api_project + + require 'google/apis/pubsub_v1' + pubsub = Google::Apis::PubsubV1::PubsubService.new + pubsub.authorization = Google::Auth.get_application_default([Google::Apis::PubsubV1::AUTH_PUBSUB]) + topic = "projects/#{Danbooru.config.google_api_project}/topics/post_updates" + request = Google::Apis::PubsubV1::PublishRequest.new(messages: []) + request.messages << Google::Apis::PubsubV1::Message.new(data: id.to_s) + pubsub.publish_topic(topic, request) end end @@ -1744,13 +1753,13 @@ class Post < ActiveRecord::Base def update_column(name, value) ret = super(name, value) - pg_notify + notify_pubsub ret end def update_columns(attributes) ret = super(attributes) - pg_notify + notify_pubsub ret end end diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index 860f58a4a..00b3a90cd 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -417,6 +417,10 @@ module Danbooru def listbooru_auth_key end + # google api options + def google_api_project + end + # AWS config options def aws_access_key_id nil