forked from e621ng/e621ng
added dmail test
This commit is contained in:
parent
1a7197807a
commit
0bb52fd63a
94
app/models/dmail.rb
Normal file
94
app/models/dmail.rb
Normal file
@ -0,0 +1,94 @@
|
||||
class Dmail < ActiveRecord::Base
|
||||
validates_presence_of :to_id
|
||||
validates_presence_of :from_id
|
||||
validates_format_of :title, :with => /\S/
|
||||
validates_format_of :body, :with => /\S/
|
||||
belongs_to :owner, :class_name => "User"
|
||||
belongs_to :to, :class_name => "User"
|
||||
belongs_to :from, :class_name => "User"
|
||||
after_create :update_recipient
|
||||
after_create :send_dmail
|
||||
attr_accessible :title, :body, :is_deleted
|
||||
scope :for, lambda {|user| where(["owner_id = ?", user])}
|
||||
scope :inbox, where("to_id = owner_id")
|
||||
scope :sent, where("from_id = owner_id")
|
||||
scope :active, where(["is_deleted = ?", false])
|
||||
scope :deleted, where(["is_deleted = ?", true])
|
||||
scope :search_message, lambda {|query| where(["message_index @@ plainto_tsquery(?)", query])}
|
||||
|
||||
module AddressMethods
|
||||
def to_name
|
||||
User.find_pretty_name(to_id)
|
||||
end
|
||||
|
||||
def from_name
|
||||
User.find_pretty_name(from_id)
|
||||
end
|
||||
|
||||
def to_name=(name)
|
||||
user = User.find_by_name(name)
|
||||
return if user.nil?
|
||||
self.to_id = user.id
|
||||
end
|
||||
|
||||
def from_name=(name)
|
||||
user = User.find_by_name(name)
|
||||
return if user.nil?
|
||||
self.from_id = user.id
|
||||
end
|
||||
end
|
||||
|
||||
module FactoryMethods
|
||||
module ClassMethods
|
||||
def create_new(dmail)
|
||||
copy = dmail.clone
|
||||
copy.owner_id = dmail.to_id
|
||||
copy.save
|
||||
|
||||
copy = dmail.clone
|
||||
copy.owner_id = dmail.from_id
|
||||
copy.save
|
||||
end
|
||||
end
|
||||
|
||||
def self.included(m)
|
||||
m.extend(ClassMethods)
|
||||
end
|
||||
|
||||
def build_response
|
||||
Dmail.new do |dmail|
|
||||
dmail.title = "Re: #{title}"
|
||||
dmail.owner_id = from_id
|
||||
dmail.body = quoted_body
|
||||
dmail.to_id = from_id
|
||||
dmail.from_id = to_id
|
||||
dmail.parent_id = id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
include AddressMethods
|
||||
include FactoryMethods
|
||||
|
||||
def quoted_body
|
||||
"[quote]#{body}[/quote]"
|
||||
end
|
||||
|
||||
def send_dmail
|
||||
if to.receive_email_notifications? && to.email.include?("@")
|
||||
UserMailer.dmail_notice(self).deliver
|
||||
end
|
||||
end
|
||||
|
||||
def mark_as_read!
|
||||
update_attribute(:is_read, true)
|
||||
|
||||
unless Dmail.exists?(["to_id = ? AND is_read = false", to_id])
|
||||
to.update_attribute(:has_mail, false)
|
||||
end
|
||||
end
|
||||
|
||||
def update_recipient
|
||||
to.update_attribute(:has_mail, true)
|
||||
end
|
||||
end
|
@ -23,6 +23,10 @@ class User < ActiveRecord::Base
|
||||
select_value_sql("SELECT name FROM users WHERE id = ?", user_id) || Danbooru.config.default_guest_name
|
||||
end
|
||||
end
|
||||
|
||||
def find_pretty_name(user_id)
|
||||
find_name.tr("_", " ")
|
||||
end
|
||||
end
|
||||
|
||||
def self.included(m)
|
||||
|
24
app/models/user_mailer.rb
Normal file
24
app/models/user_mailer.rb
Normal file
@ -0,0 +1,24 @@
|
||||
class UserMailer < ActionMailer::Base
|
||||
default :host => Danbooru.config.server_host, :from => Danbooru.config.contact_email, :content_type => "text/html"
|
||||
|
||||
def password_reset(user, new_password)
|
||||
@user = user
|
||||
@new_password = new_password
|
||||
mail(:to => @user.email, :subject => "#{Danbooru.config.app_name} - Password Reset")
|
||||
end
|
||||
|
||||
def name_reminder(user)
|
||||
@user = user
|
||||
mail(:to => user.email, :subject => "#{Danbooru.config.app_name} - Name Reminder")
|
||||
end
|
||||
|
||||
def deletion(user)
|
||||
@user = user
|
||||
mail(:to => user.email, :subject => "#{}")
|
||||
end
|
||||
|
||||
def dmail_notice(dmail)
|
||||
@dmail = dmail
|
||||
mail(:to => dmail.to.email, :subject => "#{Danbooru.config.app_name} - Message received from #{dmail.from.name}")
|
||||
end
|
||||
end
|
5
app/views/user_mailer/dmail.html.erb
Normal file
5
app/views/user_mailer/dmail.html.erb
Normal file
@ -0,0 +1,5 @@
|
||||
<p><%= h @dmail.fromr.name %> said:</p>
|
||||
|
||||
<div>
|
||||
<%= h @dmail.body %>
|
||||
</div>
|
@ -10,6 +10,11 @@ module Danbooru
|
||||
"Danbooru"
|
||||
end
|
||||
|
||||
# Contact email address of the admin.
|
||||
def contact_email
|
||||
"webmaster#{server_host}"
|
||||
end
|
||||
|
||||
# Stripped of any special characters.
|
||||
def safe_app_name
|
||||
app_name.gsub(/[^a-zA-Z0-9_-]/, "_")
|
||||
|
@ -353,6 +353,44 @@ CREATE SEQUENCE comments_id_seq
|
||||
ALTER SEQUENCE comments_id_seq OWNED BY comments.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: dmails; Type: TABLE; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
|
||||
CREATE TABLE dmails (
|
||||
id integer NOT NULL,
|
||||
owner_id integer NOT NULL,
|
||||
from_id integer NOT NULL,
|
||||
to_id integer NOT NULL,
|
||||
parent_id integer,
|
||||
title character varying(255) NOT NULL,
|
||||
body text NOT NULL,
|
||||
message_index tsvector NOT NULL,
|
||||
is_read boolean DEFAULT false NOT NULL,
|
||||
created_at timestamp without time zone,
|
||||
updated_at timestamp without time zone
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: dmails_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE SEQUENCE dmails_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MAXVALUE
|
||||
NO MINVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
--
|
||||
-- Name: dmails_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER SEQUENCE dmails_id_seq OWNED BY dmails.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: favorites_0; Type: TABLE; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
@ -1245,6 +1283,13 @@ ALTER TABLE comment_votes ALTER COLUMN id SET DEFAULT nextval('comment_votes_id_
|
||||
ALTER TABLE comments ALTER COLUMN id SET DEFAULT nextval('comments_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE dmails ALTER COLUMN id SET DEFAULT nextval('dmails_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
@ -1477,6 +1522,14 @@ ALTER TABLE ONLY comments
|
||||
ADD CONSTRAINT comments_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: dmails_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY dmails
|
||||
ADD CONSTRAINT dmails_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: favorites_0_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
@ -1788,6 +1841,27 @@ CREATE INDEX index_comments_on_body_index ON comments USING gin (body_index);
|
||||
CREATE INDEX index_comments_on_post_id ON comments USING btree (post_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_dmails_on_message_index; Type: INDEX; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
|
||||
CREATE INDEX index_dmails_on_message_index ON dmails USING gin (message_index);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_dmails_on_owner_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
|
||||
CREATE INDEX index_dmails_on_owner_id ON dmails USING btree (owner_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_dmails_on_parent_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
|
||||
CREATE INDEX index_dmails_on_parent_id ON dmails USING btree (parent_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_favorites_0_on_post_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
@ -2151,6 +2225,16 @@ CREATE TRIGGER trigger_comments_on_update
|
||||
EXECUTE PROCEDURE tsvector_update_trigger('body_index', 'pg_catalog.english', 'body');
|
||||
|
||||
|
||||
--
|
||||
-- Name: trigger_dmails_on_update; Type: TRIGGER; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TRIGGER trigger_dmails_on_update
|
||||
BEFORE INSERT OR UPDATE ON dmails
|
||||
FOR EACH ROW
|
||||
EXECUTE PROCEDURE tsvector_update_trigger('message_index', 'pg_catalog.english', 'title', 'body');
|
||||
|
||||
|
||||
--
|
||||
-- Name: trigger_posts_on_tag_index_update; Type: TRIGGER; Schema: public; Owner: -
|
||||
--
|
||||
@ -2217,4 +2301,6 @@ INSERT INTO schema_migrations (version) VALUES ('20100215224635');
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20100215225710');
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20100215230642');
|
||||
INSERT INTO schema_migrations (version) VALUES ('20100215230642');
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20100219230537');
|
26
db/migrate/20100219230537_create_dmails.rb
Normal file
26
db/migrate/20100219230537_create_dmails.rb
Normal file
@ -0,0 +1,26 @@
|
||||
class CreateDmails < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :dmails do |t|
|
||||
t.column :owner_id, :integer, :null => false
|
||||
t.column :from_id, :integer, :null => false
|
||||
t.column :to_id, :integer, :null => false
|
||||
t.column :parent_id, :integer
|
||||
t.column :title, :string, :null => false
|
||||
t.column :body, :text, :null => false
|
||||
t.column :message_index, "tsvector", :null => false
|
||||
t.column :is_read, :boolean, :null => false, :default => false
|
||||
t.column :is_deleted, :boolean, :null => false, :default => false
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :dmails, :owner_id
|
||||
add_index :dmails, :parent_id
|
||||
|
||||
execute "CREATE INDEX index_dmails_on_message_index ON dmails USING GIN (message_index)"
|
||||
execute "CREATE TRIGGER trigger_dmails_on_update BEFORE INSERT OR UPDATE ON dmails FOR EACH ROW EXECUTE PROCEDURE tsvector_update_trigger('message_index', 'pg_catalog.english', 'title', 'body')"
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :dmails
|
||||
end
|
||||
end
|
144
lib/dtext.rb
Normal file
144
lib/dtext.rb
Normal file
@ -0,0 +1,144 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
require 'cgi'
|
||||
|
||||
module DText
|
||||
def parse_inline(str, options = {})
|
||||
str = str.gsub(/&/, "&")
|
||||
str.gsub!(/</, "<")
|
||||
str.gsub!(/>/, ">")
|
||||
str.gsub!(/\[\[.+?\]\]/m) do |tag|
|
||||
tag = tag[2..-3]
|
||||
if tag =~ /^(.+?)\|(.+)$/
|
||||
tag = $1
|
||||
name = $2
|
||||
'<a href="/wiki/show?title=' + CGI.escape(CGI.unescapeHTML(tag.tr(" ", "_").downcase)) + '">' + name + '</a>'
|
||||
else
|
||||
'<a href="/wiki/show?title=' + CGI.escape(CGI.unescapeHTML(tag.tr(" ", "_").downcase)) + '">' + tag + '</a>'
|
||||
end
|
||||
end
|
||||
str.gsub!(/\{\{.+?\}\}/m) do |tag|
|
||||
tag = tag[2..-3]
|
||||
'<a href="/post/index?tags=' + CGI.escape(CGI.unescapeHTML(tag)) + '">' + tag + '</a>'
|
||||
end
|
||||
str.gsub!(/[Pp]ost #(\d+)/, '<a href="/post/show/\1">post #\1</a>')
|
||||
str.gsub!(/[Ff]orum #(\d+)/, '<a href="/forum/show/\1">forum #\1</a>')
|
||||
str.gsub!(/[Cc]omment #(\d+)/, '<a href="/comment/show/\1">comment #\1</a>')
|
||||
str.gsub!(/[Pp]ool #(\d+)/, '<a href="/pool/show/\1">pool #\1</a>')
|
||||
str.gsub!(/\n/m, "<br>")
|
||||
str.gsub!(/\[b\](.+?)\[\/b\]/, '<strong>\1</strong>')
|
||||
str.gsub!(/\[i\](.+?)\[\/i\]/, '<em>\1</em>')
|
||||
str.gsub!(/\[spoilers?\](.+?)\[\/spoilers?\]/m, '<span class="spoiler">\1</span>')
|
||||
str.gsub!(/("[^"]+":(http:\/\/|\/)\S+|http:\/\/\S+)/m) do |link|
|
||||
if link =~ /^"([^"]+)":(.+)$/
|
||||
text = $1
|
||||
link = $2
|
||||
else
|
||||
text = link
|
||||
end
|
||||
|
||||
if link =~ /([;,.!?\)\]<>])$/
|
||||
link.chop!
|
||||
ch = $1
|
||||
else
|
||||
ch = ""
|
||||
end
|
||||
|
||||
link.gsub!(/"/, '"')
|
||||
'<a href="' + link + '">' + text + '</a>' + ch
|
||||
end
|
||||
str
|
||||
end
|
||||
|
||||
def parse_list(str, options = {})
|
||||
html = ""
|
||||
layout = []
|
||||
nest = 0
|
||||
|
||||
str.split(/\n/).each do |line|
|
||||
if line =~ /^\s*(\*+) (.+)/
|
||||
nest = $1.size
|
||||
content = parse_inline($2)
|
||||
else
|
||||
content = parse_inline(line)
|
||||
end
|
||||
|
||||
if nest > layout.size
|
||||
html += "<ul>"
|
||||
layout << "ul"
|
||||
end
|
||||
|
||||
while nest < layout.size
|
||||
elist = layout.pop
|
||||
if elist
|
||||
html += "</#{elist}>"
|
||||
end
|
||||
end
|
||||
|
||||
html += "<li>#{content}</li>"
|
||||
end
|
||||
|
||||
while layout.any?
|
||||
elist = layout.pop
|
||||
html += "</#{elist}>"
|
||||
end
|
||||
|
||||
html
|
||||
end
|
||||
|
||||
def parse(str, options = {})
|
||||
return "" if str.blank?
|
||||
|
||||
# Make sure quote tags are surrounded by newlines
|
||||
|
||||
unless options[:inline]
|
||||
str.gsub!(/\s*\[quote\]\s*/m, "\n\n[quote]\n\n")
|
||||
str.gsub!(/\s*\[\/quote\]\s*/m, "\n\n[/quote]\n\n")
|
||||
end
|
||||
|
||||
str.gsub!(/(?:\r?\n){3,}/, "\n\n")
|
||||
str.strip!
|
||||
blocks = str.split(/(?:\r?\n){2}/)
|
||||
|
||||
html = blocks.map do |block|
|
||||
case block
|
||||
when /^(h[1-6])\.\s*(.+)$/
|
||||
tag = $1
|
||||
content = $2
|
||||
|
||||
if options[:inline]
|
||||
"<h6>" + parse_inline(content, options) + "</h6>"
|
||||
else
|
||||
"<#{tag}>" + parse_inline(content, options) + "</#{tag}>"
|
||||
end
|
||||
|
||||
when /^\s*\*+ /
|
||||
parse_list(block, options)
|
||||
|
||||
when "[quote]"
|
||||
if options[:inline]
|
||||
""
|
||||
else
|
||||
'<blockquote>'
|
||||
end
|
||||
|
||||
when "[/quote]"
|
||||
if options[:inline]
|
||||
""
|
||||
else
|
||||
'</blockquote>'
|
||||
end
|
||||
|
||||
else
|
||||
'<p>' + parse_inline(block) + "</p>"
|
||||
end
|
||||
end
|
||||
|
||||
html.join("")
|
||||
end
|
||||
|
||||
module_function :parse_inline
|
||||
module_function :parse_list
|
||||
module_function :parse
|
||||
end
|
||||
|
8
test/factories/dmail.rb
Normal file
8
test/factories/dmail.rb
Normal file
@ -0,0 +1,8 @@
|
||||
Factory.define(:dmail) do |f|
|
||||
f.owner {|x| x.association(:user)}
|
||||
f.from_id {|x| x.owner_id}
|
||||
f.to {|x| x.association(:user)}
|
||||
f.title {Faker::Lorem.words}
|
||||
f.body {Faker::Lorem.sentences}
|
||||
f.is_read false
|
||||
end
|
11
test/fixtures/dmails.yml
vendored
Normal file
11
test/fixtures/dmails.yml
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
|
||||
|
||||
# This model initially had no columns defined. If you add columns to the
|
||||
# model remove the '{}' from the fixture names and add the columns immediately
|
||||
# below each fixture, per the syntax in the comments below
|
||||
#
|
||||
one: {}
|
||||
# column: value
|
||||
#
|
||||
two: {}
|
||||
# column: value
|
75
test/unit/dmail_test.rb
Normal file
75
test/unit/dmail_test.rb
Normal file
@ -0,0 +1,75 @@
|
||||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
|
||||
class DmailTest < ActiveSupport::TestCase
|
||||
context "A dmail" do
|
||||
setup do
|
||||
MEMCACHE.flush_all
|
||||
ActionMailer::Base.delivery_method = :test
|
||||
ActionMailer::Base.perform_deliveries = true
|
||||
ActionMailer::Base.deliveries = []
|
||||
end
|
||||
|
||||
context "search" do
|
||||
should "return results based on title contents" do
|
||||
dmail = Factory.create(:dmail, :title => "xxx")
|
||||
matches = Dmail.search_message("xxx")
|
||||
assert(matches.any?)
|
||||
matches = Dmail.search_message("aaa")
|
||||
assert(matches.empty?)
|
||||
end
|
||||
|
||||
should "return results based on body contents" do
|
||||
dmail = Factory.create(:dmail, :body => "xxx")
|
||||
matches = Dmail.search_message("xxx")
|
||||
assert(matches.any?)
|
||||
matches = Dmail.search_message("aaa")
|
||||
assert(matches.empty?)
|
||||
end
|
||||
end
|
||||
|
||||
should "should parse user names" do
|
||||
user = Factory.create(:user)
|
||||
dmail = Factory.build(:dmail)
|
||||
dmail.to_id = nil
|
||||
dmail.to_name = user.name
|
||||
assert(dmail.to_id == user.id)
|
||||
end
|
||||
|
||||
should "construct a response" do
|
||||
dmail = Factory.create(:dmail)
|
||||
response = dmail.build_response
|
||||
assert_equal("Re: #{dmail.title}", response.title)
|
||||
assert_equal(dmail.id, response.parent_id)
|
||||
assert_equal(dmail.from_id, response.to_id)
|
||||
assert_equal(dmail.to_id, response.from_id)
|
||||
end
|
||||
|
||||
should "create a copy for each user" do
|
||||
dmail = Factory.build(:dmail)
|
||||
assert_difference("Dmail.count", 2) do
|
||||
Dmail.create_new(dmail)
|
||||
end
|
||||
end
|
||||
|
||||
should "send an email if the user wants it" do
|
||||
user = Factory.create(:user, :receive_email_notifications => true)
|
||||
assert_difference("ActionMailer::Base.deliveries.size", 1) do
|
||||
Factory.create(:dmail, :to => user)
|
||||
end
|
||||
end
|
||||
|
||||
should "be marked as read after the user reads it" do
|
||||
dmail = Factory.create(:dmail)
|
||||
assert(!dmail.is_read?)
|
||||
dmail.mark_as_read!
|
||||
assert(dmail.is_read?)
|
||||
end
|
||||
|
||||
should "notify the recipient he has mail" do
|
||||
dmail = Factory.create(:dmail)
|
||||
assert(dmail.to(true).has_mail?)
|
||||
dmail.mark_as_read!
|
||||
assert(!dmail.to(true).has_mail?)
|
||||
end
|
||||
end
|
||||
end
|
BIN
tmp/test-large.jpg
Normal file
BIN
tmp/test-large.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 89 KiB |
BIN
tmp/test.jpg
Normal file
BIN
tmp/test.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 27 KiB |
Loading…
Reference in New Issue
Block a user