/
asciicast.ex
124 lines (103 loc) · 3.53 KB
/
asciicast.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
defmodule Asciinema.Asciicasts.Asciicast do
use Ecto.Schema
import Ecto.Changeset
alias Asciinema.Accounts.User
alias Asciinema.Asciicasts.Asciicast
@default_theme "asciinema"
schema "asciicasts" do
field :version, :integer
field :file, :string
field :terminal_columns, :integer
field :terminal_lines, :integer
field :terminal_type, :string
field :stdout_data, :string
field :stdout_timing, :string
field :stdout_frames, :string
field :private, :boolean
field :featured, :boolean
field :secret_token, :string
field :duration, :float
field :title, :string
field :description, :string
field :theme_name, :string
field :theme_fg, :string
field :theme_bg, :string
field :theme_palette, :string
field :snapshot_at, :float
field :snapshot, Asciinema.Ecto.Type.JsonArray
field :command, :string
field :shell, :string
field :uname, :string
field :user_agent, :string
field :recorded_at, Timex.Ecto.DateTime
field :idle_time_limit, :float
field :views_count, :integer, default: 0
field :archived_at, Timex.Ecto.DateTime
timestamps(inserted_at: :created_at)
belongs_to :user, User
end
defimpl Phoenix.Param, for: Asciicast do
def to_param(%Asciicast{private: true, secret_token: secret_token}) do
secret_token
end
def to_param(%Asciicast{id: id}) do
Integer.to_string(id)
end
end
defp changeset(struct, attrs) do
struct
|> cast(attrs, [:title, :private, :snapshot_at])
|> validate_required([:private])
end
def create_changeset(struct, attrs) do
struct
|> changeset(attrs)
|> cast(attrs, [:version, :file, :duration, :terminal_columns, :terminal_lines, :terminal_type, :command, :shell, :uname, :user_agent, :recorded_at, :theme_fg, :theme_bg, :theme_palette, :idle_time_limit])
|> validate_required([:user_id, :version, :duration, :terminal_columns, :terminal_lines])
|> generate_secret_token
end
def update_changeset(struct, attrs) do
struct
|> changeset(attrs)
|> cast(attrs, [:description, :theme_name])
end
def snapshot_changeset(struct, snapshot) do
cast(struct, %{snapshot: snapshot}, [:snapshot])
end
defp generate_secret_token(changeset) do
put_change(changeset, :secret_token, Crypto.random_token(25))
end
def json_store_path(%Asciicast{file: v} = asciicast) when is_binary(v) do
file_store_path(asciicast, :file)
end
def json_store_path(%Asciicast{stdout_frames: v} = asciicast) when is_binary(v) do
file_store_path(asciicast, :stdout_frames)
end
def json_store_path(_asciicast) do
nil
end
def file_store_path(%Asciicast{id: id, file: fname}, :file) do
file_store_path(:file, id, fname)
end
def file_store_path(%Asciicast{id: id, stdout_frames: fname}, :stdout_frames) do
file_store_path(:stdout_frames, id, fname)
end
def file_store_path(%Asciicast{id: id, stdout_data: fname}, :stdout_data) do
file_store_path(:stdout, id, fname)
end
def file_store_path(%Asciicast{id: id, stdout_timing: fname}, :stdout_timing) do
file_store_path(:stdout_timing, id, fname)
end
def file_store_path(type, id, fname) when is_binary(fname) do
"asciicast/#{type}/#{id}/#{fname}"
end
def file_store_path(_type, _id, _fname) do
nil
end
def snapshot_at(%Asciicast{snapshot_at: snapshot_at, duration: duration}) do
snapshot_at || duration / 2
end
def theme_name(%Asciicast{theme_name: a_theme_name}, %User{theme_name: u_theme_name}) do
a_theme_name || u_theme_name || @default_theme
end
end