diff --git a/app/models/buffer_update.rb b/app/models/buffer_update.rb deleted file mode 100644 index 6465a5953..000000000 --- a/app/models/buffer_update.rb +++ /dev/null @@ -1,92 +0,0 @@ -class BufferUpdate < ApplicationRecord - SOCIAL_MEDIA = { - "twitter" => ApplicationConfig["BUFFER_TWITTER_ID"], - "facebook" => ApplicationConfig["BUFFER_FACEBOOK_ID"], - "linkedin" => ApplicationConfig["BUFFER_LINKEDIN_ID"] - }.freeze - - DEFAULT_SOCIAL_MEDIA = "twitter".freeze - - resourcify - - belongs_to :approver_user, class_name: "User", optional: true - belongs_to :article - belongs_to :composer_user, class_name: "User", optional: true - belongs_to :tag, optional: true - - validate :validate_body_text_recent_uniqueness, :validate_suggestion_limit - validates :status, inclusion: { in: %w[pending sent_direct confirmed dismissed] } - - def self.buff!(article_id, text, **kwargs) - social_service_name = kwargs.fetch(:social_service_name, DEFAULT_SOCIAL_MEDIA) - buffer_profile_id_code = kwargs.fetch(:buffer_profile_id_code, SOCIAL_MEDIA.fetch(social_service_name)) - tag_id = kwargs[:tag_id] - admin_id = kwargs[:admin_id] - - buffer_response = send_to_buffer(text, buffer_profile_id_code) - create( - article_id: article_id, - tag_id: tag_id, - body_text: text, - approver_user_id: admin_id, - buffer_profile_id_code: buffer_profile_id_code, - social_service_name: social_service_name, - buffer_response: buffer_response, - status: "sent_direct", - ) - end - - def self.upbuff!(buffer_update_id, admin_id, body_text, status) - buffer_update = BufferUpdate.find(buffer_update_id) - if status == "confirmed" - buffer_response = send_to_buffer(body_text, buffer_update.buffer_profile_id_code) - buffer_update.update!(buffer_response: buffer_response, status: status, approver_user_id: admin_id, - body_text: body_text) - else - buffer_update.update!(status: status, approver_user_id: admin_id) - end - end - - def self.send_to_buffer(text, buffer_profile_id_code) - client = Buffer::Client.new(ApplicationConfig["BUFFER_ACCESS_TOKEN"]) - client.create_update( - body: { - text: - text, - profile_ids: [ - buffer_profile_id_code, - ] - }, - ) - end - - def self.twitter_default_text(article) - [ - article.title, - "\n\n", - ("{ author: @#{article.user.twitter_username} } " if article.user.twitter_username?), - SiteConfig.twitter_hashtag.presence, - ].compact.join.strip - end - - private - - def validate_body_text_recent_uniqueness - return if persisted? - - relation = BufferUpdate - .where(body_text: body_text, article_id: article_id, tag_id: tag_id, social_service_name: social_service_name) - .where("created_at > ?", 2.minutes.ago) - - return unless relation.any? - - errors.add(:body_text, "\"#{body_text}\" has already been submitted very recently") - end - - def validate_suggestion_limit - return unless BufferUpdate.where(article_id: article_id, tag_id: tag_id, - social_service_name: social_service_name).count > 2 - - errors.add(:article_id, "already has multiple suggestions for #{social_service_name}") - end -end diff --git a/db/migrate/20210324031252_remove_buffer_columns.rb b/db/migrate/20210324031252_remove_buffer_columns.rb new file mode 100644 index 000000000..344c1a908 --- /dev/null +++ b/db/migrate/20210324031252_remove_buffer_columns.rb @@ -0,0 +1,12 @@ +class RemoveBufferColumns < ActiveRecord::Migration[6.0] + def change + safety_assured do + remove_column :articles, :facebook_last_buffered + remove_column :articles, :last_buffered + + remove_column :classified_listings, :last_buffered + + remove_column :tags, :buffer_profile_id_code + end + end +end diff --git a/db/migrate/20210324031738_remove_buffer_updates.rb b/db/migrate/20210324031738_remove_buffer_updates.rb new file mode 100644 index 000000000..d537a8899 --- /dev/null +++ b/db/migrate/20210324031738_remove_buffer_updates.rb @@ -0,0 +1,5 @@ +class RemoveBufferUpdates < ActiveRecord::Migration[6.0] + def change + drop_table :buffer_updates + end +end diff --git a/db/schema.rb b/db/schema.rb index 861e2186f..eb7a1ec2a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_03_23_190443) do +ActiveRecord::Schema.define(version: 2021_03_24_031738) do # These are extensions that must be enabled in order to support this database enable_extension "citext" @@ -105,12 +105,10 @@ ActiveRecord::Schema.define(version: 2021_03_23_190443) do t.boolean "email_digest_eligible", default: true t.float "experience_level_rating", default: 5.0 t.float "experience_level_rating_distribution", default: 5.0 - t.datetime "facebook_last_buffered" t.boolean "featured", default: false t.integer "featured_number" t.string "feed_source_url" t.integer "hotness_score", default: 0 - t.datetime "last_buffered" t.datetime "last_comment_at", default: "2017-01-01 05:00:00" t.datetime "last_experience_level_rating_at" t.string "main_image" @@ -287,21 +285,6 @@ ActiveRecord::Schema.define(version: 2021_03_23_190443) do t.index ["title", "type_of"], name: "index_broadcasts_on_title_and_type_of", unique: true end - create_table "buffer_updates", force: :cascade do |t| - t.bigint "approver_user_id" - t.bigint "article_id", null: false - t.text "body_text" - t.string "buffer_id_code" - t.string "buffer_profile_id_code" - t.text "buffer_response", default: "--- {}\n" - t.bigint "composer_user_id" - t.datetime "created_at", null: false - t.string "social_service_name" - t.string "status", default: "pending" - t.bigint "tag_id" - t.datetime "updated_at", null: false - end - create_table "chat_channel_memberships", force: :cascade do |t| t.bigint "chat_channel_id", null: false t.datetime "created_at", null: false @@ -350,7 +333,6 @@ ActiveRecord::Schema.define(version: 2021_03_23_190443) do t.boolean "contact_via_connect", default: false t.datetime "created_at", null: false t.datetime "expires_at" - t.datetime "last_buffered" t.string "location" t.bigint "organization_id" t.datetime "originally_published_at" @@ -1131,7 +1113,6 @@ ActiveRecord::Schema.define(version: 2021_03_23_190443) do t.string "alias_for" t.bigint "badge_id" t.string "bg_color_hex" - t.string "buffer_profile_id_code" t.string "category", default: "uncategorized", null: false t.datetime "created_at" t.integer "hotness_score", default: 0 @@ -1423,10 +1404,6 @@ ActiveRecord::Schema.define(version: 2021_03_23_190443) do add_foreign_key "badge_achievements", "users" add_foreign_key "badge_achievements", "users", column: "rewarder_id", on_delete: :nullify add_foreign_key "banished_users", "users", column: "banished_by_id", on_delete: :nullify - add_foreign_key "buffer_updates", "articles", on_delete: :cascade - add_foreign_key "buffer_updates", "tags", on_delete: :nullify - add_foreign_key "buffer_updates", "users", column: "approver_user_id", on_delete: :nullify - add_foreign_key "buffer_updates", "users", column: "composer_user_id", on_delete: :nullify add_foreign_key "chat_channel_memberships", "chat_channels" add_foreign_key "chat_channel_memberships", "users" add_foreign_key "classified_listings", "classified_listing_categories"