From 66d28a896a62dfe8d0fa43904f8128ec268714ed Mon Sep 17 00:00:00 2001
From: cxt <853663049@qq.com>
Date: Sun, 9 Feb 2020 14:21:08 +0800
Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E9=A2=91=E7=9B=B4=E6=92=AD=E5=BC=80?=
=?UTF-8?q?=E5=90=AF=E6=97=B6=E5=8F=91=E6=B6=88=E6=81=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
app/controllers/live_links_controller.rb | 15 +++++++++++--
app/decorators/tiding_decorator.rb | 4 ++++
app/jobs/live_publish_job.rb | 27 ++++++++++++++++++++++++
app/models/live_link.rb | 2 ++
config/locales/tidings/zh-CN.yml | 1 +
spec/jobs/live_publish_job_spec.rb | 5 +++++
6 files changed, 52 insertions(+), 2 deletions(-)
create mode 100644 app/jobs/live_publish_job.rb
create mode 100644 spec/jobs/live_publish_job_spec.rb
diff --git a/app/controllers/live_links_controller.rb b/app/controllers/live_links_controller.rb
index bc89c11d3..085a8178b 100644
--- a/app/controllers/live_links_controller.rb
+++ b/app/controllers/live_links_controller.rb
@@ -16,8 +16,19 @@ class LiveLinksController < ApplicationController
end
def update
- render_forbidden("无权限操作") unless current_user.id == current_live.user_id || current_user.admin?
- current_live.update!(on_status: params[:on_status])
+ tip_exception(403, "无权限操作") unless current_user.id == current_live.user_id || current_user.admin?
+ tip_exception("请勿重复开启") if current_live.on_status && params[:on_status].to_i == 1
+
+ ActiveRecord::Base.transaction do
+ current_live.update!(on_status: params[:on_status])
+
+ # 开启时发送消息,关闭直播时删除对应的消息
+ if params[:on_status].to_i == 1
+ LivePublishJob.perform_later(current_live.id)
+ else
+ current_live.tidings.destroy_all
+ end
+ end
render_ok
end
diff --git a/app/decorators/tiding_decorator.rb b/app/decorators/tiding_decorator.rb
index 58345b601..771df3aa2 100644
--- a/app/decorators/tiding_decorator.rb
+++ b/app/decorators/tiding_decorator.rb
@@ -321,6 +321,10 @@ module TidingDecorator
I18n.t(locale_format(parent_container_type)) % container&.exercise_name
end
+ def live_link_content
+ I18n.t(locale_format) % container&.user.try(:show_real_name)
+ end
+
def student_graduation_topic_content
I18n.t(locale_format) % container&.graduation_topic.try(:name)
end
diff --git a/app/jobs/live_publish_job.rb b/app/jobs/live_publish_job.rb
new file mode 100644
index 000000000..3296dbe52
--- /dev/null
+++ b/app/jobs/live_publish_job.rb
@@ -0,0 +1,27 @@
+# 直播开启的消息通知
+class LivePublishJob < ApplicationJob
+ queue_as :notify
+
+ def perform(live_id)
+ live = LiveLink.find_by(id: live_id)
+ return if live.blank? || live.course.blank?
+ course = live.course
+
+ attrs = %i[
+ user_id trigger_user_id container_id container_type parent_container_id parent_container_type
+ belong_container_id belong_container_type viewed tiding_type created_at updated_at
+ ]
+
+ same_attrs = {
+ trigger_user_id: live.user_id, container_id: live.id, container_type: 'LiveLink',
+ parent_container_id: live.id, parent_container_type: 'LiveLink',
+ belong_container_id: live.course_id, belong_container_type: 'Course',
+ viewed: 0, tiding_type: 'LiveLink'
+ }
+ Tiding.bulk_insert(*attrs) do |worker|
+ course.students.pluck(:user_id).each do |user_id|
+ worker.add same_attrs.merge(user_id: user_id)
+ end
+ end
+ end
+end
diff --git a/app/models/live_link.rb b/app/models/live_link.rb
index e559baa6b..cd6655c30 100644
--- a/app/models/live_link.rb
+++ b/app/models/live_link.rb
@@ -2,6 +2,8 @@ class LiveLink < ApplicationRecord
belongs_to :course
belongs_to :user
+ has_many :tidings, as: :container, dependent: :destroy
+
validates :url, presence: true
validates :description, length: { maximum: 100, too_long: "不能超过100个字符" }
diff --git a/config/locales/tidings/zh-CN.yml b/config/locales/tidings/zh-CN.yml
index 1117786c0..92c815a05 100644
--- a/config/locales/tidings/zh-CN.yml
+++ b/config/locales/tidings/zh-CN.yml
@@ -238,3 +238,4 @@
2_end: "你提交的发布视频申请:%s,审核未通过
原因:%{reason}"
PublicCourseStart_end: "你报名参与的开放课程:%s,将于%s正式开课"
SubjectStartCourse_end: "您创建的开放课程:%s 已达到开课人数要求。您可以在24小时内自主开设新一期课程。如果超过24小时未开课,平台将自动开课并复制您上一期的课程内容。"
+ LiveLink_end: "%s老师正在直播中"
diff --git a/spec/jobs/live_publish_job_spec.rb b/spec/jobs/live_publish_job_spec.rb
new file mode 100644
index 000000000..d996446d4
--- /dev/null
+++ b/spec/jobs/live_publish_job_spec.rb
@@ -0,0 +1,5 @@
+require 'rails_helper'
+
+RSpec.describe LivePublishJob, type: :job do
+ pending "add some examples to (or delete) #{__FILE__}"
+end