nulla/lib/nulla/models/actor.ex
2025-06-18 17:45:48 +02:00

107 lines
2.3 KiB
Elixir

defmodule Nulla.Models.Actor do
use Ecto.Schema
import Ecto.Changeset
alias Nulla.Repo
alias Nulla.Snowflake
alias Nulla.Models.Note
@primary_key {:id, :integer, autogenerate: false}
schema "actors" do
field :domain, :string
field :ap_id, :string
field :type, :string
field :following, :string
field :followers, :string
field :inbox, :string
field :outbox, :string
field :featured, :string
field :featuredTags, :string
field :preferredUsername, :string
field :name, :string
field :summary, :string
field :url, :string
field :manuallyApprovesFollowers, :boolean
field :discoverable, :boolean, default: true
field :indexable, :boolean, default: true
field :published, :utc_datetime
field :memorial, :boolean, default: false
field :publicKey, :map
field :tag, {:array, :map}, default: []
field :attachment, {:array, :map}, default: []
field :endpoints, :map
field :icon, :map
field :image, :map
field :vcard_bday, :date
field :vcard_Address, :string
has_many :notes, Note
has_many :media_attachments, through: [:notes, :media_attachments]
end
@doc false
def changeset(actor, attrs) do
actor
|> cast(attrs, [
:id,
:domain,
:ap_id,
:type,
:following,
:followers,
:inbox,
:outbox,
:featured,
:featuredTags,
:preferredUsername,
:name,
:summary,
:url,
:manuallyApprovesFollowers,
:discoverable,
:indexable,
:published,
:memorial,
:publicKey,
:tag,
:attachment,
:endpoints,
:icon,
:image,
:vcard_bday,
:vcard_Address
])
|> validate_required([
:domain,
:ap_id,
:type,
:following,
:followers,
:inbox,
:outbox,
:featured,
:featuredTags,
:preferredUsername,
:url,
:manuallyApprovesFollowers,
:discoverable,
:indexable,
:published,
:memorial,
:publicKey,
:endpoints
])
end
def create_actor(attrs) when is_map(attrs) do
id = Snowflake.next_id()
%__MODULE__{}
|> changeset(attrs)
|> put_change(:id, id)
|> Repo.insert()
end
def get_actor(username, domain) do
Repo.get_by(__MODULE__, preferredUsername: username, domain: domain)
end
end