From 626241ef3fec07e3d401fb311ff3f3363cf02ddf Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Thu, 13 Feb 2020 12:36:39 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=B4=E6=92=AD=E6=8E=A5=E5=8F=A3=E7=9A=84?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/live_links_controller.rb | 13 +++++++------ app/decorators/tiding_decorator.rb | 2 +- app/models/live_link.rb | 19 ++++++++++++++++++- app/views/live_links/edit.json.jbuilder | 2 +- app/views/live_links/index.json.jbuilder | 5 +++-- config/locales/tidings/zh-CN.yml | 2 +- config/locales/zh-CN.yml | 5 ++++- lib/tasks/live_notice.rake | 13 +++++++++++++ 8 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 lib/tasks/live_notice.rake diff --git a/app/controllers/live_links_controller.rb b/app/controllers/live_links_controller.rb index fe2de4afe..f5af80ead 100644 --- a/app/controllers/live_links_controller.rb +++ b/app/controllers/live_links_controller.rb @@ -7,17 +7,17 @@ class LiveLinksController < ApplicationController def index lives = @course.live_links - order_str = "on_status desc,id desc" + order_str = "on_status desc, live_time desc" @total_count = lives.size @my_live_id = @course.live_links.find_by(user_id: current_user.id)&.id - order_str = "live_links.id = #{@my_live_id} desc, #{order_str}" if @my_live_id.present? + # order_str = "live_links.id = #{@my_live_id} desc, #{order_str}" if @my_live_id.present? lives = lives.order("#{order_str}") @lives = paginate lives.includes(user: :user_extension) end def create - tip_exception("一个老师只能设置一个直播间") if @course.live_links.where(user_id: current_user.id).exists? - @course.live_links.create!(create_params.merge(user_id: current_user.id)) + on_status = params[:live_time].present? && params[:live_time].to_time <= Time.now ? 1 : 0 + @course.live_links.create!(create_params.merge(user_id: current_user.id, on_status: on_status)) render_ok end @@ -38,7 +38,8 @@ class LiveLinksController < ApplicationController end end else - current_live.update!(create_params) + on_status = params[:live_time].present? && params[:live_time].to_time <= Time.now ? 1 : 0 + current_live.update!(create_params.merge(on_status: on_status)) end render_ok end @@ -51,7 +52,7 @@ class LiveLinksController < ApplicationController private def create_params - params.permit(:url, :description) + params.permit(:url, :description, :course_name, :platform, :live_time, :duration) end def current_live diff --git a/app/decorators/tiding_decorator.rb b/app/decorators/tiding_decorator.rb index 771df3aa2..8d266b7df 100644 --- a/app/decorators/tiding_decorator.rb +++ b/app/decorators/tiding_decorator.rb @@ -322,7 +322,7 @@ module TidingDecorator end def live_link_content - I18n.t(locale_format) % container&.user.try(:show_real_name) + I18n.t(locale_format) % container&.course_name end def student_graduation_topic_content diff --git a/app/models/live_link.rb b/app/models/live_link.rb index 52c1e3657..27963a0bb 100644 --- a/app/models/live_link.rb +++ b/app/models/live_link.rb @@ -4,8 +4,25 @@ class LiveLink < ApplicationRecord has_many :tidings, as: :container, dependent: :destroy - validates :url, presence: true, format: { with: CustomRegexp::URL, message: "必须为网址超链接" } + validates :url, format: { with: CustomRegexp::URL, message: "必须为网址超链接" } validates :description, length: { maximum: 100, too_long: "不能超过100个字符" } + validates :course_name, presence: true + validates :platform, presence: true, inclusion: {in: %W(tencent douyu bilibili vbt)} + # validates :live_time, presence: true + validates :duration, numericality: { only_integer: true, greater_than: 0}, allow_blank: true + + def platform_name + case platform + when "tencent" + "腾讯课堂" + when "douyu" + "斗鱼直播" + when "vbt" + "威佰通" + else + platform + end + end def op_auth? user == User.current || User.current.admin_or_business? diff --git a/app/views/live_links/edit.json.jbuilder b/app/views/live_links/edit.json.jbuilder index 047a226e8..6b0fd385d 100644 --- a/app/views/live_links/edit.json.jbuilder +++ b/app/views/live_links/edit.json.jbuilder @@ -1 +1 @@ -json.(@live, :id, :description, :url) +json.(@live, :id, :description, :url, :platform, :live_time, :duration, :course_name) diff --git a/app/views/live_links/index.json.jbuilder b/app/views/live_links/index.json.jbuilder index e497a068b..419fb49bb 100644 --- a/app/views/live_links/index.json.jbuilder +++ b/app/views/live_links/index.json.jbuilder @@ -1,12 +1,13 @@ json.lives @lives do |live| - json.(live, :id, :description, :on_status) + json.(live, :id, :description, :on_status, :duration, :course_name) json.url live.on_status ? live.url : "" json.author_name live.user.show_real_name json.author_login live.user.login json.author_img url_to_avatar(live.user) json.op_auth live.op_auth? json.delete_auth live.delete_auth? - json.created_at live.created_at.strftime('%Y-%m-%d') + json.live_time live.live_time&.strftime('%Y-%m-%d %H:%M:%S') + json.platform live.platform_name end json.my_live_id @my_live_id json.total_count @total_count \ No newline at end of file diff --git a/config/locales/tidings/zh-CN.yml b/config/locales/tidings/zh-CN.yml index 92c815a05..cda146780 100644 --- a/config/locales/tidings/zh-CN.yml +++ b/config/locales/tidings/zh-CN.yml @@ -238,4 +238,4 @@ 2_end: "你提交的发布视频申请:%s,审核未通过
原因:%{reason}" PublicCourseStart_end: "你报名参与的开放课程:%s,将于%s正式开课" SubjectStartCourse_end: "您创建的开放课程:%s 已达到开课人数要求。您可以在24小时内自主开设新一期课程。如果超过24小时未开课,平台将自动开课并复制您上一期的课程内容。" - LiveLink_end: "%s老师正在直播中" + LiveLink_end: "%s 直播将于30分钟后开始" diff --git a/config/locales/zh-CN.yml b/config/locales/zh-CN.yml index 6900f6c51..4f41df0c5 100644 --- a/config/locales/zh-CN.yml +++ b/config/locales/zh-CN.yml @@ -173,7 +173,10 @@ zh-CN: live_link: description: '说明' url: '链接' - + course_name: '课程名称' + platform: '直播平台' + live_time: '开播时间' + duration: '直播时长' diff --git a/lib/tasks/live_notice.rake b/lib/tasks/live_notice.rake new file mode 100644 index 000000000..da57ebd4b --- /dev/null +++ b/lib/tasks/live_notice.rake @@ -0,0 +1,13 @@ +namespace :live_notice do + desc "send a live message to students before 30 minutes" + task message: :environment do + lives = LiveLink.where(on_status: 0).where("live_time <= '#{Time.now + 30*60}' and live_time > '#{Time.now}'") + lives.each do |live| + LivePublishJob.perform_later(live.id) + end + end + + task on_status: :environment do + LiveLink.where(on_status: 0).where("live_time <= '#{Time.now}'").update_all(on_status: 1) + end +end \ No newline at end of file