Merge branch 'dev_video' of https://bdgit.educoder.net/Hjqreturn/educoder into dev_video
commit
a4b26ed8a3
@ -0,0 +1,44 @@
|
||||
class LiveLinksController < ApplicationController
|
||||
before_action :require_login
|
||||
before_action :find_course, only: [:index, :create]
|
||||
before_action :user_course_identity
|
||||
before_action :teacher_allowed, only: [:create]
|
||||
|
||||
def index
|
||||
lives = @course.live_links.order("id desc")
|
||||
@total_count = lives.size
|
||||
@lives = paginate lives.includes(user: :user_extension)
|
||||
end
|
||||
|
||||
def create
|
||||
@course.live_links.create!( create_params.merge(user_id: current_user.id))
|
||||
render_ok
|
||||
end
|
||||
|
||||
def update
|
||||
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
|
||||
|
||||
private
|
||||
|
||||
def create_params
|
||||
params.permit(:url, :description)
|
||||
end
|
||||
|
||||
def current_live
|
||||
@_current_live = LiveLink.find params[:id]
|
||||
end
|
||||
end
|
@ -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
|
@ -0,0 +1,13 @@
|
||||
class LiveLink < ApplicationRecord
|
||||
belongs_to :course
|
||||
belongs_to :user
|
||||
|
||||
has_many :tidings, as: :container, dependent: :destroy
|
||||
|
||||
validates :url, presence: true, format: { with: CustomRegexp::URL, message: "必须为网址超链接" }
|
||||
validates :description, length: { maximum: 100, too_long: "不能超过100个字符" }
|
||||
|
||||
def op_auth?
|
||||
user == User.current || User.current.admin?
|
||||
end
|
||||
end
|
@ -0,0 +1,8 @@
|
||||
json.lives @lives do |live|
|
||||
json.(live, :id, :url, :description, :on_status)
|
||||
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?
|
||||
end
|
||||
json.total_count @total_count
|
@ -0,0 +1,13 @@
|
||||
class CreateLiveLinks < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :live_links do |t|
|
||||
t.references :course, index: true
|
||||
t.references :user, index: true
|
||||
t.string :url
|
||||
t.text :description
|
||||
t.boolean :on_status, default: 0
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
Before Width: | Height: | Size: 128 KiB After Width: | Height: | Size: 207 KiB |
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe LivePublishJob, type: :job do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe LiveLink, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
Loading…
Reference in new issue