From cc07e90b449b04efb4d931e1057f514fb7f85679 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Tue, 7 Feb 2017 22:45:29 -0800 Subject: [PATCH] Code to display tips submitted on news feed --- Gemfile | 1 + Gemfile.lock | 7 ++++ app/activity.rb | 11 +++++- app/site.rb | 7 ---- app/webhooks.rb | 51 +++++++++++++++++++++++++++ models/event.rb | 2 +- models/tip.rb | 10 +++++- sass/_project-sass/_project-Main.scss | 5 ++- views/_news.erb | 43 ++++++++++++++++++++++ views/site.erb | 2 +- views/site/_tip.erb | 4 +++ 11 files changed, 131 insertions(+), 12 deletions(-) diff --git a/Gemfile b/Gemfile index 9b7fe692..9eb155e4 100644 --- a/Gemfile +++ b/Gemfile @@ -47,6 +47,7 @@ gem 'image_optim' gem 'image_optim_pack' gem 'ipaddress' gem 'feedjira' +gem 'monetize' platform :mri, :rbx do gem 'magic' # sudo apt-get install file, For OSX: brew install libmagic diff --git a/Gemfile.lock b/Gemfile.lock index d3553f8f..bba22b4d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -145,6 +145,11 @@ GEM mocha (1.1.0) metaclass (~> 0.0.1) mock_redis (0.16.1) + monetize (1.6.0) + money (~> 6.8) + money (6.8.1) + i18n (>= 0.6.4, <= 0.7.0) + sixarm_ruby_unaccent (>= 1.1.1, < 2) msgpack (0.7.5) multi_json (1.12.1) multipart-post (2.0.0) @@ -238,6 +243,7 @@ GEM sinatra (>= 1.0.0) sinatra-xsendfile (0.4.2) sinatra (>= 0.9.1) + sixarm_ruby_unaccent (1.1.1) slop (3.6.0) storable (0.8.9) stripe (1.15.0) @@ -317,6 +323,7 @@ DEPENDENCIES minitest-reporters mocha mock_redis + monetize msgpack paypal-recurring pg diff --git a/app/activity.rb b/app/activity.rb index 3961db08..9a3a1b5f 100644 --- a/app/activity.rb +++ b/app/activity.rb @@ -15,7 +15,16 @@ get '/activity' do site = Site.select(:id).where(id: event.site_id).first actioning_site = Site.select(:id).where(id: event.actioning_site_id).first - events.push(event) if !site.is_a_jerk? && !actioning_site.is_a_jerk? && actioning_site.follows_dataset.count > 1 + disclude_event = false + disclude_event = true if site.is_a_jerk? + + if event.tip_id + disclude_event = true if actioning_site && actioning_site.is_a_jerk? + else + disclude_event = true if actioning_site && (actioning_site.is_a_jerk? || actioning_site.follows_dataset.count < 2) + end + + events.push(event) unless disclude_event end initial_site_change_events = Event.global_site_changes_dataset.limit(100).all diff --git a/app/site.rb b/app/site.rb index 4dbd395f..788dd3c1 100644 --- a/app/site.rb +++ b/app/site.rb @@ -146,13 +146,6 @@ post '/site/:username/comment' do |username| redirect request.referrer end -get '/site/:username/tip' do |username| - @site = Site[username: username] - redirect request.referrer unless @site.tipping_enabled? - @title = "Tip #{@site.title}" - erb :'tip' -end - post '/site/:site_id/toggle_follow' do |site_id| require_login content_type :json diff --git a/app/webhooks.rb b/app/webhooks.rb index fcf9b60c..25a3662e 100644 --- a/app/webhooks.rb +++ b/app/webhooks.rb @@ -10,6 +10,57 @@ post '/webhooks/paypal' do 'ok' end +def valid_paypal_webhook_source? + # https://www.paypal-knowledge.com/infocenter/index?page=content&widgetview=true&id=FAQ1465&viewlocale=en_US&direct=en + return true if ['127.0.0.1', '173.0.81.1', '173.0.81.33', '66.211.170.66'].include?(request.ip) + false +end + +post '/webhooks/paypal/tipping_notify' do + return 403 unless valid_paypal_webhook_source? + payload = JSON.parse Base64.strict_decode64(params[:custom]), symbolize_names: true + + site = Site[payload[:site_id]] + + @tip_hash = { + message: (params[:memo] ? params[:memo] : nil), + amount: params[:mc_gross], + currency: params[:mc_currency], + fee: params[:mc_fee], + actioning_site: (payload[:actioning_site_id] ? Site[payload[:actioning_site_id]] : nil), + paypal_payer_email: params[:payer_email], + paypal_receiver_email: params[:receiver_email], + paypal_txn_id: params[:txn_id], + created_at: DateTime.strptime(params[:payment_date], "%H:%M:%S %b %e, %Y %Z").to_time + } + + @tip = site.add_tip @tip_hash + + Event.create( + site_id: @tip.site.id, + actioning_site_id: (@tip.actioning_site ? @tip.actioning_site.id : nil), + tip_id: @tip.id + ) + + if @tip.actioning_site + subject = "You received a #{@tip.amount_string} tip from #{@tip.actioning_site.username}!" + else + subject = "You received a #{@tip.amount_string} tip!" + end + + @tip.site.send_email( + subject: subject, + body: Tilt.new('./views/templates/email/tip_received.erb', pretty: true).render(self) + ) + + EmailWorker.perform_async({ + from: 'web@neocities.org', + to: params[:payer_email], + subject: "You sent a #{@tip.amount_string} tip!", + body: Tilt.new('./views/templates/email/tip_sent.erb', pretty: true).render(self) + }) +end + post '/webhooks/stripe' do event = JSON.parse request.body.read if event['type'] == 'customer.created' diff --git a/models/event.rb b/models/event.rb index 5f454b22..4ca19b77 100644 --- a/models/event.rb +++ b/models/event.rb @@ -3,7 +3,7 @@ class Event < Sequel::Model many_to_one :site many_to_one :follow - one_to_one :tip + many_to_one :tip one_to_one :tag many_to_one :site_change many_to_one :profile_comment diff --git a/models/tip.rb b/models/tip.rb index e97c5c5e..7b8e1ff3 100644 --- a/models/tip.rb +++ b/models/tip.rb @@ -1,4 +1,12 @@ class Tip < Sequel::Model many_to_one :site many_to_one :actioning_site, class: :Site -end \ No newline at end of file + + def amount_string + Monetize.parse("#{currency} #{amount.to_f}").format + end + + def fee_string + Monetize.parse("#{currency} #{fee.to_f}").format + end +end diff --git a/sass/_project-sass/_project-Main.scss b/sass/_project-sass/_project-Main.scss index edc7aa33..bc8306ee 100644 --- a/sass/_project-sass/_project-Main.scss +++ b/sass/_project-sass/_project-Main.scss @@ -951,8 +951,11 @@ a.tag:hover { .news-item.update .icon { background: #E93250; } +.news-item.tip .title .text .headline, .news-item.tip .title .text .headline a, .news-item.tip .date a { + color: #229954!important; +} .news-item.tip .icon { - background: #FFCC00; + background: #229954; } .news-item.follow .icon { background: #3399CC; diff --git a/views/_news.erb b/views/_news.erb index 01b507b4..706031da 100644 --- a/views/_news.erb +++ b/views/_news.erb @@ -14,6 +14,49 @@ <% if event.profile_comment_id %>
<%== erb :'_news_profile_comment', layout: false, locals: {profile_comment: event.profile_comment, event: event} %> + <% elsif event.tip_id %> + +
+
+ <% actioning_site = event.actioning_site_dataset.select(:id, :username, :title, :domain, :stripe_customer_id).first %> + <% event_site = event.site_dataset.select(:id, :username, :title, :domain, :stripe_customer_id).first %> + <% tip = event.tip %> + +
+ <% if actioning_site %> + + <% end %> +
+ +
+
+ <% if actioning_site %> + <% if current_site && current_site.id == actioning_site.id %> + You + <% else %> + <% if actioning_site.supporter? %><% end %><%= actioning_site.username %> + <% end %> + <% else %> + An anonymous donor + <% end %> + + sent a <%= tip.amount_string %> tip to + + <% if current_site && event_site.id == current_site.id %> + you! + <% else %> + <% if event_site.supporter? %><% end %><%= event_site.username %>! + <% end %> +
+ + <%= tip.message %> +
+ + + <%= event.created_at.ago %> + +
+ <% elsif event.follow_id %>