Pushover: switch to subscriptions

users.pushover_device and users.pushover_sound columns can be
dropped once migrated
This commit is contained in:
joshua stein 2015-01-15 11:05:28 -06:00
parent f1cfe29b1f
commit 31df8973a8
6 changed files with 109 additions and 62 deletions

View file

@ -1164,3 +1164,25 @@ div.fieldWithErrors {
font-style: italic;
padding-left: 0.5em;
}
/* pushover */
.pushover_button {
box-sizing: border-box;
background-color: #eee;
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QAJQCeAPHNVUx7AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3wEPAh02ee0QVwAAACZpVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVAgb24gYSBNYWOV5F9bAAABqElEQVQ4y62TvUtbURjGf+fek2vQpMF2ED9QkfpR6VLaDoIEF0UKWQRBQ+yQP6GDU0Xw4y/obOgg6dDSJVS61MlBcXFQB6M1BsF+IES9NnrzcRy8xpvrBwH7bOflfc553+d5jsCF9tiRDwgBEaDXLi8B80AiGQ2Yzn7hIo8Cce5HOBkNfLo6aA7ydAVkgLjdez3BrS8rRbBJEmyU+AyBVVDE1i1SJ6psEmHvfOLktj0SjL/2snKQx8wpNg+LBJt0RjoN+j6bqOvN/ZotWAldtYK5gWpmV84YbPUw2ePlb7bI73+Kep+OLFONkGarXcK7l16+7+V57BW8qJMs7Ob5k4VQm4fVXzlyquyGiHRYBcC+WWQxnSP63AAgtn7O0FNJ9xOd0W+nbkF7pbsytXxOQw30N0t2MgX6Wzy8qtMJL5zy81jdsETaIXnjLL7trkLqGl+2svxI5/mwZt1l6ZJmJ6yEGqkYbveQOioQ2yiz7TbMa0DCWRnuMPBXaXzcsJx23YWEZmc7fJkqxdgzg8xZka/bFhVE2hSuKL+nMswko4GJ//KZxEO/8wVmfpjJTWeCTQAAAABJRU5ErkJggg==) 2px 2px no-repeat, linear-gradient(#FFF, #DEDEDE);
border: 1px solid #CCC;
border-radius: 3px;
color: #333;
display: inline-block;
font: 11px/18px "Helvetica Neue",Arial,sans-serif;
font-weight: bold;
cursor: pointer;
height: 22px;
padding-left: 20px;
padding-right: 5px;
overflow: hidden;
text-shadow: 0px 1px 0px rgba(255, 255, 255, 0.5);
text-decoration: none;
vertical-align: middle;
}

View file

@ -19,6 +19,50 @@ class SettingsController < ApplicationController
return redirect_to settings_path
end
def pushover
if !Pushover.SUBSCRIPTION_CODE
flash[:error] = "This site is not configured for Pushover"
return redirect_to "/settings"
end
session[:pushover_rand] = SecureRandom.hex
return redirect_to Pushover.subscription_url({
:success => "#{Rails.application.root_url}settings/pushover_callback?" <<
"rand=#{session[:pushover_rand]}",
:failure => "#{Rails.application.root_url}settings/",
})
end
def pushover_callback
if !session[:pushover_rand].to_s.present?
flash[:error] = "No random token present in session"
return redirect_to "/settings"
end
if !params[:rand].to_s.present?
flash[:error] = "No random token present in URL"
return redirect_to "/settings"
end
if params[:rand].to_s != session[:pushover_rand].to_s
raise "rand param #{params[:rand].inspect} != " <<
session[:pushover_rand].inspect
end
@user.pushover_user_key = params[:pushover_user_key].to_s
@user.save!
if @user.pushover_user_key.present?
flash[:success] = "Your account is now setup for Pushover notifications."
else
flash[:success] = "Your account is no longer setup for Pushover " <<
"notifications."
end
return redirect_to "/settings"
end
def update
@edit_user = @user.clone
@ -37,7 +81,6 @@ private
:username, :email, :password, :password_confirmation, :about,
:email_replies, :email_messages, :email_mentions,
:pushover_replies, :pushover_messages, :pushover_mentions,
:pushover_user_key, :pushover_device, :pushover_sound,
:mailing_list_mode
)
end

