channel subscriptions

This commit is contained in:
byte[] 2019-12-04 23:27:40 -05:00
parent 885488d40e
commit c070b515c0
11 changed files with 110 additions and 9 deletions

View file

@ -150,6 +150,16 @@ defmodule Philomena.Channels do
|> Repo.exists?()
end
def subscriptions(_channels, nil), do: %{}
def subscriptions(channels, user) do
channel_ids = Enum.map(channels, & &1.id)
Subscription
|> where([s], s.channel_id in ^channel_ids and s.user_id == ^user.id)
|> Repo.all()
|> Map.new(&{&1.id, true})
end
def clear_notification(channel, user) do
Notifications.delete_unread_notification("Channel", channel.id, user)
Notifications.delete_unread_notification("LivestreamChannel", channel.id, user)

View file

@ -0,0 +1,18 @@
defmodule PhilomenaWeb.Channel.ReadController do
import Plug.Conn
use PhilomenaWeb, :controller
alias Philomena.Channels.Channel
alias Philomena.Channels
plug :load_resource, model: Channel, id_name: "channel_id", persisted: true
def create(conn, _params) do
channel = conn.assigns.channel
user = conn.assigns.current_user
Channels.clear_notification(channel, user)
send_resp(conn, :ok, "")
end
end

View file

@ -0,0 +1,35 @@
defmodule PhilomenaWeb.Channel.SubscriptionController do
use PhilomenaWeb, :controller
alias Philomena.Channels.Channel
alias Philomena.Channels
plug PhilomenaWeb.CanaryMapPlug, create: :show, delete: :show
plug :load_and_authorize_resource, model: Channel, id_name: "channel_id", persisted: true
def create(conn, _params) do
channel = conn.assigns.channel
user = conn.assigns.current_user
case Channels.create_subscription(channel, user) do
{:ok, _subscription} ->
render(conn, "_subscription.html", channel: channel, watching: true, layout: false)
{:error, _changeset} ->
render(conn, "_error.html", layout: false)
end
end
def delete(conn, _params) do
channel = conn.assigns.channel
user = conn.assigns.current_user
case Channels.delete_subscription(channel, user) do
{:ok, _subscription} ->
render(conn, "_subscription.html", channel: channel, watching: false, layout: false)
{:error, _changeset} ->
render(conn, "_error.html", layout: false)
end
end
end

View file

@ -18,7 +18,9 @@ defmodule PhilomenaWeb.ChannelController do
|> preload(:associated_artist_tag)
|> Repo.paginate(conn.assigns.scrivener)
render(conn, "index.html", layout_class: "layout--wide", channels: channels)
subscriptions = Channels.subscriptions(channels, conn.assigns.current_user)
render(conn, "index.html", layout_class: "layout--wide", channels: channels, subscriptions: subscriptions)
end
def show(conn, _params) do

View file

@ -131,6 +131,11 @@ defmodule PhilomenaWeb.Router do
resources "/read", Gallery.ReadController, only: [:create], singleton: true
resources "/subscription", Gallery.SubscriptionController, only: [:create, :delete], singleton: true
end
resources "/channels", ChannelController, only: [] do
resources "/read", Channel.ReadController, only: [:create], singleton: true
resources "/subscription", Channel.SubscriptionController, only: [:create, :delete], singleton: true
end
end
scope "/", PhilomenaWeb do

View file

@ -28,7 +28,4 @@
- else
.media-box__header.media-box__header--channel No artist tag
/- if current_user
/ = subscription_link channel, current_user, class: link_class
/- else
/ .media-box__header.media-box__header--channel Login to subscribe
= render PhilomenaWeb.Channel.SubscriptionView, "_subscription.html", conn: @conn, watching: @subscriptions[@channel.id], channel: @channel

View file

@ -13,7 +13,7 @@ h1 Livestreams
.block__content
= for channel <- @channels do
= render PhilomenaWeb.ChannelView, "_channel_box.html", channel: channel, conn: @conn
= render PhilomenaWeb.ChannelView, "_channel_box.html", channel: channel, conn: @conn, subscriptions: @subscriptions
.block__header
= pagination

View file

@ -0,0 +1,2 @@
#js-subscription-target
' Error!

View file

@ -0,0 +1,23 @@
elixir:
watch_path = Routes.channel_subscription_path(@conn, :create, @channel)
watch_class = if @watching, do: "hidden", else: ""
unwatch_path = Routes.channel_subscription_path(@conn, :delete, @channel)
unwatch_class = if @watching, do: "", else: "hidden"
= if @conn.assigns.current_user do
span#js-subscription-target
a.js-subscription-link href=watch_path class=watch_class data-remote="true" data-method="post"
i.fa.fa-bell>
span.hide-mobile
' Subscribe
a.js-subscription-link href=unwatch_path class=unwatch_class data-remote="true" data-method="delete"
i.fa.fa-bell-slash>
span.hide-mobile
' Unsubscribe
- else
a href=Routes.pow_session_path(@conn, :new)
i.fa.fa-bell>
span.hide-mobile
' Subscribe

View file

@ -1,8 +1,14 @@
.flex.flex--centered.flex__grow
div
strong>
= link @notification.actor.title, to: "#"
/= link @notification.actor.title, to: Routes.channel_path(@conn, :show, notification.actor)
= link @notification.actor.title, to: Routes.channel_path(@conn, :show, @notification.actor)
=<> @notification.action
=> pretty_time @notification.updated_at
=> pretty_time @notification.updated_at
.flex.flex--centered.flex--no-wrap
a.button.button--separate-right title="Delete" href=Routes.channel_read_path(@conn, :create, @notification.actor) data-method="post" data-remote="true" data-fetchcomplete-hide="#notification-#{@notification.id}"
i.fa.fa-trash
a.button title="Unsubscribe" href=Routes.channel_subscription_path(@conn, :delete, @notification.actor) data-method="delete" data-remote="true" data-fetchcomplete-hide="#notification-#{@notification.id}"
i.fa.fa-bell-slash

View file

@ -0,0 +1,3 @@
defmodule PhilomenaWeb.Channel.SubscriptionView do
use PhilomenaWeb, :view
end