add dmail notices for mentions in forum posts
This commit is contained in:
@@ -2,6 +2,8 @@ require 'cgi'
|
||||
require 'uri'
|
||||
|
||||
class DText
|
||||
MENTION_REGEXP = /(?:^| )@\S+/
|
||||
|
||||
def self.u(string)
|
||||
CGI.escape(string)
|
||||
end
|
||||
@@ -36,7 +38,7 @@ class DText
|
||||
str.gsub!(/&/, "&")
|
||||
str.gsub!(/</, "<")
|
||||
str.gsub!(/>/, ">")
|
||||
str.gsub!(/(?:^| )@\S+/) do |name|
|
||||
str.gsub!(MENTION_REGEXP) do |name|
|
||||
if name =~ /([:;,.!?\)\]<>])$/
|
||||
name.chop!
|
||||
ch = $1
|
||||
|
||||
41
app/logical/mentionable.rb
Normal file
41
app/logical/mentionable.rb
Normal file
@@ -0,0 +1,41 @@
|
||||
module Mentionable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
module ClassMethods
|
||||
# options:
|
||||
# - message_field
|
||||
# - user_field
|
||||
def mentionable(options = {})
|
||||
@mentionable_options = options
|
||||
|
||||
after_create :queue_mention_messages
|
||||
end
|
||||
|
||||
def mentionable_option(key)
|
||||
@mentionable_options[key]
|
||||
end
|
||||
end
|
||||
|
||||
def queue_mention_messages
|
||||
title = self.class.mentionable_option(:title)
|
||||
from_id = read_attribute(self.class.mentionable_option(:user_field))
|
||||
text = read_attribute(self.class.mentionable_option(:message_field))
|
||||
|
||||
text.scan(DText::MENTION_REGEXP).each do |mention|
|
||||
mention.gsub!(/(?:^\s*@)|(?:[:;,.!?\)\]<>]$)/, "")
|
||||
user = User.find_by_name(mention)
|
||||
body = self.class.mentionable_option(:body).call(self, user.name)
|
||||
|
||||
if user
|
||||
dmail = Dmail.new(
|
||||
from_id: from_id,
|
||||
to_id: user.id,
|
||||
title: title,
|
||||
body: body
|
||||
)
|
||||
dmail.owner_id = user.id
|
||||
dmail.save
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,6 @@
|
||||
class ForumPost < ActiveRecord::Base
|
||||
include Mentionable
|
||||
|
||||
attr_accessible :body, :topic_id, :as => [:member, :builder, :janitor, :gold, :platinum, :contributor, :admin, :moderator, :default]
|
||||
attr_accessible :is_locked, :is_sticky, :is_deleted, :as => [:admin, :moderator]
|
||||
attr_readonly :topic_id
|
||||
@@ -16,6 +18,12 @@ class ForumPost < ActiveRecord::Base
|
||||
validate :topic_id_not_invalid
|
||||
before_destroy :validate_topic_is_unlocked
|
||||
after_save :delete_topic_if_original_post
|
||||
mentionable(
|
||||
:message_field => :body,
|
||||
:user_field => :creator_id,
|
||||
:title => "You were mentioned in a forum topic",
|
||||
:body => lambda {|rec, user_name| "You were mentioned in the forum topic \"#{rec.topic.title}\":#{Danbooru.config.hostname}/forum_topics/#{rec.topic_id}?page=#{rec.forum_topic_page}\n\n<hr>\n\n#{ActionController::Base.helpers.excerpt(rec.body, user_name)}"}
|
||||
)
|
||||
|
||||
module SearchMethods
|
||||
def body_matches(body)
|
||||
|
||||
@@ -14,6 +14,22 @@ class ForumPostTest < ActiveSupport::TestCase
|
||||
CurrentUser.ip_addr = nil
|
||||
end
|
||||
|
||||
context "that mentions a user" do
|
||||
setup do
|
||||
@user2 = FactoryGirl.create(:user)
|
||||
@post = FactoryGirl.build(:forum_post, :topic_id => @topic.id, :body => "Hey @#{@user2.name} check this out!")
|
||||
end
|
||||
|
||||
should "create a dmail" do
|
||||
assert_difference("Dmail.count", 1) do
|
||||
@post.save
|
||||
end
|
||||
|
||||
dmail = Dmail.last
|
||||
assert_equal("You were mentioned in the forum topic \"#{@topic.title}\":#{Danbooru.config.hostname}/forum_topics/#{@topic.id}?page=1\n\n<hr>\n\nHey @#{@user2.name} check this out!", dmail.body)
|
||||
end
|
||||
end
|
||||
|
||||
context "that belongs to a topic with several pages of posts" do
|
||||
setup do
|
||||
Danbooru.config.stubs(:posts_per_page).returns(3)
|
||||
|
||||
Reference in New Issue
Block a user