View file

@ -221,8 +221,7 @@ class User < ActiveRecord::Base
def pushover!(params)
if self.pushover_user_key.present?
Pushover.push(self.pushover_user_key, self.pushover_device,
params.merge({ :sound => self.pushover_sound.to_s }))
Pushover.push(self.pushover_user_key, params)
end
end

View file

@ -38,33 +38,6 @@
</span>
</div>
<br>
<div class="boxline">
<%= f.label :pushover_user_key,
raw("<a href=\"https://pushover.net/\">Pushover</a> User Key:"),
:class => "required" %>
<%= f.text_field :pushover_user_key, :size => 40 %>
<span class="hint">
For optional comment and message notifications below
</span>
</div>
<div class="boxline">
<%= f.label :pushover_device, "Pushover Device:",
:class => "required" %>
<%= f.text_field :pushover_device, :placeholder => "optional",
:size => 15 %>
</div>
<div class="boxline">
<%= f.label :pushover_sound, "Pushover Sound:",
:class => "required" %>
<%= f.select :pushover_sound, Pushover.sounds.map{|k,v| [ v, k ] } %>
</div>
<br>
<div class="boxline">
<%= f.label :about, "About:", :class => "required" %>
<%= f.text_area :about, :size => "100x5", :style => "width: 600px;" %>
@ -83,6 +56,31 @@
</div>
</div>
<p>
<%= f.submit "Save Account Settings" %>
</p>
<br>
<div class="legend">
Notification Settings
</div>
<div class="boxline">
<%= f.label :pushover_user_key,
raw("<a href=\"https://pushover.net/\">Pushover</a>:"),
:class => "required" %>
<%= link_to((f.object.pushover_user_key.present??
"Manage Pushover Subscription" : "Subscribe With Pushover"),
"/settings/pushover", :class => "pushover_button", :method => :post) %>
<span class="hint">
For optional comment and message notifications below
</span>
</div>
<br>
<div class="legend">
Comment Reply Notification Settings
</div>
@ -172,6 +170,7 @@
</span>
</div>
<br>
<%= f.submit "Save All Settings" %>
<% end %>

View file

@ -88,6 +88,8 @@ Lobsters::Application.routes.draw do
get "/settings" => "settings#index"
post "/settings" => "settings#update"
post "/settings/pushover" => "settings#pushover"
get "/settings/pushover_callback" => "settings#pushover_callback"
post "/settings/delete_account" => "settings#delete_account",
:as => "delete_account"

View file

@ -1,50 +1,32 @@
class Pushover
cattr_accessor :API_KEY
# these need to be overridden in config/initializers/production.rb
cattr_accessor :API_TOKEN
cattr_accessor :SUBSCRIPTION_CODE
# this needs to be overridden in config/initializers/production.rb
@@API_KEY = nil
@@SOUNDS = {}
def self.sounds
if !@@API_KEY
return @@SOUNDS
end
if @@SOUNDS.length <= 1
begin
s = Sponge.new
res = s.get("https://api.pushover.net/1/sounds.json?token=#{@@API_KEY}")
@@SOUNDS = JSON.parse(res)["sounds"] || {}
rescue => e
Rails.logger.error "error sending to pushover: #{e.inspect}"
end
@@SOUNDS = { "" => "Device default" }.merge(@@SOUNDS)
end
@@SOUNDS
end
def self.push(user, device, params)
if !@@API_KEY
def self.push(user, params)
if !@@API_TOKEN
return
end
begin
params[:message] = params[:message].to_s.match(/.{0,512}/m).to_s
if params[:message] == ""
if params[:message].to_s == ""
params[:message] = "(No message)"
end
s = Sponge.new
s.fetch("https://api.pushover.net/1/messages.json", :post, {
:token => @@API_KEY,
:token => @@API_TOKEN,
:user => user,
:device => device
}.merge(params))
rescue => e
Rails.logger.error "error sending to pushover: #{e.inspect}"
end
end
def self.subscription_url(params)
u = "https://pushover.net/subscribe/#{@@SUBSCRIPTION_CODE}"
u << "?success=#{CGI.escape(params[:success])}"
u << "&failure=#{CGI.escape(params[:failure])}"
u
end
end