2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-04-12 21:55:27 +08:00
|
|
|
class TopicViewDetailsSerializer < ApplicationSerializer
|
|
|
|
|
|
|
|
def self.can_attributes
|
|
|
|
[:can_move_posts,
|
|
|
|
:can_delete,
|
2021-10-13 17:53:23 +08:00
|
|
|
:can_permanently_delete,
|
2019-04-12 21:55:27 +08:00
|
|
|
:can_recover,
|
|
|
|
:can_remove_allowed_users,
|
|
|
|
:can_invite_to,
|
|
|
|
:can_invite_via_email,
|
|
|
|
:can_create_post,
|
|
|
|
:can_reply_as_new_topic,
|
|
|
|
:can_flag_topic,
|
2019-05-04 02:26:37 +08:00
|
|
|
:can_convert_topic,
|
2019-10-24 02:05:38 +08:00
|
|
|
:can_review_topic,
|
2020-04-09 00:52:36 +08:00
|
|
|
:can_edit_tags,
|
2020-07-15 00:36:19 +08:00
|
|
|
:can_publish_page,
|
|
|
|
:can_close_topic,
|
2020-07-21 03:53:47 +08:00
|
|
|
:can_archive_topic,
|
2020-08-05 22:33:25 +08:00
|
|
|
:can_split_merge_topic,
|
2020-11-06 01:18:26 +08:00
|
|
|
:can_edit_staff_notes,
|
2020-12-15 00:01:22 +08:00
|
|
|
:can_toggle_topic_visibility,
|
2021-03-10 05:05:11 +08:00
|
|
|
:can_pin_unpin_topic,
|
2020-11-06 01:18:26 +08:00
|
|
|
:can_moderate_category]
|
2019-04-12 21:55:27 +08:00
|
|
|
end
|
|
|
|
|
2020-12-03 06:21:59 +08:00
|
|
|
# NOTE: `can_edit` is defined as an attribute because we explicitly want
|
|
|
|
# it returned even if it has a value of `false`
|
2019-04-12 21:55:27 +08:00
|
|
|
attributes(
|
2020-12-03 06:21:59 +08:00
|
|
|
:can_edit,
|
2019-04-12 21:55:27 +08:00
|
|
|
:notification_level,
|
|
|
|
:notifications_reason_id,
|
|
|
|
*can_attributes,
|
|
|
|
:can_remove_self_id,
|
|
|
|
:participants,
|
|
|
|
:allowed_users
|
|
|
|
)
|
|
|
|
|
|
|
|
has_one :created_by, serializer: BasicUserSerializer, embed: :objects
|
|
|
|
has_one :last_poster, serializer: BasicUserSerializer, embed: :objects
|
|
|
|
has_many :links, serializer: TopicLinkSerializer, embed: :objects
|
|
|
|
has_many :participants, serializer: TopicPostCountSerializer, embed: :objects
|
|
|
|
has_many :allowed_users, serializer: BasicUserSerializer, embed: :objects
|
|
|
|
has_many :allowed_groups, serializer: BasicGroupSerializer, embed: :objects
|
|
|
|
|
|
|
|
def participants
|
|
|
|
object.post_counts_by_user.reject { |p| object.participants[p].blank? }.map do |pc|
|
|
|
|
{ user: object.participants[pc[0]], post_count: pc[1] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_participants?
|
|
|
|
object.post_counts_by_user.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_links?
|
|
|
|
object.links.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def created_by
|
|
|
|
object.topic.user
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_poster
|
|
|
|
object.topic.last_poster
|
|
|
|
end
|
|
|
|
|
|
|
|
def notification_level
|
|
|
|
object.topic_user&.notification_level || TopicUser.notification_levels[:regular]
|
|
|
|
end
|
|
|
|
|
|
|
|
def notifications_reason_id
|
|
|
|
object.topic_user.notifications_reason_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_notifications_reason_id?
|
|
|
|
object.topic_user.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
# confusingly this is an id, not a bool like all other `can` methods
|
|
|
|
def can_remove_self_id
|
|
|
|
scope.user.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_can_remove_self_id?
|
|
|
|
scope.can_remove_allowed_users?(object.topic, scope.user)
|
|
|
|
end
|
|
|
|
|
|
|
|
can_attributes.each do |ca|
|
|
|
|
define_method(ca) { true }
|
|
|
|
end
|
|
|
|
|
2020-12-03 06:21:59 +08:00
|
|
|
# NOTE: A Category Group Moderator moving a topic to a different category
|
|
|
|
# may result in the 'can_edit?' result changing from `true` to `false`.
|
2021-05-21 09:43:47 +08:00
|
|
|
# Explicitly returning a `false` value is required to update the client UI.
|
2020-12-03 06:21:59 +08:00
|
|
|
def can_edit
|
|
|
|
scope.can_edit?(object.topic)
|
|
|
|
end
|
|
|
|
|
2019-05-04 02:26:37 +08:00
|
|
|
def include_can_review_topic?
|
|
|
|
scope.can_review_topic?(object.topic)
|
|
|
|
end
|
|
|
|
|
2019-04-12 21:55:27 +08:00
|
|
|
def include_can_move_posts?
|
|
|
|
scope.can_move_posts?(object.topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_can_delete?
|
|
|
|
scope.can_delete?(object.topic)
|
|
|
|
end
|
|
|
|
|
2021-10-13 17:53:23 +08:00
|
|
|
def include_can_permanently_delete?
|
|
|
|
SiteSetting.can_permanently_delete && object.topic.deleted_at
|
|
|
|
end
|
|
|
|
|
2019-04-12 21:55:27 +08:00
|
|
|
def include_can_recover?
|
|
|
|
scope.can_recover_topic?(object.topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_can_remove_allowed_users?
|
|
|
|
scope.can_remove_allowed_users?(object.topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_can_invite_to?
|
|
|
|
scope.can_invite_to?(object.topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_can_invite_via_email?
|
|
|
|
scope.can_invite_via_email?(object.topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_can_create_post?
|
|
|
|
scope.can_create?(Post, object.topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_can_reply_as_new_topic?
|
|
|
|
scope.can_reply_as_new_topic?(object.topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_can_flag_topic?
|
|
|
|
object.actions_summary.any? { |a| a[:can_act] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_can_convert_topic?
|
|
|
|
scope.can_convert_topic?(object.topic)
|
|
|
|
end
|
|
|
|
|
2019-10-24 02:05:38 +08:00
|
|
|
def include_can_edit_tags?
|
|
|
|
!scope.can_edit?(object.topic) && scope.can_edit_tags?(object.topic)
|
|
|
|
end
|
|
|
|
|
2020-12-15 00:01:22 +08:00
|
|
|
def include_can_toggle_topic_visibility?
|
|
|
|
scope.can_toggle_topic_visibility?(object.topic)
|
|
|
|
end
|
|
|
|
|
2021-03-10 05:05:11 +08:00
|
|
|
def include_can_pin_unpin_topic?
|
|
|
|
scope.can_pin_unpin_topic?(object.topic)
|
|
|
|
end
|
|
|
|
|
2020-07-21 03:53:47 +08:00
|
|
|
def can_perform_action_available_to_group_moderators?
|
|
|
|
@can_perform_action_available_to_group_moderators ||= scope.can_perform_action_available_to_group_moderators?(object.topic)
|
2020-07-15 00:36:19 +08:00
|
|
|
end
|
2020-07-21 03:53:47 +08:00
|
|
|
alias :include_can_close_topic? :can_perform_action_available_to_group_moderators?
|
|
|
|
alias :include_can_archive_topic? :can_perform_action_available_to_group_moderators?
|
2020-08-05 22:33:25 +08:00
|
|
|
alias :include_can_split_merge_topic? :can_perform_action_available_to_group_moderators?
|
2020-07-21 03:53:47 +08:00
|
|
|
alias :include_can_edit_staff_notes? :can_perform_action_available_to_group_moderators?
|
2020-11-06 01:18:26 +08:00
|
|
|
alias :include_can_moderate_category? :can_perform_action_available_to_group_moderators?
|
2020-07-15 00:36:19 +08:00
|
|
|
|
2020-04-09 00:52:36 +08:00
|
|
|
def include_can_publish_page?
|
|
|
|
scope.can_publish_page?(object.topic)
|
|
|
|
end
|
|
|
|
|
2019-04-12 21:55:27 +08:00
|
|
|
def allowed_users
|
2021-01-13 22:27:23 +08:00
|
|
|
object.topic.allowed_users.reject do |user|
|
|
|
|
object.group_allowed_user_ids.include?(user.id) && user != scope.user
|
|
|
|
end
|
2019-04-12 21:55:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_allowed_users?
|
|
|
|
object.personal_message
|
|
|
|
end
|
|
|
|
|
|
|
|
def allowed_groups
|
|
|
|
object.topic.allowed_groups
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_allowed_groups?
|
|
|
|
object.personal_message
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|