2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class UserAction < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
2024-03-28 02:13:18 -04:00
|
|
|
belongs_to :acting_user, class_name: "User"
|
|
|
|
belongs_to :target_user, class_name: "User"
|
2013-03-23 11:02:59 -04:00
|
|
|
belongs_to :target_post, class_name: "Post"
|
|
|
|
belongs_to :target_topic, class_name: "Topic"
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
validates_presence_of :action_type
|
|
|
|
validates_presence_of :user_id
|
|
|
|
|
|
|
|
LIKE = 1
|
2013-02-07 10:45:24 -05:00
|
|
|
WAS_LIKED = 2
|
2013-02-05 14:16:51 -05:00
|
|
|
NEW_TOPIC = 4
|
2013-04-30 20:52:31 -04:00
|
|
|
REPLY = 5
|
2013-02-05 14:16:51 -05:00
|
|
|
RESPONSE = 6
|
|
|
|
MENTION = 7
|
|
|
|
QUOTE = 9
|
|
|
|
EDIT = 11
|
|
|
|
NEW_PRIVATE_MESSAGE = 12
|
|
|
|
GOT_PRIVATE_MESSAGE = 13
|
2016-11-29 00:17:14 -05:00
|
|
|
SOLVED = 15
|
2017-02-08 12:09:08 -05:00
|
|
|
ASSIGNED = 16
|
2024-04-11 17:54:28 -04:00
|
|
|
LINKED = 17
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
ORDER =
|
|
|
|
Hash[
|
|
|
|
*[
|
|
|
|
GOT_PRIVATE_MESSAGE,
|
2013-05-17 14:11:33 -04:00
|
|
|
NEW_PRIVATE_MESSAGE,
|
2013-02-05 14:16:51 -05:00
|
|
|
NEW_TOPIC,
|
2013-04-30 20:52:31 -04:00
|
|
|
REPLY,
|
2013-02-05 14:16:51 -05:00
|
|
|
RESPONSE,
|
|
|
|
LIKE,
|
|
|
|
WAS_LIKED,
|
|
|
|
MENTION,
|
|
|
|
QUOTE,
|
2016-11-29 00:17:14 -05:00
|
|
|
EDIT,
|
|
|
|
SOLVED,
|
2017-02-08 12:09:08 -05:00
|
|
|
ASSIGNED,
|
2024-04-11 17:54:28 -04:00
|
|
|
LINKED,
|
2013-02-05 14:16:51 -05:00
|
|
|
].each_with_index.to_a.flatten
|
|
|
|
]
|
|
|
|
|
2022-05-11 16:15:53 -04:00
|
|
|
USER_ACTED_TYPES = [LIKE, NEW_TOPIC, REPLY, NEW_PRIVATE_MESSAGE]
|
|
|
|
|
2020-08-28 14:49:19 -04:00
|
|
|
def self.types
|
|
|
|
@types ||=
|
|
|
|
Enum.new(
|
|
|
|
like: 1,
|
|
|
|
was_liked: 2,
|
2022-05-09 20:42:18 -04:00
|
|
|
# NOTE: Previously type 3 was bookmark but this was removed when we
|
|
|
|
# changed to using the Bookmark model.
|
2020-08-28 14:49:19 -04:00
|
|
|
new_topic: 4,
|
|
|
|
reply: 5,
|
|
|
|
response: 6,
|
|
|
|
mention: 7,
|
|
|
|
quote: 9,
|
|
|
|
edit: 11,
|
|
|
|
new_private_message: 12,
|
|
|
|
got_private_message: 13,
|
|
|
|
solved: 15,
|
|
|
|
assigned: 16,
|
2024-04-11 17:54:28 -04:00
|
|
|
linked: 17,
|
2020-08-28 14:49:19 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-29 08:24:28 -04:00
|
|
|
def self.private_types
|
|
|
|
@private_types ||= [WAS_LIKED, RESPONSE, MENTION, QUOTE, EDIT]
|
|
|
|
end
|
|
|
|
|
2014-04-29 12:59:14 -04:00
|
|
|
def self.last_action_in_topic(user_id, topic_id)
|
|
|
|
UserAction
|
2019-10-21 06:32:27 -04:00
|
|
|
.where(user_id: user_id, target_topic_id: topic_id, action_type: [RESPONSE, MENTION, QUOTE])
|
|
|
|
.order("created_at DESC")
|
2023-02-12 23:39:45 -05:00
|
|
|
.pick(:target_post_id)
|
2014-04-29 12:59:14 -04:00
|
|
|
end
|
2013-04-29 02:33:24 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.stats(user_id, guardian)
|
2013-04-29 02:33:24 -04:00
|
|
|
# Sam: I tried this in AR and it got complex
|
2018-06-20 03:48:02 -04:00
|
|
|
builder = DB.build <<~SQL
|
|
|
|
|
|
|
|
SELECT action_type, COUNT(*) count
|
|
|
|
FROM user_actions a
|
|
|
|
LEFT JOIN topics t ON t.id = a.target_topic_id
|
|
|
|
LEFT JOIN posts p on p.id = a.target_post_id
|
|
|
|
LEFT JOIN posts p2 on p2.topic_id = a.target_topic_id and p2.post_number = 1
|
|
|
|
LEFT JOIN categories c ON c.id = t.category_id
|
|
|
|
/*where*/
|
|
|
|
GROUP BY action_type
|
|
|
|
SQL
|
2013-04-29 02:33:24 -04:00
|
|
|
|
|
|
|
builder.where("a.user_id = :user_id", user_id: user_id)
|
2013-04-09 22:50:00 -04:00
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
apply_common_filters(builder, user_id, guardian)
|
2013-02-15 17:08:28 -05:00
|
|
|
|
2018-06-20 03:48:02 -04:00
|
|
|
results = builder.query
|
2013-02-28 13:54:12 -05:00
|
|
|
results.sort! { |a, b| ORDER[a.action_type] <=> ORDER[b.action_type] }
|
2013-02-05 14:16:51 -05:00
|
|
|
results
|
|
|
|
end
|
|
|
|
|
2014-05-02 16:36:52 -04:00
|
|
|
def self.private_messages_stats(user_id, guardian)
|
|
|
|
return unless guardian.can_see_private_messages?(user_id)
|
2015-09-10 03:07:20 -04:00
|
|
|
|
2015-12-07 12:37:03 -05:00
|
|
|
# list the stats for: all/mine/unread/groups (topic-based)
|
|
|
|
|
|
|
|
sql = <<-SQL
|
|
|
|
SELECT COUNT(*) "all"
|
|
|
|
, SUM(CASE WHEN t.user_id = :user_id THEN 1 ELSE 0 END) "mine"
|
|
|
|
, SUM(CASE WHEN tu.last_read_post_number IS NULL OR tu.last_read_post_number < t.highest_post_number THEN 1 ELSE 0 END) "unread"
|
|
|
|
FROM topics t
|
|
|
|
LEFT JOIN topic_users tu ON t.id = tu.topic_id AND tu.user_id = :user_id
|
|
|
|
WHERE t.deleted_at IS NULL
|
|
|
|
AND t.archetype = 'private_message'
|
|
|
|
AND t.id IN (SELECT topic_id FROM topic_allowed_users WHERE user_id = :user_id)
|
|
|
|
SQL
|
|
|
|
|
2018-06-19 02:13:14 -04:00
|
|
|
# map is there due to count returning nil
|
|
|
|
all, mine, unread = DB.query_single(sql, user_id: user_id).map(&:to_i)
|
2015-12-07 12:37:03 -05:00
|
|
|
|
|
|
|
sql = <<-SQL
|
2015-12-09 19:39:33 -05:00
|
|
|
SELECT g.name, COUNT(*) "count"
|
|
|
|
FROM topics t
|
|
|
|
JOIN topic_allowed_groups tg ON topic_id = t.id
|
|
|
|
JOIN group_users gu ON gu.user_id = :user_id AND gu.group_id = tg.group_id
|
|
|
|
JOIN groups g ON g.id = gu.group_id
|
2015-12-07 12:37:03 -05:00
|
|
|
WHERE deleted_at IS NULL
|
|
|
|
AND archetype = 'private_message'
|
2015-12-09 19:39:33 -05:00
|
|
|
GROUP BY g.name
|
2015-12-07 12:37:03 -05:00
|
|
|
SQL
|
2015-09-10 03:07:20 -04:00
|
|
|
|
2015-12-09 19:39:33 -05:00
|
|
|
result = { all: all, mine: mine, unread: unread }
|
|
|
|
|
2018-06-19 02:13:14 -04:00
|
|
|
DB
|
|
|
|
.query(sql, user_id: user_id)
|
|
|
|
.each { |row| (result[:groups] ||= []) << { name: row.name, count: row.count.to_i } }
|
2015-12-09 19:39:33 -05:00
|
|
|
|
|
|
|
result
|
2014-05-02 16:36:52 -04:00
|
|
|
end
|
|
|
|
|
2018-05-03 09:41:41 -04:00
|
|
|
def self.count_daily_engaged_users(start_date = nil, end_date = nil)
|
2022-05-11 16:15:53 -04:00
|
|
|
result = select(:user_id).distinct.where(action_type: USER_ACTED_TYPES)
|
2018-05-03 09:41:41 -04:00
|
|
|
|
|
|
|
if start_date && end_date
|
2018-05-10 23:30:21 -04:00
|
|
|
result = result.group("date(created_at)")
|
|
|
|
result = result.where("created_at > ? AND created_at < ?", start_date, end_date)
|
|
|
|
result = result.order("date(created_at)")
|
2018-05-03 09:41:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
result.count
|
2018-04-27 18:35:24 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.stream_item(action_id, guardian)
|
2013-02-28 13:54:12 -05:00
|
|
|
stream(action_id: action_id, guardian: guardian).first
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2018-06-20 03:48:02 -04:00
|
|
|
NULL_QUEUED_STREAM_COLS =
|
|
|
|
%i[
|
|
|
|
cooked
|
|
|
|
uploaded_avatar_id
|
|
|
|
acting_name
|
|
|
|
acting_username
|
|
|
|
acting_user_id
|
|
|
|
target_name
|
|
|
|
target_username
|
|
|
|
target_user_id
|
|
|
|
post_number
|
|
|
|
post_id
|
|
|
|
deleted
|
|
|
|
hidden
|
|
|
|
post_type
|
|
|
|
action_type
|
|
|
|
action_code
|
|
|
|
action_code_who
|
2021-11-07 22:32:17 -05:00
|
|
|
action_code_path
|
2018-06-20 03:48:02 -04:00
|
|
|
topic_closed
|
|
|
|
topic_id
|
|
|
|
topic_archived
|
|
|
|
].map! { |s| "NULL as #{s}" }.join(", ")
|
|
|
|
|
2015-04-21 14:36:46 -04:00
|
|
|
def self.stream(opts = nil)
|
|
|
|
opts ||= {}
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
action_types = opts[:action_types]
|
2015-04-21 14:36:46 -04:00
|
|
|
user_id = opts[:user_id]
|
|
|
|
action_id = opts[:action_id]
|
2013-02-05 14:16:51 -05:00
|
|
|
guardian = opts[:guardian]
|
|
|
|
ignore_private_messages = opts[:ignore_private_messages]
|
2015-04-21 14:36:46 -04:00
|
|
|
offset = opts[:offset] || 0
|
|
|
|
limit = opts[:limit] || 60
|
2019-01-15 21:40:16 -05:00
|
|
|
acting_username = opts[:acting_username]
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-12-14 16:52:54 -05:00
|
|
|
# Acting user columns. Can be extended by plugins to include custom avatar
|
|
|
|
# columns
|
|
|
|
acting_cols = ["u.id AS acting_user_id", "u.name AS acting_name"]
|
|
|
|
|
2020-07-17 05:48:08 -04:00
|
|
|
UserLookup.lookup_columns.each do |c|
|
2017-12-14 16:52:54 -05:00
|
|
|
next if c == :id || c["."]
|
|
|
|
acting_cols << "u.#{c} AS acting_#{c}"
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
# The weird thing is that target_post_id can be null, so it makes everything
|
|
|
|
# ever so more complex. Should we allow this, not sure.
|
2018-06-20 03:48:02 -04:00
|
|
|
builder = DB.build <<~SQL
|
2015-04-21 14:36:46 -04:00
|
|
|
SELECT
|
|
|
|
a.id,
|
|
|
|
t.title, a.action_type, a.created_at, t.id topic_id,
|
2015-05-11 12:51:16 -04:00
|
|
|
t.closed AS topic_closed, t.archived AS topic_archived,
|
2015-04-21 14:36:46 -04:00
|
|
|
a.user_id AS target_user_id, au.name AS target_name, au.username AS target_username,
|
|
|
|
coalesce(p.post_number, 1) post_number, p.id as post_id,
|
|
|
|
p.reply_to_post_number,
|
|
|
|
pu.username, pu.name, pu.id user_id,
|
|
|
|
pu.uploaded_avatar_id,
|
2017-12-14 16:52:54 -05:00
|
|
|
#{acting_cols.join(", ")},
|
2015-04-21 14:36:46 -04:00
|
|
|
coalesce(p.cooked, p2.cooked) cooked,
|
|
|
|
CASE WHEN coalesce(p.deleted_at, p2.deleted_at, t.deleted_at) IS NULL THEN false ELSE true END deleted,
|
|
|
|
p.hidden,
|
|
|
|
p.post_type,
|
2015-07-31 14:22:28 -04:00
|
|
|
p.action_code,
|
2018-05-17 23:28:13 -04:00
|
|
|
pc.value AS action_code_who,
|
2021-11-07 22:32:17 -05:00
|
|
|
pc2.value AS action_code_path,
|
2015-04-21 14:36:46 -04:00
|
|
|
p.edit_reason,
|
|
|
|
t.category_id
|
|
|
|
FROM user_actions as a
|
|
|
|
JOIN topics t on t.id = a.target_topic_id
|
|
|
|
LEFT JOIN posts p on p.id = a.target_post_id
|
|
|
|
JOIN posts p2 on p2.topic_id = a.target_topic_id and p2.post_number = 1
|
|
|
|
JOIN users u on u.id = a.acting_user_id
|
|
|
|
JOIN users pu on pu.id = COALESCE(p.user_id, t.user_id)
|
|
|
|
JOIN users au on au.id = a.user_id
|
|
|
|
LEFT JOIN categories c on c.id = t.category_id
|
2018-05-17 23:28:13 -04:00
|
|
|
LEFT JOIN post_custom_fields pc ON pc.post_id = a.target_post_id AND pc.name = 'action_code_who'
|
2022-10-13 05:10:18 -04:00
|
|
|
LEFT JOIN post_custom_fields pc2 ON pc2.post_id = a.target_post_id AND pc2.name = 'action_code_path'
|
2024-02-15 19:24:39 -05:00
|
|
|
/*left_join*/
|
2015-04-21 14:36:46 -04:00
|
|
|
/*where*/
|
|
|
|
/*order_by*/
|
|
|
|
/*offset*/
|
|
|
|
/*limit*/
|
|
|
|
SQL
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
apply_common_filters(builder, user_id, guardian, ignore_private_messages)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
if action_id
|
|
|
|
builder.where("a.id = :id", id: action_id.to_i)
|
2013-02-07 10:45:24 -05:00
|
|
|
else
|
2013-02-05 14:16:51 -05:00
|
|
|
builder.where("a.user_id = :user_id", user_id: user_id.to_i)
|
2013-02-07 10:45:24 -05:00
|
|
|
if action_types && action_types.length > 0
|
|
|
|
builder.where("a.action_type in (:action_types)", action_types: action_types)
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2017-12-07 16:16:53 -05:00
|
|
|
|
2019-01-15 21:40:16 -05:00
|
|
|
if acting_username
|
|
|
|
builder.where(
|
|
|
|
"u.username_lower = :acting_username",
|
|
|
|
acting_username: acting_username.downcase,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-12-07 16:16:53 -05:00
|
|
|
unless SiteSetting.enable_mentions?
|
|
|
|
builder.where("a.action_type <> :mention_type", mention_type: UserAction::MENTION)
|
|
|
|
end
|
|
|
|
|
2013-05-26 20:22:37 -04:00
|
|
|
builder.order_by("a.created_at desc").offset(offset.to_i).limit(limit.to_i)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-26 20:22:37 -04:00
|
|
|
|
2024-02-15 19:24:39 -05:00
|
|
|
DiscoursePluginRegistry.apply_modifier(:user_action_stream_builder, builder)
|
|
|
|
|
2018-06-20 03:48:02 -04:00
|
|
|
builder.query
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.log_action!(hash)
|
2024-03-18 13:05:46 -04:00
|
|
|
required_parameters = %i[action_type user_id acting_user_id target_post_id target_topic_id]
|
2015-04-21 14:36:46 -04:00
|
|
|
|
2013-07-22 19:48:18 -04:00
|
|
|
require_parameters(hash, *required_parameters)
|
2014-06-04 11:41:11 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
transaction(requires_new: true) do
|
2013-02-07 10:45:24 -05:00
|
|
|
begin
|
2013-08-16 03:04:30 -04:00
|
|
|
# TODO there are conditions when this is called and user_id was already rolled back and is invalid.
|
|
|
|
|
2013-07-22 19:48:18 -04:00
|
|
|
# protect against dupes, for some reason this is failing in some cases
|
2014-08-14 17:54:55 -04:00
|
|
|
action = self.find_by(hash.select { |k, _| required_parameters.include?(k) })
|
2013-07-22 19:48:18 -04:00
|
|
|
return action if action
|
|
|
|
|
|
|
|
action = self.new(hash)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
action.created_at = hash[:created_at] if hash[:created_at]
|
2013-02-05 14:16:51 -05:00
|
|
|
action.save!
|
2013-04-05 00:29:46 -04:00
|
|
|
|
|
|
|
user_id = hash[:user_id]
|
|
|
|
|
2014-05-06 09:41:59 -04:00
|
|
|
topic = Topic.includes(:category).find_by(id: hash[:target_topic_id])
|
2013-05-16 01:03:03 -04:00
|
|
|
|
2016-01-24 00:39:01 -05:00
|
|
|
update_like_count(user_id, hash[:action_type], 1) if topic && !topic.private_message?
|
|
|
|
|
2024-03-18 13:05:46 -04:00
|
|
|
user_ids = user_id != action.acting_user_id ? [user_id] : nil
|
|
|
|
|
2013-05-16 01:03:03 -04:00
|
|
|
group_ids = nil
|
2024-03-18 13:05:46 -04:00
|
|
|
if topic&.category&.read_restricted
|
|
|
|
group_ids = [Group::AUTO_GROUPS[:admins]] | topic.category.groups.pluck("groups.id")
|
2013-05-16 01:03:03 -04:00
|
|
|
end
|
|
|
|
|
2024-03-18 13:05:46 -04:00
|
|
|
if action.user && (user_ids.present? || group_ids.present?)
|
2021-05-20 02:58:27 -04:00
|
|
|
MessageBus.publish(
|
|
|
|
"/u/#{action.user.username_lower}",
|
|
|
|
action.id,
|
2024-03-18 13:05:46 -04:00
|
|
|
user_ids: user_ids,
|
2021-05-20 02:58:27 -04:00
|
|
|
group_ids: group_ids,
|
|
|
|
)
|
2013-09-04 15:35:10 -04:00
|
|
|
end
|
|
|
|
|
2013-07-17 02:40:15 -04:00
|
|
|
action
|
2013-02-05 14:16:51 -05:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
# can happen, don't care already logged
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.remove_action!(hash)
|
|
|
|
require_parameters(
|
|
|
|
hash,
|
|
|
|
:action_type,
|
|
|
|
:user_id,
|
|
|
|
:acting_user_id,
|
|
|
|
:target_topic_id,
|
|
|
|
:target_post_id,
|
|
|
|
)
|
2014-05-06 09:41:59 -04:00
|
|
|
if action = UserAction.find_by(hash.except(:created_at))
|
2013-02-07 10:45:24 -05:00
|
|
|
action.destroy
|
2015-05-03 22:21:00 -04:00
|
|
|
MessageBus.publish("/user/#{hash[:user_id]}", user_action_id: action.id, remove: true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-04-05 00:29:46 -04:00
|
|
|
|
2017-01-15 21:18:10 -05:00
|
|
|
if !Topic.where(id: hash[:target_topic_id], archetype: Archetype.private_message).exists?
|
|
|
|
update_like_count(hash[:user_id], hash[:action_type], -1)
|
|
|
|
end
|
2013-05-27 19:13:53 -04:00
|
|
|
end
|
|
|
|
|
2019-08-28 23:27:04 -04:00
|
|
|
def self.synchronize_target_topic_ids(post_ids = nil, limit: nil)
|
2013-07-22 22:43:34 -04:00
|
|
|
# nuke all dupes, using magic
|
2018-06-20 03:48:02 -04:00
|
|
|
builder = DB.build <<~SQL
|
|
|
|
DELETE FROM user_actions USING user_actions ua2
|
|
|
|
/*where*/
|
|
|
|
SQL
|
|
|
|
|
|
|
|
builder.where <<~SQL
|
|
|
|
user_actions.action_type = ua2.action_type AND
|
|
|
|
user_actions.user_id = ua2.user_id AND
|
|
|
|
user_actions.acting_user_id = ua2.acting_user_id AND
|
|
|
|
user_actions.target_post_id = ua2.target_post_id AND
|
|
|
|
user_actions.target_post_id > 0 AND
|
|
|
|
user_actions.id > ua2.id
|
|
|
|
SQL
|
2013-07-22 22:43:34 -04:00
|
|
|
|
2019-08-28 23:27:04 -04:00
|
|
|
builder.where(<<~SQL, limit: limit) if limit
|
|
|
|
user_actions.target_post_id IN (
|
|
|
|
SELECT target_post_id
|
|
|
|
FROM user_actions
|
|
|
|
WHERE created_at > :limit
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
2013-07-22 22:43:34 -04:00
|
|
|
builder.where("user_actions.target_post_id in (:post_ids)", post_ids: post_ids) if post_ids
|
|
|
|
|
|
|
|
builder.exec
|
|
|
|
|
2018-06-20 03:48:02 -04:00
|
|
|
builder = DB.build <<~SQL
|
|
|
|
UPDATE user_actions
|
|
|
|
SET target_topic_id = (select topic_id from posts where posts.id = target_post_id)
|
|
|
|
/*where*/
|
|
|
|
SQL
|
2013-07-17 02:40:15 -04:00
|
|
|
|
|
|
|
builder.where("target_topic_id <> (select topic_id from posts where posts.id = target_post_id)")
|
|
|
|
builder.where("target_post_id in (:post_ids)", post_ids: post_ids) if post_ids
|
|
|
|
|
2019-08-28 23:27:04 -04:00
|
|
|
builder.where(<<~SQL, limit: limit) if limit
|
|
|
|
target_post_id IN (
|
|
|
|
SELECT target_post_id
|
|
|
|
FROM user_actions
|
|
|
|
WHERE created_at > :limit
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
2013-07-17 02:40:15 -04:00
|
|
|
builder.exec
|
|
|
|
end
|
|
|
|
|
2019-08-28 23:27:04 -04:00
|
|
|
def self.ensure_consistency!(limit = nil)
|
|
|
|
self.synchronize_target_topic_ids(nil, limit: limit)
|
2013-07-17 02:40:15 -04:00
|
|
|
end
|
|
|
|
|
2013-05-27 19:13:53 -04:00
|
|
|
def self.update_like_count(user_id, action_type, delta)
|
2013-04-05 00:29:46 -04:00
|
|
|
if action_type == LIKE
|
2013-10-03 23:28:49 -04:00
|
|
|
UserStat.where(user_id: user_id).update_all("likes_given = likes_given + #{delta.to_i}")
|
2013-04-05 00:29:46 -04:00
|
|
|
elsif action_type == WAS_LIKED
|
2013-10-03 23:28:49 -04:00
|
|
|
UserStat.where(user_id: user_id).update_all("likes_received = likes_received + #{delta.to_i}")
|
2013-04-05 00:29:46 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
def self.apply_common_filters(builder, user_id, guardian, ignore_private_messages = false)
|
2014-02-28 11:14:36 -05:00
|
|
|
# We never return deleted topics in activity
|
|
|
|
builder.where("t.deleted_at is null")
|
|
|
|
|
|
|
|
# We will return deleted posts though if the user can see it
|
2013-04-29 02:33:24 -04:00
|
|
|
unless guardian.can_see_deleted_posts?
|
2014-02-28 11:14:36 -05:00
|
|
|
builder.where("p.deleted_at is null and p2.deleted_at is null")
|
2014-02-11 01:16:58 -05:00
|
|
|
|
|
|
|
current_user_id = -2
|
|
|
|
current_user_id = guardian.user.id if guardian.user
|
|
|
|
builder.where(
|
|
|
|
"NOT COALESCE(p.hidden, false) OR p.user_id = :current_user_id",
|
|
|
|
current_user_id: current_user_id,
|
|
|
|
)
|
2013-04-29 02:33:24 -04:00
|
|
|
end
|
|
|
|
|
2015-09-21 18:50:52 -04:00
|
|
|
visible_post_types = Topic.visible_post_types(guardian.user)
|
|
|
|
builder.where(
|
|
|
|
"COALESCE(p.post_type, p2.post_type) IN (:visible_post_types)",
|
|
|
|
visible_post_types: visible_post_types,
|
|
|
|
)
|
|
|
|
|
2023-02-16 04:40:11 -05:00
|
|
|
builder.where("t.visible") if guardian.user&.id != user_id && !guardian.is_staff?
|
2013-04-29 02:33:24 -04:00
|
|
|
|
2021-10-12 02:37:09 -04:00
|
|
|
filter_private_messages(builder, user_id, guardian, ignore_private_messages)
|
|
|
|
filter_categories(builder, guardian)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.filter_private_messages(builder, user_id, guardian, ignore_private_messages = false)
|
2016-12-20 23:01:26 -05:00
|
|
|
if !guardian.can_see_private_messages?(user_id) || ignore_private_messages || !guardian.user
|
|
|
|
builder.where("t.archetype <> :private_message", private_message: Archetype.private_message)
|
|
|
|
else
|
|
|
|
unless guardian.is_admin?
|
|
|
|
sql = <<~SQL
|
|
|
|
t.archetype <> :private_message OR
|
|
|
|
EXISTS (
|
|
|
|
SELECT 1 FROM topic_allowed_users tu WHERE tu.topic_id = t.id AND tu.user_id = :current_user_id
|
|
|
|
) OR
|
|
|
|
EXISTS (
|
|
|
|
SELECT 1 FROM topic_allowed_groups tg WHERE tg.topic_id = t.id AND tg.group_id IN (
|
|
|
|
SELECT group_id FROM group_users gu WHERE gu.user_id = :current_user_id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
|
|
|
builder.where(
|
|
|
|
sql,
|
|
|
|
private_message: Archetype.private_message,
|
|
|
|
current_user_id: guardian.user.id,
|
|
|
|
)
|
|
|
|
end
|
2013-04-29 02:33:24 -04:00
|
|
|
end
|
2021-10-12 02:37:09 -04:00
|
|
|
builder
|
|
|
|
end
|
2013-04-29 02:33:24 -04:00
|
|
|
|
2021-10-12 02:37:09 -04:00
|
|
|
def self.filter_categories(builder, guardian)
|
2014-02-06 22:11:52 -05:00
|
|
|
unless guardian.is_admin?
|
2013-04-29 02:33:24 -04:00
|
|
|
allowed = guardian.secure_category_ids
|
|
|
|
if allowed.present?
|
2013-07-13 21:24:16 -04:00
|
|
|
builder.where(
|
|
|
|
"( c.read_restricted IS NULL OR
|
|
|
|
NOT c.read_restricted OR
|
|
|
|
(c.read_restricted and c.id in (:cats)) )",
|
|
|
|
cats: guardian.secure_category_ids,
|
|
|
|
)
|
2013-04-29 02:33:24 -04:00
|
|
|
else
|
2013-07-13 21:24:16 -04:00
|
|
|
builder.where("(c.read_restricted IS NULL OR NOT c.read_restricted)")
|
2013-04-29 02:33:24 -04:00
|
|
|
end
|
|
|
|
end
|
2021-10-12 02:37:09 -04:00
|
|
|
builder
|
2013-04-29 02:33:24 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.require_parameters(data, *params)
|
|
|
|
params.each { |p| raise Discourse::InvalidParameters.new(p) if data[p].nil? }
|
|
|
|
end
|
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_actions
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# action_type :integer not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# target_topic_id :integer
|
|
|
|
# target_post_id :integer
|
|
|
|
# target_user_id :integer
|
|
|
|
# acting_user_id :integer
|
2014-08-27 01:19:25 -04:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2019-04-05 05:13:12 -04:00
|
|
|
# idx_unique_rows (action_type,user_id,target_topic_id,target_post_id,acting_user_id) UNIQUE
|
|
|
|
# idx_user_actions_speed_up_user_all (user_id,created_at,action_type)
|
2021-07-05 18:14:15 -04:00
|
|
|
# index_user_actions_on_acting_user_id (acting_user_id)
|
2019-04-05 05:13:12 -04:00
|
|
|
# index_user_actions_on_action_type_and_created_at (action_type,created_at)
|
|
|
|
# index_user_actions_on_target_post_id (target_post_id)
|
2019-04-26 04:23:27 -04:00
|
|
|
# index_user_actions_on_target_user_id (target_user_id) WHERE (target_user_id IS NOT NULL)
|
2021-07-05 18:14:15 -04:00
|
|
|
# index_user_actions_on_user_id_and_action_type (user_id,action_type)
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|