diff --git a/app/controllers/patreon_webhooks_controller.rb b/app/controllers/patreon_webhooks_controller.rb index ddc8fae2..87d596e7 100644 --- a/app/controllers/patreon_webhooks_controller.rb +++ b/app/controllers/patreon_webhooks_controller.rb @@ -4,6 +4,6 @@ def create # X-Patreon-Event: [trigger] # X-Patreon-Signature: [message signature] puts params.inspect - # PatreonWebhookHandler.perform params + PatreonWebhookHandler.perform params["data"] end end diff --git a/app/models/notification.rb b/app/models/notification.rb index e4c0bf12..961e25ef 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -73,7 +73,7 @@ def set_message when "show_comment" "#{self.source.title} has a new comment!" when "patreon_sub" - "#{self.source.username} subscribed to the #{self.source.tier_name} tier on patreon!" + "#{self.source.name} subscribed to the #{self.source.tier_name} tier on patreon!" end end diff --git a/app/models/patreon_pledge.rb b/app/models/patreon_pledge.rb new file mode 100644 index 00000000..5112866d --- /dev/null +++ b/app/models/patreon_pledge.rb @@ -0,0 +1,6 @@ +class PatreonPledge < ApplicationRecord + def tier_name + # decide tier name from pledge_amount_cents + "pineapple master" + end +end diff --git a/app/services/patreon_webhook_handler.rb b/app/services/patreon_webhook_handler.rb index e26e560a..138a433d 100644 --- a/app/services/patreon_webhook_handler.rb +++ b/app/services/patreon_webhook_handler.rb @@ -1,5 +1,8 @@ class PatreonWebhookHandler - def self.perform - Notification.create! notification_type: "patreon_sub", send_to_chat: true, send_to_user: false + def self.perform data + pledge_amount_cents = data["attributes"]["pledge_amount_cents"] + name = data["attributes"]["full_name"].split(" ").first + pledge = PatreonPledge.create! json_blob: data, name: name, pledge_amount_cents: pledge_amount_cents + Notification.create! notification_type: "patreon_sub", send_to_chat: true, send_to_user: false, source: pledge, user_id: 1 end end diff --git a/db/migrate/20240915231634_create_patreon_pledges.rb b/db/migrate/20240915231634_create_patreon_pledges.rb new file mode 100644 index 00000000..732b5023 --- /dev/null +++ b/db/migrate/20240915231634_create_patreon_pledges.rb @@ -0,0 +1,11 @@ +class CreatePatreonPledges < ActiveRecord::Migration[7.0] + def change + create_table :patreon_pledges do |t| + t.string :json_blob + t.string :name + t.integer :pledge_amount_cents + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 93945f04..33923855 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[7.0].define(version: 2023_09_07_203146) do +ActiveRecord::Schema[7.0].define(version: 2024_09_15_231634) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -195,9 +195,18 @@ t.string "message", null: false t.string "source_type" t.boolean "read", default: false, null: false + t.string "url" t.index ["user_id"], name: "index_notifications_on_user_id" end + create_table "patreon_pledges", force: :cascade do |t| + t.string "json_blob" + t.string "name" + t.integer "pledge_amount_cents" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "plans", id: :serial, force: :cascade do |t| t.decimal "price" t.datetime "created_at", precision: nil @@ -305,6 +314,16 @@ t.index ["track_id"], name: "index_recordings_on_track_id" end + create_table "scheduled_show_favorites", force: :cascade do |t| + t.bigint "user_id", null: false + t.bigint "scheduled_show_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["scheduled_show_id"], name: "index_scheduled_show_favorites_on_scheduled_show_id" + t.index ["user_id", "scheduled_show_id"], name: "index_scheduled_show_favorites_on_user_id_and_scheduled_show_id", unique: true + t.index ["user_id"], name: "index_scheduled_show_favorites_on_user_id" + end + create_table "scheduled_show_labels", id: :serial, force: :cascade do |t| t.integer "label_id" t.integer "scheduled_show_id" @@ -351,6 +370,9 @@ t.integer "show_series_id" t.integer "status", default: 0, null: false t.integer "recording_id" + t.string "youtube_link" + t.string "mixcloud_link" + t.string "soundcloud_link" t.index ["dj_id"], name: "index_scheduled_shows_on_dj_id" t.index ["playlist_id"], name: "index_scheduled_shows_on_playlist_id" t.index ["radio_id"], name: "index_scheduled_shows_on_radio_id" @@ -364,7 +386,7 @@ t.string "image_file_name" t.integer "image_file_size" t.string "image_content_type" - t.datetime "image_update_at", precision: nil + t.datetime "image_updated_at", precision: nil t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false t.integer "recurring_interval", default: 0, null: false @@ -378,6 +400,7 @@ t.integer "status", default: 0, null: false t.integer "radio_id", default: 1, null: false t.integer "default_playlist_id" + t.string "time_zone", default: "UTC", null: false t.index ["slug"], name: "index_show_series_on_slug", unique: true end @@ -423,10 +446,36 @@ t.text "description" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "slug" + t.integer "total_score" + t.integer "ranking" t.index ["shrimpo_id"], name: "index_shrimpo_entries_on_shrimpo_id" + t.index ["slug"], name: "index_shrimpo_entries_on_slug", unique: true t.index ["user_id"], name: "index_shrimpo_entries_on_user_id" end + create_table "shrimpo_votes", force: :cascade do |t| + t.bigint "shrimpo_voting_category_id" + t.bigint "shrimpo_entry_id", null: false + t.bigint "user_id", null: false + t.integer "score", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["shrimpo_entry_id"], name: "index_shrimpo_votes_on_shrimpo_entry_id" + t.index ["shrimpo_voting_category_id"], name: "index_shrimpo_votes_on_shrimpo_voting_category_id" + t.index ["user_id", "shrimpo_entry_id", "shrimpo_voting_category_id"], name: "index_shrimpo_votes_uid_seid_svcid", unique: true + t.index ["user_id"], name: "index_shrimpo_votes_on_user_id" + end + + create_table "shrimpo_voting_categories", force: :cascade do |t| + t.bigint "shrimpo_id", null: false + t.string "name" + t.string "emoji" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["shrimpo_id"], name: "index_shrimpo_voting_categories_on_shrimpo_id" + end + create_table "shrimpos", force: :cascade do |t| t.bigint "user_id", null: false t.string "title", null: false @@ -437,6 +486,14 @@ t.datetime "updated_at", null: false t.integer "status", default: 0, null: false t.string "slug" + t.string "emoji" + t.datetime "ended_at" + t.integer "gold_trophy_id" + t.integer "silver_trophy_id" + t.integer "bronze_trophy_id" + t.integer "consolation_trophy_id" + t.integer "shrimpo_type", default: 0 + t.integer "deposit_amount" t.index ["slug"], name: "index_shrimpos_on_slug", unique: true t.index ["user_id"], name: "index_shrimpos_on_user_id" end @@ -504,6 +561,23 @@ t.index ["uploaded_by_id"], name: "index_tracks_on_uploaded_by_id" end + create_table "trophies", force: :cascade do |t| + t.string "name", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "trophy_awards", force: :cascade do |t| + t.bigint "user_id", null: false + t.bigint "trophy_id", null: false + t.bigint "shrimpo_entry_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["shrimpo_entry_id"], name: "index_trophy_awards_on_shrimpo_entry_id" + t.index ["trophy_id"], name: "index_trophy_awards_on_trophy_id" + t.index ["user_id"], name: "index_trophy_awards_on_user_id" + end + create_table "user_radios", id: :serial, force: :cascade do |t| t.integer "user_id", null: false t.integer "radio_id", null: false diff --git a/spec/services/patreon_webhook_handler_spec.rb b/spec/services/patreon_webhook_handler_spec.rb new file mode 100644 index 00000000..fa539427 --- /dev/null +++ b/spec/services/patreon_webhook_handler_spec.rb @@ -0,0 +1,10 @@ +require 'rails_helper' + +describe PatreonWebhookHandler do + it 'saves the pledge to the db and sends a notification' do + data = {"attributes"=> {"access_expires_at"=>nil, "campaign_currency"=>"USD", "campaign_lifetime_support_cents"=>12345, "campaign_pledge_amount_cents"=>50, "full_name"=>"Anthony Allen Miller", "is_follower"=>false, "is_free_member"=>nil, "is_free_trial"=>nil, "last_charge_date"=>"2014-04-01T00:00:00.000+00:00", "last_charge_status"=>"Paid", "lifetime_support_cents"=>12345, "patron_status"=>"active_patron", "pledge_amount_cents"=>50, "pledge_relationship_start"=>"2014-03-14T00:00:00.000+00:00"} , "id"=>nil, "relationships"=>{"address"=>{"data"=>{"id"=>"826453", "type"=>"address"}, "links"=>{"related"=>"https://www.patreon.com/api/addresses/826453"}}, "campaign"=>{"data"=>{"id"=>"205222", "type"=>"campaign"}, "links"=>{"related"=>"https://www.patreon.com/api/campaigns/205222"}}, "user"=>{"data"=>{"id"=>"1018897", "type"=>"user"}, "links"=>{"related"=>"https://www.patreon.com/api/user/1018897"}}}, "type"=>"member"} + PatreonWebhookHandler.perform data + expect(Notification.count).to eq 1 + expect(Notification.last.notification_type).to eq "patreon_sub" + end +end