From c070b515c0f6c5dce8fb56242e941aae9b148488 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Wed, 4 Dec 2019 23:27:40 -0500 Subject: [PATCH] channel subscriptions --- lib/philomena/channels.ex | 10 ++++++ .../controllers/channel/read_controller.ex | 18 ++++++++++ .../channel/subscription_controller.ex | 35 +++++++++++++++++++ .../controllers/channel_controller.ex | 4 ++- lib/philomena_web/router.ex | 5 +++ .../templates/channel/_channel_box.html.slime | 5 +-- .../templates/channel/index.html.slime | 2 +- .../channel/subscription/_error.html.slime | 2 ++ .../subscription/_subscription.html.slime | 23 ++++++++++++ .../notification/_channel.html.slime | 12 +++++-- .../views/channel/subscription_view.ex | 3 ++ 11 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 lib/philomena_web/controllers/channel/read_controller.ex create mode 100644 lib/philomena_web/controllers/channel/subscription_controller.ex create mode 100644 lib/philomena_web/templates/channel/subscription/_error.html.slime create mode 100644 lib/philomena_web/templates/channel/subscription/_subscription.html.slime create mode 100644 lib/philomena_web/views/channel/subscription_view.ex diff --git a/lib/philomena/channels.ex b/lib/philomena/channels.ex index 1c86ccdd..9b3920cc 100644 --- a/lib/philomena/channels.ex +++ b/lib/philomena/channels.ex @@ -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) diff --git a/lib/philomena_web/controllers/channel/read_controller.ex b/lib/philomena_web/controllers/channel/read_controller.ex new file mode 100644 index 00000000..e1b4e656 --- /dev/null +++ b/lib/philomena_web/controllers/channel/read_controller.ex @@ -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 \ No newline at end of file diff --git a/lib/philomena_web/controllers/channel/subscription_controller.ex b/lib/philomena_web/controllers/channel/subscription_controller.ex new file mode 100644 index 00000000..d0628e70 --- /dev/null +++ b/lib/philomena_web/controllers/channel/subscription_controller.ex @@ -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 \ No newline at end of file diff --git a/lib/philomena_web/controllers/channel_controller.ex b/lib/philomena_web/controllers/channel_controller.ex index a9b32338..4780f872 100644 --- a/lib/philomena_web/controllers/channel_controller.ex +++ b/lib/philomena_web/controllers/channel_controller.ex @@ -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 diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index b472fa9b..dfdf0dcf 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -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 diff --git a/lib/philomena_web/templates/channel/_channel_box.html.slime b/lib/philomena_web/templates/channel/_channel_box.html.slime index 438097a8..23d24cf4 100644 --- a/lib/philomena_web/templates/channel/_channel_box.html.slime +++ b/lib/philomena_web/templates/channel/_channel_box.html.slime @@ -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 diff --git a/lib/philomena_web/templates/channel/index.html.slime b/lib/philomena_web/templates/channel/index.html.slime index 1cb88ab1..bcff0bba 100644 --- a/lib/philomena_web/templates/channel/index.html.slime +++ b/lib/philomena_web/templates/channel/index.html.slime @@ -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 diff --git a/lib/philomena_web/templates/channel/subscription/_error.html.slime b/lib/philomena_web/templates/channel/subscription/_error.html.slime new file mode 100644 index 00000000..b0c027de --- /dev/null +++ b/lib/philomena_web/templates/channel/subscription/_error.html.slime @@ -0,0 +1,2 @@ +#js-subscription-target + ' Error! \ No newline at end of file diff --git a/lib/philomena_web/templates/channel/subscription/_subscription.html.slime b/lib/philomena_web/templates/channel/subscription/_subscription.html.slime new file mode 100644 index 00000000..0824d385 --- /dev/null +++ b/lib/philomena_web/templates/channel/subscription/_subscription.html.slime @@ -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 \ No newline at end of file diff --git a/lib/philomena_web/templates/notification/_channel.html.slime b/lib/philomena_web/templates/notification/_channel.html.slime index 00ed7c1a..3e40d6f3 100644 --- a/lib/philomena_web/templates/notification/_channel.html.slime +++ b/lib/philomena_web/templates/notification/_channel.html.slime @@ -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 \ No newline at end of file + => 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 \ No newline at end of file diff --git a/lib/philomena_web/views/channel/subscription_view.ex b/lib/philomena_web/views/channel/subscription_view.ex new file mode 100644 index 00000000..5a58a250 --- /dev/null +++ b/lib/philomena_web/views/channel/subscription_view.ex @@ -0,0 +1,3 @@ +defmodule PhilomenaWeb.Channel.SubscriptionView do + use PhilomenaWeb, :view +end