From 0222038899c77296b8e89d8a730769726dd6d17b Mon Sep 17 00:00:00 2001 From: cxt Date: Wed, 27 Apr 2016 10:28:36 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=BA=E9=99=B7=E6=A8=A1=E6=9D=BF=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=94=B9=E4=B8=BA=E5=AF=B9=E9=A1=B9=E7=9B=AE=E6=89=80?= =?UTF-8?q?=E6=9C=89=E4=BA=BA=E5=8F=91=E9=80=81=E3=80=81=E5=8F=AF=E5=B1=8F?= =?UTF-8?q?=E8=94=BD=E5=BE=AE=E4=BF=A1=E6=A8=A1=E6=9D=BF=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1234567 | 2 +- .../shield_wechat_messages.js.coffee | 3 ++ .../shield_wechat_messages.css.scss | 3 ++ .../shield_wechat_messages_controller.rb | 36 +++++++++++++++++++ app/helpers/shield_wechat_messages_helper.rb | 2 ++ app/models/comment.rb | 6 ++-- app/models/homework_common.rb | 12 ++++--- app/models/issue.rb | 11 ++++-- app/models/journals_for_message.rb | 2 +- app/models/message.rb | 30 +++++++++------- app/models/news.rb | 8 +++-- app/models/shield_wechat_message.rb | 3 ++ app/views/layouts/_user_courses.html.erb | 13 +++++-- app/views/layouts/_user_projects.html.erb | 8 +++++ app/views/layouts/new_base_user.html.erb | 4 +-- .../shield_wechat_messages/create.js.erb | 6 ++++ .../show_messages.js.erb | 6 ++++ config/routes.rb | 6 +++- ...426084709_create_shield_wechat_messages.rb | 12 +++++++ db/schema.rb | 25 +++++++++---- public/javascripts/wechat/app.js | 2 +- public/stylesheets/share.css | 2 +- .../shield_wechat_messages_controller_spec.rb | 5 +++ spec/factories/shield_wechat_messages.rb | 8 +++++ spec/models/shield_wechat_message_spec.rb | 5 +++ 25 files changed, 181 insertions(+), 39 deletions(-) create mode 100644 app/assets/javascripts/shield_wechat_messages.js.coffee create mode 100644 app/assets/stylesheets/shield_wechat_messages.css.scss create mode 100644 app/controllers/shield_wechat_messages_controller.rb create mode 100644 app/helpers/shield_wechat_messages_helper.rb create mode 100644 app/models/shield_wechat_message.rb create mode 100644 app/views/shield_wechat_messages/create.js.erb create mode 100644 app/views/shield_wechat_messages/show_messages.js.erb create mode 100644 db/migrate/20160426084709_create_shield_wechat_messages.rb create mode 100644 spec/controllers/shield_wechat_messages_controller_spec.rb create mode 100644 spec/factories/shield_wechat_messages.rb create mode 100644 spec/models/shield_wechat_message_spec.rb diff --git a/1234567 b/1234567 index 327c92785..c7ff2f81d 100644 --- a/1234567 +++ b/1234567 @@ -1 +1 @@ -{"access_token":"x7GUTe-MLoVPMzId82-3VSdCsO6sq5cOJZCmIN-yUjDcohfI00t2lzPkNKdRhETLUBptZRQ--v-IBQTG-o5iRZZv-EelOBX7K96DcPMFt9rdzwNu7XerNnZw_ncGYncSLHXeACAQMC","expires_in":7200,"got_token_at":1461133885} \ No newline at end of file +{"access_token":"nwsA--Idg9Wk4Kyuybly8XuI8MWx-NrJbPTzZX2ynbGaVHBnXZgJ4DHkwlJK6dGfYOmeMNmRj6PSXEzs1GS6vAy0e_7_6S34-S44wtGK83dv6HCUQ8qh3vAw12QUi39fUMDhCIAFXO","expires_in":7200,"got_token_at":1461723935} \ No newline at end of file diff --git a/app/assets/javascripts/shield_wechat_messages.js.coffee b/app/assets/javascripts/shield_wechat_messages.js.coffee new file mode 100644 index 000000000..761567942 --- /dev/null +++ b/app/assets/javascripts/shield_wechat_messages.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/assets/stylesheets/shield_wechat_messages.css.scss b/app/assets/stylesheets/shield_wechat_messages.css.scss new file mode 100644 index 000000000..770d71351 --- /dev/null +++ b/app/assets/stylesheets/shield_wechat_messages.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the ShieldWechatMessages controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/shield_wechat_messages_controller.rb b/app/controllers/shield_wechat_messages_controller.rb new file mode 100644 index 000000000..f2e9e4ff2 --- /dev/null +++ b/app/controllers/shield_wechat_messages_controller.rb @@ -0,0 +1,36 @@ +class ShieldWechatMessagesController < ApplicationController + def create + if params[:user_id] + @user = User.find params[:user_id] + if params[:project_id] + ShieldWechatMessage.create(:container_type => 'User', :container_id => params[:user_id].to_i, :shield_type => 'Project', :shield_id => params[:project_id].to_i) + #@cp = 1 + #@projects = @user.projects.visible.select("projects.*, (SELECT MAX(updated_at) FROM `forge_activities` WHERE forge_activities.project_id = projects.id) AS a").order("a desc").limit(5) + elsif params[:course_id] + ShieldWechatMessage.create(:container_type => 'User', :container_id => params[:user_id].to_i, :shield_type => 'Course', :shield_id => params[:course_id].to_i) + #@cp = 0 + #@courses = @user.courses.visible.where("is_delete =?", 0).select("courses.*,(SELECT MAX(updated_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS a").order("a desc").limit(5) + end + end + end + + def show_messages + if params[:user_id] + @user = User.find params[:user_id] + if params[:project_id] + ShieldWechatMessage.where("container_type='User' and container_id=#{params[:user_id].to_i} and shield_type='Project' and shield_id=#{params[:project_id]}").each do |act| + act.destroy + end + #@cp = 1 + #@projects = @user.projects.visible.select("projects.*, (SELECT MAX(updated_at) FROM `forge_activities` WHERE forge_activities.project_id = projects.id) AS a").order("a desc").limit(5) + # ShieldActivity.create(:container_type => 'Organization', :container_id => params[:org_id].to_i, :shield_type => 'Project', :shield_id => params[:project_id].to_i) + elsif params[:course_id] + ShieldWechatMessage.where("container_type='User' and container_id=#{params[:user_id].to_i} and shield_type='Course' and shield_id=#{params[:course_id]}").each do |act| + act.destroy + end + #@cp = 0 + #@courses = @user.courses.visible.where("is_delete =?", 0).select("courses.*,(SELECT MAX(updated_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS a").order("a desc").limit(5) + end + end + end +end diff --git a/app/helpers/shield_wechat_messages_helper.rb b/app/helpers/shield_wechat_messages_helper.rb new file mode 100644 index 000000000..0c605db1b --- /dev/null +++ b/app/helpers/shield_wechat_messages_helper.rb @@ -0,0 +1,2 @@ +module ShieldWechatMessagesHelper +end diff --git a/app/models/comment.rb b/app/models/comment.rb index 6b1732eab..341a179ce 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -45,8 +45,10 @@ class Comment < ActiveRecord::Base if self.commented.course if self.author_id != self.commented.author_id self.course_messages << CourseMessage.new(:user_id => self.commented.author_id, :course_id => self.commented.course.id, :viewed => false) - content = strip_html self.comments.html_safe, 200 - ws.comment_template self.commented.author_id, "course_notice", self.id, "#{l(:label_notice_comment_template)}", self.author.try(:realname), format_time(self.created_on), content + if ShieldWechatMessage.where("container_type='User' and container_id=#{self.commented.author_id} and shield_type='Course' and shield_id=#{self.commented.course.id}").count == 0 + content = strip_html self.comments.html_safe, 200 + ws.comment_template self.commented.author_id, "course_notice", self.id, "#{l(:label_notice_comment_template)}", self.author.try(:realname), format_time(self.created_on), content + end end else # 项目相关 if self.author_id != self.commented.author_id diff --git a/app/models/homework_common.rb b/app/models/homework_common.rb index a2216bdf9..b47008393 100644 --- a/app/models/homework_common.rb +++ b/app/models/homework_common.rb @@ -59,8 +59,10 @@ class HomeworkCommon < ActiveRecord::Base self.course.members.each do |m| # if m.user_id != self.user_id self.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => self.course_id, :viewed => false) - ws = WechatService.new - ws.homework_template(m.user_id, "homework", self.id, "#{l(:label_new_homework_template)}", self.course.name, self.name.html_safe, self.end_time.to_s + " 23:59:59") + if ShieldWechatMessage.where("container_type='User' and container_id=#{m.user_id} and shield_type='Course' and shield_id=#{self.course_id}").count == 0 + ws = WechatService.new + ws.homework_template(m.user_id, "homework", self.id, "#{l(:label_new_homework_template)}", self.course.name, self.name.html_safe, self.end_time.to_s + " 23:59:59") + end # end end end @@ -106,8 +108,10 @@ class HomeworkCommon < ActiveRecord::Base #修改作业后发送微信模板消息 def wechat_message self.course.members.each do |member| - ws = WechatService.new - ws.homework_template(member.user_id, "homework", self.id, "#{l(:label_update_homework_template)}", self.course.name, self.name.html_safe, self.end_time.to_s + " 23:59:59") + if ShieldWechatMessage.where("container_type='User' and container_id=#{member.user_id} and shield_type='Course' and shield_id=#{self.course_id}").count == 0 + ws = WechatService.new + ws.homework_template(member.user_id, "homework", self.id, "#{l(:label_update_homework_template)}", self.course.name, self.name.html_safe, self.end_time.to_s + " 23:59:59") + end end end diff --git a/app/models/issue.rb b/app/models/issue.rb index fd378bc05..6e3dcc57a 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -159,9 +159,14 @@ class Issue < ActiveRecord::Base # 指派给自己的缺陷不提示消息 unless self.author_id == self.assigned_to_id self.forge_messages << ForgeMessage.new(:user_id => self.assigned_to_id, :project_id => self.project_id, :viewed => false) - ws = WechatService.new - content = strip_html self.author.try(:realname) + " 给您指派了缺陷:" + self.subject.html_safe, 200 - ws.message_update_template self.assigned_to_id, "issues", self.id, "#{l(:label_new_issue_template)}", content, format_time(self.created_on) + end + self.project.members.each do |m| + count = ShieldWechatMessage.where("container_type='User' and container_id=#{m.user_id} and shield_type='Project' and shield_id=#{self.project_id}").count + if m.user_id != self.author_id && count != 0 + ws = WechatService.new + content = strip_html self.project.name + " 有新缺陷了:" + self.subject.html_safe, 200 + ws.message_update_template m.user_id, "issues", self.id, "#{l(:label_new_issue_template)}", content, format_time(self.created_on) + end end if self.tracker_id == 5 self.project.members.each do |m| diff --git a/app/models/journals_for_message.rb b/app/models/journals_for_message.rb index 1f8b0253f..675c07b06 100644 --- a/app/models/journals_for_message.rb +++ b/app/models/journals_for_message.rb @@ -255,7 +255,7 @@ class JournalsForMessage < ActiveRecord::Base self.course_messages << CourseMessage.new(:user_id => r, :course_id => self.jour.id, :viewed => false) end end - if self.jour_type == 'HomeworkCommon' + if self.jour_type == 'HomeworkCommon' && ShieldWechatMessage.where("container_type='User' and container_id=#{self.jour.user_id} and shield_type='Course' and shield_id=#{self.jour.course_id}").count == 0 ws = WechatService.new #content = truncate(strip_tags(self.notes.to_s), length: 200) content = strip_html self.notes.html_safe, 200 diff --git a/app/models/message.rb b/app/models/message.rb index e6df9493b..eece8ef81 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -264,16 +264,19 @@ class Message < ActiveRecord::Base self.course.members.each do |m| if self.author.allowed_to?(:as_teacher, self.course) && m.user_id != self.author_id # 老师 自己的帖子不给自己发送消息 self.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => self.board.course_id, :viewed => false) - content = strip_html self.subject, 200 - ws.topic_publish_template m.user_id, "course_discussion", self.id, "#{l(:label_course_topic_template)}", content, self.author.try(:realname), format_time(self.created_on) + if ShieldWechatMessage.where("container_type='User' and container_id=#{m.user_id} and shield_type='Course' and shield_id=#{self.course.id}").count == 0 + content = strip_html self.subject, 200 + ws.topic_publish_template m.user_id, "course_discussion", self.id, "#{l(:label_course_topic_template)}", content, self.author.try(:realname), format_time(self.created_on) + end end end else # 回帖 - self.course.members.each do |m| - if m.user_id == Message.find(self.parent_id).author_id && m.user_id != self.author_id # 只针对主贴回复,回复自己的帖子不发消息 - self.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => self.board.course_id, :viewed => false) + parent_author_id = Message.find(self.parent_id).author_id + if parent_author_id != self.author_id # 只针对主贴回复,回复自己的帖子不发消息 + self.course_messages << CourseMessage.new(:user_id => parent_author_id, :course_id => self.board.course_id, :viewed => false) + if ShieldWechatMessage.where("container_type='User' and container_id=#{parent_author_id} and shield_type='Course' and shield_id=#{self.board.course_id}").count == 0 content = strip_html self.content.html_safe, 200 - ws.comment_template m.user_id, "course_discussion", self.parent_id, "#{l(:label_topic_comment_template)}", self.author.try(:realname), format_time(self.created_on), content + ws.comment_template parent_author_id, "course_discussion", self.parent_id, "#{l(:label_topic_comment_template)}", self.author.try(:realname), format_time(self.created_on), content end end end @@ -282,16 +285,19 @@ class Message < ActiveRecord::Base self.project.members.each do |m| if m.user_id != self.author_id self.forge_messages << ForgeMessage.new(:user_id => m.user_id, :project_id => self.board.project_id, :viewed => false) - content = strip_html self.subject, 200 - ws.topic_publish_template m.user_id, "project_discussion", self.id, "#{l(:label_project_topic_template)}", content, self.author.try(:realname), format_time(self.created_on) + if ShieldWechatMessage.where("container_type='User' and container_id=#{m.user_id} and shield_type='Project' and shield_id=#{self.project.id}").count == 0 + content = strip_html self.subject, 200 + ws.topic_publish_template m.user_id, "project_discussion", self.id, "#{l(:label_project_topic_template)}", content, self.author.try(:realname), format_time(self.created_on) + end end end else # 回帖 - self.project.members.each do |m| - if m.user_id == Message.find(self.parent_id).author_id && m.user_id != self.author_id # 只针对主贴回复,回复自己的帖子不发消息 - self.forge_messages << ForgeMessage.new(:user_id => m.user_id, :project_id => self.board.project_id, :viewed => false) + parent_author_id = Message.find(self.parent_id).author_id + if parent_author_id != self.author_id # 只针对主贴回复,回复自己的帖子不发消息 + self.forge_messages << ForgeMessage.new(:user_id => parent_author_id, :project_id => self.board.project_id, :viewed => false) + if ShieldWechatMessage.where("container_type='User' and container_id=#{parent_author_id} and shield_type='Project' and shield_id=#{self.board.project_id}").count == 0 content = strip_html self.content.html_safe, 200 - ws.comment_template m.user_id, "project_discussion", self.parent_id, "#{l(:label_topic_comment_template)}", self.author.try(:realname), format_time(self.created_on), content + ws.comment_template parent_author_id, "project_discussion", self.parent_id, "#{l(:label_topic_comment_template)}", self.author.try(:realname), format_time(self.created_on), content end end end diff --git a/app/models/news.rb b/app/models/news.rb index 45e5bc424..cd16442a0 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -170,9 +170,11 @@ class News < ActiveRecord::Base self.course.members.each do |m| if m.user_id != self.author_id self.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => self.course_id, :viewed => false) - ws = WechatService.new - content = strip_html self.author.try(:realname) + " 发布了通知:" + self.title.html_safe, 200 - ws.message_update_template m.user_id, "course_notice", self.id, "#{l(:label_new_notice_template)}", self.author.try(:realname) + " 发布了通知:" + content, format_time(self.created_on) + if ShieldWechatMessage.where("container_type='User' and container_id=#{m.user_id} and shield_type='Course' and shield_id=#{self.course_id}").count == 0 + ws = WechatService.new + content = strip_html self.author.try(:realname) + " 发布了通知:" + self.title.html_safe, 200 + ws.message_update_template m.user_id, "course_notice", self.id, "#{l(:label_new_notice_template)}", self.author.try(:realname) + " 发布了通知:" + content, format_time(self.created_on) + end end end else diff --git a/app/models/shield_wechat_message.rb b/app/models/shield_wechat_message.rb new file mode 100644 index 000000000..ebee3b8f1 --- /dev/null +++ b/app/models/shield_wechat_message.rb @@ -0,0 +1,3 @@ +class ShieldWechatMessage < ActiveRecord::Base + attr_accessible :container_id, :container_type, :shield_id, :shield_type +end diff --git a/app/views/layouts/_user_courses.html.erb b/app/views/layouts/_user_courses.html.erb index 20bccfef3..2968c2681 100644 --- a/app/views/layouts/_user_courses.html.erb +++ b/app/views/layouts/_user_courses.html.erb @@ -3,8 +3,10 @@ <% is_teacher = User.current.logged? && (User.current.admin? || User.current.allowed_to?(:as_teacher,course)) %> <%= link_to course.name+"("+current_time_and_term_short(course)+")", course_path(course.id,:host=>Setting.host_course), :class => "coursesLineGrey hidden #{course_endTime_timeout?(course) ? 'c_dark_grey' : ''}", :id => "show_course_#{course.id}", :target => '_blank', :title => (course.is_public? ? "公开课程:":"私有课程:")+course.name+"("+current_time_and_term(course)+")"%> - <% count = ShieldActivity.where("container_type='User' and container_id=#{user.id} and shield_type='Course' and shield_id=#{course.id}").count %> -