2019-03-16 09:37:35 +00:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 01:32:57 +00:00
|
|
|
|
2008-09-28 07:54:41 +00:00
|
|
|
# Redmine - project management software
|
2023-01-01 06:19:35 +00:00
|
|
|
# Copyright (C) 2006-2023 Jean-Philippe Lang
|
2007-03-12 17:59:02 +00:00
|
|
|
#
|
|
|
|
|
# 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.
|
2011-05-17 03:58:19 +00:00
|
|
|
#
|
2007-03-12 17:59:02 +00:00
|
|
|
# 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.
|
2011-05-17 03:58:19 +00:00
|
|
|
#
|
2007-03-12 17:59:02 +00:00
|
|
|
# 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.
|
|
|
|
|
|
2024-01-23 11:52:38 +00:00
|
|
|
class News < ApplicationRecord
|
2012-03-06 19:50:10 +00:00
|
|
|
include Redmine::SafeAttributes
|
2007-03-12 17:59:02 +00:00
|
|
|
belongs_to :project
|
2014-10-22 18:41:03 +00:00
|
|
|
belongs_to :author, :class_name => 'User'
|
2014-10-22 17:37:16 +00:00
|
|
|
has_many :comments, lambda {order("created_on")}, :as => :commented, :dependent => :delete_all
|
2011-05-17 03:58:19 +00:00
|
|
|
|
2007-03-12 17:59:02 +00:00
|
|
|
validates_presence_of :title, :description
|
2007-07-16 17:16:49 +00:00
|
|
|
validates_length_of :title, :maximum => 60
|
|
|
|
|
validates_length_of :summary, :maximum => 255
|
2007-08-29 16:52:35 +00:00
|
|
|
|
2014-11-29 13:41:53 +00:00
|
|
|
acts_as_attachable :edit_permission => :manage_news,
|
|
|
|
|
:delete_permission => :manage_news
|
2014-10-22 17:37:16 +00:00
|
|
|
acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"],
|
2014-12-12 20:49:31 +00:00
|
|
|
:preload => :project
|
2007-08-29 16:52:35 +00:00
|
|
|
acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}}
|
2020-11-18 16:16:22 +00:00
|
|
|
acts_as_activity_provider :scope => proc {preload(:project, :author)},
|
2008-11-30 11:18:22 +00:00
|
|
|
:author_key => :author_id
|
2011-03-04 14:32:58 +00:00
|
|
|
acts_as_watchable
|
2011-05-17 03:58:19 +00:00
|
|
|
|
2011-03-04 14:32:58 +00:00
|
|
|
after_create :add_author_as_watcher
|
2018-10-10 17:13:09 +00:00
|
|
|
after_create_commit :send_notification
|
2011-05-17 03:58:19 +00:00
|
|
|
|
2020-11-10 11:10:09 +00:00
|
|
|
scope :visible, (lambda do |*args|
|
2014-10-22 17:37:16 +00:00
|
|
|
joins(:project).
|
|
|
|
|
where(Project.allowed_to_condition(args.shift || User.current, :view_news, *args))
|
2020-11-10 11:10:09 +00:00
|
|
|
end)
|
2011-05-17 03:58:19 +00:00
|
|
|
|
2012-03-06 19:50:10 +00:00
|
|
|
safe_attributes 'title', 'summary', 'description'
|
|
|
|
|
|
2009-12-13 14:26:54 +00:00
|
|
|
def visible?(user=User.current)
|
|
|
|
|
!user.nil? && user.allowed_to?(:view_news, project)
|
|
|
|
|
end
|
2011-05-17 03:58:19 +00:00
|
|
|
|
2012-02-11 17:14:56 +00:00
|
|
|
# Returns true if the news can be commented by user
|
|
|
|
|
def commentable?(user=User.current)
|
|
|
|
|
user.allowed_to?(:comment_news, project)
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-17 14:14:12 +00:00
|
|
|
def notified_users
|
|
|
|
|
project.users.select {|user| user.notify_about?(self) && user.allowed_to?(:view_news, project)}
|
|
|
|
|
end
|
|
|
|
|
|
2013-01-26 18:07:57 +00:00
|
|
|
def recipients
|
2015-01-17 14:14:12 +00:00
|
|
|
notified_users.map(&:mail)
|
2013-01-26 18:07:57 +00:00
|
|
|
end
|
|
|
|
|
|
2015-01-17 14:14:12 +00:00
|
|
|
# Returns the users that should be cc'd when a new news is added
|
|
|
|
|
def notified_watchers_for_added_news
|
|
|
|
|
watchers = []
|
2014-02-09 11:54:21 +00:00
|
|
|
if m = project.enabled_module('news')
|
2015-01-17 14:14:12 +00:00
|
|
|
watchers = m.notified_watchers
|
2014-02-09 11:54:21 +00:00
|
|
|
unless project.is_public?
|
2015-01-17 14:14:12 +00:00
|
|
|
watchers = watchers.select {|user| project.users.include?(user)}
|
2014-02-09 11:54:21 +00:00
|
|
|
end
|
|
|
|
|
end
|
2015-01-17 14:14:12 +00:00
|
|
|
watchers
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Returns the email addresses that should be cc'd when a new news is added
|
|
|
|
|
def cc_for_added_news
|
|
|
|
|
notified_watchers_for_added_news.map(&:mail)
|
2014-02-09 11:54:21 +00:00
|
|
|
end
|
|
|
|
|
|
2007-03-12 17:59:02 +00:00
|
|
|
# returns latest news for projects visible by user
|
2008-09-28 07:54:41 +00:00
|
|
|
def self.latest(user = User.current, count = 5)
|
2014-11-06 18:54:23 +00:00
|
|
|
visible(user).preload(:author, :project).order("#{News.table_name}.created_on DESC").limit(count).to_a
|
2006-07-29 19:54:22 +00:00
|
|
|
end
|
2011-05-17 03:58:19 +00:00
|
|
|
|
2011-03-04 14:32:58 +00:00
|
|
|
private
|
2011-05-17 03:58:19 +00:00
|
|
|
|
2011-03-04 14:32:58 +00:00
|
|
|
def add_author_as_watcher
|
|
|
|
|
Watcher.create(:watchable => self, :user => author)
|
|
|
|
|
end
|
2013-07-14 14:26:27 +00:00
|
|
|
|
|
|
|
|
def send_notification
|
|
|
|
|
if Setting.notified_events.include?('news_added')
|
2018-10-10 17:13:09 +00:00
|
|
|
Mailer.deliver_news_added(self)
|
2013-07-14 14:26:27 +00:00
|
|
|
end
|
|
|
|
|
end
|
2006-06-28 18:11:03 +00:00
|
|
|
end
|