2019-11-17 19:05:50 +01:00
|
|
|
defmodule PhilomenaWeb.Topic.SubscriptionController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-12-14 20:46:50 +01:00
|
|
|
alias Philomena.Forums.Forum
|
|
|
|
alias Philomena.Topics
|
2019-11-17 19:05:50 +01:00
|
|
|
|
2019-11-17 19:18:21 +01:00
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :show, delete: :show
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
plug :load_and_authorize_resource,
|
|
|
|
model: Forum,
|
|
|
|
id_name: "forum_id",
|
|
|
|
id_field: "short_name",
|
|
|
|
persisted: true
|
2019-11-17 19:05:50 +01:00
|
|
|
|
2019-12-14 20:46:50 +01:00
|
|
|
plug PhilomenaWeb.LoadTopicPlug, [show_hidden: true] when action in [:delete]
|
|
|
|
plug PhilomenaWeb.LoadTopicPlug when action in [:create]
|
|
|
|
|
|
|
|
def create(conn, _params) do
|
|
|
|
topic = conn.assigns.topic
|
2019-11-17 19:05:50 +01:00
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
|
|
|
case Topics.create_subscription(topic, user) do
|
|
|
|
{:ok, _subscription} ->
|
2020-01-11 05:20:19 +01:00
|
|
|
render(conn, "_subscription.html",
|
|
|
|
forum: conn.assigns.forum,
|
|
|
|
topic: topic,
|
|
|
|
watching: true,
|
|
|
|
layout: false
|
|
|
|
)
|
2019-11-17 19:05:50 +01:00
|
|
|
|
|
|
|
{:error, _changeset} ->
|
|
|
|
render(conn, "_error.html", layout: false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-14 20:46:50 +01:00
|
|
|
def delete(conn, _params) do
|
|
|
|
topic = conn.assigns.topic
|
2019-11-17 19:05:50 +01:00
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
|
|
|
case Topics.delete_subscription(topic, user) do
|
|
|
|
{:ok, _subscription} ->
|
2020-01-11 05:20:19 +01:00
|
|
|
render(conn, "_subscription.html",
|
|
|
|
forum: conn.assigns.forum,
|
|
|
|
topic: topic,
|
|
|
|
watching: false,
|
|
|
|
layout: false
|
|
|
|
)
|
2019-11-17 19:05:50 +01:00
|
|
|
|
|
|
|
{:error, _changeset} ->
|
|
|
|
render(conn, "_error.html", layout: false)
|
|
|
|
end
|
|
|
|
end
|
2019-12-14 20:46:50 +01:00
|
|
|
end
|