2014-09-15 10:58:14 +08:00
# Redmine - project management software
# Copyright (C) 2006-2013 Jean-Philippe Lang
#
# 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.
class MessagesController < ApplicationController
include ApplicationHelper
2015-02-06 18:15:08 +08:00
before_filter :authorize1 , :only = > [ :show ]
2014-09-15 10:58:14 +08:00
menu_item :boards
default_search_scope :messages
before_filter :find_board , :only = > [ :new , :preview , :edit ]
before_filter :find_attachments , :only = > [ :preview ]
2015-12-23 15:35:26 +08:00
before_filter :find_message , :except = > [ :new , :preview , :join_org_subfield , :get_subfield_on_click_org , :join_org_subfields ]
2015-12-28 14:35:13 +08:00
before_filter :authorize , :except = > [ :preview , :edit , :destroy , :new , :join_org_subfield , :get_subfield_on_click_org , :join_org_subfields ]
2014-09-15 10:58:14 +08:00
helper :boards
helper :watchers
helper :attachments
include AttachmentsHelper
helper :project_score
2015-06-04 15:55:41 +08:00
include CoursesHelper
2014-09-15 10:58:14 +08:00
REPLIES_PER_PAGE = 25 unless const_defined? ( :REPLIES_PER_PAGE )
# Show a topic and its replies
def show
2016-10-25 14:47:05 +08:00
# 顶部导航
@project_menu_type = 3
2014-09-15 10:58:14 +08:00
@isReply = true
2016-09-23 16:44:49 +08:00
# page = params[:page]
# # Find the page of the requested reply
# if params[:r] && page.nil?
# offset = @topic.children.count(:conditions => ["#{Message.table_name}.id < ?", params[:r].to_i])
# page = 1 + offset / REPLIES_PER_PAGE
# end
2016-07-04 14:14:51 +08:00
all_comments = [ ]
2016-10-11 14:08:44 +08:00
@replies = Message . where ( " root_id = #{ @topic . id } " ) . reorder ( " created_on desc " )
2016-09-23 16:44:49 +08:00
@reply_count = @replies . count
2016-11-25 09:59:03 +08:00
@replies = get_no_children_comments_all @replies
@limit_count = @replies . count
2016-09-30 14:26:16 +08:00
@page = params [ :page ] ? params [ :page ] . to_i + 1 : 0
@limit = 10
@replies = @replies [ @page * @limit .. @page * @limit + 9 ]
2014-09-15 10:58:14 +08:00
@reply = Message . new ( :subject = > " RE: #{ @message . subject } " )
if @course
2016-09-23 16:44:49 +08:00
#@replies = @topic.children.
#includes(:author, :attachments, :praise_tread_cache, {:board => :project}).
#reorder("#{Message.table_name}.created_on DESC").
2015-03-26 16:17:33 +08:00
#limit(@reply_pages.per_page).
#offset(@reply_pages.offset).
2016-09-23 16:44:49 +08:00
#all
#@replies = paginateHelper messages_replies,10
2016-07-22 18:20:11 +08:00
@left_nav_type = 2
2016-09-30 14:26:16 +08:00
respond_to do | format |
format . js
format . html { render :layout = > 'base_courses' }
end
#render :action => "show", :layout => "base_courses"#by young
2015-12-30 15:46:01 +08:00
elsif @project
2016-09-23 16:44:49 +08:00
#@reply_pages = Paginator.new @reply_count, REPLIES_PER_PAGE, page
# @replies = @topic.children.
# includes(:author, :attachments, {:board => :project}).
# reorder("#{Message.table_name}.created_on DESC").
# limit(@reply_pages.per_page).
# offset(@reply_pages.offset).
# all
2016-09-30 14:26:16 +08:00
respond_to do | format |
format . js
format . html { render :layout = > 'base_projects' }
end
2016-12-30 16:37:20 +08:00
elsif @contest
@left_nav_type = 4
respond_to do | format |
format . js
format . html { render :layout = > 'base_contests' }
end
#render :action => "show", :layout => "base_courses"#by young
2015-12-30 15:46:01 +08:00
else
2016-09-23 16:44:49 +08:00
# @reply_pages = Paginator.new @reply_count, REPLIES_PER_PAGE, page
# @replies = @topic.children.
# includes(:author, :attachments, {:board => :project}).
# reorder("#{Message.table_name}.created_on DESC").
# limit(@reply_pages.per_page).
# offset(@reply_pages.offset).
# all
2015-12-30 15:46:01 +08:00
@organization = @org_subfield . organization
2016-09-30 14:26:16 +08:00
respond_to do | format |
format . js
2016-12-30 16:37:20 +08:00
format . html { render :layout = > 'base_org' }
2016-09-30 14:26:16 +08:00
end
2014-09-15 10:58:14 +08:00
end
end
# Create a new topic
def new
2015-09-06 17:27:20 +08:00
if User . current . logged?
@message = Message . new
@message . author = User . current
@message . board = @board
@message . safe_attributes = params [ :message ]
if request . post?
2016-09-18 15:44:27 +08:00
if @project
is_public = @project . is_public
elsif @course
is_public = @course . is_public
2016-12-30 16:37:20 +08:00
elsif @contest
is_public = @contest . is_public
2016-09-18 15:44:27 +08:00
end
# 公开项目/课程上传的资源是公开的,私有项目上传的是私有的
@message . save_attachments_containers ( params [ :attachments ] , User . current , is_public )
2015-09-06 17:27:20 +08:00
if @message . save
# 更新kindeditor上传的图片资源所有者
if params [ :asset_id ]
ids = params [ :asset_id ] . split ( ',' )
update_kindeditor_assets_owner ids , @message . id , OwnerTypeHelper :: MESSAGE
end
2015-06-04 15:55:41 +08:00
2015-09-06 17:27:20 +08:00
call_hook ( :controller_messages_new_after_save , { :params = > params , :message = > @message } )
render_attachment_warning_if_needed ( @message )
if params [ :is_board ]
if @project
redirect_to project_boards_path ( @project )
elsif @course
2016-10-14 14:28:19 +08:00
redirect_to course_boards_path ( @course , :board_id = > @board . id )
2016-12-30 16:37:20 +08:00
elsif @contest
redirect_to contest_boards_path ( @contest , :board_id = > @board . id )
2015-09-06 17:27:20 +08:00
end
else
redirect_to board_message_url ( @board , @message )
2015-05-11 17:23:53 +08:00
end
else
2015-09-06 17:27:20 +08:00
if params [ :is_board ]
if @project
redirect_to project_boards_path ( @project , :flag = > true )
elsif @course
2016-10-14 14:28:19 +08:00
redirect_to course_boards_path ( @course , :board_id = > @board . id , :flag = > true )
2016-12-30 16:37:20 +08:00
elsif @contest
redirect_to contest_boards_path ( @contest , :board_id = > @board . id , :flag = > true )
2015-09-06 17:27:20 +08:00
end
else
2016-12-30 16:37:20 +08:00
layout_file = @project ? 'base_projects' : ( @course ? 'base_courses' : 'base_contests' )
2015-09-06 17:27:20 +08:00
render :action = > 'new' , :layout = > layout_file
end
2015-05-11 17:23:53 +08:00
end
2014-09-15 10:58:14 +08:00
else
2016-07-22 15:32:37 +08:00
@left_nav_type = 2
2015-09-06 17:27:20 +08:00
respond_to do | format |
format . html {
2016-12-30 16:37:20 +08:00
layout_file = @project ? 'base_projects' : ( @course ? 'base_courses' : 'base_contests' )
2015-09-06 17:27:20 +08:00
render :layout = > layout_file
}
2015-05-11 17:35:36 +08:00
end
2014-09-15 10:58:14 +08:00
end
2015-03-24 17:12:51 +08:00
else
2015-09-06 17:27:20 +08:00
redirect_to signin_path
2014-09-15 10:58:14 +08:00
end
end
# Reply to a topic
def reply
2016-07-04 14:14:51 +08:00
if params [ :parent_id ]
parent = Message . find params [ :parent_id ]
2016-07-20 17:00:18 +08:00
@topic = params [ :activity_id ] . nil? ? parent : Message . find ( params [ :activity_id ] . to_i )
2016-07-04 14:14:51 +08:00
@reply = Message . new
@reply . author = User . current
@reply . board = parent . board
@reply . content = params [ :content ]
2016-07-20 17:00:18 +08:00
@reply . subject = " RE: #{ @topic . subject } "
@reply . reply_id = params [ :id ]
2016-10-11 14:08:44 +08:00
@reply . root_id = parent . root_id . nil? ? parent . id : parent . root_id
2016-07-04 14:14:51 +08:00
# @reply.reply_id = params[:id]
parent . children << @reply
else
@quote = params [ :quote ] [ :quote ]
@reply = Message . new
@reply . author = User . current
@reply . board = @board
@reply . safe_attributes = params [ :reply ]
@reply . content = @quote + @reply . content
@reply . subject = " RE: #{ @topic . subject } " unless params [ :reply ] [ :subject ]
2016-10-11 14:08:44 +08:00
@reply . root_id = @topic . id
2016-07-04 14:14:51 +08:00
@topic . children << @reply
# @reply.reply_id = params[:id]
2014-09-15 10:58:14 +08:00
end
2016-07-04 14:14:51 +08:00
2016-01-12 15:53:15 +08:00
update_course_activity ( @topic . class , @topic . id )
2016-12-30 16:37:20 +08:00
update_contest_activity ( @topic . class , @topic . id )
2016-01-12 15:53:15 +08:00
update_user_activity ( @topic . class , @topic . id )
update_forge_activity ( @topic . class , @topic . id )
update_org_activity ( @topic . class , @topic . id )
2014-09-15 10:58:14 +08:00
#@topic.update_attribute(:updated_on, Time.now)
if ! @reply . new_record?
2015-04-03 16:35:46 +08:00
if params [ :asset_id ]
2015-05-08 15:02:04 +08:00
ids = params [ :asset_id ] . split ( ',' )
update_kindeditor_assets_owner ids , @reply . id , OwnerTypeHelper :: MESSAGE
2015-04-03 16:35:46 +08:00
end
2015-06-04 15:55:41 +08:00
2014-09-15 10:58:14 +08:00
call_hook ( :controller_messages_reply_after_save , { :params = > params , :message = > @reply } )
attachments = Attachment . attach_files ( @reply , params [ :attachments ] )
render_attachment_warning_if_needed ( @reply )
2015-05-05 14:32:40 +08:00
else
2015-05-08 15:02:04 +08:00
#render file: 'messages#show', layout: 'base_courses'
2014-09-15 10:58:14 +08:00
end
2015-09-01 14:18:44 +08:00
if params [ :user_activity_id ]
@user_activity_id = params [ :user_activity_id ]
2016-11-25 09:59:03 +08:00
@is_course = params [ :is_course ] if params [ :is_course ]
@is_board = params [ :is_board ] if params [ :is_board ]
2015-09-01 14:18:44 +08:00
respond_to do | format |
format . js
end
2016-07-04 14:14:51 +08:00
return
2015-05-11 17:23:53 +08:00
else
2016-07-04 14:14:51 +08:00
redirect_to board_message_url ( @board , @topic )
return
2015-05-11 17:23:53 +08:00
end
2015-05-08 15:02:04 +08:00
2014-09-15 10:58:14 +08:00
end
# Edit a message
def edit
@isReply = false
2015-05-12 17:14:07 +08:00
if @project
( render_403 ; return false ) unless @message . editable_by? ( User . current )
2015-12-30 17:45:15 +08:00
elsif @course
2015-05-12 17:14:07 +08:00
( render_403 ; return false ) unless @message . course_editable_by? ( User . current )
2016-12-30 16:37:20 +08:00
elsif @contest
( render_403 ; return false ) unless @message . contest_editable_by? ( User . current )
2015-12-30 17:45:15 +08:00
else
( render_403 ; return false ) unless @message . org_subfield_editable_by? ( User . current )
2014-09-15 10:58:14 +08:00
end
2015-05-11 17:35:36 +08:00
2014-09-15 10:58:14 +08:00
@message . safe_attributes = params [ :message ]
if request . post? && @message . save
attachments = Attachment . attach_files ( @message , params [ :attachments ] )
render_attachment_warning_if_needed ( @message )
2015-12-31 17:53:05 +08:00
#flash[:notice] = l(:notice_successful_update)
2014-09-15 10:58:14 +08:00
@message . reload
2015-12-31 17:53:05 +08:00
if params [ :is_course ] && params [ :is_course ] . to_i == 0
redirect_to user_activities_path ( User . current . id )
elsif params [ :is_course ] && params [ :is_course ] . to_i == 1 && params [ :is_board ] && params [ :is_board ] . to_i == 0
2015-05-11 17:23:53 +08:00
if @project
2015-12-31 17:53:05 +08:00
redirect_to project_path ( @project )
2015-05-11 17:23:53 +08:00
elsif @course
2015-12-31 17:53:05 +08:00
redirect_to course_activity_path ( @course )
2016-12-30 16:37:20 +08:00
elsif @contest
redirect_to contest_activities_contest_path ( @contest )
2015-05-11 17:23:53 +08:00
end
2015-12-31 17:53:05 +08:00
elsif params [ :is_course ] && params [ :is_course ] . to_i == 1 && params [ :is_board ] && params [ :is_board ] . to_i == 1
2015-05-13 14:31:54 +08:00
if @project
redirect_to project_boards_path ( @project )
elsif @course
redirect_to course_boards_path ( @course )
2016-12-30 16:37:20 +08:00
elsif @contest
redirect_to contest_boards_path ( @contest )
2015-05-13 14:31:54 +08:00
end
else
2015-12-31 17:53:05 +08:00
redirect_to board_message_url ( @message . board , @message . root , :r = > ( @message . parent_id && @message . id ) )
end
elsif request . get? || request . post?
if params [ :is_course ] && params [ :is_board ]
@is_course = params [ :is_course ]
@is_board = params [ :is_board ]
end
respond_to do | format |
format . html {
if @project
layout_file = 'base_projects'
elsif @course
2016-07-22 15:32:37 +08:00
@left_nav_type = 2
2015-12-31 17:53:05 +08:00
layout_file = 'base_courses'
2016-12-30 16:37:20 +08:00
elsif @contest
@left_nav_type = 4
layout_file = 'base_contests'
2015-12-31 17:53:05 +08:00
elsif @org_subfield
@organization = @org_subfield . organization
layout_file = 'base_org'
end
render :layout = > layout_file
}
2014-09-15 10:58:14 +08:00
end
end
end
# Delete a messages
def destroy
2016-07-04 14:14:51 +08:00
if params [ :user_activity_id ]
@message . destroy
@topic = Message . find ( params [ :activity_id ] . to_i )
@user_activity_id = params [ :user_activity_id ]
2016-11-25 09:59:03 +08:00
@is_course = params [ :is_course ] if params [ :is_course ]
@is_board = params [ :is_board ] if params [ :is_board ]
2016-07-04 14:14:51 +08:00
respond_to do | format |
format . js
end
return
end
2014-09-15 10:58:14 +08:00
if @project
( render_403 ; return false ) unless @message . destroyable_by? ( User . current )
2015-12-30 18:44:54 +08:00
elsif @course
2014-09-15 10:58:14 +08:00
( render_403 ; return false ) unless @message . course_destroyable_by? ( User . current )
2016-12-30 16:37:20 +08:00
elsif @contest
( render_403 ; return false ) unless @message . contest_destroyable_by? ( User . current )
2015-12-30 18:44:54 +08:00
else
( render_403 ; return false ) unless @message . org_subfield_editable_by? ( User . current )
2014-09-15 10:58:14 +08:00
end
r = @message . to_param
@message . destroy
# modify by nwb
if @project
2015-12-31 17:53:05 +08:00
if params [ :is_course ] && params [ :is_course ] . to_i == 0
redirect_to user_activities_path ( User . current . id )
elsif params [ :is_course ] && params [ :is_course ] . to_i == 1 && params [ :is_board ] && params [ :is_board ] . to_i == 0
redirect_to project_path ( @project )
elsif params [ :is_course ] && params [ :is_course ] . to_i == 1 && params [ :is_board ] && params [ :is_board ] . to_i == 1
redirect_to project_boards_path ( @project )
elsif @message . parent
redirect_to board_message_url ( @board , @message . parent , :r = > r )
2015-05-11 17:23:53 +08:00
else
2015-12-31 17:53:05 +08:00
redirect_to project_board_url ( @project , @board )
2014-09-15 10:58:14 +08:00
end
elsif @course
2015-12-31 17:53:05 +08:00
if params [ :is_course ] && params [ :is_course ] . to_i == 0
redirect_to user_activities_path ( User . current . id )
elsif params [ :is_course ] && params [ :is_course ] . to_i == 1 && params [ :is_board ] && params [ :is_board ] . to_i == 0
redirect_to course_activity_path ( @course )
elsif params [ :is_course ] && params [ :is_course ] . to_i == 1 && params [ :is_board ] && params [ :is_board ] . to_i == 1
redirect_to course_boards_path ( @course )
elsif @message . parent
redirect_to board_message_url ( @board , @message . parent , :r = > r )
2014-09-15 10:58:14 +08:00
else
2017-01-06 18:59:11 +08:00
redirect_to course_boards_path ( @course , :board_id = > @board . id )
2014-09-15 10:58:14 +08:00
end
2016-12-30 16:37:20 +08:00
elsif @contest
if params [ :is_course ] && params [ :is_course ] . to_i == 0
redirect_to user_contest_community_path ( User . current . id )
elsif params [ :is_course ] && params [ :is_course ] . to_i == 1 && params [ :is_board ] && params [ :is_board ] . to_i == 0
redirect_to contest_activities_contest_path ( @contest )
elsif params [ :is_course ] && params [ :is_course ] . to_i == 1 && params [ :is_board ] && params [ :is_board ] . to_i == 1
redirect_to contest_boards_path ( @contest )
elsif @message . parent
redirect_to board_message_url ( @board , @message . parent , :r = > r )
else
2017-01-06 18:59:11 +08:00
redirect_to contest_boards_path ( @contest , :board_id = > @board . id )
2016-12-30 16:37:20 +08:00
end
2015-12-30 15:46:01 +08:00
elsif @org_subfield
if params [ :is_board ]
redirect_to organization_path ( :id = > @org_subfield . organization_id , :org_subfield_id = > @org_subfield . id )
else
if @message . parent
redirect_to board_message_url ( @board , @message . parent , :r = > r )
else
redirect_to organization_path ( :id = > @org_subfield . organization_id , :org_subfield_id = > @org_subfield . id )
end
end
2014-09-15 10:58:14 +08:00
end
end
def quote
2016-07-04 14:14:51 +08:00
# @subject = @message.subject
# @subject = "RE: #{@subject}" unless @subject.starts_with?('RE:')
#
# @content = "> #{ll(Setting.default_language, :text_user_wrote, @message.author)}\n> "
# @temp = Message.new
# @temp.content = "<blockquote>#{ll(Setting.default_language, :text_user_wrote, @message.author.show_name)} <br/>#{@message.content.html_safe}</blockquote>".html_safe
2014-09-15 10:58:14 +08:00
end
def preview
message = @board . messages . find_by_id ( params [ :id ] )
@text = ( params [ :message ] || params [ :reply ] ) [ :content ]
@previewed = message
render :partial = > 'common/preview'
end
2015-12-23 15:35:26 +08:00
def join_org_subfield
@message = Message . find ( params [ :message_id ] )
@organizations = User . current . organizations
end
def get_subfield_on_click_org
@org = Organization . find ( params [ :organization_id ] )
end
def join_org_subfields
org_subfield_ids = params [ :org_subfields ]
@message = Message . find ( params [ :id ] )
2015-12-23 16:49:05 +08:00
# @message.update_attribute(:updated_on, Time.now)
2015-12-30 15:46:01 +08:00
# type = @message.board.course_id.nil? ? "Project":"Course"
2015-12-23 15:35:26 +08:00
org_subfield_ids . each do | field_id |
2015-12-29 15:49:24 +08:00
@message . quotes = @message . quotes . nil? ? 1 : ( @message . quotes + 1 )
@message . save
2015-12-30 15:46:01 +08:00
board = OrgSubfield . find ( field_id ) . boards . first
mes = Message . create ( :board_id = > board . id , :subject = > @message . subject , :content = > @message . content , :author_id = > User . current . id ,
2015-12-29 15:49:24 +08:00
:created_on = > Time . now , :updated_on = > Time . now , :locked = > @message . locked , :sticky = > @message . sticky )
@message . attachments . each do | attach |
mes . attachments << Attachment . new ( :filename = > attach . filename , :disk_filename = > attach . disk_filename , :filesize = > attach . filesize , :content_type = > attach . content_type , :digest = > attach . digest ,
:downloads = > 0 , :author_id = > User . current . id , :created_on = > Time . now , :description = > attach . description , :disk_directory = > attach . disk_directory , :attachtype = > attach . attachtype ,
:is_public = > attach . is_public , :quotes = > 0 )
2015-12-23 16:49:05 +08:00
end
2015-12-30 15:46:01 +08:00
# OrgSubfieldMessage.create(:org_subfield_id => field_id.to_i, :message_id => mes.id, :message_type => type)
2015-12-29 15:49:24 +08:00
# org_acts = OrgActivity.where("container_type='OrgSubfield' and container_id=? and org_act_type='Message' and org_act_id=?", field_id.to_i, @message.id)
# if org_acts.all.size() > 0
# org_acts.first.update_attribute(:updated_at, Time.now)
# else
OrgActivity . create ( :container_type = > 'OrgSubfield' , :container_id = > field_id . to_i , :org_act_type = > 'Message' , :org_act_id = > mes . id , :user_id = > User . current . id )
# end
2015-12-23 15:35:26 +08:00
end
end
2015-05-08 15:02:04 +08:00
private
2014-09-15 10:58:14 +08:00
def find_message
return unless find_board
@message = @board . messages . find ( params [ :id ] , :include = > :parent )
@topic = @message . root
rescue ActiveRecord :: RecordNotFound
render_404
end
def find_board
#modify by nwb
@board = Board . find ( params [ :board_id ] )
if @board . project_id != - 1 && @board . project_id != nil
@project = @board . project
2016-12-30 16:37:20 +08:00
elsif @board . course_id && @board . course_id != - 1
2014-09-15 10:58:14 +08:00
@course = @board . course
2016-12-30 16:37:20 +08:00
elsif @board . contest_id
@contest = @board . contest
2015-12-30 15:46:01 +08:00
else
@org_subfield = @board . org_subfield
@organization = @org_subfield
2014-09-15 10:58:14 +08:00
end
rescue ActiveRecord :: RecordNotFound
render_404
nil
end
end