channel creation

This commit is contained in:
byte[] 2019-12-18 12:01:33 -05:00
parent d7d9743a7e
commit fe746dac0f
9 changed files with 108 additions and 8 deletions

View file

@ -3,6 +3,7 @@ defmodule Philomena.Channels.Channel do
import Ecto.Changeset
alias Philomena.Tags.Tag
alias Philomena.Repo
schema "channels" do
belongs_to :associated_artist_tag, Tag
@ -11,15 +12,15 @@ defmodule Philomena.Channels.Channel do
field :type, :string
field :short_name, :string
field :title, :string
field :title, :string, default: ""
field :description, :string
field :tags, :string
field :viewers, :integer, default: 0
field :nsfw, :boolean, default: false
field :is_live, :boolean, default: false
field :last_fetched_at, :naive_datetime
field :next_check_at, :naive_datetime
field :last_live_at, :naive_datetime
field :last_fetched_at, :utc_datetime
field :next_check_at, :utc_datetime
field :last_live_at, :utc_datetime
field :viewer_minutes_today, :integer, default: 0
field :viewer_minutes_thisweek, :integer, default: 0
@ -36,8 +37,17 @@ defmodule Philomena.Channels.Channel do
@doc false
def changeset(channel, attrs) do
tag_id =
if attrs["artist_tag"] do
Repo.get_by(Tag, name: attrs["artist_tag"]).id
else
nil
end
channel
|> cast(attrs, [])
|> validate_required([])
|> cast(attrs, [:type, :short_name])
|> validate_required([:type, :short_name])
|> validate_inclusion(:type, ["PicartoChannel", "PiczelChannel"])
|> put_change(:associated_artist_tag_id, tag_id)
end
end

View file

@ -1,6 +1,7 @@
defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
alias Philomena.Users.User
alias Philomena.Badges.Award
alias Philomena.Channels.Channel
alias Philomena.Comments.Comment
alias Philomena.Commissions.Commission
alias Philomena.Conversations.Conversation
@ -238,6 +239,9 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
# Show static pages
def can?(_user, :show, %StaticPage{}), do: true
# Show channels
def can?(_user, :show, %Channel{}), do: true
# Otherwise...
def can?(_user, _action, _model), do: false
end

View file

@ -6,7 +6,7 @@ defmodule PhilomenaWeb.ChannelController do
alias Philomena.Repo
import Ecto.Query
plug :load_resource, model: Channel
plug :load_and_authorize_resource, model: Channel, only: [:show, :new, :create, :edit, :update]
def index(conn, _params) do
show_nsfw? = conn.cookies["chan_nsfw"] == "true"
@ -32,6 +32,40 @@ defmodule PhilomenaWeb.ChannelController do
redirect(conn, external: url(channel))
end
def new(conn, _params) do
changeset = Channels.change_channel(%Channel{})
render(conn, "new.html", title: "New Channel", changeset: changeset)
end
def create(conn, %{"channel" => channel_params}) do
case Channels.create_channel(channel_params) do
{:ok, _channel} ->
conn
|> put_flash(:info, "Channel created successfully.")
|> redirect(to: Routes.channel_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def edit(conn, _params) do
changeset = Channels.change_channel(conn.assigns.channel)
render(conn, "edit.html", title: "Editing Channel", changeset: changeset)
end
def update(conn, %{"channel" => channel_params}) do
case Channels.update_channel(conn.assigns.channel, channel_params) do
{:ok, _channel} ->
conn
|> put_flash(:info, "Channel updated successfully.")
|> redirect(to: Routes.channel_path(conn, :index))
{:error, changeset} ->
render(conn, "edit.html", changeset: changeset)
end
end
defp maybe_show_nsfw(query, true), do: query
defp maybe_show_nsfw(query, _falsy), do: where(query, [c], c.nsfw == false)

View file

@ -260,6 +260,7 @@ defmodule PhilomenaWeb.Router do
end
resources "/pages", PageController, only: [:index, :new, :create, :edit, :update]
resources "/channels", ChannelController, only: [:new, :create, :edit, :update]
end
scope "/", PhilomenaWeb do

View file

@ -28,4 +28,9 @@
- else
.media-box__header.media-box__header--channel No artist tag
= if can?(@conn, :edit, @channel) do
a href=Routes.channel_path(@conn, :edit, @channel) class=link_class
i.fas.fa-fw.fa-edit>
' Edit
= render PhilomenaWeb.Channel.SubscriptionView, "_subscription.html", conn: @conn, watching: @subscriptions[@channel.id], channel: @channel

View file

@ -0,0 +1,37 @@
= form_for @changeset, @action, fn f ->
= if @changeset.action do
.alert.alert-danger
p Oops, something went wrong! Please check the errors below.
.fieldlabel
' The short name of
code>
| https://picarto.tv/
em picarto_channel_name
' is
code picarto_channel_name
' .
.fieldlabel
' The short name of
code>
| https://piczel.tv/watch/
em piczel_channel_name
' is
code piczel_channel_name
' .
br
br
.field
=> label f, :short_name, "Short name"
= text_input f, :short_name, class: "input", placeholder: "Short name", required: true
.field
=> label f, :type, "Type"
= select f, :type, ["PicartoChannel", "PiczelChannel"], class: "input"
.field
=> label f, :artist_tag, "Artist tag"
= text_input f, :artist_tag, class: "input", placeholder: "Artist tag"
= submit "Save", class: "button"

View file

@ -0,0 +1,3 @@
h1 Editing Channel
= render PhilomenaWeb.ChannelView, "_form.html", changeset: @changeset, action: Routes.channel_path(@conn, :update, @channel), conn: @conn

View file

@ -7,6 +7,7 @@ h1 Livestreams
.field
= text_field_tag :cq, params[:cq], class: 'input hform__text', placeholder: 'Search channels'
= submit_tag 'Search', class: 'hform__button button'
.block
.block__header
= pagination
@ -19,7 +20,9 @@ h1 Livestreams
= pagination
br
.clearfix  
= if can?(@conn, :create, Philomena.Channels.Channel) do
= link "New Channel", to: Routes.channel_path(@conn, :new)
h2 FAQ
p
strong> Q: Do you host streams?

View file

@ -0,0 +1,3 @@
h1 Adding Channel
= render PhilomenaWeb.ChannelView, "_form.html", changeset: @changeset, action: Routes.channel_path(@conn, :new), conn: @conn