be a good pushover user and let the user specify a sound

- move pushover particulars into User model
- Pushover class rescues errors, don't need to do it from User
This commit is contained in:
joshua stein 2014-01-21 01:05:27 -06:00
parent 00f8e9c189
commit 71eeb6c7d2
7 changed files with 73 additions and 47 deletions

View file

@ -134,18 +134,14 @@ class Comment < ActiveRecord::Base
end end
end end
if u.pushover_mentions? && u.pushover_user_key.present? if u.pushover_mentions?
begin u.pushover!({
Pushover.push(u.pushover_user_key, u.pushover_device, { :title => "#{Rails.application.name} mention by " <<
:title => "#{Rails.application.name} mention by " << "#{self.user.username} on #{self.story.title}",
"#{self.user.username} on #{self.story.title}", :message => self.plaintext_comment,
:message => self.plaintext_comment, :url => self.url,
:url => self.url, :url_title => "Reply to #{self.user.username}",
:url_title => "Reply to #{self.user.username}", })
})
rescue => e
Rails.logger.error "error sending to pushover: #{e}"
end
end end
end end
end end
@ -162,18 +158,14 @@ class Comment < ActiveRecord::Base
end end
end end
if u.pushover_replies? && u.pushover_user_key.present? if u.pushover_replies?
begin u.pushover!({
Pushover.push(u.pushover_user_key, u.pushover_device, { :title => "#{Rails.application.name} reply from " <<
:title => "#{Rails.application.name} reply from " << "#{self.user.username} on #{self.story.title}",
"#{self.user.username} on #{self.story.title}", :message => self.plaintext_comment,
:message => self.plaintext_comment, :url => self.url,
:url => self.url, :url_title => "Reply to #{self.user.username}",
:url_title => "Reply to #{self.user.username}", })
})
rescue => e
Rails.logger.error "error sending to pushover: #{e}"
end
end end
end end
end end

View file

@ -45,20 +45,14 @@ class Message < ActiveRecord::Base
end end
end end
if self.recipient.pushover_messages? && if self.recipient.pushover_messages?
self.recipient.pushover_user_key.present? self.recipient.pushover!({
begin :title => "#{Rails.application.name} message from " <<
Pushover.push(self.recipient.pushover_user_key, "#{self.author.username}: #{self.subject}",
self.recipient.pushover_device, { :message => self.plaintext_body,
:title => "#{Rails.application.name} message from " << :url => self.url,
"#{self.author.username}: #{self.subject}", :url_title => "Reply to #{self.author.username}",
:message => self.plaintext_body, })
:url => self.url,
:url_title => "Reply to #{self.author.username}",
})
rescue => e
Rails.logger.error "error sending to pushover: #{e}"
end
end end
end end

View file

@ -38,8 +38,8 @@ class User < ActiveRecord::Base
attr_accessible :username, :email, :password, :password_confirmation, attr_accessible :username, :email, :password, :password_confirmation,
:about, :email_replies, :pushover_replies, :pushover_user_key, :about, :email_replies, :pushover_replies, :pushover_user_key,
:pushover_device, :email_messages, :pushover_messages, :email_mentions, :pushover_device, :pushover_sound, :email_messages, :pushover_messages,
:pushover_mentions, :mailing_list_enabled, :delete_me :email_mentions, :pushover_mentions, :mailing_list_enabled, :delete_me
before_save :check_session_token before_save :check_session_token
before_validation :on => :create do before_validation :on => :create do
@ -187,6 +187,13 @@ class User < ActiveRecord::Base
).first ).first
end end
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 }))
end
end
def recent_threads(amount) def recent_threads(amount)
self.comments.group(:thread_id).order('MAX(created_at) DESC').limit( self.comments.group(:thread_id).order('MAX(created_at) DESC').limit(
amount).pluck(:thread_id) amount).pluck(:thread_id)

View file

@ -51,6 +51,12 @@
:size => 15 %> :size => 15 %>
</div> </div>
<div class="boxline">
<%= f.label :pushover_sound, "Pushover Sound:",
:class => "required" %>
<%= f.select :pushover_sound, Pushover.sounds.map{|k,v| [ v, k ] } %>
</div>
<div class="boxline"> <div class="boxline">
<%= f.label :about, "About:", :class => "required" %> <%= f.label :about, "About:", :class => "required" %>
<%= f.text_area :about, :size => "100x5", :style => "width: 600px;" %> <%= f.text_area :about, :size => "100x5", :style => "width: 600px;" %>

View file

@ -0,0 +1,5 @@
class PushoverSound < ActiveRecord::Migration
def change
add_column :users, :pushover_sound, :string
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140113153413) do ActiveRecord::Schema.define(version: 20140121063641) do
create_table "comments", force: true do |t| create_table "comments", force: true do |t|
t.datetime "created_at", null: false t.datetime "created_at", null: false
@ -162,6 +162,7 @@ ActiveRecord::Schema.define(version: 20140113153413) do
t.integer "banned_by_user_id" t.integer "banned_by_user_id"
t.string "banned_reason", limit: 200 t.string "banned_reason", limit: 200
t.datetime "deleted_at" t.datetime "deleted_at"
t.string "pushover_sound"
end end
add_index "users", ["mailing_list_enabled"], name: "mailing_list_enabled", using: :btree add_index "users", ["mailing_list_enabled"], name: "mailing_list_enabled", using: :btree

View file

@ -4,18 +4,39 @@ class Pushover
# this needs to be overridden in config/initializers/production.rb # this needs to be overridden in config/initializers/production.rb
@@API_KEY = nil @@API_KEY = nil
@@SOUNDS = {}
def self.sounds
if !@@API_KEY
return
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) def self.push(user, device, params)
if !@@API_KEY if !@@API_KEY
return return
end end
params[:message] = params[:message].to_s.match(/.{0,512}/m).to_s
if params[:message] == ""
params[:message] = "(No message)"
end
begin begin
params[:message] = params[:message].to_s.match(/.{0,512}/m).to_s
if params[:message] == ""
params[:message] = "(No message)"
end
s = Sponge.new s = Sponge.new
s.fetch("https://api.pushover.net/1/messages.json", :post, { s.fetch("https://api.pushover.net/1/messages.json", :post, {
:token => @@API_KEY, :token => @@API_KEY,