parent
d8b5700dc5
commit
098f074788
@ -0,0 +1,8 @@
|
||||
class WatchVideoHistoriesController < ApplicationController
|
||||
before_action :require_login
|
||||
|
||||
def create
|
||||
watch_log = CreateWatchVideoService.new(current_user, request, params).call
|
||||
render_ok(log_id: watch_log&.id)
|
||||
end
|
||||
end
|
@ -0,0 +1,9 @@
|
||||
class WatchCourseVideo < ApplicationRecord
|
||||
belongs_to :course_video
|
||||
belongs_to :user
|
||||
|
||||
has_many :watch_video_histories
|
||||
|
||||
|
||||
validates :course_video_id, uniqueness: {scope: :user_id}
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
class WatchVideoHistory < ApplicationRecord
|
||||
belongs_to :user
|
||||
belongs_to :video
|
||||
belongs_to :watch_course_video, optional: true
|
||||
end
|
@ -0,0 +1,71 @@
|
||||
class CreateWatchVideoService < ApplicationService
|
||||
attr_reader :user, :params, :request
|
||||
|
||||
def initialize(user, request, params)
|
||||
@user = user
|
||||
@request = request
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
ActiveRecord::Base.transaction do
|
||||
current_time = Time.now
|
||||
if params[:log_id].present?
|
||||
# 更新观看时长
|
||||
watch_video_history = user.watch_video_histories.find(params[:log_id])
|
||||
|
||||
if watch_video_history.present? && watch_video_history.watch_duration < params[:watch_duration].to_f
|
||||
# 如果观看时长少于原有的,说明拖放到前面重新观看了,不必再去记录
|
||||
watch_video_history.end_at = current_time
|
||||
watch_video_history.watch_duration = params[:watch_duration]
|
||||
watch_video_history.is_finished = (watch_video_history.duration <= params[:watch_duration].to_f)
|
||||
watch_video_history.save!
|
||||
|
||||
watch_course_video = watch_video_history.watch_course_video
|
||||
|
||||
if watch_course_video.present? && !watch_course_video.is_finished && watch_course_video.watch_duration < params[:watch_duration].to_f
|
||||
# 更新课程视频的时长及是否看完
|
||||
watch_course_video.watch_duration = params[:watch_duration]
|
||||
watch_course_video.is_finished = (watch_course_video.duration <= params[:watch_duration].to_f)
|
||||
watch_course_video.end_at = current_time
|
||||
watch_course_video.save!
|
||||
end
|
||||
end
|
||||
else
|
||||
# 开始播放时记录一次
|
||||
if params[:course_id].present?
|
||||
# 课堂视频
|
||||
course_video = CourseVideo.find_by(course_id: params[:course_id], video_id: params[:video_id])
|
||||
watch_course_video = WatchCourseVideo.find_or_initialize_by(course_video_id: course_video.id, user_id: user.id) do |d|
|
||||
d.start_at = current_time
|
||||
d.duration = params[:duration]
|
||||
end
|
||||
|
||||
watch_video_history = build_video_log(current_time, course_video.video_id, watch_course_video.id)
|
||||
watch_video_history.save!
|
||||
|
||||
watch_course_video.save! unless watch_course_video.persisted?
|
||||
else
|
||||
# 非课堂视频
|
||||
video = Video.find_by(params[:video_id])
|
||||
watch_video_history = build_video_log(current_time, video.id)
|
||||
watch_video_history.save!
|
||||
end
|
||||
end
|
||||
watch_video_history
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def build_video_log(current_time, video_id, watch_course_video_id)
|
||||
WatchVideoHistory.new(
|
||||
user_id: user.id,
|
||||
watch_course_video_id: watch_course_video_id,
|
||||
start_at: current_time,
|
||||
duration: params[:duration],
|
||||
video_id: video_id,
|
||||
device: params[:device],
|
||||
ip: request.remote_ip
|
||||
)
|
||||
end
|
||||
end
|
@ -0,0 +1,15 @@
|
||||
class CreateWatchCourseVideos < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :watch_course_videos do |t|
|
||||
t.references :course_video, index: true
|
||||
t.references :user, index: true
|
||||
t.boolean :is_finished, default: false
|
||||
t.float :duration, default: 0
|
||||
t.float :watch_duration, default: 0
|
||||
t.datetime :start_at
|
||||
t.datetime :end_at
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,18 @@
|
||||
class CreateWatchVideoHistories < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :watch_video_histories do |t|
|
||||
t.references :watch_course_video, index: true
|
||||
t.references :user, index: true
|
||||
t.references :video, index: true
|
||||
t.boolean :is_finished, default: false
|
||||
t.float :duration, default: 0
|
||||
t.float :watch_duration, default: 0
|
||||
t.datetime :start_at
|
||||
t.datetime :end_at
|
||||
t.string :device
|
||||
t.string :ip
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue