fixes #2191
This commit is contained in:
@@ -11,5 +11,6 @@ class DailyMaintenance
|
||||
UserUploadClamper.new.clamp_all!
|
||||
TagSubscription.process_all
|
||||
ApiCacheGenerator.new.generate_tag_cache
|
||||
ForumSubscription.process_all!
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
class UserMailer < ActionMailer::Base
|
||||
add_template_helper ApplicationHelper
|
||||
default :from => Danbooru.config.contact_email, :content_type => "text/html"
|
||||
|
||||
def dmail_notice(dmail)
|
||||
@@ -13,4 +14,10 @@ class UserMailer < ActionMailer::Base
|
||||
def upgrade_fail(email)
|
||||
mail(:to => "#{user.name} <#{email}>", :subject => "#{Danbooru.config.app_name} account upgrade")
|
||||
end
|
||||
|
||||
def forum_notice(user, forum_topic, forum_posts)
|
||||
@forum_topic = forum_topic
|
||||
@forum_posts = forum_posts
|
||||
mail(:to => "#{user.name} <#{user.email}>", :subject => "#{Danbooru.config.app_name} forum topic #{forum_topic.title} updated")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class ForumPost < ActiveRecord::Base
|
||||
attr_accessible :body, :topic_id, :as => [:member, :builder, :janitor, :gold, :platinum, :contributor, :admin, :moderator, :default]
|
||||
attr_accessible :body, :topic_id, :receive_email_notifications, :as => [:member, :builder, :janitor, :gold, :platinum, :contributor, :admin, :moderator, :default]
|
||||
attr_accessible :is_locked, :is_sticky, :is_deleted, :as => [:admin, :moderator, :janitor]
|
||||
attr_readonly :topic_id
|
||||
belongs_to :creator, :class_name => "User"
|
||||
@@ -15,6 +15,8 @@ class ForumPost < ActiveRecord::Base
|
||||
validate :validate_topic_is_unlocked
|
||||
before_destroy :validate_topic_is_unlocked
|
||||
after_save :delete_topic_if_original_post
|
||||
after_save :update_email_notifications
|
||||
attr_accessor :receive_email_notifications
|
||||
|
||||
module SearchMethods
|
||||
def body_matches(body)
|
||||
@@ -188,4 +190,22 @@ class ForumPost < ActiveRecord::Base
|
||||
def hidden_attributes
|
||||
super + [:text_index]
|
||||
end
|
||||
|
||||
def receive_email_notifications
|
||||
@receive_email_notifications ||= ForumSubscription.where(:forum_topic_id => topic_id, :user_id => CurrentUser.user.id).exists?
|
||||
end
|
||||
|
||||
def update_email_notifications
|
||||
subscription = ForumSubscription.where(:forum_topic_id => topic_id, :user_id => CurrentUser.user.id).first
|
||||
|
||||
if receive_email_notifications == "1"
|
||||
if subscription
|
||||
subscription.update_attribute(:last_read_at, updated_at)
|
||||
else
|
||||
ForumSubscription.create(:forum_topic_id => topic_id, :user_id => CurrentUser.user.id, :last_read_at => updated_at)
|
||||
end
|
||||
else
|
||||
subscription.destroy if subscription
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
22
app/models/forum_subscription.rb
Normal file
22
app/models/forum_subscription.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
class ForumSubscription < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
belongs_to :forum_topic
|
||||
attr_accessible :user, :forum_topic, :user_id, :forum_topic_id, :last_read_at, :delete_key
|
||||
|
||||
def self.prune!
|
||||
where("last_read_at < ?", 3.months.ago).delete_all
|
||||
end
|
||||
|
||||
def self.process_all!
|
||||
ForumSubscription.find_each do |subscription|
|
||||
forum_topic = subscription.forum_topic
|
||||
if forum_topic.updated_at > subscription.last_read_at
|
||||
CurrentUser.scoped(subscription.user, "127.0.0.1") do
|
||||
forum_posts = forum_topic.posts.where("created_at >= ?", subscription.last_read_at).order("id desc")
|
||||
UserMailer.forum_notice(subscription.user, forum_topic, forum_posts).deliver
|
||||
subscription.update_attribute(:last_read_at, forum_topic.updated_at)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -11,6 +11,7 @@ class ForumTopic < ActiveRecord::Base
|
||||
belongs_to :updater, :class_name => "User"
|
||||
has_many :posts, lambda {order("forum_posts.id asc")}, :class_name => "ForumPost", :foreign_key => "topic_id", :dependent => :destroy
|
||||
has_one :original_post, lambda {order("forum_posts.id asc")}, :class_name => "ForumPost", :foreign_key => "topic_id"
|
||||
has_many :subscriptions, :class_name => "ForumSubscription"
|
||||
before_validation :initialize_creator, :on => :create
|
||||
before_validation :initialize_updater
|
||||
before_validation :initialize_is_deleted, :on => :create
|
||||
@@ -99,6 +100,24 @@ class ForumTopic < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
module SubscriptionMethods
|
||||
def update_subscription!(user)
|
||||
user_subscription = subscriptions.where(:user_id => user.id).first
|
||||
|
||||
if user_subscription
|
||||
user_subscription.update_attribute(:last_read_at, updated_at)
|
||||
else
|
||||
subscriptions.create(:user_id => user.id, :last_read_at => updated_at, :delete_key => SecureRandom.urlsafe_base64(10))
|
||||
end
|
||||
end
|
||||
|
||||
def notify_subscriptions!
|
||||
ForumSubscription.where(:forum_topic_id => id).where("last_read_at < ?", updated_at).find_each do |subscription|
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
include CategoryMethods
|
||||
include VisitMethods
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
<%= f.input :topic_id, :as => :hidden %>
|
||||
<%= dtext_field "forum_post", "body" %>
|
||||
|
||||
<%= f.input :receive_email_notifications, :as => :boolean %>
|
||||
|
||||
<%= f.button :submit, "Submit" %>
|
||||
<%= dtext_preview_button "forum_post", "body" %>
|
||||
<% end %>
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<%= stylesheet_link_tag "application", :media => "screen" %>
|
||||
</head>
|
||||
<body>
|
||||
<p><%= h @dmail.from.name %> said:</p>
|
||||
|
||||
|
||||
21
app/views/user_mailer/forum_notice.html.erb
Normal file
21
app/views/user_mailer/forum_notice.html.erb
Normal file
@@ -0,0 +1,21 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<%= stylesheet_link_tag "application", :media => "screen" %>
|
||||
</head>
|
||||
<body>
|
||||
<p><%= @forum_topic.title %> was updated:</p>
|
||||
|
||||
<% @forum_posts.each do |forum_post| %>
|
||||
<div class="prose">
|
||||
<p class="author">
|
||||
<%= forum_post.creator_name %> said:
|
||||
</p>
|
||||
<%= format_text(forum_post.body) %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<p><%= link_to "View topic", forum_topic_path(@forum_topic, :page => @forum_topic.last_page, :host => Danbooru.config.hostname, :only_path => false) %></p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user