2018-06-19 02:13:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-16 15:14:33 -04:00
|
|
|
class DirectoryItem < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
has_one :user_stat, foreign_key: :user_id, primary_key: :user_id
|
|
|
|
|
|
|
|
def self.headings
|
|
|
|
@headings ||= [:likes_received,
|
|
|
|
:likes_given,
|
|
|
|
:topics_entered,
|
|
|
|
:topic_count,
|
2015-03-24 15:31:07 -04:00
|
|
|
:post_count,
|
|
|
|
:posts_read,
|
|
|
|
:days_visited]
|
2015-03-16 15:14:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.period_types
|
2016-01-24 00:39:01 -05:00
|
|
|
@types ||= Enum.new(all: 1,
|
|
|
|
yearly: 2,
|
|
|
|
monthly: 3,
|
|
|
|
weekly: 4,
|
|
|
|
daily: 5,
|
|
|
|
quarterly: 6)
|
2015-03-16 15:14:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.refresh!
|
2015-04-20 12:06:52 -04:00
|
|
|
period_types.each_key { |p| refresh_period!(p) }
|
2015-03-16 15:14:33 -04:00
|
|
|
end
|
|
|
|
|
2020-03-30 14:27:49 -04:00
|
|
|
def self.last_updated_at(period_type)
|
|
|
|
val = Discourse.redis.get("directory_#{period_type}")
|
|
|
|
return nil if val.nil?
|
|
|
|
|
|
|
|
Time.zone.at(val.to_i)
|
|
|
|
end
|
|
|
|
|
2017-11-10 12:30:20 -05:00
|
|
|
def self.refresh_period!(period_type, force: false)
|
2015-03-26 11:26:19 -04:00
|
|
|
|
2020-03-30 14:27:49 -04:00
|
|
|
Discourse.redis.set("directory_#{period_types[period_type]}", Time.zone.now.to_i)
|
|
|
|
|
2015-03-26 11:26:19 -04:00
|
|
|
# Don't calculate it if the user directory is disabled
|
2017-11-10 12:30:20 -05:00
|
|
|
return unless SiteSetting.enable_user_directory? || force
|
2015-03-26 11:26:19 -04:00
|
|
|
|
2015-03-16 15:14:33 -04:00
|
|
|
since =
|
|
|
|
case period_type
|
|
|
|
when :daily then 1.day.ago
|
|
|
|
when :weekly then 1.week.ago
|
|
|
|
when :monthly then 1.month.ago
|
2015-12-07 08:04:34 -05:00
|
|
|
when :quarterly then 3.months.ago
|
2015-03-16 15:14:33 -04:00
|
|
|
when :yearly then 1.year.ago
|
|
|
|
else 1000.years.ago
|
|
|
|
end
|
|
|
|
|
2015-04-20 12:06:52 -04:00
|
|
|
ActiveRecord::Base.transaction do
|
2017-08-09 15:57:35 -04:00
|
|
|
# Delete records that belonged to users who have been deleted
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec "DELETE FROM directory_items
|
2016-03-02 02:23:29 -05:00
|
|
|
USING directory_items di
|
2017-12-18 20:53:59 -05:00
|
|
|
LEFT JOIN users u ON (u.id = user_id AND u.active AND u.silenced_till IS NULL AND u.id > 0)
|
2016-03-02 02:23:29 -05:00
|
|
|
WHERE di.id = directory_items.id AND
|
|
|
|
u.id IS NULL AND
|
|
|
|
di.period_type = :period_type", period_type: period_types[period_type]
|
|
|
|
|
2017-08-09 15:57:35 -04:00
|
|
|
# Create new records for users who don't have one yet
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec "INSERT INTO directory_items(period_type, user_id, likes_received, likes_given, topics_entered, days_visited, posts_read, topic_count, post_count)
|
2016-03-02 02:23:29 -05:00
|
|
|
SELECT
|
2015-04-20 12:06:52 -04:00
|
|
|
:period_type,
|
|
|
|
u.id,
|
2016-03-02 02:23:29 -05:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0
|
|
|
|
FROM users u
|
|
|
|
LEFT JOIN directory_items di ON di.user_id = u.id AND di.period_type = :period_type
|
2019-09-11 15:18:17 -04:00
|
|
|
WHERE di.id IS NULL AND u.id > 0 AND u.silenced_till IS NULL AND u.active
|
|
|
|
#{SiteSetting.must_approve_users ? 'AND u.approved' : ''}
|
2016-03-02 02:23:29 -05:00
|
|
|
", period_type: period_types[period_type]
|
|
|
|
|
2017-08-09 15:57:35 -04:00
|
|
|
# Calculate new values and update records
|
2017-11-20 00:43:53 -05:00
|
|
|
#
|
|
|
|
#
|
|
|
|
# TODO
|
|
|
|
# WARNING: post_count is a wrong name, it should be reply_count (excluding topic post)
|
|
|
|
#
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec "WITH x AS (SELECT
|
2016-03-02 02:23:29 -05:00
|
|
|
u.id user_id,
|
2017-11-20 00:43:53 -05:00
|
|
|
SUM(CASE WHEN p.id IS NOT NULL AND t.id IS NOT NULL AND ua.action_type = :was_liked_type THEN 1 ELSE 0 END) likes_received,
|
|
|
|
SUM(CASE WHEN p.id IS NOT NULL AND t.id IS NOT NULL AND ua.action_type = :like_type THEN 1 ELSE 0 END) likes_given,
|
2018-11-12 09:30:05 -05:00
|
|
|
COALESCE((SELECT COUNT(topic_id) FROM topic_views AS v WHERE v.user_id = u.id AND v.viewed_at > :since), 0) topics_entered,
|
|
|
|
COALESCE((SELECT COUNT(id) FROM user_visits AS uv WHERE uv.user_id = u.id AND uv.visited_at > :since), 0) days_visited,
|
|
|
|
COALESCE((SELECT SUM(posts_read) FROM user_visits AS uv2 WHERE uv2.user_id = u.id AND uv2.visited_at > :since), 0) posts_read,
|
2017-11-20 00:43:53 -05:00
|
|
|
SUM(CASE WHEN t2.id IS NOT NULL AND ua.action_type = :new_topic_type THEN 1 ELSE 0 END) topic_count,
|
|
|
|
SUM(CASE WHEN p.id IS NOT NULL AND t.id IS NOT NULL AND ua.action_type = :reply_type THEN 1 ELSE 0 END) post_count
|
2015-04-20 12:06:52 -04:00
|
|
|
FROM users AS u
|
2018-11-12 09:30:05 -05:00
|
|
|
LEFT OUTER JOIN user_actions AS ua ON ua.user_id = u.id AND COALESCE(ua.created_at, :since) > :since
|
2017-11-20 00:43:53 -05:00
|
|
|
LEFT OUTER JOIN posts AS p ON ua.target_post_id = p.id AND p.deleted_at IS NULL AND p.post_type = :regular_post_type AND NOT p.hidden
|
|
|
|
LEFT OUTER JOIN topics AS t ON p.topic_id = t.id AND t.archetype = 'regular' AND t.deleted_at IS NULL AND t.visible
|
|
|
|
LEFT OUTER JOIN topics AS t2 ON t2.id = ua.target_topic_id AND t2.archetype = 'regular' AND t2.deleted_at IS NULL AND t2.visible
|
2015-04-20 12:06:52 -04:00
|
|
|
LEFT OUTER JOIN categories AS c ON t.category_id = c.id
|
|
|
|
WHERE u.active
|
2017-11-13 13:41:36 -05:00
|
|
|
AND u.silenced_till IS NULL
|
2015-04-20 12:06:52 -04:00
|
|
|
AND u.id > 0
|
2016-03-02 02:23:29 -05:00
|
|
|
GROUP BY u.id)
|
|
|
|
UPDATE directory_items di SET
|
|
|
|
likes_received = x.likes_received,
|
|
|
|
likes_given = x.likes_given,
|
2016-03-09 15:44:16 -05:00
|
|
|
topics_entered = x.topics_entered,
|
2016-03-02 02:23:29 -05:00
|
|
|
days_visited = x.days_visited,
|
|
|
|
posts_read = x.posts_read,
|
|
|
|
topic_count = x.topic_count,
|
|
|
|
post_count = x.post_count
|
|
|
|
FROM x
|
|
|
|
WHERE
|
|
|
|
x.user_id = di.user_id AND
|
|
|
|
di.period_type = :period_type AND (
|
|
|
|
di.likes_received <> x.likes_received OR
|
|
|
|
di.likes_given <> x.likes_given OR
|
2016-03-09 15:44:16 -05:00
|
|
|
di.topics_entered <> x.topics_entered OR
|
2016-03-02 02:23:29 -05:00
|
|
|
di.days_visited <> x.days_visited OR
|
|
|
|
di.posts_read <> x.posts_read OR
|
|
|
|
di.topic_count <> x.topic_count OR
|
|
|
|
di.post_count <> x.post_count )
|
|
|
|
|
|
|
|
",
|
2015-04-20 12:06:52 -04:00
|
|
|
period_type: period_types[period_type],
|
|
|
|
since: since,
|
|
|
|
like_type: UserAction::LIKE,
|
|
|
|
was_liked_type: UserAction::WAS_LIKED,
|
|
|
|
new_topic_type: UserAction::NEW_TOPIC,
|
|
|
|
reply_type: UserAction::REPLY,
|
2015-07-24 16:39:03 -04:00
|
|
|
regular_post_type: Post.types[:regular]
|
2016-01-24 00:39:01 -05:00
|
|
|
|
|
|
|
if period_type == :all
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec <<~SQL
|
|
|
|
UPDATE user_stats s
|
|
|
|
SET likes_given = d.likes_given,
|
|
|
|
likes_received = d.likes_received,
|
|
|
|
topic_count = d.topic_count,
|
|
|
|
post_count = d.post_count
|
2016-01-24 00:39:01 -05:00
|
|
|
|
2018-06-19 02:13:14 -04:00
|
|
|
FROM directory_items d
|
|
|
|
WHERE s.user_id = d.user_id AND
|
|
|
|
d.period_type = 1 AND
|
|
|
|
( s.likes_given <> d.likes_given OR
|
|
|
|
s.likes_received <> d.likes_received OR
|
|
|
|
s.topic_count <> d.topic_count OR
|
|
|
|
s.post_count <> d.post_count
|
|
|
|
)
|
|
|
|
SQL
|
2016-01-24 00:39:01 -05:00
|
|
|
end
|
2015-04-20 12:06:52 -04:00
|
|
|
end
|
2015-03-16 15:14:33 -04:00
|
|
|
end
|
|
|
|
end
|
2015-09-17 20:41:10 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: directory_items
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# period_type :integer not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# likes_received :integer not null
|
|
|
|
# likes_given :integer not null
|
|
|
|
# topics_entered :integer not null
|
|
|
|
# topic_count :integer not null
|
|
|
|
# post_count :integer not null
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# days_visited :integer default(0), not null
|
|
|
|
# posts_read :integer default(0), not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2016-10-31 05:32:11 -04:00
|
|
|
# index_directory_items_on_days_visited (days_visited)
|
|
|
|
# index_directory_items_on_likes_given (likes_given)
|
|
|
|
# index_directory_items_on_likes_received (likes_received)
|
2016-03-28 00:21:45 -04:00
|
|
|
# index_directory_items_on_period_type_and_user_id (period_type,user_id) UNIQUE
|
2016-10-31 05:32:11 -04:00
|
|
|
# index_directory_items_on_post_count (post_count)
|
|
|
|
# index_directory_items_on_posts_read (posts_read)
|
|
|
|
# index_directory_items_on_topic_count (topic_count)
|
|
|
|
# index_directory_items_on_topics_entered (topics_entered)
|
2015-09-17 20:41:10 -04:00
|
|
|
#
|