76 lines
1.7 KiB
Elixir
76 lines
1.7 KiB
Elixir
defmodule Podbox.Podcast.Feed do
|
|
@moduledoc """
|
|
A subscription to a feed.
|
|
"""
|
|
|
|
alias Podbox.{
|
|
Download.Asset,
|
|
Podcast,
|
|
Podcast.AssetFeed,
|
|
Podcast.FeedDownloadCompleteReactor,
|
|
Podcast.Show,
|
|
Podcast.SubscribeToFeedReactor,
|
|
Repo
|
|
}
|
|
|
|
use Ash.Resource,
|
|
data_layer: AshSqlite.DataLayer,
|
|
domain: Podcast,
|
|
notifiers: [Ash.Notifier.PubSub]
|
|
|
|
@type t :: Ash.Resource.record()
|
|
|
|
attributes do
|
|
uuid_primary_key :id
|
|
|
|
attribute :uri, :string, allow_nil?: false, public?: true
|
|
attribute :refreshed_at, :datetime, allow_nil?: true, public?: true
|
|
|
|
create_timestamp :inserted_at
|
|
update_timestamp :updated_at
|
|
end
|
|
|
|
relationships do
|
|
many_to_many :assets, Asset, through: AssetFeed, public?: true
|
|
has_many :asset_feeds, AssetFeed, public?: false
|
|
has_one :show, Show, public?: true
|
|
end
|
|
|
|
pub_sub do
|
|
module Podbox.PubSub
|
|
prefix "podcast:feed"
|
|
publish :create, "created"
|
|
end
|
|
|
|
actions do
|
|
defaults [:read, :destroy, update: :*, create: :*]
|
|
|
|
action :subscribe, :struct do
|
|
constraints instance_of: __MODULE__
|
|
argument :uri, :string, allow_nil?: false, public?: true
|
|
run SubscribeToFeedReactor
|
|
end
|
|
|
|
action :download_complete, :struct do
|
|
constraints instance_of: __MODULE__
|
|
argument :asset_id, :uuid, allow_nil?: false, public?: true
|
|
run FeedDownloadCompleteReactor
|
|
end
|
|
|
|
read :get_by_asset_id do
|
|
argument :asset_id, :uuid, allow_nil?: false, public?: true
|
|
get? true
|
|
filter expr(asset_feeds.asset_id == ^arg(:asset_id))
|
|
prepare build(limit: 1)
|
|
end
|
|
|
|
update :refreshed do
|
|
change set_attribute(:refreshed_at, &DateTime.utc_now/0)
|
|
end
|
|
end
|
|
|
|
sqlite do
|
|
table "podcast_feed"
|
|
repo Repo
|
|
end
|
|
end
|