Skip to content

Commit

Permalink
Add comment notifications and ability to toggle them as a pref
Browse files Browse the repository at this point in the history
  • Loading branch information
rwdaigle committed Dec 6, 2012
1 parent 58f65f8 commit 93da555
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -25,6 +25,7 @@ gem 'newrelic_rpm'
gem 'newrelic-faraday'
gem 'airbrake'
gem 'kanshi', :require => false
gem 'pony'

gem 'jquery-rails'
gem 'haml-rails'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -117,6 +117,8 @@ GEM
pjax_rails (0.3.3)
jquery-rails
polyglot (0.3.3)
pony (1.4)
mail (> 2.0)
queue_classic (2.0.1)
pg (~> 0.14.0)
scrolls (~> 0.2.1)
Expand Down Expand Up @@ -201,6 +203,7 @@ DEPENDENCIES
omniauth-github
pg
pjax_rails
pony
queue_classic
rack-worker
rails (= 3.2.8)
Expand Down
5 changes: 5 additions & 0 deletions app/assets/stylesheets/site.sass
Expand Up @@ -8,6 +8,11 @@
color: #ccc
font-size: 12px

.notifications
margin-right: 15px
color: #ccc
font-size: 12px

#footer
position: absolute
bottom: 0px
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/users_controller.rb
@@ -0,0 +1,9 @@
class UsersController < ApplicationController

before_filter :force_user_login

def toggle_notifications
current_user.toggle_notifications
redirect_to :back
end
end
14 changes: 13 additions & 1 deletion app/models/comment_notifier.rb
Expand Up @@ -5,7 +5,19 @@ class << self
def gist_commented(gist_id)
gist = Gist.find(gist_id)
user = gist.user
# Mail!
if(user.notify_comments?)
log({ns: self, fn: __method__, measure: true, at: 'sending-notification'}, gist, user)
Pony.mail(:to => user.gh_email, :subject => "\"#{gist.description}\" has a new comment", :body => <<-EOS)
The extremely unsophisticated comment-detection algorithm at Gisted would like to let you know that you have a new comment on your "#{gist.description}" gist.
View your gist's comments at #{gist.url}#comments
---
-Gisted (https://gistedapp.herokuapp.com/)
If this email offends your delicate sensibilities, you can opt out by clikcing the "turn off comment notifications" link on the Gisted interface: https://gistedapp.herokuapp.com/ or by complaining directly to @rwdaigle.
EOS
end
end
end
end
1 change: 1 addition & 0 deletions app/models/gist.rb
Expand Up @@ -161,6 +161,7 @@ def indexed_attributes

def check_for_new_comments
if(comment_count_changed?)
log({ns: self, fn: __method__, measure: true, at: 'comment-count-changed'}, self, user)
QC.enqueue("CommentNotifier.gist_commented", id) if comment_count.to_i > comment_count_was.to_i
end
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/user.rb
Expand Up @@ -42,6 +42,11 @@ def refresh_index(user_id, all = false)
end
end

def toggle_notifications
log({ns: self.class, fn: __method__, measure: true, to: !notify_comments}, self)
update_attribute(:notify_comments, !notify_comments)
end

def indexed!
update_attribute(:last_indexed_at, Time.now)
end
Expand Down
5 changes: 5 additions & 0 deletions app/views/layouts/application.html.haml
Expand Up @@ -27,6 +27,11 @@
#profile
- if user_logged_in?
%span.last-edited= "Last known gist activity on #{current_user.last_gist_updated_at ? current_user.last_gist_updated_at.to_formatted_s(:short) : 'never'}"
%span.notifications
- if(current_user.notify_comments?)
= link_to("turn off comment notifications", toggle_notifications_users_path, :method => :post)
- else
= link_to("notify me of gist comments", toggle_notifications_users_path, :method => :post)
%span.logout= link_to("logout", logout_path)
#header
.container
Expand Down
12 changes: 12 additions & 0 deletions config/initializers/pony.rb
@@ -0,0 +1,12 @@
Pony.options = {
:from => 'notifications@gistedapp.herokuapp.com',
:via => :smtp,
:via_options => {
:address => ENV['MAILGUN_SMTP_SERVER'],
:port => ENV['MAILGUN_SMTP_PORT'],
:user_name => ENV['MAILGUN_SMTP_LOGIN'],
:password => ENV['MAILGUN_SMTP_PASSWORD'],
:authentication => :plain,
:domain => ENV['HOST']
}
}
6 changes: 6 additions & 0 deletions config/routes.rb
Expand Up @@ -14,6 +14,12 @@
# Though this was an option at one time?
all = [:index, :show, :new, :create, :edit, :update, :destroy]

resources :users, :except => all do
collection do
post 'toggle_notifications'
end
end

resources :gists, :except => all do
collection do
post 'refresh'
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20121206183232_add_comment_preference.rb
@@ -0,0 +1,12 @@
class AddCommentPreference < ActiveRecord::Migration

def up
change_table :users do |t|
t.boolean :notify_comments, :default => false
end
end

def down
remove_column :users, :notify_comments
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20121125001827) do
ActiveRecord::Schema.define(:version => 20121206183232) do

create_table "gist_files", :force => true do |t|
t.integer "gist_id"
Expand Down Expand Up @@ -72,6 +72,7 @@
t.datetime "updated_at", :null => false
t.boolean "gh_auth_active", :default => true
t.datetime "last_indexed_at"
t.boolean "notify_comments", :default => true
end

add_index "users", ["gh_id"], :name => "index_users_on_gh_id"
Expand Down

0 comments on commit 93da555

Please sign in to comment.