Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patreon sub notifications #429

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/controllers/patreon_webhooks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class PatreonWebhooksController < ApplicationController
def create
# TODO veryify these headers
# X-Patreon-Event: [trigger]
# X-Patreon-Signature: [message signature]
puts params.inspect
PatreonWebhookHandler.perform params["data"]
end
end
6 changes: 6 additions & 0 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Notification < ApplicationRecord
:track_playback_ticket_payment,
:glorp_lottery_winner,
:show_comment,
:patreon_sub,
:new_thread,
:new_thread_reply,
:new_wiki_page,
Expand All @@ -50,6 +51,7 @@ class Notification < ApplicationRecord

private
def set_message
# TODO i18n
self.message = case self.notification_type
when "strawberry_badge_award"
"#{self.user.username} got the strawbur badge!"
Expand Down Expand Up @@ -99,6 +101,10 @@ def set_message
":#{self.source.award_type.split("py").first}:!!! #{self.user.username} got #{self.source.amount} #{self.source.award_type} points!"
when "show_comment"
"#{self.source.title} has a new comment!"
when "patreon_sub"
# TODO figure out which tier somehow
# "#{self.source.name} subscribed to the #{self.source.tier_name} tier on patreon!"
"#{self.source.name} subscribed to the on patreon!"
when "new_thread"
"New thread posted in da fruit standz: #{self.source.title}"
when "new_thread_reply"
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"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??????

end
end
8 changes: 8 additions & 0 deletions app/services/patreon_webhook_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class PatreonWebhookHandler
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
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@
resources :labels, only: [:create, :index, :show]
end

resources :patreon_webhooks, only: [:create]

post "/setup" => "setup#create"
get "/setup/allowed" => "setup#allowed"

Expand Down
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
10 changes: 9 additions & 1 deletion 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: 2024_09_10_235047) 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 @@ -199,6 +199,14 @@
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
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
Loading