Skip to content

Commit

Permalink
add patreon webhook handler stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfiredrill committed Sep 16, 2024
1 parent a41a633 commit f32894c
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/controllers/patreon_webhooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions app/models/patreon_pledge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class PatreonPledge < ApplicationRecord
def tier_name
# decide tier name from pledge_amount_cents
"pineapple master"
end
end
7 changes: 5 additions & 2 deletions app/services/patreon_webhook_handler.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions db/migrate/20240915231634_create_patreon_pledges.rb
Original file line number Diff line number Diff line change
@@ -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
78 changes: 76 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/services/patreon_webhook_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f32894c

Please sign in to comment.