merge @username mentions and notifications from @rjsamson

This commit is contained in:
joshua stein 2012-09-16 15:41:21 -05:00
commit 17d8213bc7
8 changed files with 79 additions and 4 deletions

View file

@ -9,4 +9,13 @@ class EmailReply < ActionMailer::Base
:subject => "[Lobsters] Reply from #{comment.user.username} on " <<
"#{comment.story.title}")
end
def mention(comment, user)
@comment = comment
@user = user
mail(:to => user.email, :from => "Lobsters <nobody@lobste.rs>",
:subject => "[Lobsters] Mention from #{comment.user.username} on " <<
"#{comment.story.title}")
end
end

View file

@ -12,7 +12,7 @@ class Comment < ActiveRecord::Base
:indent_level, :highlighted
before_create :assign_short_id_and_upvote, :assign_initial_confidence
after_create :assign_votes, :mark_submitter, :deliver_reply_notifications
after_create :assign_votes, :mark_submitter, :deliver_reply_notifications, :deliver_mention_notifications
after_destroy :unassign_votes
MAX_EDIT_MINS = 45
@ -98,6 +98,24 @@ class Comment < ActiveRecord::Base
Keystore.increment_value_for("user:#{self.user_id}:comments_posted")
end
def deliver_mention_notifications
self[:comment].gsub(/\B\@([\w\-]+)/) do |u|
if user = User.find_by_username(u[1..-1])
if user.email_mentions?
EmailReply.mention(self, user).deliver
end
if user.pushover_mentions? && user.pushover_user_key.present?
Pushover.push(user.pushover_user_key, user.pushover_device, {
:title => "Lobsters 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
end
def deliver_reply_notifications
begin
if self.parent_comment_id && u = self.parent_comment.try(:user)

View file

@ -24,7 +24,7 @@ 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
:pushover_device, :email_messages, :pushover_messages, :email_mentions, :pushover_mentions
before_save :check_session_token
after_create :create_default_tag_filters

View file

@ -0,0 +1,5 @@
<%= @comment.user.username %> has mention you in a comment:
<%= word_wrap(@comment.plaintext_comment, :line_width => 72).gsub(/\n/, "\n ") %>
Continue this discussion at <%= @comment.url %>

View file

@ -89,6 +89,27 @@
<br>
<div class="legend">
Comment Mention Notification Settings
</div>
<div class="boxline">
<%= f.label :email_mentions, "Receive E-mail:", :class => "required" %>
<%= f.check_box :email_mentions %>
</div>
<div class="boxline">
<%= f.label :pushover_mentions,
raw("Receive <a href=\"https://pushover.net/\">Pushover</a> Alert:"),
:class => "required" %>
<%= f.check_box :pushover_mentions %>
<span class="hint">
Requires user key entered above
</span>
</div>
<br>
<div class="legend">
Private Message Notification Settings
</div>

View file

@ -0,0 +1,11 @@
class MentionNotificationOptions < ActiveRecord::Migration
def up
add_column :users, :email_mentions, :boolean, :default => false
add_column :users, :pushover_mentions, :boolean, :default => false
end
def down
remove_column :users, :pushover_mentions
remove_column :users, :email_mentions
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120906183346) do
ActiveRecord::Schema.define(:version => 20120910172514) do
create_table "comments", :force => true do |t|
t.datetime "created_at", :null => false
@ -137,6 +137,8 @@ ActiveRecord::Schema.define(:version => 20120906183346) do
t.boolean "email_messages", :default => true
t.boolean "pushover_messages", :default => true
t.boolean "is_moderator", :default => false
t.boolean "email_mentions", :default => false
t.boolean "pushover_mentions", :default => false
end
add_index "users", ["session_token"], :name => "session_hash", :unique => true

View file

@ -22,7 +22,16 @@ class Markdowner
# make links have rel=nofollow
html.gsub!(/<a href/, "<a rel=\"nofollow\" href")
# make @mentions into links to user profile
html.gsub!(/\B\@([\w\-]+)/) do |u|
if User.find_by_username(u[1..-1])
"<a href=\"/u/#{u[1..-1]}\">#{u}</a>"
else
u
end
end
html
end
end
end
end