add dmail notices for mentions in forum posts

This commit is contained in:
r888888888
2015-10-14 15:32:35 -07:00
parent 392ca99329
commit 821c7bc2a5
4 changed files with 68 additions and 1 deletions

View File

@@ -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!(/</, "&lt;")
str.gsub!(/>/, "&gt;")
str.gsub!(/(?:^| )@\S+/) do |name|
str.gsub!(MENTION_REGEXP) do |name|
if name =~ /([:;,.!?\)\]<>])$/
name.chop!
ch = $1

View 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