-
Notifications
You must be signed in to change notification settings - Fork 181
/
scene_listing.ex
88 lines (74 loc) · 2.86 KB
/
scene_listing.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
defmodule Ret.SceneListing.SceneListingSlug do
use EctoAutoslugField.Slug, from: :name, to: :slug, always_change: true
def get_sources(_changeset, _opts) do
[:scene_listing_sid, :name]
end
end
defmodule Ret.SceneListing do
use Ecto.Schema
import Ecto.Changeset
alias Ret.{Repo, SceneListing, Scene}
alias Ret.SceneListing.{SceneListingSlug}
@type t :: %__MODULE__{}
@schema_prefix "ret0"
@primary_key {:scene_listing_id, :id, autogenerate: true}
schema "scene_listings" do
field :scene_listing_sid, :string
field :slug, SceneListingSlug.Type
field :name, :string
field :description, :string
field :tags, :map
field :attributions, :map
field :order, :integer
field :state, SceneListing.State
belongs_to :scene, Ret.Scene, references: :scene_id
belongs_to :model_owned_file, Ret.OwnedFile, references: :owned_file_id
belongs_to :screenshot_owned_file, Ret.OwnedFile, references: :owned_file_id
belongs_to :scene_owned_file, Ret.OwnedFile, references: :owned_file_id
has_one :account, through: [:scene, :account]
has_one :project, through: [:scene, :project]
timestamps()
end
def has_any_in_filter?(filter) do
%Ret.MediaSearchQuery{source: "scene_listings", cursor: "1", filter: filter, q: ""}
|> Ret.MediaSearch.search()
|> elem(1)
|> Map.get(:entries)
|> Enum.empty?()
|> Kernel.not()
end
def changeset_for_listing_for_scene(
%SceneListing{} = listing,
scene,
params \\ %{}
) do
listing
|> cast(params, [:name, :description, :order, :tags])
|> maybe_add_scene_listing_sid_to_changeset
|> unique_constraint(:scene_listing_sid)
|> put_assoc(:scene, scene)
|> put_change(:name, params[:name] || scene.name)
|> put_change(:description, params[:description] || scene.description)
|> put_change(:attributions, scene.attributions)
|> put_change(:model_owned_file_id, scene.model_owned_file.owned_file_id)
|> put_change(:screenshot_owned_file_id, scene.screenshot_owned_file.owned_file_id)
|> put_change(:scene_owned_file_id, scene.scene_owned_file.owned_file_id)
|> SceneListingSlug.maybe_generate_slug()
end
def get_random_default_scene_listing do
{:commit, results} =
%Ret.MediaSearchQuery{source: "scene_listings", cursor: "1", filter: "default"}
|> Ret.MediaSearch.search()
if length(results.entries) > 0 do
scene_listing_sid = results.entries |> Enum.map(& &1[:id]) |> Enum.shuffle() |> Enum.at(0)
Repo.get_by(SceneListing, scene_listing_sid: scene_listing_sid)
|> Repo.preload(scene: Scene.scene_preloads())
else
nil
end
end
defp maybe_add_scene_listing_sid_to_changeset(changeset) do
scene_listing_sid = changeset |> get_field(:scene_listing_sid) || Ret.Sids.generate_sid()
put_change(changeset, :scene_listing_sid, "#{scene_listing_sid}")
end
end