Port seeds

ex-seed
Marcin Kulik 7 years ago
parent 03f34f4c28
commit 591e503727

@ -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

@ -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

@ -3,4 +3,4 @@
set -e
bundle exec rake db:migrate
bundle exec rake db:seed
mix run priv/repo/seeds.exs

@ -3,4 +3,4 @@
set -e
bundle exec rake db:migrate
bundle exec rake db:seed
mix run priv/repo/seeds.exs

@ -1,3 +1,7 @@
defmodule Asciinema.Repo do
use Ecto.Repo, otp_app: :asciinema
def count(query) do
aggregate(query, :count, :id)
end
end

@ -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,

@ -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!()

@ -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

Loading…
Cancel
Save