diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index e0d708d3e..5027ba88e 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -189,6 +189,7 @@ class ArticlesController < ApplicationController def assign_video_attributes if params[:article][:video] @article.video = params[:article][:video] + @article.video_state = "PROGRESSING" @article.video_code = @article.video.split("dev-to-input-v0/")[1] @article.video_source_url = "https://dw71fyauz7yz9.cloudfront.net/#{@article.video_code}/#{@article.video_code}.m3u8" @article.video_thumbnail_url = "https://dw71fyauz7yz9.cloudfront.net/#{@article.video_code}/thumbs-#{@article.video_code}-00001.png" diff --git a/app/controllers/video_states_controller.rb b/app/controllers/video_states_controller.rb new file mode 100644 index 000000000..ab3332f5b --- /dev/null +++ b/app/controllers/video_states_controller.rb @@ -0,0 +1,20 @@ +class VideoStatesController < ApplicationController + skip_before_action :verify_authenticity_token + def create + unless valid_user + render json: { message: "invalid_user" }, :status => 422 + return + end + @article = Article.find_by_video_code(params[:input][:key]) + @article.update(video_state: "COMPLETED") #Only is called on completion + NotifyMailer.video_upload_complete_email(@article).deliver + render json: { message: "Video state updated" } + end + + def valid_user + user = User.find_by_secret(params[:key]) + user = nil if !user.has_role?(:super_admin) + user + end + +end \ No newline at end of file diff --git a/app/mailers/notify_mailer.rb b/app/mailers/notify_mailer.rb index 75661ba35..4b2bf0b3f 100644 --- a/app/mailers/notify_mailer.rb +++ b/app/mailers/notify_mailer.rb @@ -35,4 +35,10 @@ class NotifyMailer < ApplicationMailer subject = "🔥 You have #{@unread_notifications_count} unread notifications on dev.to" mail(to: @user.email, subject: subject) end + + def video_upload_complete_email(article) + @article = article + @user = @article.user + mail(to: @user.email, subject: "Your video upload is complete") + end end diff --git a/app/models/article.rb b/app/models/article.rb index b0451023d..fe2883ad3 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -31,6 +31,7 @@ class Article < ApplicationRecord # validates :description, length: { in: 10..170, if: :published? } validates :body_markdown, uniqueness: { scope: :user_id } validate :validate_tag + validates :video_state, inclusion: { in: %w(PROGRESSING COMPLETED) }, allow_nil: true before_validation :evaluate_markdown before_validation :create_slug diff --git a/app/views/articles/_video_player.html.erb b/app/views/articles/_video_player.html.erb index adb82bd96..dd6634a9b 100644 --- a/app/views/articles/_video_player.html.erb +++ b/app/views/articles/_video_player.html.erb @@ -40,7 +40,7 @@ playerInstance.setup({ file: "<%= article.video_source_url %>", mediaid: "<%= article.video_code %>", - image: "<%= cloudinary(article.video_thumbnail_url + "bust=" + article.updated_at.to_i.to_s, 880) %>", + image: "<%= cloudinary(article.video_thumbnail_url, 880) %>", playbackRateControls: true, tracks: [{ file: "<%= article.video_closed_caption_track_url %>", diff --git a/app/views/articles/edit.html.erb b/app/views/articles/edit.html.erb index d7efb975f..3d211de63 100644 --- a/app/views/articles/edit.html.erb +++ b/app/views/articles/edit.html.erb @@ -13,11 +13,15 @@ <% if @user.has_role?(:super_admin) %>
<%= stylesheet_link_tag 's3_direct_upload', media: 'all' %> - <% if @article.video %> + <% if @article.video_state == "PROGRESSING" %> +

Video Transcoding In Progress

+ +



+ <% elsif @article.video_state == "COMPLETED" %>

Video uploaded - <%= @article.video_code %>

<%= render "articles/video_player", meta_tags: false, article: @article %>

Thumbnail:

- +

Change thumbnail:

