philomena/lib/philomena/channels.ex

158 lines
3.1 KiB
Elixir
Raw Normal View History

2019-10-05 00:52:44 +02:00
defmodule Philomena.Channels do
@moduledoc """
The Channels context.
"""
import Ecto.Query, warn: false
alias Philomena.Repo
2024-06-26 03:48:32 +02:00
alias Philomena.Channels.AutomaticUpdater
2019-10-05 00:52:44 +02:00
alias Philomena.Channels.Channel
2024-07-08 00:09:20 +02:00
alias Philomena.Notifications
2024-07-14 20:48:11 +02:00
alias Philomena.Tags
2024-06-25 04:21:13 +02:00
use Philomena.Subscriptions,
on_delete: :clear_channel_notification,
2024-06-25 04:21:13 +02:00
id_name: :channel_id
2019-10-05 00:52:44 +02:00
@doc """
2024-06-26 03:48:32 +02:00
Updates all the tracked channels for which an update scheme is known.
2019-10-05 00:52:44 +02:00
"""
2020-10-26 22:01:29 +01:00
def update_tracked_channels! do
2024-06-26 03:48:32 +02:00
AutomaticUpdater.update_tracked_channels!()
2019-10-05 00:52:44 +02:00
end
@doc """
Gets a single channel.
Raises `Ecto.NoResultsError` if the Channel does not exist.
## Examples
iex> get_channel!(123)
%Channel{}
iex> get_channel!(456)
** (Ecto.NoResultsError)
"""
def get_channel!(id), do: Repo.get!(Channel, id)
@doc """
Creates a channel.
## Examples
iex> create_channel(%{field: value})
{:ok, %Channel{}}
iex> create_channel(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_channel(attrs \\ %{}) do
%Channel{}
2024-07-14 20:48:11 +02:00
|> update_artist_tag(attrs)
2019-10-05 00:52:44 +02:00
|> Channel.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a channel.
## Examples
iex> update_channel(channel, %{field: new_value})
{:ok, %Channel{}}
iex> update_channel(channel, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_channel(%Channel{} = channel, attrs) do
channel
2024-07-14 20:48:11 +02:00
|> update_artist_tag(attrs)
2019-10-05 00:52:44 +02:00
|> Channel.changeset(attrs)
|> Repo.update()
end
2024-07-14 20:48:11 +02:00
@doc """
Adds the artist tag from the `"artist_tag"` tag name attribute.
## Examples
iex> update_artist_tag(%Channel{}, %{"artist_tag" => "artist:nighty"})
%Ecto.Changeset{}
"""
def update_artist_tag(%Channel{} = channel, attrs) do
tag =
attrs
|> Map.get("artist_tag", "")
|> Tags.get_tag_by_name()
Channel.artist_tag_changeset(channel, tag)
end
2024-06-26 03:48:32 +02:00
@doc """
Updates a channel's state when it goes live.
## Examples
iex> update_channel_state(channel, %{field: new_value})
{:ok, %Channel{}}
iex> update_channel_state(channel, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_channel_state(%Channel{} = channel, attrs) do
channel
|> Channel.update_changeset(attrs)
|> Repo.update()
end
2019-10-05 00:52:44 +02:00
@doc """
Deletes a Channel.
## Examples
iex> delete_channel(channel)
{:ok, %Channel{}}
iex> delete_channel(channel)
{:error, %Ecto.Changeset{}}
"""
def delete_channel(%Channel{} = channel) do
Repo.delete(channel)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking channel changes.
## Examples
iex> change_channel(channel)
%Ecto.Changeset{source: %Channel{}}
"""
def change_channel(%Channel{} = channel) do
Channel.changeset(channel, %{})
end
2024-07-08 00:09:20 +02:00
@doc """
Removes all channel notifications for a given channel and user.
## Examples
iex> clear_channel_notification(channel, user)
:ok
"""
def clear_channel_notification(%Channel{} = channel, user) do
Notifications.clear_channel_live_notification(channel, user)
:ok
end
2019-10-05 00:52:44 +02:00
end