2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-01-06 07:44:30 -05:00
|
|
|
class PostEvent < ApplicationRecord
|
|
|
|
belongs_to :creator, class_name: "User"
|
2024-04-21 12:14:01 -04:00
|
|
|
enum :action, {
|
2022-01-06 07:44:30 -05:00
|
|
|
deleted: 0,
|
|
|
|
undeleted: 1,
|
|
|
|
approved: 2,
|
|
|
|
unapproved: 3,
|
2022-01-06 07:54:40 -05:00
|
|
|
flag_created: 4,
|
|
|
|
flag_removed: 5,
|
|
|
|
favorites_moved: 6,
|
|
|
|
favorites_received: 7,
|
|
|
|
rating_locked: 8,
|
|
|
|
rating_unlocked: 9,
|
|
|
|
status_locked: 10,
|
|
|
|
status_unlocked: 11,
|
|
|
|
note_locked: 12,
|
|
|
|
note_unlocked: 13,
|
2023-09-05 11:27:05 -04:00
|
|
|
comment_locked: 18,
|
|
|
|
comment_unlocked: 19,
|
2024-10-28 16:56:22 -04:00
|
|
|
comment_disabled: 22,
|
|
|
|
comment_enabled: 23,
|
2022-01-06 07:54:40 -05:00
|
|
|
replacement_accepted: 14,
|
|
|
|
replacement_rejected: 15,
|
2023-02-03 13:47:52 -05:00
|
|
|
replacement_promoted: 20,
|
2022-01-06 07:54:40 -05:00
|
|
|
replacement_deleted: 16,
|
2023-02-03 13:47:52 -05:00
|
|
|
expunged: 17,
|
2024-06-28 12:26:01 -04:00
|
|
|
changed_bg_color: 21,
|
2022-01-06 07:44:30 -05:00
|
|
|
}
|
2024-10-28 16:56:22 -04:00
|
|
|
MOD_ONLY_SEARCH_ACTIONS = [
|
2023-09-05 11:27:05 -04:00
|
|
|
actions[:comment_locked],
|
|
|
|
actions[:comment_unlocked],
|
2024-10-28 16:56:22 -04:00
|
|
|
actions[:comment_disabled],
|
|
|
|
actions[:comment_enabled],
|
2023-09-05 11:27:05 -04:00
|
|
|
].freeze
|
2015-07-28 18:45:01 -04:00
|
|
|
|
2022-01-08 19:47:06 -05:00
|
|
|
def self.add(post_id, creator, action, data = {})
|
|
|
|
create!(post_id: post_id, creator: creator, action: action.to_s, extra_data: data)
|
2017-03-19 21:45:51 -04:00
|
|
|
end
|
2015-07-28 18:45:01 -04:00
|
|
|
|
2022-01-06 07:44:30 -05:00
|
|
|
def is_creator_visible?(user)
|
|
|
|
case action
|
|
|
|
when "flag_created"
|
|
|
|
user.can_view_flagger?(creator_id)
|
|
|
|
else
|
|
|
|
true
|
2015-07-28 18:45:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-06 07:44:30 -05:00
|
|
|
def self.search(params)
|
|
|
|
q = super
|
2018-05-05 14:54:30 -04:00
|
|
|
|
2022-01-06 07:44:30 -05:00
|
|
|
if params[:post_id].present?
|
2024-10-28 16:56:22 -04:00
|
|
|
q = q.where(post_id: params[:post_id])
|
2022-01-06 07:44:30 -05:00
|
|
|
end
|
2018-05-05 14:54:30 -04:00
|
|
|
|
2023-08-03 16:01:53 -04:00
|
|
|
q = q.where_user(:creator_id, :creator, params) do |condition, user_ids|
|
|
|
|
condition.where.not(
|
|
|
|
action: actions[:flag_created],
|
|
|
|
creator_id: user_ids.reject { |user_id| CurrentUser.can_view_flagger?(user_id) },
|
|
|
|
)
|
2022-01-06 07:44:30 -05:00
|
|
|
end
|
2018-05-05 14:54:30 -04:00
|
|
|
|
2022-01-06 07:44:30 -05:00
|
|
|
if params[:action].present?
|
2024-10-28 16:56:22 -04:00
|
|
|
if !CurrentUser.user.is_moderator? && MOD_ONLY_SEARCH_ACTIONS.include?(actions[params[:action]])
|
|
|
|
raise(User::PrivilegeError)
|
|
|
|
end
|
|
|
|
q = q.where(action: actions[params[:action]])
|
2017-06-14 11:43:25 -04:00
|
|
|
end
|
2022-01-06 07:44:30 -05:00
|
|
|
|
2023-07-07 08:32:57 -04:00
|
|
|
q.apply_basic_order(params)
|
2017-06-14 11:43:25 -04:00
|
|
|
end
|
2024-10-28 16:56:22 -04:00
|
|
|
|
|
|
|
def self.search_options_for(user)
|
|
|
|
options = actions.keys
|
|
|
|
return options if user.is_moderator?
|
|
|
|
options.reject { |action| MOD_ONLY_SEARCH_ACTIONS.any?(actions[action]) }
|
|
|
|
end
|
2015-07-28 18:45:01 -04:00
|
|
|
end
|