2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-12-30 21:08:02 +08:00
|
|
|
# mixin for all guardian methods dealing with post permissions
|
2014-05-12 22:30:10 +08:00
|
|
|
module PostGuardian
|
2018-06-14 02:57:32 +08:00
|
|
|
def unrestricted_link_posting?
|
2024-02-08 09:19:28 +08:00
|
|
|
authenticated? && (is_staff? || @user.in_any_groups?(SiteSetting.post_links_allowed_groups_map))
|
2018-06-14 02:57:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def link_posting_access
|
|
|
|
if unrestricted_link_posting?
|
|
|
|
"full"
|
2020-07-27 08:23:54 +08:00
|
|
|
elsif SiteSetting.allowed_link_domains.present?
|
2018-06-14 02:57:32 +08:00
|
|
|
"limited"
|
|
|
|
else
|
|
|
|
"none"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_post_link?(host: nil)
|
|
|
|
return false if host.blank?
|
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
unrestricted_link_posting? || SiteSetting.allowed_link_domains.split("|").include?(host)
|
2018-02-09 01:56:10 +08:00
|
|
|
end
|
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
# Can the user act on the post in a particular way.
|
|
|
|
# taken_actions = the list of actions the user has already taken
|
2017-09-08 13:07:22 +08:00
|
|
|
def post_can_act?(post, action_key, opts: {}, can_see_post: nil)
|
2023-02-16 17:40:11 +08:00
|
|
|
return false if !(can_see_post.nil? && can_see_post?(post)) && !can_see_post
|
2016-12-21 12:01:26 +08:00
|
|
|
|
|
|
|
# no warnings except for staff
|
2020-03-11 20:03:20 +08:00
|
|
|
if action_key == :notify_user &&
|
|
|
|
(
|
|
|
|
post.user.blank? ||
|
|
|
|
(!is_staff? && opts[:is_warning].present? && opts[:is_warning] == "true")
|
2023-01-09 20:10:19 +08:00
|
|
|
)
|
2020-03-11 20:03:20 +08:00
|
|
|
return false
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2016-12-21 12:01:26 +08:00
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
taken = opts[:taken_actions].try(:keys).to_a
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-14 10:13:46 +08:00
|
|
|
post_action_type_view = opts[:post_action_type_view] || PostActionTypeView.new
|
2020-08-20 07:31:40 +08:00
|
|
|
is_flag =
|
2024-07-22 15:35:49 +08:00
|
|
|
if (opts[:notify_flag_types] && opts[:additional_message_types])
|
|
|
|
opts[:notify_flag_types][action_key] || opts[:additional_message_types][action_key]
|
|
|
|
else
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-14 10:13:46 +08:00
|
|
|
post_action_type_view.notify_flag_types[action_key] ||
|
|
|
|
post_action_type_view.additional_message_types[action_key]
|
2024-07-22 15:35:49 +08:00
|
|
|
end
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-14 10:13:46 +08:00
|
|
|
already_taken_this_action =
|
|
|
|
taken.any? && taken.include?(post_action_type_view.types[action_key])
|
|
|
|
already_did_flagging =
|
|
|
|
taken.any? && (taken & post_action_type_view.notify_flag_types.values).any?
|
2023-01-09 20:10:19 +08:00
|
|
|
|
2015-04-08 10:29:43 +08:00
|
|
|
result =
|
2023-07-21 21:21:07 +08:00
|
|
|
if authenticated? && post
|
|
|
|
# Allow anonymous users to like if feature is enabled and short-circuit otherwise
|
|
|
|
return SiteSetting.allow_anonymous_likes? && (action_key == :like) if @user.anonymous?
|
|
|
|
|
2018-08-17 23:06:01 +08:00
|
|
|
# Silenced users can't flag
|
|
|
|
return false if is_flag && @user.silenced?
|
2023-01-09 20:10:19 +08:00
|
|
|
|
2018-11-05 23:00:59 +08:00
|
|
|
# Hidden posts can't be flagged
|
|
|
|
return false if is_flag && post.hidden?
|
2023-01-09 20:10:19 +08:00
|
|
|
|
2018-02-13 03:56:21 +08:00
|
|
|
# post made by staff, but we don't allow staff flags
|
2018-02-15 04:46:04 +08:00
|
|
|
return false if is_flag && (!SiteSetting.allow_flagging_staff?) && post&.user&.staff?
|
2023-01-09 20:10:19 +08:00
|
|
|
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-14 10:13:46 +08:00
|
|
|
if is_flag && post_action_type_view.disabled_flag_types.keys.include?(action_key)
|
|
|
|
return false
|
|
|
|
end
|
2024-05-29 12:39:58 +08:00
|
|
|
|
2022-10-05 08:50:20 +08:00
|
|
|
if action_key == :notify_user &&
|
|
|
|
!@user.in_any_groups?(SiteSetting.personal_message_enabled_groups_map)
|
2023-10-11 01:21:57 +08:00
|
|
|
# The modifier below is used to add additional permissions for notifying users.
|
|
|
|
# In core the only method of notifying a user is personal messages so we check if the
|
|
|
|
# user can PM. Plugins can extend the behavior of how users are notifier via `notify_user`
|
|
|
|
# post action, and this allows extension for that use case.
|
|
|
|
can_notify = false
|
|
|
|
can_notify =
|
|
|
|
DiscoursePluginRegistry.apply_modifier(
|
|
|
|
:post_guardian_can_notify_user,
|
|
|
|
can_notify,
|
|
|
|
self,
|
|
|
|
post,
|
|
|
|
)
|
|
|
|
return can_notify
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
|
|
|
|
2014-03-10 23:48:27 +08:00
|
|
|
# we allow flagging for trust level 1 and higher
|
2015-01-08 23:06:43 +08:00
|
|
|
# always allowed for private messages
|
2023-01-09 20:10:19 +08:00
|
|
|
(
|
2018-02-07 06:12:27 +08:00
|
|
|
is_flag && not(already_did_flagging) &&
|
2023-01-09 20:10:19 +08:00
|
|
|
(
|
2023-12-13 17:18:42 +08:00
|
|
|
@user.in_any_groups?(SiteSetting.flag_post_allowed_groups_map) ||
|
2018-02-07 06:12:27 +08:00
|
|
|
post.topic.private_message?
|
2023-01-09 20:10:19 +08:00
|
|
|
)
|
|
|
|
) ||
|
2014-01-10 07:25:14 +08:00
|
|
|
# not a flagging action, and haven't done it already
|
|
|
|
not(is_flag || already_taken_this_action) &&
|
2014-08-08 01:12:35 +08:00
|
|
|
# nothing except flagging on archived topics
|
2018-02-28 11:22:51 +08:00
|
|
|
not(post.topic&.archived?) &&
|
2014-08-08 01:12:35 +08:00
|
|
|
# nothing except flagging on deleted posts
|
|
|
|
not(post.trashed?) &&
|
2014-01-10 07:25:14 +08:00
|
|
|
# don't like your own stuff
|
2020-03-11 20:03:20 +08:00
|
|
|
not(action_key == :like && (post.user.blank? || is_my_own?(post)))
|
2018-02-28 11:22:51 +08:00
|
|
|
end
|
2014-12-20 05:47:39 +08:00
|
|
|
|
2015-04-08 10:29:43 +08:00
|
|
|
!!result
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
2018-01-26 04:38:40 +08:00
|
|
|
def can_lock_post?(post)
|
|
|
|
can_see_post?(post) && is_staff?
|
|
|
|
end
|
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
# Can we see who acted on a post in a particular way?
|
|
|
|
def can_see_post_actors?(topic, post_action_type_id)
|
2014-08-08 01:12:35 +08:00
|
|
|
return true if is_admin?
|
2014-01-10 07:25:14 +08:00
|
|
|
return false unless topic
|
|
|
|
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-14 10:13:46 +08:00
|
|
|
post_action_type_view = PostActionTypeView.new
|
|
|
|
type_symbol = post_action_type_view.types[post_action_type_id]
|
2016-10-19 14:36:35 +08:00
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
return false if type_symbol == :bookmark
|
2016-10-19 14:36:35 +08:00
|
|
|
return false if type_symbol == :notify_user && !is_moderator?
|
|
|
|
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-14 10:13:46 +08:00
|
|
|
return can_see_flags?(topic) if post_action_type_view.is_flag?(type_symbol)
|
2014-01-10 07:25:14 +08:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_delete_all_posts?(user)
|
2014-07-29 01:17:37 +08:00
|
|
|
is_staff? && user && !user.admin? &&
|
2021-08-25 12:44:22 +08:00
|
|
|
(
|
|
|
|
is_admin? ||
|
|
|
|
(
|
2023-01-09 20:10:19 +08:00
|
|
|
(
|
2021-08-25 12:44:22 +08:00
|
|
|
user.first_post_created_at.nil? ||
|
|
|
|
user.first_post_created_at >= SiteSetting.delete_user_max_post_age.days.ago
|
|
|
|
) && user.post_count <= SiteSetting.delete_all_posts_max.to_i
|
2023-01-09 20:10:19 +08:00
|
|
|
)
|
|
|
|
)
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
2023-01-03 09:00:42 +08:00
|
|
|
def can_create_post?(topic)
|
|
|
|
return can_create_post_in_topic?(topic) if !topic
|
2018-01-24 02:11:39 +08:00
|
|
|
|
2023-01-03 09:00:42 +08:00
|
|
|
key = topic_memoize_key(topic)
|
|
|
|
@can_create_post ||= {}
|
|
|
|
|
|
|
|
@can_create_post.fetch(key) { @can_create_post[key] = can_create_post_in_topic?(topic) }
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_edit_post?(post)
|
2014-07-29 22:40:02 +08:00
|
|
|
return false if Discourse.static_doc_topic_ids.include?(post.topic_id) && !is_admin?
|
|
|
|
|
2016-04-13 13:59:38 +08:00
|
|
|
return true if is_admin?
|
|
|
|
|
2018-01-26 04:38:40 +08:00
|
|
|
# Must be staff to edit a locked post
|
|
|
|
return false if post.locked? && !is_staff?
|
|
|
|
|
2023-07-07 23:48:14 +08:00
|
|
|
if (is_staff? || is_in_edit_post_groups? || is_category_group_moderator?(post.topic&.category))
|
2018-10-04 09:34:47 +08:00
|
|
|
return can_create_post?(post.topic)
|
2014-05-13 20:53:11 +08:00
|
|
|
end
|
|
|
|
|
2021-10-13 17:53:23 +08:00
|
|
|
return false if post.topic&.archived? || post.user_deleted || post.deleted_at
|
2023-01-09 20:10:19 +08:00
|
|
|
|
2021-04-14 13:54:09 +08:00
|
|
|
# Editing a shared draft.
|
2020-12-03 22:07:57 +08:00
|
|
|
if (
|
|
|
|
can_see_post?(post) && can_create_post?(post.topic) &&
|
|
|
|
post.topic.category_id == SiteSetting.shared_drafts_category.to_i &&
|
|
|
|
can_see_category?(post.topic.category) && can_see_shared_draft?
|
|
|
|
)
|
|
|
|
return true
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2020-12-03 22:07:57 +08:00
|
|
|
|
2023-12-12 12:20:37 +08:00
|
|
|
if post.wiki && @user.in_any_groups?(SiteSetting.edit_wiki_post_allowed_groups_map)
|
2017-05-09 04:23:11 +08:00
|
|
|
return can_create_post?(post.topic)
|
2014-05-13 20:53:11 +08:00
|
|
|
end
|
|
|
|
|
2024-02-08 11:10:26 +08:00
|
|
|
return false if !trusted_with_post_edits?
|
2016-10-01 00:12:27 +08:00
|
|
|
|
2014-06-21 03:38:03 +08:00
|
|
|
if is_my_own?(post)
|
2018-08-15 12:29:36 +08:00
|
|
|
return false if @user.silenced?
|
|
|
|
|
2022-02-25 22:09:31 +08:00
|
|
|
return can_edit_hidden_post?(post) if post.hidden?
|
2014-06-21 03:38:03 +08:00
|
|
|
|
2021-04-14 13:54:09 +08:00
|
|
|
if post.is_first_post? && post.topic.category_allows_unlimited_owner_edits_on_first_post?
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2019-09-06 19:44:12 +08:00
|
|
|
return !post.edit_time_limit_expired?(@user)
|
2014-05-13 20:53:11 +08:00
|
|
|
end
|
|
|
|
|
2020-07-23 21:50:00 +08:00
|
|
|
if post.is_category_description?
|
|
|
|
return true if can_edit_category_description?(post.topic.category)
|
|
|
|
end
|
|
|
|
|
2014-05-13 20:53:11 +08:00
|
|
|
false
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
2023-07-07 23:48:14 +08:00
|
|
|
def is_in_edit_post_groups?
|
|
|
|
SiteSetting.edit_all_post_groups.present? &&
|
|
|
|
user.in_any_groups?(SiteSetting.edit_all_post_groups.to_s.split("|").map(&:to_i))
|
|
|
|
end
|
|
|
|
|
2022-02-25 22:09:31 +08:00
|
|
|
def can_edit_hidden_post?(post)
|
|
|
|
return false if post.nil?
|
|
|
|
post.hidden_at.nil? ||
|
|
|
|
post.hidden_at < SiteSetting.cooldown_minutes_after_hiding_posts.minutes.ago
|
|
|
|
end
|
|
|
|
|
2019-05-31 04:42:59 +08:00
|
|
|
def can_delete_post_or_topic?(post)
|
|
|
|
post.is_first_post? ? post.topic && can_delete_topic?(post.topic) : can_delete_post?(post)
|
|
|
|
end
|
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
def can_delete_post?(post)
|
2019-03-30 00:10:05 +08:00
|
|
|
return false if !can_see_post?(post)
|
2016-12-21 12:01:26 +08:00
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
# Can't delete the first post
|
2015-04-24 01:33:29 +08:00
|
|
|
return false if post.is_first_post?
|
2014-01-10 07:25:14 +08:00
|
|
|
|
2021-06-30 20:51:35 +08:00
|
|
|
return true if is_staff? || is_category_group_moderator?(post.topic&.category)
|
2021-01-08 23:05:13 +08:00
|
|
|
|
2023-12-14 09:56:42 +08:00
|
|
|
return true if user.in_any_groups?(SiteSetting.delete_all_posts_and_topics_allowed_groups_map)
|
2023-01-20 10:31:51 +08:00
|
|
|
|
2021-01-08 23:05:13 +08:00
|
|
|
# Can't delete posts in archived topics unless you are staff
|
|
|
|
return false if post.topic&.archived?
|
2014-01-18 06:42:12 +08:00
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
# You can delete your own posts
|
2021-01-08 23:05:13 +08:00
|
|
|
if is_my_own?(post)
|
|
|
|
if (
|
|
|
|
SiteSetting.max_post_deletions_per_minute < 1 ||
|
|
|
|
SiteSetting.max_post_deletions_per_day < 1
|
|
|
|
)
|
|
|
|
return false
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2021-01-08 23:05:13 +08:00
|
|
|
return true if !post.user_deleted?
|
|
|
|
end
|
2014-01-10 07:25:14 +08:00
|
|
|
|
2021-01-08 23:05:13 +08:00
|
|
|
false
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
2021-10-13 17:53:23 +08:00
|
|
|
def can_permanently_delete_post?(post)
|
|
|
|
return false if !SiteSetting.can_permanently_delete
|
|
|
|
return false if !post
|
|
|
|
return false if post.is_first_post?
|
|
|
|
return false if !is_admin? || !can_edit_post?(post)
|
|
|
|
return false if !post.deleted_at
|
|
|
|
if post.deleted_by_id == @user.id && post.deleted_at >= Post::PERMANENT_DELETE_TIMER.ago
|
|
|
|
return false
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2021-10-13 17:53:23 +08:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
def can_recover_post?(post)
|
2020-11-06 01:18:26 +08:00
|
|
|
return false unless post
|
|
|
|
|
2020-12-30 21:08:02 +08:00
|
|
|
# PERF, vast majority of the time topic will not be deleted
|
|
|
|
topic = (post.topic || Topic.with_deleted.find(post.topic_id)) if post.topic_id
|
2021-01-08 23:05:13 +08:00
|
|
|
return true if can_moderate_topic?(topic) && !!post.deleted_at
|
2020-11-06 01:18:26 +08:00
|
|
|
|
2021-01-08 23:05:13 +08:00
|
|
|
if is_my_own?(post)
|
|
|
|
if (
|
|
|
|
SiteSetting.max_post_deletions_per_minute < 1 ||
|
|
|
|
SiteSetting.max_post_deletions_per_day < 1
|
|
|
|
)
|
|
|
|
return false
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2021-01-08 23:05:13 +08:00
|
|
|
return true if post.user_deleted && !post.deleted_at
|
2017-03-06 13:17:57 +08:00
|
|
|
end
|
2021-01-08 23:05:13 +08:00
|
|
|
|
|
|
|
false
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_delete_post_action?(post_action)
|
2019-01-04 01:03:01 +08:00
|
|
|
return false unless is_my_own?(post_action) && !post_action.is_private_message?
|
|
|
|
|
2024-03-22 06:12:12 +08:00
|
|
|
ok_to_delete =
|
|
|
|
post_action.created_at > SiteSetting.post_undo_action_window_mins.minutes.ago &&
|
|
|
|
!post_action.post&.topic&.archived?
|
|
|
|
|
|
|
|
# NOTE: This looks strange...but we are checking if someone is posting anonymously
|
|
|
|
# as a AnonymousUser model, _not_ as Guardian::AnonymousUser which is a different thing
|
|
|
|
# used when !authenticated?
|
|
|
|
if authenticated? && is_anonymous?
|
|
|
|
return(
|
|
|
|
ok_to_delete && SiteSetting.allow_anonymous_likes? && post_action.is_like? &&
|
|
|
|
is_my_own?(post_action)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
ok_to_delete
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
2023-02-14 13:45:06 +08:00
|
|
|
def can_receive_post_notifications?(post)
|
|
|
|
return false if !authenticated?
|
|
|
|
|
|
|
|
if is_admin? && SiteSetting.suppress_secured_categories_from_admin
|
|
|
|
topic = post.topic
|
|
|
|
if !topic.private_message? && topic.category.read_restricted
|
|
|
|
return secure_category_ids.include?(topic.category_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
can_see_post?(post)
|
|
|
|
end
|
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
def can_see_post?(post)
|
2015-09-11 04:01:23 +08:00
|
|
|
return false if post.blank?
|
|
|
|
return true if is_admin?
|
2023-01-03 09:00:42 +08:00
|
|
|
return false unless can_see_post_topic?(post)
|
2015-09-22 06:50:52 +08:00
|
|
|
unless post.user == @user || Topic.visible_post_types(@user).include?(post.post_type)
|
|
|
|
return false
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2021-06-22 00:10:02 +08:00
|
|
|
return true if is_moderator? || is_category_group_moderator?(post.topic.category)
|
2023-04-25 13:37:29 +08:00
|
|
|
if (!post.trashed? || can_see_deleted_post?(post)) &&
|
|
|
|
(!post.hidden? || can_see_hidden_post?(post))
|
|
|
|
return true
|
|
|
|
end
|
2021-06-22 00:10:02 +08:00
|
|
|
false
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
2023-01-09 14:12:10 +08:00
|
|
|
def can_see_deleted_post?(post)
|
|
|
|
return false if !post.trashed?
|
|
|
|
return false if @user.anonymous?
|
|
|
|
return true if is_staff?
|
|
|
|
post.deleted_by_id == @user.id && @user.has_trust_level?(TrustLevel[4])
|
|
|
|
end
|
|
|
|
|
2023-04-25 13:37:29 +08:00
|
|
|
def can_see_hidden_post?(post)
|
2023-06-01 11:32:05 +08:00
|
|
|
if SiteSetting.hidden_post_visible_groups_map.include?(Group::AUTO_GROUPS[:everyone])
|
|
|
|
return true
|
|
|
|
end
|
2023-04-25 13:37:29 +08:00
|
|
|
return false if anonymous?
|
2023-06-01 11:32:05 +08:00
|
|
|
return true if is_staff?
|
|
|
|
post.user_id == @user.id || @user.in_any_groups?(SiteSetting.hidden_post_visible_groups_map)
|
2023-04-25 13:37:29 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
def can_view_edit_history?(post)
|
2014-05-12 22:30:10 +08:00
|
|
|
return false unless post
|
2014-06-27 01:19:35 +08:00
|
|
|
|
|
|
|
if !post.hidden
|
2016-07-16 19:30:00 +08:00
|
|
|
return true if post.wiki || SiteSetting.edit_history_visible_to_public
|
2014-06-27 01:19:35 +08:00
|
|
|
end
|
2014-05-12 22:30:10 +08:00
|
|
|
|
2020-06-18 18:27:51 +08:00
|
|
|
authenticated? && (is_staff? || @user.id == post.user_id) && can_see_post?(post)
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
2014-03-28 09:28:14 +08:00
|
|
|
def can_change_post_owner?
|
2021-07-13 22:40:11 +08:00
|
|
|
return true if is_admin?
|
|
|
|
|
|
|
|
SiteSetting.moderators_change_post_ownership && is_staff?
|
2014-03-28 09:28:14 +08:00
|
|
|
end
|
2014-05-13 20:53:11 +08:00
|
|
|
|
2016-11-07 03:14:09 +08:00
|
|
|
def can_change_post_timestamps?
|
2019-02-22 17:03:52 +08:00
|
|
|
is_staff?
|
2016-11-07 03:14:09 +08:00
|
|
|
end
|
|
|
|
|
2016-01-11 23:26:00 +08:00
|
|
|
def can_wiki?(post)
|
|
|
|
return false unless authenticated?
|
2023-12-23 18:31:46 +08:00
|
|
|
return true if is_staff? || @user.has_trust_level?(TrustLevel[4])
|
2016-03-15 17:13:52 +08:00
|
|
|
|
2023-12-27 09:21:39 +08:00
|
|
|
if @user.in_any_groups?(SiteSetting.self_wiki_allowed_groups_map) && is_my_own?(post)
|
2016-03-15 17:13:52 +08:00
|
|
|
return false if post.hidden?
|
2019-09-06 19:44:12 +08:00
|
|
|
return !post.edit_time_limit_expired?(@user)
|
2016-03-15 17:13:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
false
|
2014-05-13 20:53:11 +08:00
|
|
|
end
|
2014-07-17 03:04:55 +08:00
|
|
|
|
2014-09-11 05:08:33 +08:00
|
|
|
def can_change_post_type?
|
|
|
|
is_staff?
|
|
|
|
end
|
|
|
|
|
2014-09-11 22:04:40 +08:00
|
|
|
def can_rebake?
|
2015-02-04 01:19:01 +08:00
|
|
|
is_staff? || @user.has_trust_level?(TrustLevel[4])
|
2014-09-11 22:04:40 +08:00
|
|
|
end
|
|
|
|
|
2014-07-17 03:04:55 +08:00
|
|
|
def can_see_flagged_posts?
|
|
|
|
is_staff?
|
|
|
|
end
|
|
|
|
|
2020-11-06 01:18:26 +08:00
|
|
|
def can_see_deleted_posts?(category = nil)
|
2023-01-20 10:31:51 +08:00
|
|
|
is_staff? || is_category_group_moderator?(category) ||
|
2023-12-14 09:56:42 +08:00
|
|
|
@user.in_any_groups?(SiteSetting.delete_all_posts_and_topics_allowed_groups_map)
|
2014-07-17 03:04:55 +08:00
|
|
|
end
|
2014-09-23 00:55:13 +08:00
|
|
|
|
2014-11-12 21:49:42 +08:00
|
|
|
def can_view_raw_email?(post)
|
2024-03-04 11:48:16 +08:00
|
|
|
post && @user.in_any_groups?(SiteSetting.view_raw_email_allowed_groups_map)
|
2014-10-18 03:18:29 +08:00
|
|
|
end
|
|
|
|
|
2014-09-23 00:55:13 +08:00
|
|
|
def can_unhide?(post)
|
|
|
|
post.try(:hidden) && is_staff?
|
|
|
|
end
|
2018-08-10 08:48:30 +08:00
|
|
|
|
|
|
|
def can_skip_bump?
|
2019-01-03 21:13:36 +08:00
|
|
|
is_staff? || @user.has_trust_level?(TrustLevel[4])
|
2018-08-10 08:48:30 +08:00
|
|
|
end
|
2023-01-03 09:00:42 +08:00
|
|
|
|
2024-02-08 11:10:26 +08:00
|
|
|
def trusted_with_post_edits?
|
|
|
|
is_staff? || @user.in_any_groups?(SiteSetting.edit_post_allowed_groups_map)
|
2023-12-13 13:25:13 +08:00
|
|
|
end
|
|
|
|
|
2024-02-08 11:10:26 +08:00
|
|
|
private
|
|
|
|
|
2023-01-03 09:00:42 +08:00
|
|
|
def can_create_post_in_topic?(topic)
|
|
|
|
if !SiteSetting.enable_system_message_replies? && topic.try(:subtype) == "system_message"
|
|
|
|
return false
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2023-01-03 09:00:42 +08:00
|
|
|
|
|
|
|
(
|
|
|
|
!SpamRule::AutoSilence.prevent_posting?(@user) ||
|
|
|
|
(!!topic.try(:private_message?) && topic.allowed_users.include?(@user))
|
|
|
|
) &&
|
|
|
|
(
|
|
|
|
!topic || !topic.category ||
|
|
|
|
Category.post_create_allowed(self).where(id: topic.category.id).count == 1
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic_memoize_key(topic)
|
|
|
|
# Milliseconds precision on Topic#updated_at so that we don't use memoized results after topic has been updated.
|
|
|
|
"#{topic.id}-#{(topic.updated_at.to_f * 1000).to_i}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_see_post_topic?(post)
|
|
|
|
topic = post.topic
|
|
|
|
return false if !topic
|
|
|
|
|
|
|
|
key = topic_memoize_key(topic)
|
|
|
|
@can_see_post_topic ||= {}
|
|
|
|
|
|
|
|
@can_see_post_topic.fetch(key) { @can_see_post_topic[key] = can_see_topic?(topic) }
|
|
|
|
end
|
2014-01-18 06:42:12 +08:00
|
|
|
end
|