Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

152 lines
4.8 KiB
Ruby
Raw Permalink Normal View History

2025-07-18 17:35:57 +01:00
# frozen_string_literal: true
#-- copyright
2020-01-15 11:31:26 +01:00
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
2011-05-30 20:52:25 +02:00
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
2011-05-30 20:52:25 +02:00
#
2013-09-16 17:59:31 +02:00
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
2021-01-13 17:47:45 +01:00
# Copyright (C) 2006-2013 Jean-Philippe Lang
2013-09-16 17:59:31 +02:00
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
class Message < ApplicationRecord
include OpenProject::Journal::AttachmentHelper
2019-03-12 10:34:45 +01:00
belongs_to :forum
has_one :project, through: :forum
2014-11-03 21:10:39 +01:00
belongs_to :author, class_name: "User"
2020-12-03 12:00:19 +01:00
acts_as_tree counter_cache: :replies_count, order: "#{Message.table_name}.created_at ASC"
2013-10-21 10:58:15 +02:00
acts_as_attachable after_add: :attachments_changed,
after_remove: :attachments_changed,
add_on_new_permission: :add_messages,
add_on_persisted_permission: :edit_messages
2014-11-03 21:10:39 +01:00
belongs_to :last_reply, class_name: "Message"
2011-05-30 20:52:25 +02:00
2013-11-27 11:48:21 +01:00
acts_as_journalized
2019-03-12 10:34:45 +01:00
acts_as_event title: Proc.new { |o| "#{o.forum.name}: #{o.subject}" },
2013-11-28 15:33:24 +01:00
description: :content,
2014-11-03 21:35:22 +01:00
type: Proc.new { |o| o.parent_id.nil? ? "message" : "reply" },
2013-11-28 15:33:24 +01:00
url: (Proc.new do |o|
msg = o
if msg.parent_id.nil?
2014-11-03 21:35:22 +01:00
{ id: msg.id }
2013-11-28 15:33:24 +01:00
else
2014-11-03 21:35:22 +01:00
{ id: msg.parent_id, r: msg.id, anchor: "message-#{msg.id}" }
2019-03-12 10:34:45 +01:00
end.reverse_merge controller: "/messages", action: "show", forum_id: msg.forum_id
2013-11-28 15:33:24 +01:00
end)
2014-11-03 21:10:39 +01:00
acts_as_searchable columns: ["subject", "content"],
2019-03-12 10:34:45 +01:00
include: { forum: :project },
references: [:forums],
2014-11-03 21:10:39 +01:00
project_key: "project_id",
2020-12-03 12:00:19 +01:00
date_column: "#{table_name}.created_at"
2008-07-27 17:54:09 +00:00
acts_as_watchable
2011-05-30 20:52:25 +02:00
2019-03-12 10:34:45 +01:00
validates :forum, :subject, :content, presence: true
2014-11-03 21:10:39 +01:00
validates :subject, length: { maximum: 255 }
2011-05-30 20:52:25 +02:00
before_save :set_sticked_on_date
after_create :add_author_as_watcher,
2021-08-23 15:25:47 +02:00
:update_last_reply_in_parent
2019-03-12 10:34:45 +01:00
after_update :update_ancestors, if: :saved_change_to_forum_id?
after_destroy :reset_counters
2011-05-30 20:52:25 +02:00
scope :visible, ->(*args) {
2019-03-12 10:34:45 +01:00
includes(forum: :project)
2016-08-24 14:10:11 +02:00
.references(:projects)
.merge(Project.allowed_to(args.first || User.current, :view_messages))
2014-11-03 21:35:22 +01:00
}
2011-05-30 20:52:25 +02:00
2014-11-03 21:35:22 +01:00
def visible?(user = User.current)
2023-10-31 10:35:36 +01:00
!user.nil? && user.allowed_in_project?(:view_messages, project)
end
2011-05-30 20:52:25 +02:00
2014-11-03 21:10:39 +01:00
validate :validate_unlocked_root, on: :create
# Can not reply to a locked topic
def validate_unlocked_root
errors.add :base, "Topic is locked" if root.locked? && self != root
2007-11-24 12:25:07 +00:00
end
2011-05-30 20:52:25 +02:00
def set_sticked_on_date
self.sticked_on = if sticky?
sticked_on.nil? ? Time.now : sticked_on
end
end
2020-03-02 14:05:11 +01:00
# TODO: move into create contract
def update_last_reply_in_parent
2007-05-13 17:09:56 +00:00
if parent
2020-03-02 14:05:11 +01:00
parent.reload
parent.update_attribute(:last_reply_id, id)
end
2020-03-02 14:05:11 +01:00
2019-03-12 10:34:45 +01:00
forum.reset_counters!
end
2011-05-30 20:52:25 +02:00
def reset_counters
2019-03-12 10:34:45 +01:00
forum.reset_counters!
2007-11-24 12:25:07 +00:00
end
2011-05-30 20:52:25 +02:00
def sticky=(arg)
write_attribute :sticky, (arg == true || arg.to_s == "1" ? 1 : 0)
end
2011-05-30 20:52:25 +02:00
2007-11-24 12:25:07 +00:00
def sticky?
sticky == 1
end
2011-05-30 20:52:25 +02:00
def editable_by?(usr)
2023-10-31 10:35:36 +01:00
usr && usr.logged? &&
(usr.allowed_in_project?(:edit_messages, project) || (author == usr && usr.allowed_in_project?(:edit_own_messages, project)))
end
def destroyable_by?(usr)
2023-10-31 10:35:36 +01:00
usr && usr.logged? &&
(usr.allowed_in_project?(:delete_messages,
project) || (author == usr && usr.allowed_in_project?(:delete_own_messages, project)))
end
2011-05-30 20:52:25 +02:00
private
2011-05-30 20:52:25 +02:00
def update_ancestors
with_id = Message.where(id: root.id)
with_parent_id = Message.where(parent_id: root.id)
with_id
.or(with_parent_id)
2019-03-12 10:34:45 +01:00
.update_all(forum_id:)
2019-03-12 10:34:45 +01:00
Forum.reset_counters!(forum_id_before_last_save)
Forum.reset_counters!(forum_id)
end
def add_author_as_watcher
2014-11-03 21:35:22 +01:00
Watcher.create(watchable: root, user: author)
# update watchers and watcher_users
2016-09-08 08:49:50 +02:00
watchers.reload
watcher_users.reload
end
2007-05-13 17:09:56 +00:00
end