From ec1bcb9704f9092c8842b0aa26ec0117363d7afd Mon Sep 17 00:00:00 2001 From: Robert J Samson Date: Mon, 10 Sep 2012 13:40:33 -0400 Subject: [PATCH] Added mentions in comments using @username convention. @username is linked to user profile in comment text, and notifications are available via email and pushover (with notification preferences) when a user is mention. --- app/mailers/email_reply.rb | 9 ++++++++ app/models/comment.rb | 20 +++++++++++++++++- app/models/user.rb | 2 +- app/views/email_reply/mention.text.erb | 5 +++++ app/views/settings/index.html.erb | 21 +++++++++++++++++++ ...0910172514_mention_notification_options.rb | 11 ++++++++++ db/schema.rb | 4 +++- extras/markdowner.rb | 11 +++++++++- 8 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 app/views/email_reply/mention.text.erb create mode 100644 db/migrate/20120910172514_mention_notification_options.rb diff --git a/app/mailers/email_reply.rb b/app/mailers/email_reply.rb index d7db75e..3cc1fe7 100644 --- a/app/mailers/email_reply.rb +++ b/app/mailers/email_reply.rb @@ -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 ", + :subject => "[Lobsters] Mention from #{comment.user.username} on " << + "#{comment.story.title}") + end end diff --git a/app/models/comment.rb b/app/models/comment.rb index faa8050..189c073 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -12,7 +12,7 @@ class Comment < ActiveRecord::Base :indent_level, :highlighted before_create :assign_short_id_and_upvote - 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) diff --git a/app/models/user.rb b/app/models/user.rb index 2218733..0eef1e7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -22,7 +22,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 diff --git a/app/views/email_reply/mention.text.erb b/app/views/email_reply/mention.text.erb new file mode 100644 index 0000000..1556c3c --- /dev/null +++ b/app/views/email_reply/mention.text.erb @@ -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 %> \ No newline at end of file diff --git a/app/views/settings/index.html.erb b/app/views/settings/index.html.erb index 6a22472..fc76f54 100644 --- a/app/views/settings/index.html.erb +++ b/app/views/settings/index.html.erb @@ -89,6 +89,27 @@
+
+ Comment Mention Notification Settings +
+ +
+ <%= f.label :email_mentions, "Receive E-mail:", :class => "required" %> + <%= f.check_box :email_mentions %> +
+ +
+ <%= f.label :pushover_mentions, + raw("Receive Pushover Alert:"), + :class => "required" %> + <%= f.check_box :pushover_mentions %> + + Requires user key entered above + +
+ +
+
Private Message Notification Settings
diff --git a/db/migrate/20120910172514_mention_notification_options.rb b/db/migrate/20120910172514_mention_notification_options.rb new file mode 100644 index 0000000..3ef0cf9 --- /dev/null +++ b/db/migrate/20120910172514_mention_notification_options.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 018a07a..c74936e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/extras/markdowner.rb b/extras/markdowner.rb index 76a72e5..6aa306b 100644 --- a/extras/markdowner.rb +++ b/extras/markdowner.rb @@ -22,7 +22,16 @@ class Markdowner # make links have rel=nofollow html.gsub!(/#{u}" + else + u + end + end + html end end -end +end \ No newline at end of file