<%= form_for(@article) do |f| %> <%= f.text_field :video_thumbnail_url, placeholder: "New Thumbnail URL" %> diff --git a/app/views/mailers/notify_mailer/video_upload_complete_email.html.erb b/app/views/mailers/notify_mailer/video_upload_complete_email.html.erb new file mode 100644 index 000000000..cb3385c85 --- /dev/null +++ b/app/views/mailers/notify_mailer/video_upload_complete_email.html.erb @@ -0,0 +1,7 @@ +

+ Hey <%= @user.name %>, your video is finished processing. +

+ +

+ 🎥 Finalize and publish your video 😊 +

\ No newline at end of file diff --git a/app/views/mailers/notify_mailer/video_upload_complete_email.text.erb b/app/views/mailers/notify_mailer/video_upload_complete_email.text.erb new file mode 100644 index 000000000..e9e445ba4 --- /dev/null +++ b/app/views/mailers/notify_mailer/video_upload_complete_email.text.erb @@ -0,0 +1,3 @@ +Your video is finished being processed. Check it out here: + +https://dev.to<%= @article.path %>/edit \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 38055b1d6..91b529d3d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -93,6 +93,7 @@ Rails.application.routes.draw do resources :buffered_articles, only: [:index] resources :events, only: [:index, :show] resources :additional_content_boxes, only: [:index] + resources :video_states, only: [:create] get "/notifications/:username" => "notifications#index" patch "/onboarding_update" => "users#onboarding_update" get "email_subscriptions/unsubscribe" diff --git a/db/migrate/20180507191509_add_video_state_to_articles.rb b/db/migrate/20180507191509_add_video_state_to_articles.rb new file mode 100644 index 000000000..2ce224870 --- /dev/null +++ b/db/migrate/20180507191509_add_video_state_to_articles.rb @@ -0,0 +1,5 @@ +class AddVideoStateToArticles < ActiveRecord::Migration[5.1] + def change + add_column :articles, :video_state, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 6f48d5b00..26acde88c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180502174301) do +ActiveRecord::Schema.define(version: 20180507191509) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -107,6 +107,7 @@ ActiveRecord::Schema.define(version: 20180502174301) do t.string "video_closed_caption_track_url" t.string "video_code" t.string "video_source_url" + t.string "video_state" t.string "video_thumbnail_url" t.index ["boost_states"], name: "index_articles_on_boost_states", using: :gin t.index ["featured_number"], name: "index_articles_on_featured_number" diff --git a/spec/mailers/previews/notify_mailer_preview.rb b/spec/mailers/previews/notify_mailer_preview.rb index 1f8ee9f63..5c7e35cc0 100644 --- a/spec/mailers/previews/notify_mailer_preview.rb +++ b/spec/mailers/previews/notify_mailer_preview.rb @@ -15,4 +15,8 @@ class NotifyMailerPreview < ActionMailer::Preview def new_mention_email NotifyMailer.new_mention_email(Mention.last) end + + def video_upload_complete_email + NotifyMailer.video_upload_complete_email(Article.last) + end end diff --git a/spec/requests/video_states_update_spec.rb b/spec/requests/video_states_update_spec.rb new file mode 100644 index 000000000..adcc1be92 --- /dev/null +++ b/spec/requests/video_states_update_spec.rb @@ -0,0 +1,22 @@ +# http://localhost:3000/api/comments?a_id=23 +require "rails_helper" + +RSpec.describe "VideoStatesUpdate", type: :request do + before do + @user = FactoryBot.create(:user) + @user.update(secret:"TEST_SECRET") + @user.add_role(:super_admin) + @article = FactoryBot.create(:article, video_code: "DUMMY_VID_CODE") + end + describe "POST /video_states" do + it "updates video state" do + post "/video_states?key=#{@user.secret}", params: {input: {key: Article.last.video_code}} + expect(Article.last.video_state).to eq("COMPLETED") + end + it "rejects non-authorized users" do + @user.remove_role(:super_admin) + post "/video_states?key=#{@user.secret}", params: {input: {key: Article.last.video_code}} + expect(response).to have_http_status(422) + end + end +end