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:
parent
00f8e9c189
commit
71eeb6c7d2
|
@ -134,18 +134,14 @@ class Comment < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
if u.pushover_mentions? && u.pushover_user_key.present?
|
||||
begin
|
||||
Pushover.push(u.pushover_user_key, u.pushover_device, {
|
||||
:title => "#{Rails.application.name} mention by " <<
|
||||
"#{self.user.username} on #{self.story.title}",
|
||||
:message => self.plaintext_comment,
|
||||
:url => self.url,
|
||||
:url_title => "Reply to #{self.user.username}",
|
||||
})
|
||||
rescue => e
|
||||
Rails.logger.error "error sending to pushover: #{e}"
|
||||
end
|
||||
if u.pushover_mentions?
|
||||
u.pushover!({
|
||||
:title => "#{Rails.application.name} mention by " <<
|
||||
"#{self.user.username} on #{self.story.title}",
|
||||
:message => self.plaintext_comment,
|
||||
:url => self.url,
|
||||
:url_title => "Reply to #{self.user.username}",
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -162,18 +158,14 @@ class Comment < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
if u.pushover_replies? && u.pushover_user_key.present?
|
||||
begin
|
||||
Pushover.push(u.pushover_user_key, u.pushover_device, {
|
||||
:title => "#{Rails.application.name} reply from " <<
|
||||
"#{self.user.username} on #{self.story.title}",
|
||||
:message => self.plaintext_comment,
|
||||
:url => self.url,
|
||||
:url_title => "Reply to #{self.user.username}",
|
||||
})
|
||||
rescue => e
|
||||
Rails.logger.error "error sending to pushover: #{e}"
|
||||
end
|
||||
if u.pushover_replies?
|
||||
u.pushover!({
|
||||
:title => "#{Rails.application.name} reply from " <<
|
||||
"#{self.user.username} on #{self.story.title}",
|
||||
:message => self.plaintext_comment,
|
||||
:url => self.url,
|
||||
:url_title => "Reply to #{self.user.username}",
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -45,20 +45,14 @@ class Message < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
if self.recipient.pushover_messages? &&
|
||||
self.recipient.pushover_user_key.present?
|
||||
begin
|
||||
Pushover.push(self.recipient.pushover_user_key,
|
||||
self.recipient.pushover_device, {
|
||||
:title => "#{Rails.application.name} message from " <<
|
||||
"#{self.author.username}: #{self.subject}",
|
||||
: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
|
||||
if self.recipient.pushover_messages?
|
||||
self.recipient.pushover!({
|
||||
:title => "#{Rails.application.name} message from " <<
|
||||
"#{self.author.username}: #{self.subject}",
|
||||
:message => self.plaintext_body,
|
||||
:url => self.url,
|
||||
:url_title => "Reply to #{self.author.username}",
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -38,8 +38,8 @@ class User < ActiveRecord::Base
|
|||
|
||||
attr_accessible :username, :email, :password, :password_confirmation,
|
||||
:about, :email_replies, :pushover_replies, :pushover_user_key,
|
||||
:pushover_device, :email_messages, :pushover_messages, :email_mentions,
|
||||
:pushover_mentions, :mailing_list_enabled, :delete_me
|
||||
:pushover_device, :pushover_sound, :email_messages, :pushover_messages,
|
||||
:email_mentions, :pushover_mentions, :mailing_list_enabled, :delete_me
|
||||
|
||||
before_save :check_session_token
|
||||
before_validation :on => :create do
|
||||
|
@ -187,6 +187,13 @@ class User < ActiveRecord::Base
|
|||
).first
|
||||
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)
|
||||
self.comments.group(:thread_id).order('MAX(created_at) DESC').limit(
|
||||
amount).pluck(:thread_id)
|
||||
|
|
|
@ -51,6 +51,12 @@
|
|||
: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>
|
||||
|
||||
<div class="boxline">
|
||||
<%= f.label :about, "About:", :class => "required" %>
|
||||
<%= f.text_area :about, :size => "100x5", :style => "width: 600px;" %>
|
||||
|
|
5
db/migrate/20140121063641_pushover_sound.rb
Normal file
5
db/migrate/20140121063641_pushover_sound.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class PushoverSound < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :users, :pushover_sound, :string
|
||||
end
|
||||
end
|
|
@ -11,7 +11,7 @@
|
|||
#
|
||||
# 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|
|
||||
t.datetime "created_at", null: false
|
||||
|
@ -162,6 +162,7 @@ ActiveRecord::Schema.define(version: 20140113153413) do
|
|||
t.integer "banned_by_user_id"
|
||||
t.string "banned_reason", limit: 200
|
||||
t.datetime "deleted_at"
|
||||
t.string "pushover_sound"
|
||||
end
|
||||
|
||||
add_index "users", ["mailing_list_enabled"], name: "mailing_list_enabled", using: :btree
|
||||
|
|
|
@ -4,18 +4,39 @@ class Pushover
|
|||
# this needs to be overridden in config/initializers/production.rb
|
||||
@@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)
|
||||
if !@@API_KEY
|
||||
return
|
||||
end
|
||||
|
||||
params[:message] = params[:message].to_s.match(/.{0,512}/m).to_s
|
||||
|
||||
if params[:message] == ""
|
||||
params[:message] = "(No message)"
|
||||
end
|
||||
|
||||
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.fetch("https://api.pushover.net/1/messages.json", :post, {
|
||||
:token => @@API_KEY,
|
||||
|
|
Loading…
Reference in a new issue