|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +#-- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2013 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See COPYRIGHT and LICENSE files for more details. |
| 29 | +#++ |
| 30 | + |
| 31 | +class WorkPackages::ActivitiesTab::RestrictedMentionsSanitizer |
| 32 | + def self.sanitize(work_package, notes) |
| 33 | + new(work_package, notes).call |
| 34 | + end |
| 35 | + |
| 36 | + def initialize(work_package, notes) |
| 37 | + @work_package = work_package |
| 38 | + @notes = notes |
| 39 | + end |
| 40 | + |
| 41 | + def call |
| 42 | + return "" if notes.blank? |
| 43 | + |
| 44 | + convert_unmentionable_principals_to_plain_text |
| 45 | + CGI.unescapeHTML(parser.to_html) |
| 46 | + end |
| 47 | + |
| 48 | + private |
| 49 | + |
| 50 | + attr_reader :work_package, :notes |
| 51 | + |
| 52 | + def convert_unmentionable_principals_to_plain_text |
| 53 | + mentionable_principals_ids = mentionable_principals.pluck(:id) |
| 54 | + |
| 55 | + parser.css("mention").each do |mention| |
| 56 | + unless mentionable_principals_ids.include?(mention["data-id"].to_i) |
| 57 | + mention.replace(mention.content) |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + def parser |
| 63 | + @parser ||= Nokogiri::HTML.fragment(notes) |
| 64 | + end |
| 65 | + |
| 66 | + def mentionable_principals |
| 67 | + @mentionable_principals ||= Queries::Principals::PrincipalQuery.new(user: User.current) |
| 68 | + .where(:restricted_mentionable_on_work_package, "=", [work_package.id]) |
| 69 | + .where(:status, "!", [Principal.statuses[:locked]]) |
| 70 | + .where(:type, "=", %w[User Group]) |
| 71 | + .results |
| 72 | + end |
| 73 | +end |
0 commit comments