This commit is contained in:
r888888888 2014-07-08 10:20:01 -07:00
parent 3cac0458df
commit a6d69e63be
6 changed files with 114 additions and 10 deletions

View File

@ -0,0 +1,2 @@
module ForumTopicVisitsHelper
end

View File

@ -105,16 +105,8 @@ class ForumTopic < ActiveRecord::Base
super + [:text_index]
end
def read_by?(user, read_forum_topic_ids)
if read_forum_topic_ids.any? {|topic_id, timestamp| id.to_s == topic_id && updated_at.to_i > timestamp.to_i}
return false
end
if read_forum_topic_ids.any? {|topic_id, timestamp| id.to_s == topic_id && updated_at.to_i <= timestamp.to_i}
return true
end
return false if user.last_forum_read_at.nil?
return true if updated_at < user.last_forum_read_at
return false
def check!(user)
ForumTopicVisit.check!(user, self)
end
def mark_as_read(read_forum_topic_ids)

View File

@ -0,0 +1,30 @@
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
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
end
end

View File

@ -124,6 +124,7 @@ Rails.application.routes.draw do
collection do
post :mark_all_as_read
end
resource :visit, :controller => "forum_topic_visits"
end
resources :ip_bans
resources :iqdb_queries, :only => [:create]

View File

@ -0,0 +1,14 @@
class CreateForumTopicVisits < ActiveRecord::Migration
def change
create_table :forum_topic_visits do |t|
t.integer :user_id
t.integer :forum_topic_id
t.timestamp :last_read_at
t.timestamps
end
add_index :forum_topic_visits, :user_id
add_index :forum_topic_visits, :forum_topic_id
end
end

View File

@ -3,6 +3,7 @@
--
SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
@ -1979,6 +1980,39 @@ CREATE SEQUENCE forum_posts_id_seq
ALTER SEQUENCE forum_posts_id_seq OWNED BY forum_posts.id;
--
-- Name: forum_topic_visits; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE forum_topic_visits (
id integer NOT NULL,
user_id integer,
forum_topic_id integer,
last_read_at timestamp without time zone,
created_at timestamp without time zone,
updated_at timestamp without time zone
);
--
-- Name: forum_topic_visits_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE forum_topic_visits_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: forum_topic_visits_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE forum_topic_visits_id_seq OWNED BY forum_topic_visits.id;
--
-- Name: forum_topics; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
@ -3881,6 +3915,13 @@ ALTER TABLE ONLY favorites_99 ALTER COLUMN id SET DEFAULT nextval('favorites_id_
ALTER TABLE ONLY forum_posts ALTER COLUMN id SET DEFAULT nextval('forum_posts_id_seq'::regclass);
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY forum_topic_visits ALTER COLUMN id SET DEFAULT nextval('forum_topic_visits_id_seq'::regclass);
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
@ -4212,6 +4253,14 @@ ALTER TABLE ONLY forum_posts
ADD CONSTRAINT forum_posts_pkey PRIMARY KEY (id);
--
-- Name: forum_topic_visits_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
ALTER TABLE ONLY forum_topic_visits
ADD CONSTRAINT forum_topic_visits_pkey PRIMARY KEY (id);
--
-- Name: forum_topics_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
@ -6075,6 +6124,20 @@ CREATE INDEX index_forum_posts_on_text_index ON forum_posts USING gin (text_inde
CREATE INDEX index_forum_posts_on_topic_id ON forum_posts USING btree (topic_id);
--
-- Name: index_forum_topic_visits_on_forum_topic_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_forum_topic_visits_on_forum_topic_id ON forum_topic_visits USING btree (forum_topic_id);
--
-- Name: index_forum_topic_visits_on_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_forum_topic_visits_on_user_id ON forum_topic_visits USING btree (user_id);
--
-- Name: index_forum_topics_on_creator_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -6864,3 +6927,5 @@ INSERT INTO schema_migrations (version) VALUES ('20140604002414');
INSERT INTO schema_migrations (version) VALUES ('20140613004559');
INSERT INTO schema_migrations (version) VALUES ('20140701224800');