From 591e5037275637151b95bb57bc497447eceff9dc Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Sun, 25 Jun 2017 19:57:09 +0200 Subject: [PATCH] Port seeds --- app/models/asciicast_params.rb | 89 ---------------------------------- db/seeds.rb | 11 ----- docker/bin/setup | 2 +- docker/bin/upgrade | 2 +- lib/asciinema/repo.ex | 4 ++ lib/asciinema/users.ex | 26 +++++++++- priv/repo/seeds.exs | 2 + web/models/asciicast.ex | 2 +- 8 files changed, 34 insertions(+), 104 deletions(-) delete mode 100644 app/models/asciicast_params.rb diff --git a/app/models/asciicast_params.rb b/app/models/asciicast_params.rb deleted file mode 100644 index 62b6c3e..0000000 --- a/app/models/asciicast_params.rb +++ /dev/null @@ -1,89 +0,0 @@ -class AsciicastParams - FormatError = Class.new(StandardError) - - def self.build(asciicast_params, user, user_agent) - attributes = if asciicast_params.try(:respond_to?, :read) - from_format_1_request(asciicast_params, user, user_agent) - else - from_format_0_request(asciicast_params, user, user_agent) - end - - attributes.merge(private: user.new_asciicast_private?) - end - - def self.from_format_0_request(params, user, user_agent) - meta = params.delete(:meta) - - attributes = { - command: meta['command'], - duration: meta['duration'], - shell: meta['shell'], - stdin_data: params[:stdin], - stdin_timing: params[:stdin_timing], - stdout_data: params[:stdout], - stdout_timing: params[:stdout_timing], - terminal_columns: meta['term']['columns'], - terminal_lines: meta['term']['lines'], - terminal_type: meta['term']['type'], - title: meta['title'], - user: user, - version: 0, - } - - if meta['uname'] # old client, with useless user_agent - attributes[:uname] = meta['uname'] - else - attributes[:user_agent] = user_agent - end - - attributes - end - - def self.from_format_1_request(asciicast_file, user, user_agent) - begin - asciicast = Oj.sc_parse(AsciicastHandler.new, asciicast_file) - rescue Oj::ParseError - end - - if asciicast.nil? - raise FormatError, "This doesn't look like a valid asciicast file" - end - - version = asciicast['version'] - - if version != 1 - raise FormatError, "Unsupported asciicast format version: #{version}" - end - - env = asciicast['env'] - - { - command: asciicast['command'], - duration: asciicast['duration'], - file: asciicast_file, - shell: env && env['SHELL'], - terminal_columns: asciicast['width'], - terminal_lines: asciicast['height'], - terminal_type: env && env['TERM'], - title: asciicast['title'], - user: user, - user_agent: user_agent, - version: version, - } - end - - class AsciicastHandler < ::Oj::ScHandler - META_ATTRIBUTES = %w[version width height duration command title env SHELL TERM] - - def hash_start - {} - end - - def hash_set(h, k, v) - if META_ATTRIBUTES.include?(k) - h[k] = v - end - end - end - -end diff --git a/db/seeds.rb b/db/seeds.rb index ab5e8bf..5dde233 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,13 +1,2 @@ # This file should contain all the record creation needed to seed the database with its default values. # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). - -# Create asciinema user - -asciinema_user = User.find_by_username("asciinema") || User.create!(username: "asciinema", name: "asciinema", email: "support@asciinema.org") - -# Create "welcome" asciicast - -if asciinema_user.asciicasts.count == 0 - attrs = AsciicastParams.build(File.open("resources/welcome.json"), asciinema_user, nil) - AsciicastCreator.new.create(attrs.merge(private: false, snapshot_at: 76.2)) -end diff --git a/docker/bin/setup b/docker/bin/setup index ba18ab7..79312cf 100755 --- a/docker/bin/setup +++ b/docker/bin/setup @@ -3,4 +3,4 @@ set -e bundle exec rake db:migrate -bundle exec rake db:seed \ No newline at end of file +mix run priv/repo/seeds.exs \ No newline at end of file diff --git a/docker/bin/upgrade b/docker/bin/upgrade index ba18ab7..79312cf 100755 --- a/docker/bin/upgrade +++ b/docker/bin/upgrade @@ -3,4 +3,4 @@ set -e bundle exec rake db:migrate -bundle exec rake db:seed \ No newline at end of file +mix run priv/repo/seeds.exs \ No newline at end of file diff --git a/lib/asciinema/repo.ex b/lib/asciinema/repo.ex index 356765c..8ba104e 100644 --- a/lib/asciinema/repo.ex +++ b/lib/asciinema/repo.ex @@ -1,3 +1,7 @@ defmodule Asciinema.Repo do use Ecto.Repo, otp_app: :asciinema + + def count(query) do + aggregate(query, :count, :id) + end end diff --git a/lib/asciinema/users.ex b/lib/asciinema/users.ex index fc689a9..47958b2 100644 --- a/lib/asciinema/users.ex +++ b/lib/asciinema/users.ex @@ -1,7 +1,31 @@ defmodule Asciinema.Users do import Ecto.Query, warn: false import Ecto, only: [assoc: 2] - alias Asciinema.{Repo, User, ApiToken} + alias Asciinema.{Repo, User, ApiToken, Asciicasts, Asciicast} + + def create_asciinema_user!() do + user = + Repo.get_by(User, username: "asciinema") || + Repo.insert!(%User{username: "asciinema", + name: "asciinema", + email: "support@asciinema.org"}) + + if Repo.count(assoc(user, :asciicasts)) == 0 do + upload = %Plug.Upload{path: "resources/welcome.json", + filename: "asciicast.json", + content_type: "application/json"} + + Repo.transaction(fn -> + {:ok, asciicast} = Asciicasts.create_asciicast(user, upload, nil) + + asciicast + |> Asciicast.update_changeset(%{private: false, snapshot_at: 76.2}) + |> Repo.update! + end) + end + + :ok + end def authenticate(api_token) do q = from u in User, diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 70f86e8..71d0563 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -9,3 +9,5 @@ # # We recommend using the bang functions (`insert!`, `update!` # and so on) as they will fail if something goes wrong. + +Asciinema.Users.create_asciinema_user!() diff --git a/web/models/asciicast.ex b/web/models/asciicast.ex index 7e83d7c..4f2496d 100644 --- a/web/models/asciicast.ex +++ b/web/models/asciicast.ex @@ -49,7 +49,7 @@ defmodule Asciinema.Asciicast do def update_changeset(struct, attrs) do struct - |> cast(attrs, [:title, :theme_name, :snapshot_at]) + |> cast(attrs, [:title, :theme_name, :private, :snapshot_at]) end defp generate_secret_token(changeset) do