This commit is contained in:
r888888888
2014-07-17 15:53:36 -07:00
parent a6d69e63be
commit bb402f5a27
11 changed files with 134 additions and 178 deletions

View File

@@ -74,8 +74,34 @@ class ForumTopic < ActiveRecord::Base
end
end
module VisitMethods
def read_by?(user = nil)
user ||= CurrentUser.user
if user.last_forum_read_at && updated_at <= user.last_forum_read_at
return true
end
ForumTopicVisit.where("user_id = ? and forum_topic_id = ? and last_read_at >= ?", user.id, id, updated_at).exists?
end
def mark_as_read!(user = nil)
user ||= CurrentUser.user
match = ForumTopicVisit.where(:user_id => user.id, :forum_topic_id => id).first
if match
match.update_attribute(:last_read_at, updated_at)
else
ForumTopicVisit.create(:user_id => user.id, :forum_topic_id => id, :last_read_at => updated_at)
end
# user.update_attribute(:last';¬≥÷_forum_read_at, ForumTopicVisit.where(:user_id => user.id, :forum_topic_id => id).minimum(:last_read_at) || updated_at)
end
end
extend SearchMethods
include CategoryMethods
include VisitMethods
def editable_by?(user)
creator_id == user.id || user.is_janitor?
@@ -94,7 +120,7 @@ class ForumTopic < ActiveRecord::Base
end
def last_page
(posts.count / Danbooru.config.posts_per_page.to_f).ceil
(response_count / Danbooru.config.posts_per_page.to_f).ceil
end
def presenter(forum_posts)
@@ -105,42 +131,6 @@ class ForumTopic < ActiveRecord::Base
super + [:text_index]
end
def check!(user)
ForumTopicVisit.check!(user, self)
end
def mark_as_read(read_forum_topic_ids)
hash = read_forum_topic_ids.inject({}) do |hash, x|
hash[x[0].to_s] = x[1].to_s
hash
end
hash[id.to_s] = updated_at.to_i.to_s
result = hash.to_a.flatten.join(" ")
while result.size > 500
ids = result.scan(/\S+/)
result = ids[(ids.size / 2)..-1].join(" ")
end
update_last_forum_read_at(hash.keys)
result
end
def update_last_forum_read_at(read_forum_topic_ids)
query = ForumTopic.where("true")
if CurrentUser.user.last_forum_read_at.present?
query = query.where("updated_at >= ?", CurrentUser.last_forum_read_at)
end
if read_forum_topic_ids.any?
query = query.where("id not in (?)", read_forum_topic_ids)
end
query = query.order("updated_at asc")
topic = query.first
if topic
CurrentUser.user.update_attribute(:last_forum_read_at, topic.updated_at)
else
CurrentUser.user.update_attribute(:last_forum_read_at, Time.now)
end
end
def merge(topic)
ForumPost.where(:id => topic.posts.map(&:id)).update_all(:topic_id => id)
update_attribute(:is_deleted, true)

View File

@@ -1,30 +1,9 @@
class ForumTopicVisit < ActiveRecord::Base
def self.check!(user, topic)
match = where(:user_id => user.id, :forum_topic_id => topic.id).first
result = false
if match
if match.last_read_at < topic.updated_at
result = true
end
match.update_attribute(:last_read_at, topic.updated_at)
else
create(:user_id => user.id, :forum_topic_id => topic.id, :last_read_at => topic.updated_at)
end
result
end
belongs_to :user
belongs_to :forum_topic
attr_accessible :user_id, :user, :forum_topic_id, :forum_topic, :last_read_at
def self.check_list!(user, topics)
matches = where(:user_id => user.id, :forum_topic_id => topics.map(&:id)).to_a.inject({}) do |hash, x|
hash[x.forum_topic_id] = x
hash
end
topics.each do |topic|
if matches[topic.id]
matches[topic.id].update_attribute(:last_read_at, topic.updated_at)
else
create(:user_id => user.id,, :forum_topic_id => topic.id, :last_read_at => topic.updated_at)
end
end
matches
def self.prune!(user)
where("last_read_at < ?", user.last_forum_read_at).delete_all
end
end

View File

@@ -428,10 +428,10 @@ class User < ActiveRecord::Base
module ForumMethods
def has_forum_been_updated?
return false unless is_gold?
newest_topic = ForumTopic.order("updated_at desc").first
return false if newest_topic.nil?
max_updated_at = ForumTopic.maximum(:updated_at)
return false if max_updated_at.nil?
return true if last_forum_read_at.nil?
return newest_topic.updated_at > last_forum_read_at
return max_updated_at > last_forum_read_at
end
end