Music streaming on ATProto!

feat(appview): add basic changesets for schemas

ovyerus.com 154949f5 d7daf070

verified
+9
apps/backend/lib/comet/repo/comment.ex
···
Schema containing information about a Comet comment.
"""
use Comet.Schema
+
import Ecto.Changeset
schema "comments" do
field :rkey, :string
···
has_many :replies, __MODULE__, foreign_key: :reply_id
timestamps(inserted_at: :indexed_at, updated_at: false)
+
end
+
+
def new(params \\ %{}), do: changeset(%__MODULE__{}, params)
+
+
def changeset(struct, params \\ %{}) do
+
struct
+
|> cast(params, [:rkey, :did, :text, :facets, :subject_id, :subject_type, :langs, :created_at])
+
|> validate_required([:rkey, :text])
end
end
+8
apps/backend/lib/comet/repo/embed/facet.ex
···
defmodule Comet.Repo.Embed.Facet do
use Comet.Schema
+
import Ecto.Changeset
@primary_key false
embedded_schema do
···
# Sadly Ecto doesn't support union types/embeds so this has to be generic, without doing weirdness in the database at least
field :features, {:array, :map}
+
end
+
+
def changeset(struct, params \\ %{}) do
+
struct
+
|> cast(params, [:features])
+
|> cast_embed(:index, required: true)
+
|> validate_required([:features])
end
end
+7
apps/backend/lib/comet/repo/embed/link.ex
···
defmodule Comet.Repo.Embed.Link do
use Comet.Schema
+
import Ecto.Changeset
@primary_key false
embedded_schema do
field :type, :string
field :value, :string
+
end
+
+
def changeset(struct, params \\ %{}) do
+
struct
+
|> cast(params, [:type, :value])
+
|> validate_required([:type, :value])
end
end
+9 -1
apps/backend/lib/comet/repo/identity.ex
···
Schema containing information about an ATProtocol identity.
"""
use Ecto.Schema
+
import Ecto.Changeset
@primary_key {:did, :string, autogenerate: false}
@foreign_key_type :string
···
schema "identity" do
field :handle, :string
field :active, :boolean
-
# TODO: see if it'd be possible to set this to an enum, if ecto allows open enums
field :status, :string
timestamps(inserted_at: :indexed_at, updated_at: false)
+
end
+
+
def new(params \\ %{}), do: changeset(%__MODULE__{}, params)
+
+
def changeset(struct, params \\ %{}) do
+
struct
+
|> cast(params, [:did, :handle, :active, :status])
+
|> validate_required([:did, :active])
end
end
+9
apps/backend/lib/comet/repo/like.ex
···
Schema containing information about a Comet like.
"""
use Comet.Schema
+
import Ecto.Changeset
schema "likes" do
field :rkey, :string
···
belongs_to :identity, Repo.Identity, foreign_key: :did, references: :did
timestamps(inserted_at: :indexed_at, updated_at: false)
+
end
+
+
def new(params \\ %{}), do: changeset(%__MODULE__{}, params)
+
+
def changeset(struct, params \\ %{}) do
+
struct
+
|> cast(params, [:rkey, :did, :subject_id, :subject_type, :created_at])
+
|> validate_required([:rkey, :did, :subject_id, :subject_type, :created_at])
end
end
+13 -1
apps/backend/lib/comet/repo/playlist.ex
···
defmodule Comet.Repo.Playlist do
@moduledoc """
-
Schema containing information about a Comet playlist.
+
Sch ema containing information about a Comet playlist.
"""
use Comet.Schema
+
import Ecto.Changeset
schema "playlists" do
field :rkey, :string
field :title, :string
field :image, :string
field :description, :string
+
# TODO: see how this looks with/without primary id
embeds_one :description_facets, Repo.Embed.Facet, on_replace: :update
field :type, :string
field :tags, {:array, :string}
···
has_many :tracks, Repo.Playlist
timestamps(inserted_at: :indexed_at, updated_at: false)
+
end
+
+
def new(params \\ %{}), do: changeset(%__MODULE__{}, params)
+
+
def changeset(struct, params \\ %{}) do
+
struct
+
|> cast(params, [:rkey, :did, :title, :image, :description, :type, :tags, :created_at])
+
|> cast_embed(:description_facets)
+
|> cast_embed(:link)
+
|> validate_required([:rkey, :did, :title, :type, :created_at])
end
end
+9
apps/backend/lib/comet/repo/playlist_track.ex
···
Schema containing information about a track in a Comet playlist.
"""
use Comet.Schema
+
import Ecto.Changeset
schema "playlist_tracks" do
field :rkey, :string
···
belongs_to :playlist, Repo.Playlist
timestamps(inserted_at: :indexed_at, updated_at: false)
+
end
+
+
def new(params \\ %{}), do: changeset(%__MODULE__{}, params)
+
+
def changeset(struct, params \\ %{}) do
+
struct
+
|> cast(params, [:rkey, :did, :position, :created_at, :track_id, :playlist_id])
+
|> validate_required([:rkey, :did, :position, :created_at, :track_id, :playlist_id])
end
end
+20
apps/backend/lib/comet/repo/profile.ex
···
Schema containing information about a Comet profile.
"""
use Comet.Schema
+
import Ecto.Changeset
+
# TODO: should probably keep track of CID so as to not do unnecessary writes
schema "profiles" do
field :rkey, :string, default: "self"
field :display_name, :string
···
belongs_to :identity, Repo.Identity, foreign_key: :did, references: :did
timestamps(inserted_at: :indexed_at, updated_at: false)
+
end
+
+
def new(params \\ %{}), do: changeset(%__MODULE__{}, params)
+
+
def changeset(struct, params \\ %{}) do
+
struct
+
|> cast(params, [
+
:rkey,
+
:did,
+
:display_name,
+
:description,
+
:avatar,
+
:banner,
+
:featured_items,
+
:created_at
+
])
+
|> cast_embed(:description_facets)
+
|> validate_required([:rkey, :did])
end
end
+9
apps/backend/lib/comet/repo/repost.ex
···
Schema containing information about a Comet repost.
"""
use Comet.Schema
+
import Ecto.Changeset
schema "reposts" do
field :rkey, :string
···
belongs_to :identity, Repo.Identity, foreign_key: :did, references: :did
timestamps(inserted_at: :indexed_at, updated_at: false)
+
end
+
+
def new(params \\ %{}), do: changeset(%__MODULE__{}, params)
+
+
def changeset(struct, params \\ %{}) do
+
struct
+
|> cast(params, [:rkey, :did, :subject_id, :subject_type, :created_at])
+
|> validate_required([:rkey, :did, :subject_id, :subject_type, :created_at])
end
end
+22
apps/backend/lib/comet/repo/track.ex
···
Schema containing information about a Comet track.
"""
use Comet.Schema
+
import Ecto.Changeset
schema "tracks" do
field :rkey, :string
···
belongs_to :identity, Repo.Identity, foreign_key: :did, references: :did
timestamps(inserted_at: :indexed_at, updated_at: false)
+
end
+
+
def new(params \\ %{}), do: changeset(%__MODULE__{}, params)
+
+
def changeset(struct, params \\ %{}) do
+
struct
+
|> cast(params, [
+
:rkey,
+
:did,
+
:title,
+
:audio,
+
:image,
+
:description,
+
:explicit,
+
:tags,
+
:created_at,
+
:released_at
+
])
+
|> cast_embed(:description_facets)
+
|> cast_embed(:link)
+
|> validate_required([:rkey, :did, :audio, :title, :created_at])
end
end