2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
class KnowledgeBase::Answer < ApplicationModel
|
|
|
|
include HasTranslations
|
|
|
|
include HasAgentAllowedParams
|
2021-08-16 08:20:07 +00:00
|
|
|
include HasTags
|
2019-06-04 03:40:48 +00:00
|
|
|
include CanBePublished
|
|
|
|
include ChecksKbClientNotification
|
2020-04-20 09:47:45 +00:00
|
|
|
include CanCloneAttachments
|
2019-06-04 03:40:48 +00:00
|
|
|
|
|
|
|
AGENT_ALLOWED_ATTRIBUTES = %i[category_id promoted internal_note].freeze
|
|
|
|
AGENT_ALLOWED_NESTED_RELATIONS = %i[translations].freeze
|
|
|
|
|
2020-06-22 09:57:45 +00:00
|
|
|
belongs_to :category, class_name: 'KnowledgeBase::Category', inverse_of: :answers, touch: true
|
2019-06-04 03:40:48 +00:00
|
|
|
|
|
|
|
scope :include_contents, -> { eager_load(translations: :content) }
|
|
|
|
scope :sorted, -> { order(position: :asc) }
|
|
|
|
|
|
|
|
acts_as_list scope: :category, top_of_list: 0
|
|
|
|
|
2019-09-18 17:25:04 +00:00
|
|
|
# provide consistent naming with KB category
|
|
|
|
alias_attribute :parent, :category
|
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
alias assets_essential assets
|
|
|
|
|
|
|
|
def attributes_with_association_ids
|
|
|
|
key = "#{self.class}::aws::#{id}"
|
|
|
|
|
2021-05-31 13:05:54 +00:00
|
|
|
cache = Cache.read(key)
|
2019-06-04 03:40:48 +00:00
|
|
|
return cache if cache
|
|
|
|
|
|
|
|
attrs = super
|
|
|
|
|
|
|
|
attrs[:attachments] = attachments_sorted.map { |elem| self.class.attachment_to_hash(elem) }
|
2021-08-16 08:20:07 +00:00
|
|
|
attrs[:tags] = tag_list
|
2019-06-04 03:40:48 +00:00
|
|
|
|
|
|
|
Cache.write(key, attrs)
|
|
|
|
|
|
|
|
attrs
|
|
|
|
end
|
|
|
|
|
|
|
|
def assets(data = {})
|
|
|
|
return data if assets_added_to?(data)
|
|
|
|
|
|
|
|
data = super(data)
|
|
|
|
data = category.assets(data)
|
|
|
|
|
|
|
|
# include all siblings to make sure ordering is always up to date. Reader gets only accessible siblings.
|
|
|
|
siblings = category.answers
|
|
|
|
|
|
|
|
if !User.lookup(id: UserInfo.current_user_id)&.permissions?('knowledge_base.editor')
|
|
|
|
siblings = siblings.internal
|
|
|
|
end
|
|
|
|
|
|
|
|
data = ApplicationModel::CanAssets.reduce(siblings, data)
|
2019-06-28 11:38:49 +00:00
|
|
|
ApplicationModel::CanAssets.reduce(translations, data)
|
2019-06-04 03:40:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
attachments_cleanup!
|
|
|
|
|
|
|
|
def attachments_sorted
|
|
|
|
attachments.sort_by { |elem| elem.filename.downcase }
|
|
|
|
end
|
|
|
|
|
2020-02-19 16:41:25 +00:00
|
|
|
def add_attachment(file)
|
|
|
|
filename = file.try(:original_filename) || File.basename(file.path)
|
|
|
|
content_type = file.try(:content_type) || MIME::Types.type_for(filename).first&.content_type || 'application/octet-stream'
|
|
|
|
|
|
|
|
Store.add(
|
|
|
|
object: self.class.name,
|
|
|
|
o_id: id,
|
|
|
|
data: file.read,
|
|
|
|
filename: filename,
|
|
|
|
preferences: { 'Content-Type': content_type }
|
|
|
|
)
|
|
|
|
|
|
|
|
touch # rubocop:disable Rails/SkipsModelValidations
|
|
|
|
translations.each(&:touch)
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_attachment(attachment_id)
|
|
|
|
attachment = attachments.find { |elem| elem.id == attachment_id.to_i }
|
|
|
|
|
|
|
|
raise ActiveRecord::RecordNotFound if attachment.nil?
|
|
|
|
|
|
|
|
Store.remove_item(attachment.id)
|
|
|
|
|
|
|
|
touch # rubocop:disable Rails/SkipsModelValidations
|
|
|
|
translations.each(&:touch)
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
def api_url
|
|
|
|
Rails.application.routes.url_helpers.knowledge_base_answer_path(category.knowledge_base, self)
|
|
|
|
end
|
|
|
|
|
2020-04-20 09:47:45 +00:00
|
|
|
# required by CanCloneAttachments
|
|
|
|
def content_type
|
|
|
|
'text/html'
|
|
|
|
end
|
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def reordering_callback
|
|
|
|
return if !category_id_changed? && !position_changed?
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# drop siblings cache to make sure ordering is always up to date
|
2019-06-04 03:40:48 +00:00
|
|
|
category.answers.each(&:cache_delete)
|
|
|
|
end
|
|
|
|
before_save :reordering_callback
|
|
|
|
|
2021-08-16 08:20:07 +00:00
|
|
|
def touch_translations
|
|
|
|
translations.each(&:touch) # move to #touch_all when migrationg to Rails 6
|
|
|
|
end
|
|
|
|
after_touch :touch_translations
|
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
class << self
|
|
|
|
def attachment_to_hash(attachment)
|
|
|
|
url = Rails.application.routes.url_helpers.attachment_path(attachment.id)
|
|
|
|
|
|
|
|
{
|
|
|
|
id: attachment.id,
|
|
|
|
url: url,
|
2020-09-30 09:07:01 +00:00
|
|
|
preview_url: "#{url}?preview=1",
|
2019-06-04 03:40:48 +00:00
|
|
|
filename: attachment.filename,
|
|
|
|
size: attachment.size,
|
|
|
|
preferences: attachment.preferences
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|